| 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 * SecureSocket provides a secure (SSL or TLS) client connection to a server. | 6 * SecureSocket provides a secure (SSL or TLS) client connection to a server. |
| 7 * The certificate provided by the server is checked | 7 * The certificate provided by the server is checked |
| 8 * using the certificate database provided in setCertificateDatabase. | 8 * using the certificate database provided in setCertificateDatabase. |
| 9 */ | 9 */ |
| 10 abstract class SecureSocket implements Socket { | 10 abstract class SecureSocket implements Socket { |
| 11 /** | 11 /** |
| 12 * Constructs a new secure client socket and connect it to the given | 12 * Constructs a new secure client socket and connect it to the given |
| 13 * host on the given port. The returned socket is not yet connected | 13 * host on the given port. The returned socket is not yet connected |
| 14 * but ready for registration of callbacks. | 14 * but ready for registration of callbacks. |
| 15 */ | 15 */ |
| 16 factory SecureSocket(String host, int port) => new _SecureSocket(host, port); | 16 factory SecureSocket(String host, int port) => new _SecureSocket(host, port); |
| 17 | 17 |
| 18 /** |
| 19 * Install a handler for unverifiable certificates. The handler can inspect |
| 20 * the certificate, and decide (or let the user decide) whether to accept |
| 21 * the connection or not. The callback should return true |
| 22 * to continue the SecureSocket connection. |
| 23 */ |
| 24 void set onBadCertificate(bool callback(X509Certificate certificate)); |
| 25 |
| 18 /** | 26 /** |
| 19 * Initializes the NSS library with the path to a certificate database | 27 * Initializes the NSS library with the path to a certificate database |
| 20 * containing root certificates for verifying certificate paths on | 28 * containing root certificates for verifying certificate paths on |
| 21 * client connections, and server certificates to provide on server | 29 * client connections, and server certificates to provide on server |
| 22 * connections. The password argument should be used when creating | 30 * connections. The password argument should be used when creating |
| 23 * secure server sockets, to allow the private key of the server | 31 * secure server sockets, to allow the private key of the server |
| 24 * certificate to be fetched. If useBuiltinRoots is true (the default), | 32 * certificate to be fetched. If useBuiltinRoots is true (the default), |
| 25 * then a built-in set of root certificates for trusted certificate | 33 * then a built-in set of root certificates for trusted certificate |
| 26 * authorities is merged with the certificates in the database. | 34 * authorities is merged with the certificates in the database. |
| 27 * | 35 * |
| (...skipping 15 matching lines...) Expand all Loading... |
| 43 * the database can be created using the NSS certutil tool with "sql:" in | 51 * the database can be created using the NSS certutil tool with "sql:" in |
| 44 * front of the absolute path of the database directory, or setting the | 52 * front of the absolute path of the database directory, or setting the |
| 45 * environment variable NSS_DEFAULT_DB_TYPE to "sql". | 53 * environment variable NSS_DEFAULT_DB_TYPE to "sql". |
| 46 */ | 54 */ |
| 47 external static void initialize({String database, | 55 external static void initialize({String database, |
| 48 String password, | 56 String password, |
| 49 bool useBuiltinRoots: true}); | 57 bool useBuiltinRoots: true}); |
| 50 } | 58 } |
| 51 | 59 |
| 52 | 60 |
| 61 /** |
| 62 * X509Certificate represents an SSL certificate, with accessors to |
| 63 * get the fields of the certificate. |
| 64 */ |
| 65 class X509Certificate { |
| 66 X509Certificate(this.subject, |
| 67 this.issuer, |
| 68 this.startValidity, |
| 69 this.endValidity); |
| 70 final String subject; |
| 71 final String issuer; |
| 72 final Date startValidity; |
| 73 final Date endValidity; |
| 74 } |
| 75 |
| 76 |
| 53 class _SecureSocket implements SecureSocket { | 77 class _SecureSocket implements SecureSocket { |
| 54 // Status states | 78 // Status states |
| 55 static final int NOT_CONNECTED = 200; | 79 static final int NOT_CONNECTED = 200; |
| 56 static final int HANDSHAKE = 201; | 80 static final int HANDSHAKE = 201; |
| 57 static final int CONNECTED = 202; | 81 static final int CONNECTED = 202; |
| 58 static final int CLOSED = 203; | 82 static final int CLOSED = 203; |
| 59 | 83 |
| 60 // Buffer identifiers. | 84 // Buffer identifiers. |
| 61 // These must agree with those in the native C++ implementation. | 85 // These must agree with those in the native C++ implementation. |
| 62 static final int READ_PLAINTEXT = 0; | 86 static final int READ_PLAINTEXT = 0; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 } | 176 } |
| 153 _onWrite = callback; | 177 _onWrite = callback; |
| 154 } | 178 } |
| 155 | 179 |
| 156 void set _onWrite(void callback()) { | 180 void set _onWrite(void callback()) { |
| 157 _socketWriteHandler = callback; | 181 _socketWriteHandler = callback; |
| 158 // Reset the one-shot onWrite handler. | 182 // Reset the one-shot onWrite handler. |
| 159 _socket.onWrite = _secureWriteHandler; | 183 _socket.onWrite = _secureWriteHandler; |
| 160 } | 184 } |
| 161 | 185 |
| 186 void set onBadCertificate(bool callback(X509Certificate certificate)) { |
| 187 if (callback is! Function && callback != null) { |
| 188 throw new SocketIOException( |
| 189 "Callback provided to onBadCertificate is not a function or null"); |
| 190 } |
| 191 _secureFilter.registerBadCertificateCallback(callback); |
| 192 } |
| 193 |
| 162 InputStream get inputStream { | 194 InputStream get inputStream { |
| 163 if (_inputStream == null) { | 195 if (_inputStream == null) { |
| 164 if (_socketDataHandler != null || _socketCloseHandler != null) { | 196 if (_socketDataHandler != null || _socketCloseHandler != null) { |
| 165 throw new StreamException( | 197 throw new StreamException( |
| 166 "Cannot get input stream when socket handlers are used"); | 198 "Cannot get input stream when socket handlers are used"); |
| 167 } | 199 } |
| 168 _inputStream = new _SocketInputStream(this); | 200 _inputStream = new _SocketInputStream(this); |
| 169 } | 201 } |
| 170 return _inputStream; | 202 return _inputStream; |
| 171 } | 203 } |
| (...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 external factory _SecureFilter(); | 609 external factory _SecureFilter(); |
| 578 | 610 |
| 579 void connect(String hostName, | 611 void connect(String hostName, |
| 580 int port, | 612 int port, |
| 581 bool is_server, | 613 bool is_server, |
| 582 String certificateName); | 614 String certificateName); |
| 583 void destroy(); | 615 void destroy(); |
| 584 void handshake(); | 616 void handshake(); |
| 585 void init(); | 617 void init(); |
| 586 int processBuffer(int bufferIndex); | 618 int processBuffer(int bufferIndex); |
| 619 void registerBadCertificateCallback(Function callback); |
| 587 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler); | 620 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler); |
| 588 | 621 |
| 589 List<_ExternalBuffer> get buffers; | 622 List<_ExternalBuffer> get buffers; |
| 590 } | 623 } |
| OLD | NEW |