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: 'dartdart') | |
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) => |
25 ..setTrustedCertificatesSync(localFile( | 27 new SecurityContext() |
26 'certificates/trusted_certs.$certType')) | 28 ..setTrustedCertificatesSync( |
27 ..useCertificateChainSync(localFile('certificates/client1.$certType')) | 29 localFile('certificates/trusted_certs.$certType'), password: password) |
28 ..usePrivateKeySync(localFile('certificates/client1_key.$certType'), | 30 ..useCertificateChainSync( |
29 password: 'dartdart'); | 31 localFile('certificates/client1.$certType'), password: password) |
32 ..usePrivateKeySync( | |
33 localFile('certificates/client1_key.$certType'), password: 'dartdart'); | |
30 | 34 |
31 SecurityContext clientNoCertContext(String certType) => new SecurityContext() | 35 SecurityContext clientNoCertContext(String certType, String password) => |
36 new SecurityContext() | |
32 ..setTrustedCertificatesSync(localFile( | 37 ..setTrustedCertificatesSync(localFile( |
33 'certificates/trusted_certs.$certType')); | 38 '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 22 matching lines...) Expand all Loading... | |
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'); | 85 await testClientCertificate(required: false, sendCert: true, certType: 'pem'); |
78 await testClientCertificate(required: true, sendCert: true, certType: 'pem'); | 86 await testClientCertificate(required: true, sendCert: true, certType: 'pem'); |
79 await testClientCertificate( | 87 await testClientCertificate( |
80 required: false, sendCert: false, certType: 'pem'); | 88 required: false, sendCert: false, certType: 'pem'); |
81 await testClientCertificate(required: true, sendCert: false, certType: 'pem'); | 89 await testClientCertificate(required: true, sendCert: false, certType: 'pem'); |
82 | 90 |
83 await testClientCertificate(required: false, sendCert: true, certType: 'p12'); | |
84 await testClientCertificate(required: true, sendCert: true, certType: 'p12'); | |
85 await testClientCertificate( | 91 await testClientCertificate( |
86 required: false, sendCert: false, certType: 'p12'); | 92 required: false, sendCert: true, certType: 'p12', password: ''); |
87 await testClientCertificate(required: true, sendCert: false, certType: 'p12'); | 93 await testClientCertificate( |
94 required: true, sendCert: true, certType: 'p12', password: ''); | |
95 await testClientCertificate( | |
96 required: false, sendCert: false, certType: 'p12', password: ''); | |
97 await testClientCertificate( | |
98 required: true, sendCert: false, certType: 'p12', password: ''); | |
Bill Hesse
2016/02/16 20:33:05
Should you test PK12 files with a real password?
zra
2016/02/17 18:42:44
Done.
| |
88 asyncEnd(); | 99 asyncEnd(); |
89 } | 100 } |
OLD | NEW |