| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // | |
| 5 // Client that tests that a certificate authority certificate loaded | |
| 6 // at runtime can be used to verify a certificate chain. The server it | |
| 7 // connects to uses localhost_cert, signed by myauthority_cert, to connect | |
| 8 // securely. | |
| 9 | |
| 10 import 'dart:io'; | |
| 11 | |
| 12 void main() { | |
| 13 int port = int.parse(new Options().arguments[0]); | |
| 14 String certificate = new Options().arguments[1]; | |
| 15 SecureSocket.initialize(); | |
| 16 var mycert = new File(certificate).readAsBytesSync(); | |
| 17 bool threw = false; | |
| 18 try { | |
| 19 SecureSocket.addCertificate("I am not a cert".codeUnits, | |
| 20 SecureSocket.TRUST_ISSUE_SERVER_CERTIFICATES); | |
| 21 } on CertificateException catch (e) { | |
| 22 threw = true; | |
| 23 } | |
| 24 if (!threw) throw "Expected bad certificate to throw"; | |
| 25 | |
| 26 threw = false; | |
| 27 try { | |
| 28 SecureSocket.addCertificate(mycert, "Trust me, I'm a string"); | |
| 29 } on CertificateException catch (e) { | |
| 30 threw = true; | |
| 31 } | |
| 32 if (!threw) throw "Expected bad trust string to throw"; | |
| 33 | |
| 34 SecureSocket.addCertificate(mycert, | |
| 35 SecureSocket.TRUST_ISSUE_SERVER_CERTIFICATES); | |
| 36 | |
| 37 SecureSocket.connect('localhost', port).then((SecureSocket socket) { | |
| 38 socket.writeln('hello world'); | |
| 39 socket.listen((data) { }); | |
| 40 return socket.close(); | |
| 41 }).then((_) { | |
| 42 SecureSocket.changeTrust('myauthority_cert', ',,'); | |
| 43 return SecureSocket.connect('localhost', port); | |
| 44 }).then((_) { | |
| 45 throw "Expected untrusted authority to stop connection"; | |
| 46 }, onError: (e) { | |
| 47 if (e is! CertificateException) throw e; | |
| 48 }).then((_) { | |
| 49 SecureSocket.changeTrust('myauthority_cert', 'C,,'); | |
| 50 return SecureSocket.connect('localhost', port); | |
| 51 }).then((SecureSocket socket) { | |
| 52 socket.writeln('hello world'); | |
| 53 socket.listen((data) { }); | |
| 54 return socket.close(); | |
| 55 }).then((_) { | |
| 56 SecureSocket.removeCertificate('myauthority_cert'); | |
| 57 return SecureSocket.connect('localhost', port); | |
| 58 }).then((_) { | |
| 59 throw "Expected untrusted root to stop connection"; | |
| 60 }, onError: (e) { | |
| 61 if (e is! CertificateException) throw e; | |
| 62 }).then((_) { | |
| 63 print('SUCCESS'); // Checked by parent process. | |
| 64 }); | |
| 65 } | |
| OLD | NEW |