Home | History | Annotate | Download | only in jsse
      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 
     18 package org.apache.harmony.xnet.provider.jsse;
     19 
     20 import org.apache.harmony.xnet.provider.jsse.SSLEngineImpl;
     21 import 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 
     26 import java.security.KeyManagementException;
     27 import java.security.SecureRandom;
     28 
     29 import javax.net.ssl.KeyManager;
     30 import javax.net.ssl.SSLContextSpi;
     31 import javax.net.ssl.SSLEngine;
     32 import javax.net.ssl.SSLServerSocketFactory;
     33 import javax.net.ssl.SSLSessionContext;
     34 import javax.net.ssl.SSLSocketFactory;
     35 import 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  */
     45 public 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      * @since Android 1.1
     78      */
     79     public void engineInit(KeyManager[] kms, TrustManager[] tms,
     80             SecureRandom sr, SSLClientSessionCache clientCache,
     81             SSLServerSessionCache serverCache) throws KeyManagementException {
     82         sslParameters = new SSLParameters(kms, tms, sr,
     83                 clientCache, serverCache);
     84         clientSessionContext = sslParameters.getClientSessionContext();
     85         serverSessionContext = sslParameters.getServerSessionContext();
     86     }
     87 
     88     public SSLSocketFactory engineGetSocketFactory() {
     89         if (sslParameters == null) {
     90             throw new IllegalStateException("SSLContext is not initiallized.");
     91         }
     92         return new OpenSSLSocketFactoryImpl(sslParameters);
     93     }
     94 
     95     @Override
     96     public SSLServerSocketFactory engineGetServerSocketFactory() {
     97         if (sslParameters == null) {
     98             throw new IllegalStateException("SSLContext is not initiallized.");
     99         }
    100         return new OpenSSLServerSocketFactoryImpl(sslParameters);
    101     }
    102 
    103     @Override
    104     public SSLEngine engineCreateSSLEngine(String host, int port) {
    105         if (sslParameters == null) {
    106             throw new IllegalStateException("SSLContext is not initiallized.");
    107         }
    108         return new SSLEngineImpl(host, port,
    109                 (SSLParameters) sslParameters.clone());
    110     }
    111 
    112     @Override
    113     public SSLEngine engineCreateSSLEngine() {
    114         if (sslParameters == null) {
    115             throw new IllegalStateException("SSLContext is not initiallized.");
    116         }
    117         return new SSLEngineImpl((SSLParameters) sslParameters.clone());
    118     }
    119 
    120     @Override
    121     public ServerSessionContext engineGetServerSessionContext() {
    122         return serverSessionContext;
    123     }
    124 
    125     @Override
    126     public ClientSessionContext engineGetClientSessionContext() {
    127         return clientSessionContext;
    128     }
    129 }