| OLD | NEW |
| 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 import "dart:async"; | 5 import "dart:async"; |
| 6 import "dart:io"; | 6 import "dart:io"; |
| 7 | 7 |
| 8 import "package:async_helper/async_helper.dart"; | 8 import "package:async_helper/async_helper.dart"; |
| 9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
| 10 | 10 |
| 11 InternetAddress HOST; | 11 InternetAddress HOST; |
| 12 const CERTIFICATE = "localhost_cert"; | 12 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
| 13 |
| 14 SecurityContext serverContext = new SecurityContext() |
| 15 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
| 16 ..usePrivateKey(localFile('certificates/server_key.pem'), |
| 17 password: 'dartdart'); |
| 18 |
| 19 SecurityContext clientContext = new SecurityContext() |
| 20 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); |
| 13 | 21 |
| 14 Future testNoClientCertificate() { | 22 Future testNoClientCertificate() { |
| 15 var completer = new Completer(); | 23 var completer = new Completer(); |
| 16 SecureServerSocket.bind(HOST, | 24 SecureServerSocket.bind(HOST, |
| 17 0, | 25 0, |
| 18 CERTIFICATE, | 26 serverContext, |
| 19 requestClientCertificate: true).then((server) { | 27 requestClientCertificate: true).then((server) { |
| 20 var clientEndFuture = SecureSocket.connect(HOST, | 28 var clientEndFuture = SecureSocket.connect(HOST, |
| 21 server.port); | 29 server.port, |
| 30 context: clientContext); |
| 22 server.listen((serverEnd) { | 31 server.listen((serverEnd) { |
| 23 X509Certificate certificate = serverEnd.peerCertificate; | 32 X509Certificate certificate = serverEnd.peerCertificate; |
| 24 Expect.isNull(certificate); | 33 Expect.isNull(certificate); |
| 25 clientEndFuture.then((clientEnd) { | 34 clientEndFuture.then((clientEnd) { |
| 26 clientEnd.close(); | 35 clientEnd.close(); |
| 27 serverEnd.close(); | 36 serverEnd.close(); |
| 28 server.close(); | 37 server.close(); |
| 29 completer.complete(); | 38 completer.complete(); |
| 30 }); | 39 }); |
| 31 }); | 40 }); |
| 32 }); | 41 }); |
| 33 return completer.future; | 42 return completer.future; |
| 34 } | 43 } |
| 35 | 44 |
| 36 Future testNoRequiredClientCertificate() { | 45 Future testNoRequiredClientCertificate() { |
| 37 var completer = new Completer(); | 46 var completer = new Completer(); |
| 38 bool clientError = false; | 47 bool clientError = false; |
| 39 SecureServerSocket.bind(HOST, | 48 SecureServerSocket.bind(HOST, |
| 40 0, | 49 0, |
| 41 CERTIFICATE, | 50 serverContext, |
| 42 requireClientCertificate: true).then((server) { | 51 requireClientCertificate: true).then((server) { |
| 43 Future clientDone = SecureSocket.connect(HOST, server.port) | 52 Future clientDone = |
| 44 .catchError((e) { clientError = true; }); | 53 SecureSocket.connect(HOST, server.port, context: clientContext) |
| 54 .catchError((e) { clientError = true; }); |
| 45 server.listen((serverEnd) { | 55 server.listen((serverEnd) { |
| 46 Expect.fail("Got a unverifiable connection"); | 56 Expect.fail("Got a unverifiable connection"); |
| 47 }, | 57 }, |
| 48 onError: (e) { | 58 onError: (e) { |
| 49 clientDone.then((_) { | 59 clientDone.then((_) { |
| 50 Expect.isTrue(clientError); | 60 Expect.isTrue(clientError); |
| 51 server.close(); | 61 server.close(); |
| 52 completer.complete(); | 62 completer.complete(); |
| 53 }); | 63 }); |
| 54 }); | 64 }); |
| 55 }); | 65 }); |
| 56 return completer.future; | 66 return completer.future; |
| 57 } | 67 } |
| 58 | 68 |
| 59 void main() { | 69 void main() { |
| 60 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | |
| 61 SecureSocket.initialize(database: certificateDatabase, | |
| 62 password: 'dartdart', | |
| 63 useBuiltinRoots: false); | |
| 64 | |
| 65 asyncStart(); | 70 asyncStart(); |
| 66 InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first) | 71 InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first) |
| 67 .then((_) => testNoRequiredClientCertificate()) | 72 .then((_) => testNoRequiredClientCertificate()) |
| 68 .then((_) => testNoClientCertificate()) | 73 .then((_) => testNoClientCertificate()) |
| 69 .then((_) => asyncEnd()); | 74 .then((_) => asyncEnd()); |
| 70 } | 75 } |
| OLD | NEW |