Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(525)

Side by Side Diff: sdk/lib/io/secure_socket.dart

Issue 14083007: Add new InternetAddress class with a static lookup function (including IPv6 results). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix new test. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/io/http_impl.dart ('k') | sdk/lib/io/socket.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 part of dart.io; 5 part of dart.io;
6 6
7 /** 7 /**
8 * A high-level class for communicating securely over a TCP socket, using 8 * A high-level class for communicating securely over a TCP socket, using
9 * TLS and SSL. The [SecureSocket] exposes both a [Stream] and an 9 * TLS and SSL. The [SecureSocket] exposes both a [Stream] and an
10 * [IOSink] interface, making it ideal for using together with 10 * [IOSink] interface, making it ideal for using together with
(...skipping 17 matching lines...) Expand all
28 * appropriate certificate will be searched for in the database and 28 * appropriate certificate will be searched for in the database and
29 * sent automatically, based on what the server says it will accept. 29 * sent automatically, based on what the server says it will accept.
30 * 30 *
31 * [onBadCertificate] is an optional handler for unverifiable certificates. 31 * [onBadCertificate] is an optional handler for unverifiable certificates.
32 * The handler receives the [X509Certificate], and can inspect it and 32 * The handler receives the [X509Certificate], and can inspect it and
33 * decide (or let the user decide) whether to accept 33 * decide (or let the user decide) whether to accept
34 * the connection or not. The handler should return true 34 * the connection or not. The handler should return true
35 * to continue the [SecureSocket] connection. 35 * to continue the [SecureSocket] connection.
36 */ 36 */
37 static Future<SecureSocket> connect( 37 static Future<SecureSocket> connect(
38 String host, 38 host,
39 int port, 39 int port,
40 {bool sendClientCertificate: false, 40 {bool sendClientCertificate: false,
41 String certificateName, 41 String certificateName,
42 bool onBadCertificate(X509Certificate certificate)}) { 42 bool onBadCertificate(X509Certificate certificate)}) {
43 return RawSecureSocket.connect(host, 43 return RawSecureSocket.connect(host,
44 port, 44 port,
45 sendClientCertificate: sendClientCertificate, 45 sendClientCertificate: sendClientCertificate,
46 certificateName: certificateName, 46 certificateName: certificateName,
47 onBadCertificate: onBadCertificate) 47 onBadCertificate: onBadCertificate)
48 .then((rawSocket) => new SecureSocket._(rawSocket)); 48 .then((rawSocket) => new SecureSocket._(rawSocket));
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 * appropriate certificate will be searched for in the database and 201 * appropriate certificate will be searched for in the database and
202 * sent automatically, based on what the server says it will accept. 202 * sent automatically, based on what the server says it will accept.
203 * 203 *
204 * [onBadCertificate] is an optional handler for unverifiable certificates. 204 * [onBadCertificate] is an optional handler for unverifiable certificates.
205 * The handler receives the [X509Certificate], and can inspect it and 205 * The handler receives the [X509Certificate], and can inspect it and
206 * decide (or let the user decide) whether to accept 206 * decide (or let the user decide) whether to accept
207 * the connection or not. The handler should return true 207 * the connection or not. The handler should return true
208 * to continue the [RawSecureSocket] connection. 208 * to continue the [RawSecureSocket] connection.
209 */ 209 */
210 static Future<RawSecureSocket> connect( 210 static Future<RawSecureSocket> connect(
211 String host, 211 host,
212 int port, 212 int port,
213 {bool sendClientCertificate: false, 213 {bool sendClientCertificate: false,
214 String certificateName, 214 String certificateName,
215 bool onBadCertificate(X509Certificate certificate)}) { 215 bool onBadCertificate(X509Certificate certificate)}) {
216 return _RawSecureSocket.connect( 216 return _RawSecureSocket.connect(
217 host, 217 host,
218 port, 218 port,
219 certificateName, 219 certificateName,
220 is_server: false, 220 is_server: false,
221 sendClientCertificate: sendClientCertificate, 221 sendClientCertificate: sendClientCertificate,
(...skipping 15 matching lines...) Expand all
237 * See [connect] for more information on the arguments. 237 * See [connect] for more information on the arguments.
238 * 238 *
239 */ 239 */
240 static Future<RawSecureSocket> secure( 240 static Future<RawSecureSocket> secure(
241 RawSocket socket, 241 RawSocket socket,
242 {StreamSubscription subscription, 242 {StreamSubscription subscription,
243 bool sendClientCertificate: false, 243 bool sendClientCertificate: false,
244 String certificateName, 244 String certificateName,
245 bool onBadCertificate(X509Certificate certificate)}) { 245 bool onBadCertificate(X509Certificate certificate)}) {
246 return _RawSecureSocket.connect( 246 return _RawSecureSocket.connect(
247 socket.host, 247 socket.address,
248 socket.port, 248 socket.port,
249 certificateName, 249 certificateName,
250 is_server: false, 250 is_server: false,
251 socket: socket, 251 socket: socket,
252 subscription: subscription, 252 subscription: subscription,
253 sendClientCertificate: sendClientCertificate, 253 sendClientCertificate: sendClientCertificate,
254 onBadCertificate: onBadCertificate); 254 onBadCertificate: onBadCertificate);
255 } 255 }
256 256
257 /** 257 /**
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 static final int NUM_BUFFERS = 4; 338 static final int NUM_BUFFERS = 4;
339 339
340 RawSocket _socket; 340 RawSocket _socket;
341 final Completer<_RawSecureSocket> _handshakeComplete = 341 final Completer<_RawSecureSocket> _handshakeComplete =
342 new Completer<_RawSecureSocket>(); 342 new Completer<_RawSecureSocket>();
343 StreamController<RawSocketEvent> _controller; 343 StreamController<RawSocketEvent> _controller;
344 Stream<RawSocketEvent> _stream; 344 Stream<RawSocketEvent> _stream;
345 StreamSubscription<RawSocketEvent> _socketSubscription; 345 StreamSubscription<RawSocketEvent> _socketSubscription;
346 List<int> _carryOverData; 346 List<int> _carryOverData;
347 int _carryOverDataIndex = 0; 347 int _carryOverDataIndex = 0;
348 final String host; 348 final InternetAddress address;
349 final bool is_server; 349 final bool is_server;
350 final String certificateName; 350 final String certificateName;
351 final bool requestClientCertificate; 351 final bool requestClientCertificate;
352 final bool requireClientCertificate; 352 final bool requireClientCertificate;
353 final bool sendClientCertificate; 353 final bool sendClientCertificate;
354 final Function onBadCertificate; 354 final Function onBadCertificate;
355 355
356 var _status = NOT_CONNECTED; 356 var _status = NOT_CONNECTED;
357 bool _writeEventsEnabled = true; 357 bool _writeEventsEnabled = true;
358 bool _readEventsEnabled = true; 358 bool _readEventsEnabled = true;
359 bool _socketClosedRead = false; // The network socket is closed for reading. 359 bool _socketClosedRead = false; // The network socket is closed for reading.
360 bool _socketClosedWrite = false; // The network socket is closed for writing. 360 bool _socketClosedWrite = false; // The network socket is closed for writing.
361 bool _closedRead = false; // The secure socket has fired an onClosed event. 361 bool _closedRead = false; // The secure socket has fired an onClosed event.
362 bool _closedWrite = false; // The secure socket has been closed for writing. 362 bool _closedWrite = false; // The secure socket has been closed for writing.
363 bool _filterReadEmpty = true; // There is no buffered data to read. 363 bool _filterReadEmpty = true; // There is no buffered data to read.
364 bool _filterWriteEmpty = true; // There is no buffered data to be written. 364 bool _filterWriteEmpty = true; // There is no buffered data to be written.
365 bool _connectPending = false; 365 bool _connectPending = false;
366 _SecureFilter _secureFilter = new _SecureFilter(); 366 _SecureFilter _secureFilter = new _SecureFilter();
367 367
368 static Future<_RawSecureSocket> connect( 368 static Future<_RawSecureSocket> connect(
369 String host, 369 host,
370 int requestedPort, 370 int requestedPort,
371 String certificateName, 371 String certificateName,
372 {bool is_server, 372 {bool is_server,
373 RawSocket socket, 373 RawSocket socket,
374 StreamSubscription subscription, 374 StreamSubscription subscription,
375 List<int> carryOverData, 375 List<int> carryOverData,
376 bool requestClientCertificate: false, 376 bool requestClientCertificate: false,
377 bool requireClientCertificate: false, 377 bool requireClientCertificate: false,
378 bool sendClientCertificate: false, 378 bool sendClientCertificate: false,
379 bool onBadCertificate(X509Certificate certificate)}){ 379 bool onBadCertificate(X509Certificate certificate)}) {
380 return new _RawSecureSocket(host, 380 var future;
381 if (host is String) {
382 future = InternetAddress.lookup(host).then((addrs) => addrs.first);
383 } else {
384 future = new Future.value(host);
385 }
386 return future.then((addr) {
387 return new _RawSecureSocket(addr,
381 requestedPort, 388 requestedPort,
382 certificateName, 389 certificateName,
383 is_server, 390 is_server,
384 socket, 391 socket,
385 subscription, 392 subscription,
386 carryOverData, 393 carryOverData,
387 requestClientCertificate, 394 requestClientCertificate,
388 requireClientCertificate, 395 requireClientCertificate,
389 sendClientCertificate, 396 sendClientCertificate,
390 onBadCertificate) 397 onBadCertificate)
391 ._handshakeComplete.future; 398 ._handshakeComplete.future;
399 });
392 } 400 }
393 401
394 _RawSecureSocket( 402 _RawSecureSocket(
395 String this.host, 403 InternetAddress this.address,
396 int requestedPort, 404 int requestedPort,
397 String this.certificateName, 405 String this.certificateName,
398 bool this.is_server, 406 bool this.is_server,
399 RawSocket socket, 407 RawSocket socket,
400 StreamSubscription this._socketSubscription, 408 StreamSubscription this._socketSubscription,
401 List<int> this._carryOverData, 409 List<int> this._carryOverData,
402 bool this.requestClientCertificate, 410 bool this.requestClientCertificate,
403 bool this.requireClientCertificate, 411 bool this.requireClientCertificate,
404 bool this.sendClientCertificate, 412 bool this.sendClientCertificate,
405 bool this.onBadCertificate(X509Certificate certificate)) { 413 bool this.onBadCertificate(X509Certificate certificate)) {
406 _controller = new StreamController<RawSocketEvent>( 414 _controller = new StreamController<RawSocketEvent>(
407 onListen: _onSubscriptionStateChange, 415 onListen: _onSubscriptionStateChange,
408 onPause: _onPauseStateChange, 416 onPause: _onPauseStateChange,
409 onResume: _onPauseStateChange, 417 onResume: _onPauseStateChange,
410 onCancel: _onSubscriptionStateChange); 418 onCancel: _onSubscriptionStateChange);
411 _stream = _controller.stream; 419 _stream = _controller.stream;
412 // Throw an ArgumentError if any field is invalid. After this, all 420 // Throw an ArgumentError if any field is invalid. After this, all
413 // errors will be reported through the future or the stream. 421 // errors will be reported through the future or the stream.
414 _verifyFields(); 422 _verifyFields();
415 _secureFilter.init(); 423 _secureFilter.init();
416 if (_carryOverData != null) _readFromCarryOver(); 424 if (_carryOverData != null) _readFromCarryOver();
417 _secureFilter.registerHandshakeCompleteCallback( 425 _secureFilter.registerHandshakeCompleteCallback(
418 _secureHandshakeCompleteHandler); 426 _secureHandshakeCompleteHandler);
419 if (onBadCertificate != null) { 427 if (onBadCertificate != null) {
420 _secureFilter.registerBadCertificateCallback(onBadCertificate); 428 _secureFilter.registerBadCertificateCallback(onBadCertificate);
421 } 429 }
422 var futureSocket; 430 var futureSocket;
423 if (socket == null) { 431 if (socket == null) {
424 futureSocket = RawSocket.connect(host, requestedPort); 432 futureSocket = RawSocket.connect(address, requestedPort);
425 } else { 433 } else {
426 futureSocket = new Future.value(socket); 434 futureSocket = new Future.value(socket);
427 } 435 }
428 futureSocket.then((rawSocket) { 436 futureSocket.then((rawSocket) {
429 _socket = rawSocket; 437 _socket = rawSocket;
430 _socket.readEventsEnabled = true; 438 _socket.readEventsEnabled = true;
431 _socket.writeEventsEnabled = false; 439 _socket.writeEventsEnabled = false;
432 if (_socketSubscription == null) { 440 if (_socketSubscription == null) {
433 // If a current subscription is provided use this otherwise 441 // If a current subscription is provided use this otherwise
434 // create a new one. 442 // create a new one.
435 _socketSubscription = _socket.listen(_eventDispatcher, 443 _socketSubscription = _socket.listen(_eventDispatcher,
436 onError: _errorHandler, 444 onError: _errorHandler,
437 onDone: _doneHandler); 445 onDone: _doneHandler);
438 } else { 446 } else {
439 _socketSubscription.onData(_eventDispatcher); 447 _socketSubscription.onData(_eventDispatcher);
440 _socketSubscription.onError(_errorHandler); 448 _socketSubscription.onError(_errorHandler);
441 _socketSubscription.onDone(_doneHandler); 449 _socketSubscription.onDone(_doneHandler);
442 } 450 }
443 _connectPending = true; 451 _connectPending = true;
444 _secureFilter.connect(host, 452 _secureFilter.connect(rawSocket.address.host,
445 port, 453 port,
446 is_server, 454 is_server,
447 certificateName, 455 certificateName,
448 requestClientCertificate || 456 requestClientCertificate ||
449 requireClientCertificate, 457 requireClientCertificate,
450 requireClientCertificate, 458 requireClientCertificate,
451 sendClientCertificate); 459 sendClientCertificate);
452 _status = HANDSHAKE; 460 _status = HANDSHAKE;
453 _secureHandshake(); 461 _secureHandshake();
454 }) 462 })
(...skipping 13 matching lines...) Expand all
468 } 476 }
469 return _stream.listen(onData, 477 return _stream.listen(onData,
470 onError: onError, 478 onError: onError,
471 onDone: onDone, 479 onDone: onDone,
472 cancelOnError: cancelOnError); 480 cancelOnError: cancelOnError);
473 } 481 }
474 482
475 void _verifyFields() { 483 void _verifyFields() {
476 assert(is_server is bool); 484 assert(is_server is bool);
477 assert(_socket == null || _socket is RawSocket); 485 assert(_socket == null || _socket is RawSocket);
478 if (host is! String) { 486 if (address is! InternetAddress) {
479 throw new ArgumentError( 487 throw new ArgumentError(
480 "RawSecureSocket constructor: host is not a String"); 488 "RawSecureSocket constructor: host is not an InternetAddress");
481 } 489 }
482 if (certificateName != null && certificateName is! String) { 490 if (certificateName != null && certificateName is! String) {
483 throw new ArgumentError("certificateName is not null or a String"); 491 throw new ArgumentError("certificateName is not null or a String");
484 } 492 }
485 if (certificateName == null && is_server) { 493 if (certificateName == null && is_server) {
486 throw new ArgumentError("certificateName is null on a server"); 494 throw new ArgumentError("certificateName is null on a server");
487 } 495 }
488 if (requestClientCertificate is! bool) { 496 if (requestClientCertificate is! bool) {
489 throw new ArgumentError("requestClientCertificate is not a bool"); 497 throw new ArgumentError("requestClientCertificate is not a bool");
490 } 498 }
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after
945 void destroy(); 953 void destroy();
946 void handshake(); 954 void handshake();
947 void init(); 955 void init();
948 X509Certificate get peerCertificate; 956 X509Certificate get peerCertificate;
949 int processBuffer(int bufferIndex); 957 int processBuffer(int bufferIndex);
950 void registerBadCertificateCallback(Function callback); 958 void registerBadCertificateCallback(Function callback);
951 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler); 959 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler);
952 960
953 List<_ExternalBuffer> get buffers; 961 List<_ExternalBuffer> get buffers;
954 } 962 }
OLDNEW
« no previous file with comments | « sdk/lib/io/http_impl.dart ('k') | sdk/lib/io/socket.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698