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