Home | History | Annotate | Download | only in ssl
      1 /*
      2  * Copyright (C) 2009 Google Inc.  All rights reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.polo.ssl;
     18 
     19 import java.io.IOException;
     20 import java.net.InetAddress;
     21 import java.net.ServerSocket;
     22 import java.security.KeyManagementException;
     23 import java.security.NoSuchAlgorithmException;
     24 
     25 import javax.net.ssl.KeyManager;
     26 import javax.net.ssl.SSLContext;
     27 import javax.net.ssl.SSLServerSocketFactory;
     28 import javax.net.ssl.TrustManager;
     29 
     30 
     31 /**
     32  * A convenience wrapper to generate an {@link SSLServerSocketFactory} that uses
     33  * the given {@link KeyManager} and {@link TrustManager} instances.
     34  */
     35 public class SSLServerSocketFactoryWrapper extends SSLServerSocketFactory {
     36 
     37   /**
     38    * The internal SSLServerSocketFactory which will be wrapped.
     39    */
     40   private SSLServerSocketFactory mFactory;
     41 
     42   public SSLServerSocketFactoryWrapper(KeyManager[] keyManagers,
     43       TrustManager[] trustManagers)
     44       throws NoSuchAlgorithmException, KeyManagementException {
     45     SSLContext sslcontext = SSLContext.getInstance("TLS");
     46     sslcontext.init(keyManagers, trustManagers, null);
     47     mFactory = sslcontext.getServerSocketFactory();
     48   }
     49 
     50   public static SSLServerSocketFactoryWrapper CreateWithDummyTrustManager(
     51       KeyManager[] keyManagers) throws KeyManagementException,
     52       NoSuchAlgorithmException {
     53     TrustManager[] trustManagers = { new DummyTrustManager() };
     54     return new SSLServerSocketFactoryWrapper(keyManagers, trustManagers);
     55   }
     56 
     57   @Override
     58   public ServerSocket createServerSocket(int port) throws IOException {
     59     return mFactory.createServerSocket(port);
     60   }
     61 
     62   @Override
     63   public ServerSocket createServerSocket(int port, int backlog)
     64       throws IOException {
     65     return mFactory.createServerSocket(port, backlog);
     66   }
     67 
     68   @Override
     69   public ServerSocket createServerSocket(int port, int backlog,
     70       InetAddress ifAddress) throws IOException {
     71     return mFactory.createServerSocket(port, backlog, ifAddress);
     72   }
     73 
     74   @Override
     75   public String[] getDefaultCipherSuites() {
     76     return mFactory.getDefaultCipherSuites();
     77   }
     78 
     79   @Override
     80   public String[] getSupportedCipherSuites() {
     81     return mFactory.getSupportedCipherSuites();
     82   }
     83 
     84 }
     85