SSLContextImpl.java revision 6b811c5daec1b28e6f63b57f98a032236f2c3cf7
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.xnet.provider.jsse; 19 20import org.apache.harmony.xnet.provider.jsse.SSLEngineImpl; 21import org.apache.harmony.xnet.provider.jsse.SSLParameters; 22// BEGIN android-removed 23// import org.apache.harmony.xnet.provider.jsse.SSLServerSocketFactoryImpl; 24// END android-removed 25 26import java.security.KeyManagementException; 27import java.security.SecureRandom; 28 29import javax.net.ssl.KeyManager; 30import javax.net.ssl.SSLContextSpi; 31import javax.net.ssl.SSLEngine; 32import javax.net.ssl.SSLServerSocketFactory; 33import javax.net.ssl.SSLSessionContext; 34import javax.net.ssl.SSLSocketFactory; 35import javax.net.ssl.TrustManager; 36 37// BEGIN android-note 38// Modified heavily during SSLSessionContext refactoring. Added support for 39// persistent session caches. 40// END android-note 41 42/** 43 * Implementation of SSLContext service provider interface. 44 */ 45public class SSLContextImpl extends SSLContextSpi { 46 47 /** Client session cache. */ 48 private ClientSessionContext clientSessionContext; 49 50 /** Server session cache. */ 51 private ServerSessionContext serverSessionContext; 52 53 protected SSLParameters sslParameters; 54 55 public SSLContextImpl() { 56 super(); 57 } 58 59 @Override 60 public void engineInit(KeyManager[] kms, TrustManager[] tms, 61 SecureRandom sr) throws KeyManagementException { 62 engineInit(kms, tms, sr, null, null); 63 } 64 65 /** 66 * Initializes this {@code SSLContext} instance. All of the arguments are 67 * optional, and the security providers will be searched for the required 68 * implementations of the needed algorithms. 69 * 70 * @param kms the key sources or {@code null} 71 * @param tms the trust decision sources or {@code null} 72 * @param sr the randomness source or {@code null} 73 * @param clientCache persistent client session cache or {@code null} 74 * @param serverCache persistent server session cache or {@code null} 75 * @throws KeyManagementException if initializing this instance fails 76 */ 77 public void engineInit(KeyManager[] kms, TrustManager[] tms, 78 SecureRandom sr, SSLClientSessionCache clientCache, 79 SSLServerSessionCache serverCache) throws KeyManagementException { 80 sslParameters = new SSLParameters(kms, tms, sr, 81 clientCache, serverCache); 82 clientSessionContext = sslParameters.getClientSessionContext(); 83 serverSessionContext = sslParameters.getServerSessionContext(); 84 } 85 86 public SSLSocketFactory engineGetSocketFactory() { 87 if (sslParameters == null) { 88 throw new IllegalStateException("SSLContext is not initiallized."); 89 } 90 return new OpenSSLSocketFactoryImpl(sslParameters); 91 } 92 93 @Override 94 public SSLServerSocketFactory engineGetServerSocketFactory() { 95 if (sslParameters == null) { 96 throw new IllegalStateException("SSLContext is not initiallized."); 97 } 98 return new OpenSSLServerSocketFactoryImpl(sslParameters); 99 } 100 101 @Override 102 public SSLEngine engineCreateSSLEngine(String host, int port) { 103 if (sslParameters == null) { 104 throw new IllegalStateException("SSLContext is not initiallized."); 105 } 106 return new SSLEngineImpl(host, port, 107 (SSLParameters) sslParameters.clone()); 108 } 109 110 @Override 111 public SSLEngine engineCreateSSLEngine() { 112 if (sslParameters == null) { 113 throw new IllegalStateException("SSLContext is not initiallized."); 114 } 115 return new SSLEngineImpl((SSLParameters) sslParameters.clone()); 116 } 117 118 @Override 119 public ServerSessionContext engineGetServerSessionContext() { 120 return serverSessionContext; 121 } 122 123 @Override 124 public ClientSessionContext engineGetClientSessionContext() { 125 return clientSessionContext; 126 } 127} 128