1 /* 2 * Copyright (c) 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 package jdk.test.lib.security; 25 26 import java.security.KeyStore; 27 import java.security.SecureRandom; 28 29 import javax.net.ssl.KeyManagerFactory; 30 import javax.net.ssl.SSLContext; 31 import javax.net.ssl.TrustManagerFactory; 32 33 /* 34 * SSL context builder. 35 */ 36 public class SSLContextBuilder { 37 38 // Trust store 39 private KeyStore trustStore = null; 40 41 // Key store 42 private KeyStore keyStore = null; 43 44 // Trust manager factory algorithm 45 private String tmfAlgo = TrustManagerFactory.getDefaultAlgorithm(); 46 47 // Key manager factory algorithm 48 private String kmfAlgo = KeyManagerFactory.getDefaultAlgorithm(); 49 50 // Key manager factory passphrase 51 private String kmfPassphrase = null; 52 53 // Context protocol 54 private String protocol = "TLS"; 55 56 private SecureRandom random = null; 57 58 public SSLContextBuilder trustStore(KeyStore trustStore) { 59 this.trustStore = trustStore; 60 return this; 61 } 62 63 public SSLContextBuilder keyStore(KeyStore keyStore) { 64 this.keyStore = keyStore; 65 return this; 66 } 67 68 public SSLContextBuilder tmfAlgo(String tmfAlgo) { 69 this.tmfAlgo = tmfAlgo; 70 return this; 71 } 72 73 public SSLContextBuilder kmfAlgo(String kmfAlgo) { 74 this.kmfAlgo = kmfAlgo; 75 return this; 76 } 77 78 public SSLContextBuilder kmfPassphrase(String kmfPassphrase) { 79 this.kmfPassphrase = kmfPassphrase; 80 return this; 81 } 82 83 public SSLContextBuilder protocol(String protocol) { 84 this.protocol = protocol; 85 return this; 86 } 87 88 public SSLContextBuilder random(SecureRandom random) { 89 this.random = random; 90 return this; 91 } 92 93 public SSLContext build() throws Exception { 94 return buildSSLContext( 95 trustStore, keyStore, 96 tmfAlgo, kmfAlgo, kmfPassphrase, 97 protocol, random); 98 } 99 100 public static SSLContextBuilder builder() { 101 return new SSLContextBuilder(); 102 } 103 104 /** 105 * The default TLS context. 106 */ 107 public static SSLContext defaultTLSContext() throws Exception { 108 return builder() 109 .trustStore(KeyStoreUtils.defaultTrustStore()) 110 .keyStore(KeyStoreUtils.defaultKeyStore()) 111 .build(); 112 } 113 114 /** 115 * The default DTLS context. 116 */ 117 public static SSLContext defaultDTLSContext() throws Exception { 118 return builder() 119 .trustStore(KeyStoreUtils.defaultTrustStore()) 120 .keyStore(KeyStoreUtils.defaultKeyStore()) 121 .protocol("DTLS") 122 .build(); 123 } 124 125 private static SSLContext buildSSLContext( 126 KeyStore trustStore, KeyStore keyStore, 127 String tmfAlgo, String kmfAlgo, String kmfPassphrase, 128 String protocol, SecureRandom random) throws Exception { 129 TrustManagerFactory tmf = null; 130 if (trustStore != null) { 131 tmf = TrustManagerFactory.getInstance(tmfAlgo); 132 tmf.init(trustStore); 133 } 134 135 KeyManagerFactory kmf = null; 136 if (keyStore != null) { 137 kmf = KeyManagerFactory.getInstance(kmfAlgo); 138 kmf.init(keyStore, 139 kmfPassphrase == null ? null : kmfPassphrase.toCharArray()); 140 } 141 142 SSLContext context = SSLContext.getInstance(protocol); 143 context.init( 144 kmf == null ? null : kmf.getKeyManagers(), 145 tmf == null ? null : tmf.getTrustManagers(), 146 random); 147 return context; 148 } 149 }