blob: 4a708431da28bef30d0917a32f8867542c3d0f0e [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
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 javax.net.ssl;
19
20import java.io.IOException;
21import java.net.InetAddress;
22import java.net.Socket;
23import java.net.UnknownHostException;
24
25/**
26 * The extension of {@code Socket} providing secure protocols like SSL (Secure
Jesse Wilsonf9215792009-08-25 16:30:17 -070027 * Socket Layer") or TLS (Transport Layer Security).
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080028 */
29public abstract class SSLSocket extends Socket {
Jesse Wilsonf9215792009-08-25 16:30:17 -070030
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080031 /**
32 * Only to be used by subclasses.
33 * <p>
34 * Creates a TCP socket.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080035 */
36 protected SSLSocket() {
37 super();
38 }
39
40 /**
41 * Only to be used by subclasses.
42 * <p>
43 * Creates a TCP socket connection to the specified host at the specified
44 * port.
Jesse Wilsonf9215792009-08-25 16:30:17 -070045 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080046 * @param host
47 * the host name to connect to.
48 * @param port
49 * the port number to connect to.
50 * @throws IOException
51 * if creating the socket fails.
52 * @throws UnknownHostException
53 * if the specified host is not known.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080054 */
Jesse Wilsonf9215792009-08-25 16:30:17 -070055 protected SSLSocket(String host, int port) throws IOException, UnknownHostException {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080056 super(host, port);
57 }
58
59 /**
60 * Only to be used by subclasses.
61 * <p>
62 * Creates a TCP socket connection to the specified address at the specified
63 * port.
Jesse Wilsonf9215792009-08-25 16:30:17 -070064 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080065 * @param address
66 * the address to connect to.
67 * @param port
68 * the port number to connect to.
69 * @throws IOException
70 * if creating the socket fails.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080071 */
72 protected SSLSocket(InetAddress address, int port) throws IOException {
73 super(address, port);
74 }
75
76 /**
77 * Only to be used by subclasses.
78 * <p>
79 * Creates a TCP socket connection to the specified host at the specified
80 * port with the client side bound to the specified address and port.
Jesse Wilsonf9215792009-08-25 16:30:17 -070081 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080082 * @param host
83 * the host name to connect to.
84 * @param port
85 * the port number to connect to.
86 * @param clientAddress
87 * the client address to bind to
88 * @param clientPort
89 * the client port number to bind to.
90 * @throws IOException
91 * if creating the socket fails.
92 * @throws UnknownHostException
93 * if the specified host is not known.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080094 */
Jesse Wilsonf9215792009-08-25 16:30:17 -070095 protected SSLSocket(String host, int port, InetAddress clientAddress, int clientPort)
96 throws IOException, UnknownHostException {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080097 super(host, port, clientAddress, clientPort);
98 }
99
100 /**
101 * Only to be used by subclasses.
102 * <p>
103 * Creates a TCP socket connection to the specified address at the specified
104 * port with the client side bound to the specified address and port.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700105 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800106 * @param address
107 * the address to connect to.
108 * @param port
109 * the port number to connect to.
110 * @param clientAddress
111 * the client address to bind to.
112 * @param clientPort
113 * the client port number to bind to.
114 * @throws IOException
115 * if creating the socket fails.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800116 */
Jesse Wilsonf9215792009-08-25 16:30:17 -0700117 protected SSLSocket(InetAddress address, int port, InetAddress clientAddress, int clientPort)
118 throws IOException {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800119 super(address, port, clientAddress, clientPort);
120 }
Jesse Wilsonf9215792009-08-25 16:30:17 -0700121
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800122 /**
123 * Returns the names of the supported cipher suites.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700124 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800125 * @return the names of the supported cipher suites.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800126 */
127 public abstract String[] getSupportedCipherSuites();
Jesse Wilsonf9215792009-08-25 16:30:17 -0700128
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800129 /**
130 * Returns the names of the enabled cipher suites.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700131 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800132 * @return the names of the enabled cipher suites.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800133 */
134 public abstract String[] getEnabledCipherSuites();
Jesse Wilsonf9215792009-08-25 16:30:17 -0700135
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800136 /**
137 * Sets the names of the cipher suites to be enabled.
138 * Only cipher suites returned by {@link #getSupportedCipherSuites()} are
139 * allowed.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700140 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800141 * @param suites
142 * the names of the to be enabled cipher suites.
143 * @throws IllegalArgumentException
144 * if one of the cipher suite names is not supported.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800145 */
146 public abstract void setEnabledCipherSuites(String[] suites);
Jesse Wilsonf9215792009-08-25 16:30:17 -0700147
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800148 /**
149 * Returns the names of the supported protocols.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700150 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800151 * @return the names of the supported protocols.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800152 */
153 public abstract String[] getSupportedProtocols();
Jesse Wilsonf9215792009-08-25 16:30:17 -0700154
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800155 /**
156 * Returns the names of the enabled protocols.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700157 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800158 * @return the names of the enabled protocols.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800159 */
160 public abstract String[] getEnabledProtocols();
Jesse Wilsonf9215792009-08-25 16:30:17 -0700161
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800162 /**
163 * Sets the names of the protocols to be enabled. Only
164 * protocols returned by {@link #getSupportedProtocols()} are allowed.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700165 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800166 * @param protocols
167 * the names of the to be enabled protocols.
168 * @throws IllegalArgumentException
169 * if one of the protocols is not supported.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800170 */
171 public abstract void setEnabledProtocols(String[] protocols);
Jesse Wilsonf9215792009-08-25 16:30:17 -0700172
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800173 /**
174 * Returns the {@code SSLSession} for this connection. If necessary, a
175 * handshake will be initiated, in which case this method will block until the handshake
176 * has been established. If the handshake fails, an invalid session object
177 * will be returned.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700178 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800179 * @return the session object.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800180 */
181 public abstract SSLSession getSession();
Jesse Wilsonf9215792009-08-25 16:30:17 -0700182
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800183 /**
184 * Registers the specified listener to receive notification on completion of a
185 * handshake on this connection.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700186 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800187 * @param listener
188 * the listener to register.
189 * @throws IllegalArgumentException
190 * if {@code listener} is {@code null}.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800191 */
192 public abstract void addHandshakeCompletedListener(HandshakeCompletedListener listener);
Jesse Wilsonf9215792009-08-25 16:30:17 -0700193
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800194 /**
195 * Removes the specified handshake completion listener.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700196 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800197 * @param listener
198 * the listener to remove.
199 * @throws IllegalArgumentException
200 * if the specified listener is not registered or {@code null}.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800201 */
202 public abstract void removeHandshakeCompletedListener(HandshakeCompletedListener listener);
Jesse Wilsonf9215792009-08-25 16:30:17 -0700203
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800204 /**
205 * Starts a new SSL handshake on this connection.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700206 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800207 * @throws IOException
208 * if an error occurs.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800209 */
210 public abstract void startHandshake() throws IOException;
Jesse Wilsonf9215792009-08-25 16:30:17 -0700211
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800212 /**
213 * Sets whether this connection should act in client mode when handshaking.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700214 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800215 * @param mode
216 * {@code true} if this connection should act in client mode,
217 * {@code false} if not.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800218 */
219 public abstract void setUseClientMode(boolean mode);
Jesse Wilsonf9215792009-08-25 16:30:17 -0700220
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800221 /**
222 * Returns whether this connection will act in client mode when handshaking.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700223 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800224 * @return {@code true} if this connections will act in client mode when
225 * handshaking, {@code false} if not.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800226 */
227 public abstract boolean getUseClientMode();
Jesse Wilsonf9215792009-08-25 16:30:17 -0700228
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800229 /**
230 * Sets whether this connection should require client authentication. This
231 * is only useful for sockets in server mode. The client authentication is
232 * one of the following:
233 * <ul>
234 * <li>authentication required</li>
235 * <li>authentication requested</li>
236 * <li>no authentication needed</li>
237 * </ul>
238 * This method overrides the setting of {@link #setWantClientAuth(boolean)}.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700239 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800240 * @param need
241 * {@code true} if client authentication is required,
242 * {@code false} if no authentication is needed.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800243 */
244 public abstract void setNeedClientAuth(boolean need);
Jesse Wilsonf9215792009-08-25 16:30:17 -0700245
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800246 /**
247 * Returns whether this connection requires client authentication.
248 * This is only useful for sockets in server mode.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700249 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800250 * @return {@code true} if client authentication is required, {@code false}
251 * if no client authentication is needed.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800252 */
253 public abstract boolean getNeedClientAuth();
Jesse Wilsonf9215792009-08-25 16:30:17 -0700254
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800255 /**
256 * Sets whether this connections should request client authentication. This
257 * is only useful for sockets in server mode. The client authentication is
258 * one of:
259 * <ul>
260 * <li>authentication required</li>
261 * <li>authentication requested</li>
262 * <li>no authentication needed</li>
263 * </ul>
264 * This method overrides the setting of {@link #setNeedClientAuth(boolean)}.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700265 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800266 * @param want
267 * {@code true} if client authentication should be requested,
268 * {@code false} if not authentication is needed.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800269 */
270 public abstract void setWantClientAuth(boolean want);
Jesse Wilsonf9215792009-08-25 16:30:17 -0700271
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800272 /**
273 * Returns whether this connections will request client authentication.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700274 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800275 * @return {@code true} is client authentication will be requested,
276 * {@code false} if no client authentication is needed.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800277 */
278 public abstract boolean getWantClientAuth();
Jesse Wilsonf9215792009-08-25 16:30:17 -0700279
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800280 /**
281 * Sets whether new SSL sessions may be created by this socket or if
282 * existing sessions must be reused.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700283 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800284 * @param flag
285 * {@code true} if new sessions may be created, otherwise
286 * {@code false}.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800287 */
288 public abstract void setEnableSessionCreation(boolean flag);
Jesse Wilsonf9215792009-08-25 16:30:17 -0700289
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800290 /**
291 * Returns whether new SSL sessions may be created by this socket or if
292 * existing sessions must be reused.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700293 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800294 * @return {@code true} if new sessions may be created, otherwise
295 * {@code false}.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800296 */
297 public abstract boolean getEnableSessionCreation();
Jesse Wilsonf9215792009-08-25 16:30:17 -0700298
299}