blob: 5532155df6eb3b06140de5a5bbdfd51839fc086d [file] [log] [blame]
Jerome Poichet7c997852014-05-20 10:50:05 -07001/*
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
17package com.google.polo.ssl;
18
19import java.io.IOException;
20import java.net.InetAddress;
21import java.net.Socket;
22import java.security.KeyManagementException;
23import java.security.NoSuchAlgorithmException;
24
25import javax.net.SocketFactory;
26import javax.net.ssl.KeyManager;
27import javax.net.ssl.SSLContext;
28import javax.net.ssl.SSLSocketFactory;
29import javax.net.ssl.TrustManager;
30
31
32/**
33 * A convenience wrapper to generate an {@link SSLSocketFactory} that uses the
34 * {@link KeyManager} and {@link TrustManager} instances that are passed in.
35 */
36public class SSLSocketFactoryWrapper extends SSLSocketFactory {
37
38 /**
39 * The internal SSLSocketFactory which will be wrapped.
40 */
41 private SSLSocketFactory mFactory;
42
43 public static SocketFactory getDefault() {
44 throw new IllegalStateException("Not implemented.");
45 }
46
47 public SSLSocketFactoryWrapper() {
48 throw new IllegalStateException("Not implemented.");
49 }
50
51 public SSLSocketFactoryWrapper(KeyManager[] keyManagers,
52 TrustManager[] trustManagers) throws NoSuchAlgorithmException,
53 KeyManagementException {
54 java.security.Security.addProvider(
55 new org.bouncycastle.jce.provider.BouncyCastleProvider());
56
57 SSLContext sslcontext = SSLContext.getInstance("TLS");
58 sslcontext.init(keyManagers, trustManagers, null);
59 mFactory = sslcontext.getSocketFactory();
60 }
61
62 public static SSLSocketFactoryWrapper CreateWithDummyTrustManager(
63 KeyManager[] keyManagers) throws KeyManagementException,
64 NoSuchAlgorithmException {
65 TrustManager[] trustManagers = { new DummyTrustManager() };
66 return new SSLSocketFactoryWrapper(keyManagers, trustManagers);
67 }
68
69 @Override
70 public Socket createSocket() throws IOException {
71 return mFactory.createSocket();
72 }
73
74
75 @Override
76 public Socket createSocket(InetAddress inaddr, int i)
77 throws IOException {
78 return mFactory.createSocket(inaddr, i);
79 }
80
81 @Override
82 public Socket createSocket(InetAddress inaddr, int i,
83 InetAddress inaddr1, int j) throws IOException {
84 return mFactory.createSocket(inaddr, i, inaddr1, j);
85 }
86
87 @Override
88 public Socket createSocket(Socket socket, String s, int i, boolean flag)
89 throws IOException {
90 return mFactory.createSocket(socket, s, i, flag);
91 }
92
93 @Override
94 public Socket createSocket(String s, int i) throws IOException {
95 return mFactory.createSocket(s, i);
96 }
97
98 @Override
99 public Socket createSocket(String s, int i, InetAddress inaddr, int j)
100 throws IOException {
101 return mFactory.createSocket(s, i, inaddr, j);
102 }
103
104 @Override
105 public String[] getDefaultCipherSuites() {
106 return mFactory.getDefaultCipherSuites();
107 }
108
109 @Override
110 public String[] getSupportedCipherSuites() {
111 return mFactory.getSupportedCipherSuites();
112 }
113}