< prev index next >

test/jdk/javax/net/ssl/DTLS/DTLSOverDatagram.java

Print this page
rev 53269 : 8228967: Trust/Key store and SSL context utilities for tests
Reviewed-by: xuelei

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -27,20 +27,22 @@
 /*
  * @test
  * @bug 8043758
  * @summary Datagram Transport Layer Security (DTLS)
  * @modules java.base/sun.security.util
+ * @library /test/lib
  * @run main/othervm DTLSOverDatagram
  */
 
-import java.io.*;
 import java.nio.*;
 import java.net.*;
 import java.util.*;
-import java.security.*;
-import java.security.cert.*;
 import javax.net.ssl.*;
+
+import jdk.test.lib.security.KeyStoreUtils;
+import jdk.test.lib.security.SSLContextBuilder;
+
 import java.util.concurrent.*;
 
 import sun.security.util.HexDumpEncoder;
 
 /**

@@ -58,11 +60,10 @@
      * The following is to set up the keystores.
      */
     private static String pathToStores = "../etc";
     private static String keyStoreFile = "keystore";
     private static String trustStoreFile = "truststore";
-    private static String passwd = "passphrase";
 
     private static String keyFilename =
             System.getProperty("test.src", ".") + "/" + pathToStores +
                 "/" + keyStoreFile;
     private static String trustFilename =

@@ -535,34 +536,17 @@
         }
     }
 
     // get DTSL context
     SSLContext getDTLSContext() throws Exception {
-        KeyStore ks = KeyStore.getInstance("JKS");
-        KeyStore ts = KeyStore.getInstance("JKS");
-
-        char[] passphrase = "passphrase".toCharArray();
-
-        try (FileInputStream fis = new FileInputStream(keyFilename)) {
-            ks.load(fis, passphrase);
-        }
-
-        try (FileInputStream fis = new FileInputStream(trustFilename)) {
-            ts.load(fis, passphrase);
-        }
-
-        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
-        kmf.init(ks, passphrase);
-
-        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
-        tmf.init(ts);
-
-        SSLContext sslCtx = SSLContext.getInstance("DTLS");
-
-        sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
-
-        return sslCtx;
+        String passphrase = "passphrase";
+        return SSLContextBuilder.builder()
+                .trustStore(KeyStoreUtils.loadKeyStore(trustFilename, passphrase))
+                .keyStore(KeyStoreUtils.loadKeyStore(keyFilename, passphrase))
+                .kmfPassphrase(passphrase)
+                .protocol("DTLS")
+                .build();
     }
 
 
     /*
      * =============================================================
< prev index next >