| OLD | NEW |
| 1 // Copyright (c) 2012, 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:io"; | 6 import "dart:io"; |
| 6 import "dart:isolate"; | 7 import "dart:isolate"; |
| 7 | 8 |
| 8 const SERVER_ADDRESS = "127.0.0.1"; | 9 const SERVER_ADDRESS = "127.0.0.1"; |
| 9 const HOST_NAME = "localhost"; | 10 const HOST_NAME = "localhost"; |
| 11 const CERTIFICATE = "localhost_cert"; |
| 10 | 12 |
| 11 void WriteAndClose(Socket socket, String message) { | 13 void testClientCertificate() { |
| 12 var data = message.codeUnits; | 14 ReceivePort port = new ReceivePort(); |
| 13 int written = 0; | 15 SecureServerSocket.bind(SERVER_ADDRESS, |
| 14 void write() { | 16 0, |
| 15 written += socket.writeList(data, written, data.length - written); | 17 5, |
| 16 if (written < data.length) { | 18 CERTIFICATE, |
| 17 socket.onWrite = write; | 19 requestClientCertificate: true).then((server) { |
| 18 } else { | 20 var clientEndFuture = SecureSocket.connect(HOST_NAME, |
| 19 socket.close(true); | 21 server.port, |
| 20 } | 22 sendClientCertificate: true); |
| 21 } | 23 server.listen((serverEnd) { |
| 22 write(); | 24 X509Certificate certificate = serverEnd.peerCertificate; |
| 25 Expect.isNotNull(certificate); |
| 26 Expect.equals("CN=localhost", certificate.subject); |
| 27 Expect.equals("CN=myauthority", certificate.issuer); |
| 28 clientEndFuture.then((clientEnd) { |
| 29 X509Certificate certificate = clientEnd.peerCertificate; |
| 30 Expect.isNotNull(certificate); |
| 31 Expect.equals("CN=localhost", certificate.subject); |
| 32 Expect.equals("CN=myauthority", certificate.issuer); |
| 33 clientEnd.close(); |
| 34 serverEnd.close(); |
| 35 server.close(); |
| 36 port.close(); |
| 37 }); |
| 38 }); |
| 39 }); |
| 23 } | 40 } |
| 24 | 41 |
| 25 class SecureTestServer { | 42 void testRequiredClientCertificate() { |
| 26 void onConnection(Socket connection) { | 43 ReceivePort port = new ReceivePort(); |
| 27 connection.onConnect = () { | 44 SecureServerSocket.bind(SERVER_ADDRESS, |
| 28 numConnections++; | 45 0, |
| 29 var certificate = connection.peerCertificate; | 46 5, |
| 30 Expect.isTrue(certificate.subject.contains("CN=")); | 47 CERTIFICATE, |
| 31 }; | 48 requireClientCertificate: true).then((server) { |
| 32 String received = ""; | 49 var clientEndFuture = SecureSocket.connect(HOST_NAME, |
| 33 connection.onData = () { | 50 server.port, |
| 34 received = received.concat(new String.fromCharCodes(connection.read())); | 51 sendClientCertificate: true); |
| 35 }; | 52 server.listen((serverEnd) { |
| 36 connection.onClosed = () { | 53 X509Certificate certificate = serverEnd.peerCertificate; |
| 37 Expect.isTrue(received.contains("Hello from client ")); | 54 Expect.isNotNull(certificate); |
| 38 String name = received.substring(received.indexOf("client ") + 7); | 55 Expect.equals("CN=localhost", certificate.subject); |
| 39 WriteAndClose(connection, "Welcome, client $name"); | 56 Expect.equals("CN=myauthority", certificate.issuer); |
| 40 }; | 57 clientEndFuture.then((clientEnd) { |
| 41 } | 58 X509Certificate certificate = clientEnd.peerCertificate; |
| 42 | 59 Expect.isNotNull(certificate); |
| 43 void errorHandlerServer(Exception e) { | 60 Expect.equals("CN=localhost", certificate.subject); |
| 44 Expect.fail("Server socket error $e"); | 61 Expect.equals("CN=myauthority", certificate.issuer); |
| 45 } | 62 clientEnd.close(); |
| 46 | 63 serverEnd.close(); |
| 47 int start() { | 64 server.close(); |
| 48 server = new SecureServerSocket(SERVER_ADDRESS, | 65 port.close(); |
| 49 0, | 66 }); |
| 50 10, | 67 }); |
| 51 "localhost_cert", | 68 }); |
| 52 requireClientCertificate: true); | |
| 53 Expect.isNotNull(server); | |
| 54 server.onConnection = onConnection; | |
| 55 server.onError = errorHandlerServer; | |
| 56 return server.port; | |
| 57 } | |
| 58 | |
| 59 void stop() { | |
| 60 server.close(); | |
| 61 } | |
| 62 | |
| 63 int numConnections = 0; | |
| 64 SecureServerSocket server; | |
| 65 } | 69 } |
| 66 | 70 |
| 67 class SecureTestClient { | 71 void testNoClientCertificate() { |
| 68 SecureTestClient(int this.port, String this.name) { | 72 ReceivePort port = new ReceivePort(); |
| 69 socket = new SecureSocket(HOST_NAME, port, sendClientCertificate: true); | 73 SecureServerSocket.bind(SERVER_ADDRESS, |
| 70 socket.onConnect = this.onConnect; | 74 0, |
| 71 socket.onData = () { | 75 5, |
| 72 reply = reply.concat(new String.fromCharCodes(socket.read())); | 76 CERTIFICATE, |
| 73 }; | 77 requestClientCertificate: true).then((server) { |
| 74 socket.onClosed = done; | 78 var clientEndFuture = SecureSocket.connect(HOST_NAME, |
| 75 reply = ""; | 79 server.port); |
| 76 } | 80 server.listen((serverEnd) { |
| 77 | 81 X509Certificate certificate = serverEnd.peerCertificate; |
| 78 void onConnect() { | 82 Expect.isNull(certificate); |
| 79 numRequests++; | 83 clientEndFuture.then((clientEnd) { |
| 80 WriteAndClose(socket, "Hello from client $name"); | 84 clientEnd.close(); |
| 81 } | 85 serverEnd.close(); |
| 82 | 86 server.close(); |
| 83 void done() { | 87 port.close(); |
| 84 Expect.equals("Welcome, client $name", reply); | 88 }); |
| 85 numReplies++; | 89 }); |
| 86 if (numReplies == CLIENT_NAMES.length) { | 90 }); |
| 87 Expect.equals(numRequests, numReplies); | |
| 88 EndTest(); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 static int numRequests = 0; | |
| 93 static int numReplies = 0; | |
| 94 | |
| 95 int port; | |
| 96 String name; | |
| 97 SecureSocket socket; | |
| 98 String reply; | |
| 99 } | 91 } |
| 100 | 92 |
| 101 Function EndTest; | 93 void testNoRequiredClientCertificate() { |
| 102 | 94 ReceivePort port = new ReceivePort(); |
| 103 const CLIENT_NAMES = const ['able', 'baker', 'camera', 'donut', 'echo']; | 95 bool clientError = false; |
| 96 SecureServerSocket.bind(SERVER_ADDRESS, |
| 97 0, |
| 98 5, |
| 99 CERTIFICATE, |
| 100 requireClientCertificate: true).then((server) { |
| 101 Future clientDone = SecureSocket.connect(HOST_NAME, server.port) |
| 102 .catchError((e) { clientError = true; }); |
| 103 server.listen((serverEnd) { |
| 104 Expect.fail("Got a unverifiable connection"); |
| 105 }, |
| 106 onError: (e) { |
| 107 clientDone.then((_) { |
| 108 Expect.isTrue(clientError); |
| 109 server.close(); |
| 110 port.close(); |
| 111 }); |
| 112 }); |
| 113 }); |
| 114 } |
| 104 | 115 |
| 105 void main() { | 116 void main() { |
| 106 ReceivePort keepAlive = new ReceivePort(); | |
| 107 Path scriptDir = new Path(new Options().script).directoryPath; | 117 Path scriptDir = new Path(new Options().script).directoryPath; |
| 108 Path certificateDatabase = scriptDir.append('pkcert'); | 118 Path certificateDatabase = scriptDir.append('pkcert'); |
| 109 SecureSocket.initialize(database: certificateDatabase.toNativePath(), | 119 SecureSocket.initialize(database: certificateDatabase.toNativePath(), |
| 110 password: 'dartdart', | 120 password: 'dartdart', |
| 111 useBuiltinRoots: false); | 121 useBuiltinRoots: false); |
| 112 | 122 |
| 113 var server = new SecureTestServer(); | 123 testClientCertificate(); |
| 114 int port = server.start(); | 124 testRequiredClientCertificate(); |
| 115 | 125 testNoClientCertificate(); |
| 116 EndTest = () { | 126 testNoRequiredClientCertificate(); |
| 117 Expect.equals(CLIENT_NAMES.length, server.numConnections); | |
| 118 server.stop(); | |
| 119 keepAlive.close(); | |
| 120 }; | |
| 121 | |
| 122 for (var x in CLIENT_NAMES) { | |
| 123 new SecureTestClient(port, x); | |
| 124 } | |
| 125 } | 127 } |
| OLD | NEW |