< prev index next >

test/jdk/sun/security/util/HostnameMatcher/NullHostnameCheck.java

Print this page
rev 53269 : 8228967: Trust/Key store and SSL context utilities for tests
Reviewed-by: xuelei
   1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import javax.net.ssl.KeyManagerFactory;



  25 import javax.net.ssl.SSLContext;
  26 import javax.net.ssl.SSLEngine;
  27 import javax.net.ssl.SSLEngineResult;
  28 import javax.net.ssl.SSLException;
  29 import javax.net.ssl.SSLHandshakeException;
  30 import javax.net.ssl.SSLParameters;
  31 import javax.net.ssl.TrustManager;
  32 import javax.net.ssl.X509TrustManager;
  33 import java.io.ByteArrayInputStream;
  34 import java.nio.ByteBuffer;
  35 import java.security.KeyStore;
  36 import java.security.cert.CertificateException;
  37 import java.security.cert.X509Certificate;
  38 import java.util.Base64;
  39 
  40 /*
  41  * @test
  42  * @bug 8211339 8234728
  43  * @summary Verify hostname returns an exception instead of null pointer when
  44  * creating a new engine

  45  * @run main NullHostnameCheck TLSv1
  46  * @run main NullHostnameCheck TLSv1.1
  47  * @run main NullHostnameCheck TLSv1.2
  48  * @run main NullHostnameCheck TLSv1.3
  49  */
  50 
  51 
  52 public final class NullHostnameCheck {
  53 
  54     public static void main(String[] args) throws Exception {
  55         String protocol = args[0];
  56         KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  57         keyStore.load(
  58                 new ByteArrayInputStream(Base64.getDecoder().
  59                         decode(keystoreB64)),
  60                 "123456".toCharArray());
  61         KeyManagerFactory kmf = KeyManagerFactory.getInstance(
  62                 KeyManagerFactory.getDefaultAlgorithm());
  63         kmf.init(keyStore, "123456".toCharArray());
  64         SSLContext serverCtx = SSLContext.getInstance(protocol);
  65         serverCtx.init(kmf.getKeyManagers(), null, null);
  66         SSLEngine serverEngine = serverCtx.createSSLEngine("localhost", -1);
  67         serverEngine.setUseClientMode(false);
  68 
  69         SSLContext clientCtx = SSLContext.getInstance(protocol);
  70         clientCtx.init(null, new TrustManager[] {
  71                 new X509TrustManager() {
  72                     @Override
  73                     public void checkClientTrusted(
  74                             X509Certificate[] x509Certificates, String s) {
  75                     }
  76 
  77                     @Override
  78                     public void checkServerTrusted(
  79                             X509Certificate[] x509Certificates, String s) {
  80                     }
  81 
  82                     @Override
  83                     public X509Certificate[] getAcceptedIssuers() {
  84                         return new X509Certificate[0];
  85                     }


   1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.nio.ByteBuffer;
  25 import java.security.cert.CertificateException;
  26 import java.security.cert.X509Certificate;
  27 
  28 import javax.net.ssl.SSLContext;
  29 import javax.net.ssl.SSLEngine;
  30 import javax.net.ssl.SSLEngineResult;
  31 import javax.net.ssl.SSLException;
  32 import javax.net.ssl.SSLHandshakeException;
  33 import javax.net.ssl.SSLParameters;
  34 import javax.net.ssl.TrustManager;
  35 import javax.net.ssl.X509TrustManager;
  36 
  37 import jdk.test.lib.security.KeyStoreUtils;
  38 import jdk.test.lib.security.SSLContextBuilder;



  39 
  40 /*
  41  * @test
  42  * @bug 8211339 8234728
  43  * @summary Verify hostname returns an exception instead of null pointer when
  44  * creating a new engine
  45  * @library /test/lib
  46  * @run main NullHostnameCheck TLSv1
  47  * @run main NullHostnameCheck TLSv1.1
  48  * @run main NullHostnameCheck TLSv1.2
  49  * @run main NullHostnameCheck TLSv1.3
  50  */
  51 
  52 
  53 public final class NullHostnameCheck {
  54 
  55     public static void main(String[] args) throws Exception {
  56         String protocol = args[0];
  57         String password = "123456";
  58         SSLContext serverCtx = SSLContextBuilder.builder()
  59                 .keyStore(KeyStoreUtils.loadKeyStoreBase64(
  60                         keystoreB64, password))
  61                 .kmfPassphrase(password)
  62                 .protocol(protocol)
  63                 .build();



  64         SSLEngine serverEngine = serverCtx.createSSLEngine("localhost", -1);
  65         serverEngine.setUseClientMode(false);
  66 
  67         SSLContext clientCtx = SSLContext.getInstance(protocol);
  68         clientCtx.init(null, new TrustManager[] {
  69                 new X509TrustManager() {
  70                     @Override
  71                     public void checkClientTrusted(
  72                         X509Certificate[] x509Certificates, String s) {
  73                     }
  74 
  75                     @Override
  76                     public void checkServerTrusted(
  77                         X509Certificate[] x509Certificates, String s) {
  78                     }
  79 
  80                     @Override
  81                     public X509Certificate[] getAcceptedIssuers() {
  82                         return new X509Certificate[0];
  83                     }


< prev index next >