| 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 import "package:path/path.dart"; | 10 import "package:path/path.dart"; | 
| 11 | 11 | 
| 12 const HOST_NAME = "localhost"; | 12 const HOST_NAME = "localhost"; | 
|  | 13 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 
| 13 | 14 | 
|  | 15 SecurityContext serverContext = new SecurityContext() | 
|  | 16   ..useCertificateChain(localFile('certificates/server_chain.pem')) | 
|  | 17   ..usePrivateKey(localFile('certificates/server_key.pem'), | 
|  | 18                   password: 'dartdart'); | 
|  | 19 // TODO: Specify which client certificate roots to trust. | 
| 14 | 20 | 
| 15 Function test() { | 21 SecurityContext clientContext = new SecurityContext() | 
|  | 22   ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')) | 
|  | 23 // TODO: Set a client certificate here. | 
|  | 24   ..useCertificateChain(localFile('certificates/server_chain.pem')) | 
|  | 25   ..usePrivateKey(localFile('certificates/server_key.pem'), | 
|  | 26                   password: 'dartdart'); | 
|  | 27 | 
|  | 28 void main() { | 
| 16   asyncStart(); | 29   asyncStart(); | 
| 17   HttpServer.bindSecure(HOST_NAME, | 30   HttpServer.bindSecure(HOST_NAME, | 
| 18                         0, | 31                         0, | 
|  | 32                         serverContext, | 
| 19                         backlog: 5, | 33                         backlog: 5, | 
| 20                         certificateName: 'localhost_cert', |  | 
| 21                         requestClientCertificate: true).then((server) { | 34                         requestClientCertificate: true).then((server) { | 
| 22     server.listen((HttpRequest request) { | 35     server.listen((HttpRequest request) { | 
| 23       Expect.isNotNull(request.certificate); | 36       Expect.isNotNull(request.certificate); | 
| 24       Expect.equals('CN=localhost', request.certificate.subject); | 37       Expect.equals('CN=localhost', request.certificate.subject); | 
| 25       request.response.write("Hello"); | 38       request.response.write("Hello"); | 
| 26       request.response.close(); | 39       request.response.close(); | 
| 27     }); | 40     }); | 
| 28 | 41 | 
| 29     HttpClient client = new HttpClient(); | 42     HttpClient client = new HttpClient(context: clientContext); | 
| 30     client.getUrl(Uri.parse("https://$HOST_NAME:${server.port}/")) | 43     client.getUrl(Uri.parse("https://$HOST_NAME:${server.port}/")) | 
| 31         .then((request) => request.close()) | 44         .then((request) => request.close()) | 
| 32         .then((response) { | 45         .then((response) { | 
| 33           Expect.equals('CN=localhost', response.certificate.subject); | 46           Expect.equals('CN=localhost', response.certificate.subject); | 
| 34           Expect.equals('CN=myauthority', response.certificate.issuer); | 47           Expect.equals('CN=myauthority', response.certificate.issuer); | 
| 35           return response.fold(<int>[], | 48           return response.fold(<int>[], | 
| 36                                (message, data) => message..addAll(data)); | 49                                (message, data) => message..addAll(data)); | 
| 37         }) | 50         }) | 
| 38         .then((message) { | 51         .then((message) { | 
| 39           String received = new String.fromCharCodes(message); | 52           String received = new String.fromCharCodes(message); | 
| 40           Expect.equals(received, "Hello"); | 53           Expect.equals(received, "Hello"); | 
| 41           client.close(); | 54           client.close(); | 
| 42           server.close(); | 55           server.close(); | 
| 43           asyncEnd(); | 56           asyncEnd(); | 
| 44         }); | 57         }); | 
| 45   }); | 58   }); | 
| 46 } | 59 } | 
| 47 |  | 
| 48 void InitializeSSL() { |  | 
| 49   var testPkcertDatabase = Platform.script.resolve('pkcert').toFilePath(); |  | 
| 50   SecureSocket.initialize(database: testPkcertDatabase, |  | 
| 51                           password: 'dartdart'); |  | 
| 52 } |  | 
| 53 |  | 
| 54 void main() { |  | 
| 55   InitializeSSL(); |  | 
| 56   test(); |  | 
| 57 } |  | 
| OLD | NEW | 
|---|