1 module hunt.net.ssl.SSLContext; 2 3 // dfmt off 4 version(WITH_HUNT_SECURITY): 5 // dfmt on 6 7 // import hunt.net.ssl.KeyManager; 8 import hunt.net.ssl.SSLContextSpi; 9 import hunt.net.ssl.SSLEngine; 10 import hunt.net.ssl.SSLParameters; 11 import hunt.net.ssl.SSLSessionContext; 12 13 import hunt.net.KeyCertOptions; 14 import hunt.net.secure.conscrypt.OpenSSLContextImpl; 15 // import hunt.security.Provider; 16 17 import hunt.Exceptions; 18 19 /** 20 * Instances of this class represent a secure socket protocol 21 * implementation which acts as a factory for secure socket 22 * factories or <code>SSLEngine</code>s. This class is initialized 23 * with an optional set of key and trust managers and source of 24 * secure random bytes. 25 * 26 * <p> Every implementation of the Java platform is required to support the 27 * following standard <code>SSLContext</code> protocol: 28 * <ul> 29 * <li><tt>TLSv1</tt></li> 30 * </ul> 31 * This protocol is described in the <a href= 32 * "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext"> 33 * SSLContext section</a> of the 34 * Java Cryptography Architecture Standard Algorithm Name Documentation. 35 * Consult the release documentation for your implementation to see if any 36 * other algorithms are supported. 37 * 38 */ 39 class SSLContext { 40 // private Provider provider; 41 42 private SSLContextSpi contextSpi; 43 44 private string protocol; 45 46 /** 47 * Creates an SSLContext object. 48 * 49 * @param contextSpi the delegate 50 * @param provider the provider 51 * @param protocol the protocol 52 */ 53 protected this(SSLContextSpi contextSpi, // Provider provider, 54 string protocol) { 55 this.contextSpi = contextSpi; 56 // this.provider = provider; 57 this.protocol = protocol; 58 } 59 60 private static SSLContext defaultContext; 61 62 /** 63 * Returns the default SSL context. 64 * 65 * <p>If a default context was set using the {@link #setDefault 66 * SSLContext.setDefault()} method, it is returned. Otherwise, the first 67 * call of this method triggers the call 68 * <code>SSLContext.getInstance("Default")</code>. 69 * If successful, that object is made the default SSL context and returned. 70 * 71 * <p>The default context is immediately 72 * usable and does not require {@linkplain #init initialization}. 73 * 74 * @return the default SSL context 75 * @throws NoSuchAlgorithmException if the 76 * {@link SSLContext#getInstance SSLContext.getInstance()} call fails 77 */ 78 // static synchronized SSLContext getDefault(){ 79 // if (defaultContext is null) { 80 // defaultContext = SSLContext.getInstance("Default"); 81 // } 82 // return defaultContext; 83 // } 84 85 /** 86 * Sets the default SSL context. It will be returned by subsequent calls 87 * to {@link #getDefault}. The default context must be immediately usable 88 * and not require {@linkplain #init initialization}. 89 * 90 * @param context the SSLContext 91 * @throws NullPointerException if context is null 92 * @throws SecurityException if a security manager exists and its 93 * <code>checkPermission</code> method does not allow 94 * <code>SSLPermission("setDefaultSSLContext")</code> 95 */ 96 static void setDefault(SSLContext context) { 97 if (context is null) { 98 throw new NullPointerException(""); 99 } 100 implementationMissing(); 101 // SecurityManager sm = System.getSecurityManager(); 102 // if (sm !is null) { 103 // sm.checkPermission(new SSLPermission("setDefaultSSLContext")); 104 // } 105 defaultContext = context; 106 } 107 108 /** 109 * Returns a <code>SSLContext</code> object that implements the 110 * specified secure socket protocol. 111 * 112 * <p> This method traverses the list of registered security Providers, 113 * starting with the most preferred Provider. 114 * A new SSLContext object encapsulating the 115 * SSLContextSpi implementation from the first 116 * Provider that supports the specified protocol is returned. 117 * 118 * <p> Note that the list of registered providers may be retrieved via 119 * the {@link Security#getProviders() Security.getProviders()} method. 120 * 121 * @param protocol the standard name of the requested protocol. 122 * See the SSLContext section in the <a href= 123 * "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext"> 124 * Java Cryptography Architecture Standard Algorithm Name 125 * Documentation</a> 126 * for information about standard protocol names. 127 * 128 * @return the new <code>SSLContext</code> object. 129 * 130 * @exception NoSuchAlgorithmException if no Provider supports a 131 * SSLContextSpi implementation for the 132 * specified protocol. 133 * @exception NullPointerException if protocol is null. 134 * 135 */ 136 // static SSLContext getInstance(string protocol) { 137 // return getInstance(protocol, ""); 138 // // GetInstance.Instance instance = GetInstance.getInstance 139 // // ("SSLContext", SSLContextSpi.class, protocol); 140 // // return new SSLContext(cast(SSLContextSpi)instance.impl, instance.provider, 141 // // protocol); 142 // } 143 144 /** 145 * Returns a <code>SSLContext</code> object that implements the 146 * specified secure socket protocol. 147 * 148 * <p> A new SSLContext object encapsulating the 149 * SSLContextSpi implementation from the specified provider 150 * is returned. The specified provider must be registered 151 * in the security provider list. 152 * 153 * <p> Note that the list of registered providers may be retrieved via 154 * the {@link Security#getProviders() Security.getProviders()} method. 155 * 156 * @param protocol the standard name of the requested protocol. 157 * See the SSLContext section in the <a href= 158 * "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext"> 159 * Java Cryptography Architecture Standard Algorithm Name 160 * Documentation</a> 161 * for information about standard protocol names. 162 * 163 * @param provider the name of the provider. 164 * 165 * @return the new <code>SSLContext</code> object. 166 * 167 * @throws NoSuchAlgorithmException if a SSLContextSpi 168 * implementation for the specified protocol is not 169 * available from the specified provider. 170 * 171 * @throws NoSuchProviderException if the specified provider is not 172 * registered in the security provider list. 173 * 174 * @throws IllegalArgumentException if the provider name is null or empty. 175 * @throws NullPointerException if protocol is null. 176 * 177 */ 178 static SSLContext getInstance(KeyCertOptions options, string protocol) { 179 SSLContextSpi impl = new DefaultSSLContextImpl(options); 180 return new SSLContext(impl, protocol); 181 } 182 183 // static SSLContext getInstance(string certificate, string privatekey, string protocol, string provider) { 184 // SSLContextSpi impl = new DefaultSSLContextImpl(certificate, privatekey); 185 // return new SSLContext(impl, null, protocol); 186 // } 187 188 /** 189 * Returns a <code>SSLContext</code> object that implements the 190 * specified secure socket protocol. 191 * 192 * <p> A new SSLContext object encapsulating the 193 * SSLContextSpi implementation from the specified Provider 194 * object is returned. Note that the specified Provider object 195 * does not have to be registered in the provider list. 196 * 197 * @param protocol the standard name of the requested protocol. 198 * See the SSLContext section in the <a href= 199 * "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext"> 200 * Java Cryptography Architecture Standard Algorithm Name 201 * Documentation</a> 202 * for information about standard protocol names. 203 * 204 * @param provider an instance of the provider. 205 * 206 * @return the new <code>SSLContext</code> object. 207 * 208 * @throws NoSuchAlgorithmException if a SSLContextSpi 209 * implementation for the specified protocol is not available 210 * from the specified Provider object. 211 * 212 * @throws IllegalArgumentException if the provider is null. 213 * @throws NullPointerException if protocol is null. 214 * 215 */ 216 // static SSLContext getInstance(string protocol, Provider provider) { 217 218 // implementationMissing(); 219 // return null; 220 221 // // GetInstance.Instance instance = GetInstance.getInstance 222 // // ("SSLContext", SSLContextSpi.class, protocol, provider); 223 // // return new SSLContext(cast(SSLContextSpi)instance.impl, instance.provider, 224 // // protocol); 225 // } 226 227 /** 228 * Returns the protocol name of this <code>SSLContext</code> object. 229 * 230 * <p>This is the same name that was specified in one of the 231 * <code>getInstance</code> calls that created this 232 * <code>SSLContext</code> object. 233 * 234 * @return the protocol name of this <code>SSLContext</code> object. 235 */ 236 final string getProtocol() { 237 return this.protocol; 238 } 239 240 /** 241 * Returns the provider of this <code>SSLContext</code> object. 242 * 243 * @return the provider of this <code>SSLContext</code> object 244 */ 245 // final Provider getProvider() { 246 // return this.provider; 247 // } 248 249 /** 250 * Initializes this context. Either of the first two parameters 251 * may be null in which case the installed security providers will 252 * be searched for the highest priority implementation of the 253 * appropriate factory. Likewise, the secure random parameter may 254 * be null in which case the default implementation will be used. 255 * <P> 256 * Only the first instance of a particular key and/or trust manager 257 * implementation type in the array is used. (For example, only 258 * the first javax.net.ssl.X509KeyManager in the array will be used.) 259 * 260 * @param km the sources of authentication keys or null 261 * @param tm the sources of peer authentication trust decisions or null 262 * @param random the source of randomness for this generator or null 263 * @throws KeyManagementException if this operation fails 264 */ 265 // final void init(KeyManager[] km, TrustManager[] tm) { 266 // contextSpi.engineInit(km, tm); 267 // } 268 269 final void initialize(KeyCertOptions options) { 270 contextSpi.engineInit(options); 271 } 272 273 /** 274 * Returns a <code>SocketFactory</code> object for this 275 * context. 276 * 277 * @return the <code>SocketFactory</code> object 278 * @throws IllegalStateException if the SSLContextImpl requires 279 * initialization and the <code>init()</code> has not been called 280 */ 281 // final SSLSocketFactory getSocketFactory() { 282 // return contextSpi.engineGetSocketFactory(); 283 // } 284 285 /** 286 * Returns a <code>ServerSocketFactory</code> object for 287 * this context. 288 * 289 * @return the <code>ServerSocketFactory</code> object 290 * @throws IllegalStateException if the SSLContextImpl requires 291 * initialization and the <code>init()</code> has not been called 292 */ 293 // final SSLServerSocketFactory getServerSocketFactory() { 294 // return contextSpi.engineGetServerSocketFactory(); 295 // } 296 297 /** 298 * Creates a new <code>SSLEngine</code> using this context. 299 * <P> 300 * Applications using this factory method are providing no hints 301 * for an internal session reuse strategy. If hints are desired, 302 * {@link #createSSLEngine(string, int)} should be used 303 * instead. 304 * <P> 305 * Some cipher suites (such as Kerberos) require remote hostname 306 * information, in which case this factory method should not be used. 307 * 308 * @return the <code>SSLEngine</code> object 309 * @throws UnsupportedOperationException if the underlying provider 310 * does not implement the operation. 311 * @throws IllegalStateException if the SSLContextImpl requires 312 * initialization and the <code>init()</code> has not been called 313 * @since 1.5 314 */ 315 final SSLEngine createSSLEngine(bool clientMode) { 316 return contextSpi.engineCreateSSLEngine(clientMode); 317 } 318 319 /** 320 * Creates a new <code>SSLEngine</code> using this context using 321 * advisory peer information. 322 * <P> 323 * Applications using this factory method are providing hints 324 * for an internal session reuse strategy. 325 * <P> 326 * Some cipher suites (such as Kerberos) require remote hostname 327 * information, in which case peerHost needs to be specified. 328 * 329 * @param peerHost the non-authoritative name of the host 330 * @param peerPort the non-authoritative port 331 * @return the new <code>SSLEngine</code> object 332 * @throws UnsupportedOperationException if the underlying provider 333 * does not implement the operation. 334 * @throws IllegalStateException if the SSLContextImpl requires 335 * initialization and the <code>init()</code> has not been called 336 * @since 1.5 337 */ 338 final SSLEngine createSSLEngine(bool clientMode, string peerHost, int peerPort) { 339 return contextSpi.engineCreateSSLEngine(clientMode, peerHost, peerPort); 340 } 341 342 /** 343 * Returns the server session context, which represents the set of 344 * SSL sessions available for use during the handshake phase of 345 * server-side SSL sockets. 346 * <P> 347 * This context may be unavailable in some environments, in which 348 * case this method returns null. For example, when the underlying 349 * SSL provider does not provide an implementation of SSLSessionContext 350 * interface, this method returns null. A non-null session context 351 * is returned otherwise. 352 * 353 * @return server session context bound to this SSL context 354 */ 355 final SSLSessionContext getServerSessionContext() { 356 return contextSpi.engineGetServerSessionContext(); 357 } 358 359 /** 360 * Returns the client session context, which represents the set of 361 * SSL sessions available for use during the handshake phase of 362 * client-side SSL sockets. 363 * <P> 364 * This context may be unavailable in some environments, in which 365 * case this method returns null. For example, when the underlying 366 * SSL provider does not provide an implementation of SSLSessionContext 367 * interface, this method returns null. A non-null session context 368 * is returned otherwise. 369 * 370 * @return client session context bound to this SSL context 371 */ 372 final SSLSessionContext getClientSessionContext() { 373 return contextSpi.engineGetClientSessionContext(); 374 } 375 376 /** 377 * Returns a copy of the SSLParameters indicating the default 378 * settings for this SSL context. 379 * 380 * <p>The parameters will always have the ciphersuites and protocols 381 * arrays set to non-null values. 382 * 383 * @return a copy of the SSLParameters object with the default settings 384 * @throws UnsupportedOperationException if the default SSL parameters 385 * could not be obtained. 386 */ 387 // final SSLParameters getDefaultSSLParameters() { 388 // return contextSpi.engineGetDefaultSSLParameters(); 389 // } 390 391 /** 392 * Returns a copy of the SSLParameters indicating the supported 393 * settings for this SSL context. 394 * 395 * <p>The parameters will always have the ciphersuites and protocols 396 * arrays set to non-null values. 397 * 398 * @return a copy of the SSLParameters object with the supported 399 * settings 400 * @throws UnsupportedOperationException if the supported SSL parameters 401 * could not be obtained. 402 */ 403 // final SSLParameters getSupportedSSLParameters() { 404 // return contextSpi.engineGetSupportedSSLParameters(); 405 // } 406 407 }