| 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 | 12 |
| 13 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 13 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
| 14 | 14 |
| 15 SecurityContext serverContext = new SecurityContext() | 15 SecurityContext serverContext = new SecurityContext() |
| 16 ..useCertificateChain(localFile('certificates/server_chain.pem')) | 16 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
| 17 ..usePrivateKey(localFile('certificates/server_key.pem'), | 17 ..usePrivateKey(localFile('certificates/server_key.pem'), |
| 18 password: 'dartdart'); | 18 password: 'dartdart') |
| 19 ..setTrustedCertificates(file: localFile('certificates/client_authority.pem')) |
| 20 ..setClientAuthorities(localFile('certificates/client_authority.pem')); |
| 19 | 21 |
| 20 SecurityContext clientContext = new SecurityContext() | 22 SecurityContext clientCertContext = new SecurityContext() |
| 23 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')) |
| 24 ..useCertificateChain(localFile('certificates/client1.pem')) |
| 25 ..usePrivateKey(localFile('certificates/client1_key.pem'), |
| 26 password: 'dartdart'); |
| 27 |
| 28 SecurityContext clientNoCertContext = new SecurityContext() |
| 21 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); | 29 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); |
| 22 | 30 |
| 23 Future testClientCertificate() { | 31 Future testClientCertificate({bool required, bool sendCert}) async { |
| 24 var completer = new Completer(); | 32 var server = await SecureServerSocket.bind(HOST, 0, serverContext, |
| 25 SecureServerSocket.bind(HOST, | 33 requestClientCertificate: true, requireClientCertificate: required); |
| 26 0, | 34 var clientContext = sendCert ? clientCertContext : clientNoCertContext; |
| 27 serverContext, | 35 var clientEndFuture = SecureSocket.connect(HOST, server.port, |
| 28 requestClientCertificate: true).then((server) { | 36 context: clientContext, sendClientCertificate: true); |
| 29 var clientEndFuture = SecureSocket.connect(HOST, | 37 if (required && !sendCert) { |
| 30 server.port, | 38 try { |
| 31 context: clientContext, | 39 await server.first; |
| 32 sendClientCertificate: true); | 40 } catch (e) { |
| 33 server.listen((serverEnd) { | 41 try { |
| 34 X509Certificate certificate = serverEnd.peerCertificate; | 42 await clientEndFuture; |
| 35 Expect.isNotNull(certificate); | 43 } catch (e) { |
| 36 Expect.equals("CN=localhost", certificate.subject); | 44 return; |
| 37 Expect.equals("CN=myauthority", certificate.issuer); | 45 } |
| 38 clientEndFuture.then((clientEnd) { | 46 } |
| 39 X509Certificate certificate = clientEnd.peerCertificate; | 47 Expect.fail("Connection succeeded with no required client certificate"); |
| 40 Expect.isNotNull(certificate); | 48 } |
| 41 Expect.equals("CN=localhost", certificate.subject); | 49 var serverEnd = await server.first; |
| 42 Expect.equals("CN=myauthority", certificate.issuer); | 50 var clientEnd = await clientEndFuture; |
| 43 clientEnd.close(); | 51 |
| 44 serverEnd.close(); | 52 X509Certificate clientCertificate = serverEnd.peerCertificate; |
| 45 server.close(); | 53 if (sendCert) { |
| 46 completer.complete(); | 54 Expect.isNotNull(clientCertificate); |
| 47 }); | 55 Expect.equals("/CN=user1", clientCertificate.subject); |
| 48 }); | 56 Expect.equals("/CN=clientauthority", clientCertificate.issuer); |
| 49 }); | 57 } else { |
| 50 return completer.future; | 58 Expect.isNull(clientCertificate); |
| 59 } |
| 60 X509Certificate serverCertificate = clientEnd.peerCertificate; |
| 61 Expect.isNotNull(serverCertificate); |
| 62 Expect.equals("/CN=localhost", serverCertificate.subject); |
| 63 Expect.equals("/CN=intermediateauthority", serverCertificate.issuer); |
| 64 clientEnd.close(); |
| 65 serverEnd.close(); |
| 51 } | 66 } |
| 52 | 67 |
| 53 Future testRequiredClientCertificate() { | 68 main() async { |
| 54 var completer = new Completer(); | 69 asyncStart(); |
| 55 SecureServerSocket.bind(HOST, | 70 HOST = (await InternetAddress.lookup("localhost")).first; |
| 56 0, | 71 await testClientCertificate(required: false, sendCert: true); |
| 57 serverContext, | 72 await testClientCertificate(required: true, sendCert: true); |
| 58 requireClientCertificate: true).then((server) { | 73 await testClientCertificate(required: false, sendCert: false); |
| 59 var clientEndFuture = SecureSocket.connect(HOST, | 74 await testClientCertificate(required: true, sendCert: false); |
| 60 server.port, | 75 asyncEnd(); |
| 61 context: clientContext, | |
| 62 sendClientCertificate: true); | |
| 63 server.listen((serverEnd) { | |
| 64 X509Certificate certificate = serverEnd.peerCertificate; | |
| 65 Expect.isNotNull(certificate); | |
| 66 Expect.equals("CN=localhost", certificate.subject); | |
| 67 Expect.equals("CN=myauthority", certificate.issuer); | |
| 68 clientEndFuture.then((clientEnd) { | |
| 69 X509Certificate certificate = clientEnd.peerCertificate; | |
| 70 Expect.isNotNull(certificate); | |
| 71 Expect.equals("CN=localhost", certificate.subject); | |
| 72 Expect.equals("CN=myauthority", certificate.issuer); | |
| 73 clientEnd.close(); | |
| 74 serverEnd.close(); | |
| 75 server.close(); | |
| 76 completer.complete(); | |
| 77 }); | |
| 78 }); | |
| 79 }); | |
| 80 return completer.future; | |
| 81 } | 76 } |
| 82 | |
| 83 void main() { | |
| 84 asyncStart(); | |
| 85 InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first) | |
| 86 .then((_) => testClientCertificate()) | |
| 87 .then((_) => testRequiredClientCertificate()) | |
| 88 .then((_) => asyncEnd()); | |
| 89 } | |
| OLD | NEW |