OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 @patch class SecureSocket { | 5 @patch |
6 @patch factory SecureSocket._(RawSecureSocket rawSocket) => | 6 class SecureSocket { |
| 7 @patch |
| 8 factory SecureSocket._(RawSecureSocket rawSocket) => |
7 new _SecureSocket(rawSocket); | 9 new _SecureSocket(rawSocket); |
8 } | 10 } |
9 | 11 |
10 | 12 @patch |
11 @patch class _SecureFilter { | 13 class _SecureFilter { |
12 @patch factory _SecureFilter() => new _SecureFilterImpl(); | 14 @patch |
| 15 factory _SecureFilter() => new _SecureFilterImpl(); |
13 } | 16 } |
14 | 17 |
15 @patch class X509Certificate { | 18 @patch |
16 @patch factory X509Certificate._() => new _X509CertificateImpl(); | 19 class X509Certificate { |
| 20 @patch |
| 21 factory X509Certificate._() => new _X509CertificateImpl(); |
17 } | 22 } |
18 | 23 |
19 class _SecureSocket extends _Socket implements SecureSocket { | 24 class _SecureSocket extends _Socket implements SecureSocket { |
20 _SecureSocket(RawSecureSocket raw) : super(raw); | 25 _SecureSocket(RawSecureSocket raw) : super(raw); |
21 | 26 |
22 void set onBadCertificate(bool callback(X509Certificate certificate)) { | 27 void set onBadCertificate(bool callback(X509Certificate certificate)) { |
23 if (_raw == null) { | 28 if (_raw == null) { |
24 throw new StateError("onBadCertificate called on destroyed SecureSocket"); | 29 throw new StateError("onBadCertificate called on destroyed SecureSocket"); |
25 } | 30 } |
26 _raw.onBadCertificate = callback; | 31 _raw.onBadCertificate = callback; |
27 } | 32 } |
28 | 33 |
29 void renegotiate({bool useSessionCache: true, | 34 void renegotiate( |
30 bool requestClientCertificate: false, | 35 {bool useSessionCache: true, |
31 bool requireClientCertificate: false}) { | 36 bool requestClientCertificate: false, |
32 _raw.renegotiate(useSessionCache: useSessionCache, | 37 bool requireClientCertificate: false}) { |
33 requestClientCertificate: requestClientCertificate, | 38 _raw.renegotiate( |
34 requireClientCertificate: requireClientCertificate); | 39 useSessionCache: useSessionCache, |
| 40 requestClientCertificate: requestClientCertificate, |
| 41 requireClientCertificate: requireClientCertificate); |
35 } | 42 } |
36 | 43 |
37 X509Certificate get peerCertificate { | 44 X509Certificate get peerCertificate { |
38 if (_raw == null) { | 45 if (_raw == null) { |
39 throw new StateError("peerCertificate called on destroyed SecureSocket"); | 46 throw new StateError("peerCertificate called on destroyed SecureSocket"); |
40 } | 47 } |
41 return _raw.peerCertificate; | 48 return _raw.peerCertificate; |
42 } | 49 } |
43 | 50 |
44 String get selectedProtocol { | 51 String get selectedProtocol { |
45 if (_raw == null) { | 52 if (_raw == null) { |
46 throw new StateError("selectedProtocol called on destroyed SecureSocket"); | 53 throw new StateError("selectedProtocol called on destroyed SecureSocket"); |
47 } | 54 } |
48 return _raw.selectedProtocol; | 55 return _raw.selectedProtocol; |
49 } | 56 } |
50 } | 57 } |
51 | 58 |
52 | |
53 /** | 59 /** |
54 * _SecureFilterImpl wraps a filter that encrypts and decrypts data travelling | 60 * _SecureFilterImpl wraps a filter that encrypts and decrypts data travelling |
55 * over an encrypted socket. The filter also handles the handshaking | 61 * over an encrypted socket. The filter also handles the handshaking |
56 * and certificate verification. | 62 * and certificate verification. |
57 * | 63 * |
58 * The filter exposes its input and output buffers as Dart objects that | 64 * The filter exposes its input and output buffers as Dart objects that |
59 * are backed by an external C array of bytes, so that both Dart code and | 65 * are backed by an external C array of bytes, so that both Dart code and |
60 * native code can access the same data. | 66 * native code can access the same data. |
61 */ | 67 */ |
62 class _SecureFilterImpl | 68 class _SecureFilterImpl extends NativeFieldWrapperClass1 |
63 extends NativeFieldWrapperClass1 | |
64 implements _SecureFilter { | 69 implements _SecureFilter { |
65 // Performance is improved if a full buffer of plaintext fits | 70 // Performance is improved if a full buffer of plaintext fits |
66 // in the encrypted buffer, when encrypted. | 71 // in the encrypted buffer, when encrypted. |
67 static final int SIZE = 8 * 1024; | 72 static final int SIZE = 8 * 1024; |
68 static final int ENCRYPTED_SIZE = 10 * 1024; | 73 static final int ENCRYPTED_SIZE = 10 * 1024; |
69 | 74 |
70 _SecureFilterImpl() { | 75 _SecureFilterImpl() { |
71 buffers = new List<_ExternalBuffer>(_RawSecureSocket.NUM_BUFFERS); | 76 buffers = new List<_ExternalBuffer>(_RawSecureSocket.NUM_BUFFERS); |
72 for (int i = 0; i < _RawSecureSocket.NUM_BUFFERS; ++i) { | 77 for (int i = 0; i < _RawSecureSocket.NUM_BUFFERS; ++i) { |
73 buffers[i] = new _ExternalBuffer(_RawSecureSocket._isBufferEncrypted(i) ? | 78 buffers[i] = new _ExternalBuffer( |
74 ENCRYPTED_SIZE : | 79 _RawSecureSocket._isBufferEncrypted(i) ? ENCRYPTED_SIZE : SIZE); |
75 SIZE); | |
76 } | 80 } |
77 } | 81 } |
78 | 82 |
79 void connect(String hostName, | 83 void connect( |
80 SecurityContext context, | 84 String hostName, |
81 bool is_server, | 85 SecurityContext context, |
82 bool requestClientCertificate, | 86 bool is_server, |
83 bool requireClientCertificate, | 87 bool requestClientCertificate, |
84 Uint8List protocols) native "SecureSocket_Connect"; | 88 bool requireClientCertificate, |
| 89 Uint8List protocols) native "SecureSocket_Connect"; |
85 | 90 |
86 void destroy() { | 91 void destroy() { |
87 buffers = null; | 92 buffers = null; |
88 _destroy(); | 93 _destroy(); |
89 } | 94 } |
90 | 95 |
91 void _destroy() native "SecureSocket_Destroy"; | 96 void _destroy() native "SecureSocket_Destroy"; |
92 | 97 |
93 void handshake() native "SecureSocket_Handshake"; | 98 void handshake() native "SecureSocket_Handshake"; |
94 | 99 |
95 String selectedProtocol() native "SecureSocket_GetSelectedProtocol"; | 100 String selectedProtocol() native "SecureSocket_GetSelectedProtocol"; |
96 | 101 |
97 void renegotiate(bool useSessionCache, | 102 void renegotiate(bool useSessionCache, bool requestClientCertificate, |
98 bool requestClientCertificate, | 103 bool requireClientCertificate) native "SecureSocket_Renegotiate"; |
99 bool requireClientCertificate) | |
100 native "SecureSocket_Renegotiate"; | |
101 | 104 |
102 void init() native "SecureSocket_Init"; | 105 void init() native "SecureSocket_Init"; |
103 | 106 |
104 X509Certificate get peerCertificate native "SecureSocket_PeerCertificate"; | 107 X509Certificate get peerCertificate native "SecureSocket_PeerCertificate"; |
105 | 108 |
106 void registerBadCertificateCallback(Function callback) | 109 void registerBadCertificateCallback(Function callback) |
107 native "SecureSocket_RegisterBadCertificateCallback"; | 110 native "SecureSocket_RegisterBadCertificateCallback"; |
108 | 111 |
109 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler) | 112 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler) |
110 native "SecureSocket_RegisterHandshakeCompleteCallback"; | 113 native "SecureSocket_RegisterHandshakeCompleteCallback"; |
111 | 114 |
112 // This is a security issue, as it exposes a raw pointer to Dart code. | 115 // This is a security issue, as it exposes a raw pointer to Dart code. |
113 int _pointer() native "SecureSocket_FilterPointer"; | 116 int _pointer() native "SecureSocket_FilterPointer"; |
114 | 117 |
115 List<_ExternalBuffer> buffers; | 118 List<_ExternalBuffer> buffers; |
116 } | 119 } |
117 | 120 |
118 @patch class SecurityContext { | 121 @patch |
119 @patch factory SecurityContext() { | 122 class SecurityContext { |
| 123 @patch |
| 124 factory SecurityContext() { |
120 return new _SecurityContext(); | 125 return new _SecurityContext(); |
121 } | 126 } |
122 | 127 |
123 @patch static SecurityContext get defaultContext { | 128 @patch |
| 129 static SecurityContext get defaultContext { |
124 return _SecurityContext.defaultContext; | 130 return _SecurityContext.defaultContext; |
125 } | 131 } |
126 | 132 |
127 @patch static bool get alpnSupported { | 133 @patch |
| 134 static bool get alpnSupported { |
128 return _SecurityContext.alpnSupported; | 135 return _SecurityContext.alpnSupported; |
129 } | 136 } |
130 } | 137 } |
131 | 138 |
132 class _SecurityContext | 139 class _SecurityContext extends NativeFieldWrapperClass1 |
133 extends NativeFieldWrapperClass1 | |
134 implements SecurityContext { | 140 implements SecurityContext { |
135 _SecurityContext() { | 141 _SecurityContext() { |
136 _createNativeContext(); | 142 _createNativeContext(); |
137 } | 143 } |
138 | 144 |
139 void _createNativeContext() native "SecurityContext_Allocate"; | 145 void _createNativeContext() native "SecurityContext_Allocate"; |
140 | 146 |
141 static final SecurityContext defaultContext = | 147 static final SecurityContext defaultContext = new _SecurityContext() |
142 new _SecurityContext().._trustBuiltinRoots(); | 148 .._trustBuiltinRoots(); |
143 | 149 |
144 void usePrivateKey(String file, {String password}) { | 150 void usePrivateKey(String file, {String password}) { |
145 List<int> bytes = (new File(file)).readAsBytesSync(); | 151 List<int> bytes = (new File(file)).readAsBytesSync(); |
146 usePrivateKeyBytes(bytes, password: password); | 152 usePrivateKeyBytes(bytes, password: password); |
147 } | 153 } |
| 154 |
148 void usePrivateKeyBytes(List<int> keyBytes, {String password}) | 155 void usePrivateKeyBytes(List<int> keyBytes, {String password}) |
149 native "SecurityContext_UsePrivateKeyBytes"; | 156 native "SecurityContext_UsePrivateKeyBytes"; |
150 | 157 |
151 void setTrustedCertificates(String file, {String password}) { | 158 void setTrustedCertificates(String file, {String password}) { |
152 List<int> bytes = (new File(file)).readAsBytesSync(); | 159 List<int> bytes = (new File(file)).readAsBytesSync(); |
153 setTrustedCertificatesBytes(bytes, password: password); | 160 setTrustedCertificatesBytes(bytes, password: password); |
154 } | 161 } |
| 162 |
155 void setTrustedCertificatesBytes(List<int> certBytes, {String password}) | 163 void setTrustedCertificatesBytes(List<int> certBytes, {String password}) |
156 native "SecurityContext_SetTrustedCertificatesBytes"; | 164 native "SecurityContext_SetTrustedCertificatesBytes"; |
157 | 165 |
158 void useCertificateChain(String file, {String password}) { | 166 void useCertificateChain(String file, {String password}) { |
159 List<int> bytes = (new File(file)).readAsBytesSync(); | 167 List<int> bytes = (new File(file)).readAsBytesSync(); |
160 useCertificateChainBytes(bytes, password: password); | 168 useCertificateChainBytes(bytes, password: password); |
161 } | 169 } |
| 170 |
162 void useCertificateChainBytes(List<int> chainBytes, {String password}) | 171 void useCertificateChainBytes(List<int> chainBytes, {String password}) |
163 native "SecurityContext_UseCertificateChainBytes"; | 172 native "SecurityContext_UseCertificateChainBytes"; |
164 | 173 |
165 void setClientAuthorities(String file, {String password}) { | 174 void setClientAuthorities(String file, {String password}) { |
166 List<int> bytes = (new File(file)).readAsBytesSync(); | 175 List<int> bytes = (new File(file)).readAsBytesSync(); |
167 setClientAuthoritiesBytes(bytes, password: password); | 176 setClientAuthoritiesBytes(bytes, password: password); |
168 } | 177 } |
| 178 |
169 void setClientAuthoritiesBytes(List<int> authCertBytes, {String password}) | 179 void setClientAuthoritiesBytes(List<int> authCertBytes, {String password}) |
170 native "SecurityContext_SetClientAuthoritiesBytes"; | 180 native "SecurityContext_SetClientAuthoritiesBytes"; |
171 | 181 |
172 static bool get alpnSupported => _alpnSupported(); | 182 static bool get alpnSupported => _alpnSupported(); |
173 static bool _alpnSupported() native "SecurityContext_AlpnSupported"; | 183 static bool _alpnSupported() native "SecurityContext_AlpnSupported"; |
174 void setAlpnProtocols(List<String> protocols, bool isServer) { | 184 void setAlpnProtocols(List<String> protocols, bool isServer) { |
175 Uint8List encodedProtocols = | 185 Uint8List encodedProtocols = |
176 SecurityContext._protocolsToLengthEncoding(protocols); | 186 SecurityContext._protocolsToLengthEncoding(protocols); |
177 _setAlpnProtocols(encodedProtocols, isServer); | 187 _setAlpnProtocols(encodedProtocols, isServer); |
178 } | 188 } |
| 189 |
179 void _setAlpnProtocols(Uint8List protocols, bool isServer) | 190 void _setAlpnProtocols(Uint8List protocols, bool isServer) |
180 native "SecurityContext_SetAlpnProtocols"; | 191 native "SecurityContext_SetAlpnProtocols"; |
181 void _trustBuiltinRoots() | 192 void _trustBuiltinRoots() native "SecurityContext_TrustBuiltinRoots"; |
182 native "SecurityContext_TrustBuiltinRoots"; | |
183 } | 193 } |
184 | 194 |
185 /** | 195 /** |
186 * _X509CertificateImpl wraps an X509 certificate object held by the BoringSSL | 196 * _X509CertificateImpl wraps an X509 certificate object held by the BoringSSL |
187 * library. It exposes the fields of the certificate object. | 197 * library. It exposes the fields of the certificate object. |
188 */ | 198 */ |
189 class _X509CertificateImpl extends NativeFieldWrapperClass1 | 199 class _X509CertificateImpl extends NativeFieldWrapperClass1 |
190 implements X509Certificate { | 200 implements X509Certificate { |
191 // The native field must be set manually on a new object, in native code. | 201 // The native field must be set manually on a new object, in native code. |
192 // This is done by WrappedX509 in secure_socket.cc. | 202 // This is done by WrappedX509 in secure_socket.cc. |
193 _X509CertificateImpl(); | 203 _X509CertificateImpl(); |
194 | 204 |
195 String get subject native "X509_Subject"; | 205 String get subject native "X509_Subject"; |
196 String get issuer native "X509_Issuer"; | 206 String get issuer native "X509_Issuer"; |
197 DateTime get startValidity { | 207 DateTime get startValidity { |
198 return new DateTime.fromMillisecondsSinceEpoch(_startValidity(), | 208 return new DateTime.fromMillisecondsSinceEpoch(_startValidity(), |
199 isUtc: true); | 209 isUtc: true); |
200 } | 210 } |
| 211 |
201 DateTime get endValidity { | 212 DateTime get endValidity { |
202 return new DateTime.fromMillisecondsSinceEpoch(_endValidity(), | 213 return new DateTime.fromMillisecondsSinceEpoch(_endValidity(), isUtc: true); |
203 isUtc: true); | |
204 } | 214 } |
| 215 |
205 int _startValidity() native "X509_StartValidity"; | 216 int _startValidity() native "X509_StartValidity"; |
206 int _endValidity() native "X509_EndValidity"; | 217 int _endValidity() native "X509_EndValidity"; |
207 } | 218 } |
OLD | NEW |