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(String certType) => new SecurityContext() |
16 ..useCertificateChainSync(localFile('certificates/server_chain.pem')) | 16 ..useCertificateChainSync(localFile('certificates/server_chain.$certType')) |
17 ..usePrivateKeySync(localFile('certificates/server_key.pem'), | 17 ..usePrivateKeySync(localFile('certificates/server_key.$certType'), |
18 password: 'dartdart') | 18 password: 'dartdart') |
19 ..setTrustedCertificatesSync(localFile('certificates/client_authority.pem')) | 19 ..setTrustedCertificatesSync(localFile( |
20 ..setClientAuthoritiesSync(localFile('certificates/client_authority.pem')); | 20 'certificates/client_authority.$certType')) |
| 21 ..setClientAuthoritiesSync(localFile( |
| 22 'certificates/client_authority.$certType')); |
21 | 23 |
22 SecurityContext clientCertContext = new SecurityContext() | 24 SecurityContext clientCertContext(String certType) => new SecurityContext() |
23 ..setTrustedCertificatesSync(localFile('certificates/trusted_certs.pem')) | 25 ..setTrustedCertificatesSync(localFile( |
24 ..useCertificateChainSync(localFile('certificates/client1.pem')) | 26 'certificates/trusted_certs.$certType')) |
25 ..usePrivateKeySync(localFile('certificates/client1_key.pem'), | 27 ..useCertificateChainSync(localFile('certificates/client1.$certType')) |
| 28 ..usePrivateKeySync(localFile('certificates/client1_key.$certType'), |
26 password: 'dartdart'); | 29 password: 'dartdart'); |
27 | 30 |
28 SecurityContext clientNoCertContext = new SecurityContext() | 31 SecurityContext clientNoCertContext(String certType) => new SecurityContext() |
29 ..setTrustedCertificatesSync(localFile('certificates/trusted_certs.pem')); | 32 ..setTrustedCertificatesSync(localFile( |
| 33 'certificates/trusted_certs.$certType')); |
30 | 34 |
31 Future testClientCertificate({bool required, bool sendCert}) async { | 35 Future testClientCertificate( |
32 var server = await SecureServerSocket.bind(HOST, 0, serverContext, | 36 {bool required, bool sendCert, String certType}) async { |
| 37 var server = await SecureServerSocket.bind(HOST, 0, serverContext(certType), |
33 requestClientCertificate: true, requireClientCertificate: required); | 38 requestClientCertificate: true, requireClientCertificate: required); |
34 var clientContext = sendCert ? clientCertContext : clientNoCertContext; | 39 var clientContext = |
| 40 sendCert ? clientCertContext(certType) : clientNoCertContext(certType); |
35 var clientEndFuture = | 41 var clientEndFuture = |
36 SecureSocket.connect(HOST, server.port, context: clientContext); | 42 SecureSocket.connect(HOST, server.port, context: clientContext); |
37 if (required && !sendCert) { | 43 if (required && !sendCert) { |
38 try { | 44 try { |
39 await server.first; | 45 await server.first; |
40 } catch (e) { | 46 } catch (e) { |
41 try { | 47 try { |
42 await clientEndFuture; | 48 await clientEndFuture; |
43 } catch (e) { | 49 } catch (e) { |
44 return; | 50 return; |
(...skipping 16 matching lines...) Expand all Loading... |
61 Expect.isNotNull(serverCertificate); | 67 Expect.isNotNull(serverCertificate); |
62 Expect.equals("/CN=localhost", serverCertificate.subject); | 68 Expect.equals("/CN=localhost", serverCertificate.subject); |
63 Expect.equals("/CN=intermediateauthority", serverCertificate.issuer); | 69 Expect.equals("/CN=intermediateauthority", serverCertificate.issuer); |
64 clientEnd.close(); | 70 clientEnd.close(); |
65 serverEnd.close(); | 71 serverEnd.close(); |
66 } | 72 } |
67 | 73 |
68 main() async { | 74 main() async { |
69 asyncStart(); | 75 asyncStart(); |
70 HOST = (await InternetAddress.lookup("localhost")).first; | 76 HOST = (await InternetAddress.lookup("localhost")).first; |
71 await testClientCertificate(required: false, sendCert: true); | 77 await testClientCertificate(required: false, sendCert: true, certType: 'pem'); |
72 await testClientCertificate(required: true, sendCert: true); | 78 await testClientCertificate(required: true, sendCert: true, certType: 'pem'); |
73 await testClientCertificate(required: false, sendCert: false); | 79 await testClientCertificate( |
74 await testClientCertificate(required: true, sendCert: false); | 80 required: false, sendCert: false, certType: 'pem'); |
| 81 await testClientCertificate(required: true, sendCert: false, certType: 'pem'); |
| 82 |
| 83 await testClientCertificate(required: false, sendCert: true, certType: 'p12'); |
| 84 await testClientCertificate(required: true, sendCert: true, certType: 'p12'); |
| 85 await testClientCertificate( |
| 86 required: false, sendCert: false, certType: 'p12'); |
| 87 await testClientCertificate(required: true, sendCert: false, certType: 'p12'); |
75 asyncEnd(); | 88 asyncEnd(); |
76 } | 89 } |
OLD | NEW |