| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 abstract class SecureServerSocket implements ServerSocket { | 6 abstract class SecureServerSocket implements ServerSocket { |
| 7 /** | 7 /** |
| 8 * Constructs a new secure server socket, binds it to a given address | 8 * Constructs a new secure server socket, binds it to a given address |
| 9 * and port, and listens on it. Incoming client connections are | 9 * and port, and listens on it. Incoming client connections are |
| 10 * promoted to secure connections, using the server certificate given by | 10 * promoted to secure connections, using the server certificate given by |
| 11 * certificate_name. The bindAddress must be given as a numeric address, | 11 * certificate_name. The bindAddress must be given as a numeric address, |
| 12 * not a host name. The certificate name is the distinguished name (DN) of | 12 * not a host name. The certificate name is the distinguished name (DN) of |
| 13 * the certificate, such as "CN=localhost" or "CN=myserver.mydomain.com". | 13 * the certificate, such as "CN=localhost" or "CN=myserver.mydomain.com". |
| 14 * The certificate is looked up in the NSS certificate database set by | 14 * The certificate is looked up in the NSS certificate database set by |
| 15 * SecureSocket.setCertificateDatabase. | 15 * SecureSocket.setCertificateDatabase. |
| 16 * |
| 17 * To request or require that clients authenticate by providing an SSL (TLS) |
| 18 * client certificate, set the optional parameters requestClientCertificate or |
| 19 * requireClientCertificate to true. Require implies request, so one doesn't |
| 20 * need to specify both. To check whether a client certificate was received, |
| 21 * check SecureSocket.peerCertificate after connecting. If no certificate |
| 22 * was received, the result will be null. |
| 16 */ | 23 */ |
| 17 factory SecureServerSocket(String bindAddress, | 24 factory SecureServerSocket(String bindAddress, |
| 18 int port, | 25 int port, |
| 19 int backlog, | 26 int backlog, |
| 20 String certificate_name) => | 27 String certificate_name, |
| 21 new _SecureServerSocket(bindAddress, port, backlog, certificate_name); | 28 {bool requestClientCertificate: false, |
| 29 bool requireClientCertificate: false}) { |
| 30 return new _SecureServerSocket(bindAddress, |
| 31 port, |
| 32 backlog, |
| 33 certificate_name, |
| 34 requestClientCertificate, |
| 35 requireClientCertificate); |
| 36 } |
| 22 } | 37 } |
| 23 | 38 |
| 24 | 39 |
| 25 class _SecureServerSocket implements SecureServerSocket { | 40 class _SecureServerSocket implements SecureServerSocket { |
| 26 | 41 |
| 27 _SecureServerSocket(String bindAddress, | 42 _SecureServerSocket(String bindAddress, |
| 28 int port, | 43 int port, |
| 29 int backlog, | 44 int backlog, |
| 30 String certificate_name) { | 45 String this.certificate_name, |
| 31 _socket = new ServerSocket(bindAddress, port, backlog); | 46 bool this.requestClientCertificate, |
| 32 _socket.onConnection = this._onConnectionHandler; | 47 bool this.requireClientCertificate) { |
| 33 _certificate_name = certificate_name; | 48 socket = new ServerSocket(bindAddress, port, backlog); |
| 49 socket.onConnection = this._onConnectionHandler; |
| 34 } | 50 } |
| 35 | 51 |
| 36 void set onConnection(void callback(Socket connection)) { | 52 void set onConnection(void callback(Socket connection)) { |
| 37 _onConnectionCallback = callback; | 53 _onConnectionCallback = callback; |
| 38 } | 54 } |
| 39 | 55 |
| 40 void set onError(void callback(e)) { | 56 void set onError(void callback(e)) { |
| 41 _socket.onError = callback; | 57 socket.onError = callback; |
| 42 } | 58 } |
| 43 | 59 |
| 44 /** | 60 /** |
| 45 * Returns the port used by this socket. | 61 * Returns the port used by this socket. |
| 46 */ | 62 */ |
| 47 int get port => _socket.port; | 63 int get port => socket.port; |
| 48 | 64 |
| 49 /** | 65 /** |
| 50 * Closes the socket. | 66 * Closes the socket. |
| 51 */ | 67 */ |
| 52 void close() { | 68 void close() { |
| 53 _socket.close(); | 69 socket.close(); |
| 54 } | 70 } |
| 55 | 71 |
| 56 void _onConnectionHandler(Socket connection) { | 72 void _onConnectionHandler(Socket connection) { |
| 57 if (_onConnectionCallback == null) { | 73 if (_onConnectionCallback == null) { |
| 58 connection.close(); | 74 connection.close(); |
| 59 throw new SocketIOException( | 75 throw new SocketIOException( |
| 60 "SecureServerSocket with no onConnection callback connected to"); | 76 "SecureServerSocket with no onConnection callback connected to"); |
| 61 } | 77 } |
| 62 if (_certificate_name == null) { | 78 if (certificate_name == null) { |
| 63 connection.close(); | 79 connection.close(); |
| 64 throw new SocketIOException( | 80 throw new SocketIOException( |
| 65 "SecureServerSocket with server certificate not set connected to"); | 81 "SecureServerSocket with server certificate not set connected to"); |
| 66 } | 82 } |
| 67 var secure_connection = new _SecureSocket.server(connection.remoteHost, | 83 var secure_connection = new _SecureSocket( |
| 68 connection.remotePort, | 84 connection.remoteHost, |
| 69 connection, | 85 connection.remotePort, |
| 70 _certificate_name); | 86 certificate_name, |
| 87 is_server: true, |
| 88 socket: connection, |
| 89 requestClientCertificate: requestClientCertificate, |
| 90 requireClientCertificate: requireClientCertificate); |
| 71 _onConnectionCallback(secure_connection); | 91 _onConnectionCallback(secure_connection); |
| 72 } | 92 } |
| 73 | 93 |
| 74 ServerSocket _socket; | 94 ServerSocket socket; |
| 75 var _onConnectionCallback; | 95 var _onConnectionCallback; |
| 76 String _certificate_name; | 96 final String certificate_name; |
| 97 final bool requestClientCertificate; |
| 98 final bool requireClientCertificate; |
| 77 } | 99 } |
| OLD | NEW |