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.conscrypt; 19 20 import java.io.IOException; 21 import java.net.InetAddress; 22 import java.net.Socket; 23 import javax.net.ssl.SSLServerSocket; 24 25 /** 26 * SSLServerSocket implementation 27 * @see javax.net.ssl.SSLServerSocket class documentation for more information. 28 */ 29 public class SSLServerSocketImpl extends SSLServerSocket { 30 31 // the sslParameters object encapsulates all the info 32 // about supported and enabled cipher suites and protocols, 33 // as well as the information about client/server mode of 34 // ssl socket, whether it require/want client authentication or not, 35 // and controls whether new SSL sessions may be established by this 36 // socket or not. 37 private final SSLParametersImpl sslParameters; 38 39 // logger 40 private Logger.Stream logger = Logger.getStream("ssocket"); 41 42 /** 43 * Ctor 44 * @param sslParameters: SSLParameters 45 * @throws IOException 46 */ 47 protected SSLServerSocketImpl(SSLParametersImpl sslParameters) throws IOException { 48 this.sslParameters = sslParameters; 49 } 50 51 /** 52 * Ctor 53 * @param port: int 54 * @param sslParameters: SSLParameters 55 * @throws IOException 56 */ 57 protected SSLServerSocketImpl(int port, SSLParametersImpl sslParameters) 58 throws IOException { 59 super(port); 60 this.sslParameters = sslParameters; 61 } 62 63 /** 64 * Ctor 65 * @param port: int 66 * @param backlog: int 67 * @param sslParameters: SSLParameters 68 * @throws IOException 69 */ 70 protected SSLServerSocketImpl(int port, int backlog, 71 SSLParametersImpl sslParameters) throws IOException { 72 super(port, backlog); 73 this.sslParameters = sslParameters; 74 } 75 76 /** 77 * Ctor 78 * @param port: int 79 * @param backlog: int 80 * @param iAddress: InetAddress 81 * @param sslParameters: SSLParameters 82 * @throws IOException 83 */ 84 protected SSLServerSocketImpl(int port, int backlog, 85 InetAddress iAddress, 86 SSLParametersImpl sslParameters) 87 throws IOException { 88 super(port, backlog, iAddress); 89 this.sslParameters = sslParameters; 90 } 91 92 // --------------- SSLParameters based methods --------------------- 93 94 /** 95 * This method works according to the specification of implemented class. 96 * @see javax.net.ssl.SSLServerSocket#getSupportedCipherSuites() 97 * method documentation for more information 98 */ 99 @Override 100 public String[] getSupportedCipherSuites() { 101 return CipherSuite.getSupportedCipherSuiteNames(); 102 } 103 104 /** 105 * This method works according to the specification of implemented class. 106 * @see javax.net.ssl.SSLServerSocket#getEnabledCipherSuites() 107 * method documentation for more information 108 */ 109 @Override 110 public String[] getEnabledCipherSuites() { 111 return sslParameters.getEnabledCipherSuites(); 112 } 113 114 /** 115 * This method works according to the specification of implemented class. 116 * @see javax.net.ssl.SSLServerSocket#setEnabledCipherSuites(String[]) 117 * method documentation for more information 118 */ 119 @Override 120 public void setEnabledCipherSuites(String[] suites) { 121 sslParameters.setEnabledCipherSuites(suites); 122 } 123 124 /** 125 * This method works according to the specification of implemented class. 126 * @see javax.net.ssl.SSLServerSocket#getSupportedProtocols() 127 * method documentation for more information 128 */ 129 @Override 130 public String[] getSupportedProtocols() { 131 return ProtocolVersion.supportedProtocols.clone(); 132 } 133 134 /** 135 * This method works according to the specification of implemented class. 136 * @see javax.net.ssl.SSLServerSocket#getEnabledProtocols() 137 * method documentation for more information 138 */ 139 @Override 140 public String[] getEnabledProtocols() { 141 return sslParameters.getEnabledProtocols(); 142 } 143 144 /** 145 * This method works according to the specification of implemented class. 146 * @see javax.net.ssl.SSLServerSocket#setEnabledProtocols(String[]) 147 * method documentation for more information 148 */ 149 @Override 150 public void setEnabledProtocols(String[] protocols) { 151 sslParameters.setEnabledProtocols(protocols); 152 } 153 154 /** 155 * This method works according to the specification of implemented class. 156 * @see javax.net.ssl.SSLServerSocket#setUseClientMode(boolean) 157 * method documentation for more information 158 */ 159 @Override 160 public void setUseClientMode(boolean mode) { 161 sslParameters.setUseClientMode(mode); 162 } 163 164 /** 165 * This method works according to the specification of implemented class. 166 * @see javax.net.ssl.SSLServerSocket#getUseClientMode() 167 * method documentation for more information 168 */ 169 @Override 170 public boolean getUseClientMode() { 171 return sslParameters.getUseClientMode(); 172 } 173 174 /** 175 * This method works according to the specification of implemented class. 176 * @see javax.net.ssl.SSLServerSocket#setNeedClientAuth(boolean) 177 * method documentation for more information 178 */ 179 @Override 180 public void setNeedClientAuth(boolean need) { 181 sslParameters.setNeedClientAuth(need); 182 } 183 184 /** 185 * This method works according to the specification of implemented class. 186 * @see javax.net.ssl.SSLServerSocket#getNeedClientAuth() 187 * method documentation for more information 188 */ 189 @Override 190 public boolean getNeedClientAuth() { 191 return sslParameters.getNeedClientAuth(); 192 } 193 194 /** 195 * This method works according to the specification of implemented class. 196 * @see javax.net.ssl.SSLServerSocket#setWantClientAuth(boolean) 197 * method documentation for more information 198 */ 199 @Override 200 public void setWantClientAuth(boolean want) { 201 sslParameters.setWantClientAuth(want); 202 } 203 204 /** 205 * This method works according to the specification of implemented class. 206 * @see javax.net.ssl.SSLServerSocket#getWantClientAuth() 207 * method documentation for more information 208 */ 209 @Override 210 public boolean getWantClientAuth() { 211 return sslParameters.getWantClientAuth(); 212 } 213 214 /** 215 * This method works according to the specification of implemented class. 216 * @see javax.net.ssl.SSLServerSocket#setEnableSessionCreation(boolean) 217 * method documentation for more information 218 */ 219 @Override 220 public void setEnableSessionCreation(boolean flag) { 221 sslParameters.setEnableSessionCreation(flag); 222 } 223 224 /** 225 * This method works according to the specification of implemented class. 226 * @see javax.net.ssl.SSLServerSocket#getEnableSessionCreation() 227 * method documentation for more information 228 */ 229 @Override 230 public boolean getEnableSessionCreation() { 231 return sslParameters.getEnableSessionCreation(); 232 } 233 234 235 // ------------- ServerSocket's methods overridings ---------------- 236 237 /** 238 * This method works according to the specification of implemented class. 239 * @see java.net.ServerSocket#accept() 240 * method documentation for more information 241 */ 242 @Override 243 public Socket accept() throws IOException { 244 if (logger != null) { 245 logger.println("SSLServerSocketImpl.accept .."); 246 } 247 SSLSocketImpl s = new SSLSocketImpl( 248 (SSLParametersImpl) sslParameters.clone()); 249 implAccept(s); 250 s.init(); 251 if (logger != null) { 252 logger.println("SSLServerSocketImpl: accepted, initialized"); 253 } 254 return s; 255 } 256 257 /** 258 * Returns the string representation of the object. 259 */ 260 @Override 261 public String toString() { 262 return "[SSLServerSocketImpl]"; 263 } 264 265 // ----------------------------------------------------------------- 266 } 267