summaryrefslogtreecommitdiff
path: root/java/src/com/google/polo/ssl/SSLSocketFactoryWrapper.java
blob: 5532155df6eb3b06140de5a5bbdfd51839fc086d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (C) 2009 Google Inc.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.polo.ssl;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import javax.net.SocketFactory;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;


/**
 * A convenience wrapper to generate an {@link SSLSocketFactory} that uses the
 * {@link KeyManager} and {@link TrustManager} instances that are passed in.
 */
public class SSLSocketFactoryWrapper extends SSLSocketFactory {
  
  /**
   * The internal SSLSocketFactory which will be wrapped.
   */
  private SSLSocketFactory mFactory;

  public static SocketFactory getDefault() {
    throw new IllegalStateException("Not implemented.");
  }

  public SSLSocketFactoryWrapper() {
    throw new IllegalStateException("Not implemented.");
  }
  
  public SSLSocketFactoryWrapper(KeyManager[] keyManagers,
      TrustManager[] trustManagers) throws NoSuchAlgorithmException,
      KeyManagementException {
    java.security.Security.addProvider(
        new org.bouncycastle.jce.provider.BouncyCastleProvider());
    
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(keyManagers, trustManagers, null);
    mFactory = sslcontext.getSocketFactory();
  }
  
  public static SSLSocketFactoryWrapper CreateWithDummyTrustManager(
      KeyManager[] keyManagers) throws KeyManagementException,
      NoSuchAlgorithmException {
    TrustManager[] trustManagers = { new DummyTrustManager() };
    return new SSLSocketFactoryWrapper(keyManagers, trustManagers);
  }

  @Override
  public Socket createSocket() throws IOException {
    return mFactory.createSocket();
  }


  @Override
  public Socket createSocket(InetAddress inaddr, int i)
      throws IOException {
    return mFactory.createSocket(inaddr, i);
  }

  @Override
  public Socket createSocket(InetAddress inaddr, int i,
      InetAddress inaddr1, int j) throws IOException {
    return mFactory.createSocket(inaddr, i, inaddr1, j);
  }

  @Override
  public Socket createSocket(Socket socket, String s, int i, boolean flag)
      throws IOException {
    return mFactory.createSocket(socket, s, i, flag);
  }

  @Override
  public Socket createSocket(String s, int i) throws IOException {
    return mFactory.createSocket(s, i);
  }

  @Override
  public Socket createSocket(String s, int i, InetAddress inaddr, int j)
      throws IOException {
    return mFactory.createSocket(s, i, inaddr, j);
  }

  @Override
  public String[] getDefaultCipherSuites() {
    return mFactory.getDefaultCipherSuites();
  }

  @Override
  public String[] getSupportedCipherSuites() {
    return mFactory.getSupportedCipherSuites();
  }
}