< 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 /*
   2  * Copyright (c) 2015, 2016, 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 // SunJSSE does not support dynamic system properties, no way to re-use
  25 // system properties in samevm/agentvm mode.
  26 
  27 /*
  28  * @test
  29  * @bug 8043758
  30  * @summary Datagram Transport Layer Security (DTLS)
  31  * @modules java.base/sun.security.util

  32  * @run main/othervm DTLSOverDatagram
  33  */
  34 
  35 import java.io.*;
  36 import java.nio.*;
  37 import java.net.*;
  38 import java.util.*;
  39 import java.security.*;
  40 import java.security.cert.*;
  41 import javax.net.ssl.*;




  42 import java.util.concurrent.*;
  43 
  44 import sun.security.util.HexDumpEncoder;
  45 
  46 /**
  47  * An example to show the way to use SSLEngine in datagram connections.
  48  */
  49 public class DTLSOverDatagram {
  50 
  51     private static int MAX_HANDSHAKE_LOOPS = 200;
  52     private static int MAX_APP_READ_LOOPS = 60;
  53     private static int SOCKET_TIMEOUT = 10 * 1000; // in millis
  54     private static int BUFFER_SIZE = 1024;
  55     private static int MAXIMUM_PACKET_SIZE = 1024;
  56 
  57     /*
  58      * The following is to set up the keystores.
  59      */
  60     private static String pathToStores = "../etc";
  61     private static String keyStoreFile = "keystore";
  62     private static String trustStoreFile = "truststore";
  63     private static String passwd = "passphrase";
  64 
  65     private static String keyFilename =
  66             System.getProperty("test.src", ".") + "/" + pathToStores +
  67                 "/" + keyStoreFile;
  68     private static String trustFilename =
  69             System.getProperty("test.src", ".") + "/" + pathToStores +
  70                 "/" + trustStoreFile;
  71     private static Exception clientException = null;
  72     private static Exception serverException = null;
  73 
  74     private static ByteBuffer serverApp =
  75                 ByteBuffer.wrap("Hi Client, I'm Server".getBytes());
  76     private static ByteBuffer clientApp =
  77                 ByteBuffer.wrap("Hi Server, I'm Client".getBytes());
  78 
  79     /*
  80      * =============================================================
  81      * The test case
  82      */
  83     public static void main(String[] args) throws Exception {


 520         if (hs == SSLEngineResult.HandshakeStatus.NEED_TASK) {
 521             throw new Exception("handshake shouldn't need additional tasks");
 522         }
 523     }
 524 
 525     // retransmission if timeout
 526     boolean onReceiveTimeout(SSLEngine engine, SocketAddress socketAddr,
 527             String side, List<DatagramPacket> packets) throws Exception {
 528 
 529         SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus();
 530         if (hs == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {
 531             return false;
 532         } else {
 533             // retransmission of handshake messages
 534             return produceHandshakePackets(engine, socketAddr, side, packets);
 535         }
 536     }
 537 
 538     // get DTSL context
 539     SSLContext getDTLSContext() throws Exception {
 540         KeyStore ks = KeyStore.getInstance("JKS");
 541         KeyStore ts = KeyStore.getInstance("JKS");
 542 
 543         char[] passphrase = "passphrase".toCharArray();
 544 
 545         try (FileInputStream fis = new FileInputStream(keyFilename)) {
 546             ks.load(fis, passphrase);
 547         }
 548 
 549         try (FileInputStream fis = new FileInputStream(trustFilename)) {
 550             ts.load(fis, passphrase);
 551         }
 552 
 553         KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
 554         kmf.init(ks, passphrase);
 555 
 556         TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
 557         tmf.init(ts);
 558 
 559         SSLContext sslCtx = SSLContext.getInstance("DTLS");
 560 
 561         sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
 562 
 563         return sslCtx;
 564     }
 565 
 566 
 567     /*
 568      * =============================================================
 569      * The remainder is support stuff to kickstart the testing.
 570      */
 571 
 572     // Will the handshaking and application data exchange succeed?
 573     public boolean isGoodJob() {
 574         return true;
 575     }
 576 
 577     public final void runTest(DTLSOverDatagram testCase) throws Exception {
 578         try (DatagramSocket serverSocket = new DatagramSocket();
 579                 DatagramSocket clientSocket = new DatagramSocket()) {
 580 
 581             serverSocket.setSoTimeout(SOCKET_TIMEOUT);
 582             clientSocket.setSoTimeout(SOCKET_TIMEOUT);
 583 


   1 /*
   2  * Copyright (c) 2015, 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 // SunJSSE does not support dynamic system properties, no way to re-use
  25 // system properties in samevm/agentvm mode.
  26 
  27 /*
  28  * @test
  29  * @bug 8043758
  30  * @summary Datagram Transport Layer Security (DTLS)
  31  * @modules java.base/sun.security.util
  32  * @library /test/lib
  33  * @run main/othervm DTLSOverDatagram
  34  */
  35 

  36 import java.nio.*;
  37 import java.net.*;
  38 import java.util.*;


  39 import javax.net.ssl.*;
  40 
  41 import jdk.test.lib.security.KeyStoreUtils;
  42 import jdk.test.lib.security.SSLContextBuilder;
  43 
  44 import java.util.concurrent.*;
  45 
  46 import sun.security.util.HexDumpEncoder;
  47 
  48 /**
  49  * An example to show the way to use SSLEngine in datagram connections.
  50  */
  51 public class DTLSOverDatagram {
  52 
  53     private static int MAX_HANDSHAKE_LOOPS = 200;
  54     private static int MAX_APP_READ_LOOPS = 60;
  55     private static int SOCKET_TIMEOUT = 10 * 1000; // in millis
  56     private static int BUFFER_SIZE = 1024;
  57     private static int MAXIMUM_PACKET_SIZE = 1024;
  58 
  59     /*
  60      * The following is to set up the keystores.
  61      */
  62     private static String pathToStores = "../etc";
  63     private static String keyStoreFile = "keystore";
  64     private static String trustStoreFile = "truststore";

  65 
  66     private static String keyFilename =
  67             System.getProperty("test.src", ".") + "/" + pathToStores +
  68                 "/" + keyStoreFile;
  69     private static String trustFilename =
  70             System.getProperty("test.src", ".") + "/" + pathToStores +
  71                 "/" + trustStoreFile;
  72     private static Exception clientException = null;
  73     private static Exception serverException = null;
  74 
  75     private static ByteBuffer serverApp =
  76                 ByteBuffer.wrap("Hi Client, I'm Server".getBytes());
  77     private static ByteBuffer clientApp =
  78                 ByteBuffer.wrap("Hi Server, I'm Client".getBytes());
  79 
  80     /*
  81      * =============================================================
  82      * The test case
  83      */
  84     public static void main(String[] args) throws Exception {


 521         if (hs == SSLEngineResult.HandshakeStatus.NEED_TASK) {
 522             throw new Exception("handshake shouldn't need additional tasks");
 523         }
 524     }
 525 
 526     // retransmission if timeout
 527     boolean onReceiveTimeout(SSLEngine engine, SocketAddress socketAddr,
 528             String side, List<DatagramPacket> packets) throws Exception {
 529 
 530         SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus();
 531         if (hs == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {
 532             return false;
 533         } else {
 534             // retransmission of handshake messages
 535             return produceHandshakePackets(engine, socketAddr, side, packets);
 536         }
 537     }
 538 
 539     // get DTSL context
 540     SSLContext getDTLSContext() throws Exception {
 541         String passphrase = "passphrase";
 542         return SSLContextBuilder.builder()
 543                 .trustStore(KeyStoreUtils.loadKeyStore(trustFilename, passphrase))
 544                 .keyStore(KeyStoreUtils.loadKeyStore(keyFilename, passphrase))
 545                 .kmfPassphrase(passphrase)
 546                 .protocol("DTLS")
 547                 .build();

















 548     }
 549 
 550 
 551     /*
 552      * =============================================================
 553      * The remainder is support stuff to kickstart the testing.
 554      */
 555 
 556     // Will the handshaking and application data exchange succeed?
 557     public boolean isGoodJob() {
 558         return true;
 559     }
 560 
 561     public final void runTest(DTLSOverDatagram testCase) throws Exception {
 562         try (DatagramSocket serverSocket = new DatagramSocket();
 563                 DatagramSocket clientSocket = new DatagramSocket()) {
 564 
 565             serverSocket.setSoTimeout(SOCKET_TIMEOUT);
 566             clientSocket.setSoTimeout(SOCKET_TIMEOUT);
 567 


< prev index next >