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 #if !defined(DART_IO_DISABLED) && !defined(DART_IO_SECURE_SOCKET_DISABLED) | 5 #if !defined(DART_IO_DISABLED) && !defined(DART_IO_SECURE_SOCKET_DISABLED) |
6 | 6 |
7 #include "platform/globals.h" | 7 #include "platform/globals.h" |
8 #if defined(TARGET_OS_ANDROID) || \ | 8 #if defined(TARGET_OS_ANDROID) || defined(TARGET_OS_LINUX) || \ |
9 defined(TARGET_OS_LINUX) || \ | |
10 defined(TARGET_OS_WINDOWS) | 9 defined(TARGET_OS_WINDOWS) |
11 | 10 |
12 #include "bin/secure_socket.h" | 11 #include "bin/secure_socket.h" |
13 #include "bin/secure_socket_boringssl.h" | 12 #include "bin/secure_socket_boringssl.h" |
14 | 13 |
15 #include <errno.h> | 14 #include <errno.h> |
16 #include <fcntl.h> | 15 #include <fcntl.h> |
17 #include <stdio.h> | 16 #include <stdio.h> |
18 #include <string.h> | 17 #include <string.h> |
19 #include <sys/stat.h> | 18 #include <sys/stat.h> |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 } | 109 } |
111 Dart_ThrowException(exception); | 110 Dart_ThrowException(exception); |
112 UNREACHABLE(); | 111 UNREACHABLE(); |
113 } | 112 } |
114 | 113 |
115 | 114 |
116 static SSLFilter* GetFilter(Dart_NativeArguments args) { | 115 static SSLFilter* GetFilter(Dart_NativeArguments args) { |
117 SSLFilter* filter; | 116 SSLFilter* filter; |
118 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | 117 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
119 ASSERT(Dart_IsInstance(dart_this)); | 118 ASSERT(Dart_IsInstance(dart_this)); |
120 ThrowIfError(Dart_GetNativeInstanceField( | 119 ThrowIfError( |
121 dart_this, | 120 Dart_GetNativeInstanceField(dart_this, kSSLFilterNativeFieldIndex, |
122 kSSLFilterNativeFieldIndex, | 121 reinterpret_cast<intptr_t*>(&filter))); |
123 reinterpret_cast<intptr_t*>(&filter))); | |
124 return filter; | 122 return filter; |
125 } | 123 } |
126 | 124 |
127 | 125 |
128 static void DeleteFilter( | 126 static void DeleteFilter(void* isolate_data, |
129 void* isolate_data, | 127 Dart_WeakPersistentHandle handle, |
130 Dart_WeakPersistentHandle handle, | 128 void* context_pointer) { |
131 void* context_pointer) { | |
132 SSLFilter* filter = reinterpret_cast<SSLFilter*>(context_pointer); | 129 SSLFilter* filter = reinterpret_cast<SSLFilter*>(context_pointer); |
133 filter->Release(); | 130 filter->Release(); |
134 } | 131 } |
135 | 132 |
136 | 133 |
137 static Dart_Handle SetFilter(Dart_NativeArguments args, SSLFilter* filter) { | 134 static Dart_Handle SetFilter(Dart_NativeArguments args, SSLFilter* filter) { |
138 ASSERT(filter != NULL); | 135 ASSERT(filter != NULL); |
139 Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); | 136 Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); |
140 RETURN_IF_ERROR(dart_this); | 137 RETURN_IF_ERROR(dart_this); |
141 ASSERT(Dart_IsInstance(dart_this)); | 138 ASSERT(Dart_IsInstance(dart_this)); |
142 Dart_Handle err = Dart_SetNativeInstanceField( | 139 Dart_Handle err = |
143 dart_this, | 140 Dart_SetNativeInstanceField(dart_this, kSSLFilterNativeFieldIndex, |
144 kSSLFilterNativeFieldIndex, | 141 reinterpret_cast<intptr_t>(filter)); |
145 reinterpret_cast<intptr_t>(filter)); | |
146 RETURN_IF_ERROR(err); | 142 RETURN_IF_ERROR(err); |
147 Dart_NewWeakPersistentHandle(dart_this, | 143 Dart_NewWeakPersistentHandle(dart_this, reinterpret_cast<void*>(filter), |
148 reinterpret_cast<void*>(filter), | 144 sizeof(*filter), DeleteFilter); |
149 sizeof(*filter), | |
150 DeleteFilter); | |
151 return Dart_Null(); | 145 return Dart_Null(); |
152 } | 146 } |
153 | 147 |
154 | 148 |
155 static SSLContext* GetSecurityContext(Dart_NativeArguments args) { | 149 static SSLContext* GetSecurityContext(Dart_NativeArguments args) { |
156 SSLContext* context; | 150 SSLContext* context; |
157 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | 151 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
158 ASSERT(Dart_IsInstance(dart_this)); | 152 ASSERT(Dart_IsInstance(dart_this)); |
159 ThrowIfError(Dart_GetNativeInstanceField( | 153 ThrowIfError( |
160 dart_this, | 154 Dart_GetNativeInstanceField(dart_this, kSecurityContextNativeFieldIndex, |
161 kSecurityContextNativeFieldIndex, | 155 reinterpret_cast<intptr_t*>(&context))); |
162 reinterpret_cast<intptr_t*>(&context))); | |
163 return context; | 156 return context; |
164 } | 157 } |
165 | 158 |
166 | 159 |
167 static void DeleteSecurityContext( | 160 static void DeleteSecurityContext(void* isolate_data, |
168 void* isolate_data, | 161 Dart_WeakPersistentHandle handle, |
169 Dart_WeakPersistentHandle handle, | 162 void* context_pointer) { |
170 void* context_pointer) { | |
171 SSLContext* context = static_cast<SSLContext*>(context_pointer); | 163 SSLContext* context = static_cast<SSLContext*>(context_pointer); |
172 delete context; | 164 delete context; |
173 } | 165 } |
174 | 166 |
175 | 167 |
176 static Dart_Handle SetSecurityContext(Dart_NativeArguments args, | 168 static Dart_Handle SetSecurityContext(Dart_NativeArguments args, |
177 SSLContext* context) { | 169 SSLContext* context) { |
178 const int approximate_size_of_context = 1500; | 170 const int approximate_size_of_context = 1500; |
179 Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); | 171 Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); |
180 RETURN_IF_ERROR(dart_this); | 172 RETURN_IF_ERROR(dart_this); |
181 ASSERT(Dart_IsInstance(dart_this)); | 173 ASSERT(Dart_IsInstance(dart_this)); |
182 Dart_Handle err = Dart_SetNativeInstanceField( | 174 Dart_Handle err = |
183 dart_this, | 175 Dart_SetNativeInstanceField(dart_this, kSecurityContextNativeFieldIndex, |
184 kSecurityContextNativeFieldIndex, | 176 reinterpret_cast<intptr_t>(context)); |
185 reinterpret_cast<intptr_t>(context)); | |
186 RETURN_IF_ERROR(err); | 177 RETURN_IF_ERROR(err); |
187 Dart_NewWeakPersistentHandle(dart_this, | 178 Dart_NewWeakPersistentHandle(dart_this, context, approximate_size_of_context, |
188 context, | |
189 approximate_size_of_context, | |
190 DeleteSecurityContext); | 179 DeleteSecurityContext); |
191 return Dart_Null(); | 180 return Dart_Null(); |
192 } | 181 } |
193 | 182 |
194 | 183 |
195 static X509* GetX509Certificate(Dart_NativeArguments args) { | 184 static X509* GetX509Certificate(Dart_NativeArguments args) { |
196 X509* certificate; | 185 X509* certificate; |
197 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | 186 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
198 ASSERT(Dart_IsInstance(dart_this)); | 187 ASSERT(Dart_IsInstance(dart_this)); |
199 ThrowIfError(Dart_GetNativeInstanceField( | 188 ThrowIfError( |
200 dart_this, | 189 Dart_GetNativeInstanceField(dart_this, kX509NativeFieldIndex, |
201 kX509NativeFieldIndex, | 190 reinterpret_cast<intptr_t*>(&certificate))); |
202 reinterpret_cast<intptr_t*>(&certificate))); | |
203 return certificate; | 191 return certificate; |
204 } | 192 } |
205 | 193 |
206 | 194 |
207 // Forward declaration. | 195 // Forward declaration. |
208 static void SetAlpnProtocolList(Dart_Handle protocols_handle, | 196 static void SetAlpnProtocolList(Dart_Handle protocols_handle, |
209 SSL* ssl, | 197 SSL* ssl, |
210 SSLContext* context, | 198 SSLContext* context, |
211 bool is_server); | 199 bool is_server); |
212 | 200 |
(...skipping 17 matching lines...) Expand all Loading... |
230 | 218 |
231 | 219 |
232 void FUNCTION_NAME(SecureSocket_Connect)(Dart_NativeArguments args) { | 220 void FUNCTION_NAME(SecureSocket_Connect)(Dart_NativeArguments args) { |
233 Dart_Handle host_name_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); | 221 Dart_Handle host_name_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
234 Dart_Handle context_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); | 222 Dart_Handle context_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); |
235 bool is_server = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); | 223 bool is_server = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); |
236 bool request_client_certificate = | 224 bool request_client_certificate = |
237 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); | 225 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); |
238 bool require_client_certificate = | 226 bool require_client_certificate = |
239 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 5)); | 227 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 5)); |
240 Dart_Handle protocols_handle = | 228 Dart_Handle protocols_handle = ThrowIfError(Dart_GetNativeArgument(args, 6)); |
241 ThrowIfError(Dart_GetNativeArgument(args, 6)); | |
242 | 229 |
243 const char* host_name = NULL; | 230 const char* host_name = NULL; |
244 // TODO(whesse): Is truncating a Dart string containing \0 what we want? | 231 // TODO(whesse): Is truncating a Dart string containing \0 what we want? |
245 ThrowIfError(Dart_StringToCString(host_name_object, &host_name)); | 232 ThrowIfError(Dart_StringToCString(host_name_object, &host_name)); |
246 | 233 |
247 SSLContext* context = NULL; | 234 SSLContext* context = NULL; |
248 if (!Dart_IsNull(context_object)) { | 235 if (!Dart_IsNull(context_object)) { |
249 ThrowIfError(Dart_GetNativeInstanceField( | 236 ThrowIfError(Dart_GetNativeInstanceField( |
250 context_object, | 237 context_object, kSecurityContextNativeFieldIndex, |
251 kSecurityContextNativeFieldIndex, | |
252 reinterpret_cast<intptr_t*>(&context))); | 238 reinterpret_cast<intptr_t*>(&context))); |
253 } | 239 } |
254 | 240 |
255 // The protocols_handle is guaranteed to be a valid Uint8List. | 241 // The protocols_handle is guaranteed to be a valid Uint8List. |
256 // It will have the correct length encoding of the protocols array. | 242 // It will have the correct length encoding of the protocols array. |
257 ASSERT(!Dart_IsNull(protocols_handle)); | 243 ASSERT(!Dart_IsNull(protocols_handle)); |
258 | 244 |
259 GetFilter(args)->Connect(host_name, | 245 GetFilter(args)->Connect(host_name, context->context(), is_server, |
260 context->context(), | |
261 is_server, | |
262 request_client_certificate, | 246 request_client_certificate, |
263 require_client_certificate, | 247 require_client_certificate, protocols_handle); |
264 protocols_handle); | |
265 } | 248 } |
266 | 249 |
267 | 250 |
268 void FUNCTION_NAME(SecureSocket_Destroy)(Dart_NativeArguments args) { | 251 void FUNCTION_NAME(SecureSocket_Destroy)(Dart_NativeArguments args) { |
269 SSLFilter* filter = GetFilter(args); | 252 SSLFilter* filter = GetFilter(args); |
270 // The SSLFilter is deleted in the finalizer for the Dart object created by | 253 // The SSLFilter is deleted in the finalizer for the Dart object created by |
271 // SetFilter. There is no need to NULL-out the native field for the SSLFilter | 254 // SetFilter. There is no need to NULL-out the native field for the SSLFilter |
272 // here because the SSLFilter won't be deleted until the finalizer for the | 255 // here because the SSLFilter won't be deleted until the finalizer for the |
273 // Dart object runs while the Dart object is being GCd. This approach avoids a | 256 // Dart object runs while the Dart object is being GCd. This approach avoids a |
274 // leak if Destroy isn't called, and avoids a NULL-dereference if Destroy is | 257 // leak if Destroy isn't called, and avoids a NULL-dereference if Destroy is |
(...skipping 13 matching lines...) Expand all Loading... |
288 } | 271 } |
289 | 272 |
290 | 273 |
291 void FUNCTION_NAME(SecureSocket_Renegotiate)(Dart_NativeArguments args) { | 274 void FUNCTION_NAME(SecureSocket_Renegotiate)(Dart_NativeArguments args) { |
292 bool use_session_cache = | 275 bool use_session_cache = |
293 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 1)); | 276 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 1)); |
294 bool request_client_certificate = | 277 bool request_client_certificate = |
295 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 2)); | 278 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 2)); |
296 bool require_client_certificate = | 279 bool require_client_certificate = |
297 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); | 280 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); |
298 GetFilter(args)->Renegotiate(use_session_cache, | 281 GetFilter(args)->Renegotiate(use_session_cache, request_client_certificate, |
299 request_client_certificate, | |
300 require_client_certificate); | 282 require_client_certificate); |
301 } | 283 } |
302 | 284 |
303 | 285 |
304 void FUNCTION_NAME(SecureSocket_RegisterHandshakeCompleteCallback)( | 286 void FUNCTION_NAME(SecureSocket_RegisterHandshakeCompleteCallback)( |
305 Dart_NativeArguments args) { | 287 Dart_NativeArguments args) { |
306 Dart_Handle handshake_complete = | 288 Dart_Handle handshake_complete = |
307 ThrowIfError(Dart_GetNativeArgument(args, 1)); | 289 ThrowIfError(Dart_GetNativeArgument(args, 1)); |
308 if (!Dart_IsClosure(handshake_complete)) { | 290 if (!Dart_IsClosure(handshake_complete)) { |
309 Dart_ThrowException(DartUtils::NewDartArgumentError( | 291 Dart_ThrowException(DartUtils::NewDartArgumentError( |
310 "Illegal argument to RegisterHandshakeCompleteCallback")); | 292 "Illegal argument to RegisterHandshakeCompleteCallback")); |
311 } | 293 } |
312 GetFilter(args)->RegisterHandshakeCompleteCallback(handshake_complete); | 294 GetFilter(args)->RegisterHandshakeCompleteCallback(handshake_complete); |
313 } | 295 } |
314 | 296 |
315 | 297 |
316 void FUNCTION_NAME(SecureSocket_RegisterBadCertificateCallback)( | 298 void FUNCTION_NAME(SecureSocket_RegisterBadCertificateCallback)( |
317 Dart_NativeArguments args) { | 299 Dart_NativeArguments args) { |
318 Dart_Handle callback = | 300 Dart_Handle callback = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
319 ThrowIfError(Dart_GetNativeArgument(args, 1)); | |
320 if (!Dart_IsClosure(callback) && !Dart_IsNull(callback)) { | 301 if (!Dart_IsClosure(callback) && !Dart_IsNull(callback)) { |
321 Dart_ThrowException(DartUtils::NewDartArgumentError( | 302 Dart_ThrowException(DartUtils::NewDartArgumentError( |
322 "Illegal argument to RegisterBadCertificateCallback")); | 303 "Illegal argument to RegisterBadCertificateCallback")); |
323 } | 304 } |
324 GetFilter(args)->RegisterBadCertificateCallback(callback); | 305 GetFilter(args)->RegisterBadCertificateCallback(callback); |
325 } | 306 } |
326 | 307 |
327 | 308 |
328 void FUNCTION_NAME(SecureSocket_PeerCertificate) | 309 void FUNCTION_NAME(SecureSocket_PeerCertificate)(Dart_NativeArguments args) { |
329 (Dart_NativeArguments args) { | |
330 Dart_Handle cert = ThrowIfError(GetFilter(args)->PeerCertificate()); | 310 Dart_Handle cert = ThrowIfError(GetFilter(args)->PeerCertificate()); |
331 Dart_SetReturnValue(args, cert); | 311 Dart_SetReturnValue(args, cert); |
332 } | 312 } |
333 | 313 |
334 | 314 |
335 void FUNCTION_NAME(SecureSocket_FilterPointer)(Dart_NativeArguments args) { | 315 void FUNCTION_NAME(SecureSocket_FilterPointer)(Dart_NativeArguments args) { |
336 SSLFilter* filter = GetFilter(args); | 316 SSLFilter* filter = GetFilter(args); |
337 // This filter pointer is passed to the IO Service thread. The IO Service | 317 // This filter pointer is passed to the IO Service thread. The IO Service |
338 // thread must Release() the pointer when it is done with it. | 318 // thread must Release() the pointer when it is done with it. |
339 filter->Retain(); | 319 filter->Retain(); |
340 intptr_t filter_pointer = reinterpret_cast<intptr_t>(filter); | 320 intptr_t filter_pointer = reinterpret_cast<intptr_t>(filter); |
341 Dart_SetReturnValue(args, Dart_NewInteger(filter_pointer)); | 321 Dart_SetReturnValue(args, Dart_NewInteger(filter_pointer)); |
342 } | 322 } |
343 | 323 |
344 | 324 |
345 static void ReleaseCertificate( | 325 static void ReleaseCertificate(void* isolate_data, |
346 void* isolate_data, | 326 Dart_WeakPersistentHandle handle, |
347 Dart_WeakPersistentHandle handle, | 327 void* context_pointer) { |
348 void* context_pointer) { | |
349 X509* cert = reinterpret_cast<X509*>(context_pointer); | 328 X509* cert = reinterpret_cast<X509*>(context_pointer); |
350 X509_free(cert); | 329 X509_free(cert); |
351 } | 330 } |
352 | 331 |
353 | 332 |
354 // Returns the handle for a Dart object wrapping the X509 certificate object. | 333 // Returns the handle for a Dart object wrapping the X509 certificate object. |
355 // The caller should own a reference to the X509 object whose reference count | 334 // The caller should own a reference to the X509 object whose reference count |
356 // won't drop to zero before the ReleaseCertificate finalizer runs. | 335 // won't drop to zero before the ReleaseCertificate finalizer runs. |
357 static Dart_Handle WrappedX509Certificate(X509* certificate) { | 336 static Dart_Handle WrappedX509Certificate(X509* certificate) { |
358 const intptr_t approximate_size_of_certificate = 1500; | 337 const intptr_t approximate_size_of_certificate = 1500; |
359 if (certificate == NULL) { | 338 if (certificate == NULL) { |
360 return Dart_Null(); | 339 return Dart_Null(); |
361 } | 340 } |
362 Dart_Handle x509_type = | 341 Dart_Handle x509_type = |
363 DartUtils::GetDartType(DartUtils::kIOLibURL, "X509Certificate"); | 342 DartUtils::GetDartType(DartUtils::kIOLibURL, "X509Certificate"); |
364 if (Dart_IsError(x509_type)) { | 343 if (Dart_IsError(x509_type)) { |
365 X509_free(certificate); | 344 X509_free(certificate); |
366 return x509_type; | 345 return x509_type; |
367 } | 346 } |
368 Dart_Handle arguments[] = { NULL }; | 347 Dart_Handle arguments[] = {NULL}; |
369 Dart_Handle result = | 348 Dart_Handle result = |
370 Dart_New(x509_type, DartUtils::NewString("_"), 0, arguments); | 349 Dart_New(x509_type, DartUtils::NewString("_"), 0, arguments); |
371 if (Dart_IsError(result)) { | 350 if (Dart_IsError(result)) { |
372 X509_free(certificate); | 351 X509_free(certificate); |
373 return result; | 352 return result; |
374 } | 353 } |
375 ASSERT(Dart_IsInstance(result)); | 354 ASSERT(Dart_IsInstance(result)); |
376 Dart_Handle status = Dart_SetNativeInstanceField( | 355 Dart_Handle status = Dart_SetNativeInstanceField( |
377 result, | 356 result, kX509NativeFieldIndex, reinterpret_cast<intptr_t>(certificate)); |
378 kX509NativeFieldIndex, | |
379 reinterpret_cast<intptr_t>(certificate)); | |
380 if (Dart_IsError(status)) { | 357 if (Dart_IsError(status)) { |
381 X509_free(certificate); | 358 X509_free(certificate); |
382 return status; | 359 return status; |
383 } | 360 } |
384 Dart_NewWeakPersistentHandle(result, | 361 Dart_NewWeakPersistentHandle(result, reinterpret_cast<void*>(certificate), |
385 reinterpret_cast<void*>(certificate), | |
386 approximate_size_of_certificate, | 362 approximate_size_of_certificate, |
387 ReleaseCertificate); | 363 ReleaseCertificate); |
388 return result; | 364 return result; |
389 } | 365 } |
390 | 366 |
391 | 367 |
392 int CertificateCallback(int preverify_ok, X509_STORE_CTX* store_ctx) { | 368 int CertificateCallback(int preverify_ok, X509_STORE_CTX* store_ctx) { |
393 if (preverify_ok == 1) { | 369 if (preverify_ok == 1) { |
394 return 1; | 370 return 1; |
395 } | 371 } |
396 Dart_Isolate isolate = Dart_CurrentIsolate(); | 372 Dart_Isolate isolate = Dart_CurrentIsolate(); |
397 if (isolate == NULL) { | 373 if (isolate == NULL) { |
398 FATAL("CertificateCallback called with no current isolate\n"); | 374 FATAL("CertificateCallback called with no current isolate\n"); |
399 } | 375 } |
400 X509* certificate = X509_STORE_CTX_get_current_cert(store_ctx); | 376 X509* certificate = X509_STORE_CTX_get_current_cert(store_ctx); |
401 int ssl_index = SSL_get_ex_data_X509_STORE_CTX_idx(); | 377 int ssl_index = SSL_get_ex_data_X509_STORE_CTX_idx(); |
402 SSL* ssl = static_cast<SSL*>( | 378 SSL* ssl = |
403 X509_STORE_CTX_get_ex_data(store_ctx, ssl_index)); | 379 static_cast<SSL*>(X509_STORE_CTX_get_ex_data(store_ctx, ssl_index)); |
404 SSLFilter* filter = static_cast<SSLFilter*>( | 380 SSLFilter* filter = static_cast<SSLFilter*>( |
405 SSL_get_ex_data(ssl, SSLFilter::filter_ssl_index)); | 381 SSL_get_ex_data(ssl, SSLFilter::filter_ssl_index)); |
406 Dart_Handle callback = filter->bad_certificate_callback(); | 382 Dart_Handle callback = filter->bad_certificate_callback(); |
407 if (Dart_IsNull(callback)) { | 383 if (Dart_IsNull(callback)) { |
408 return 0; | 384 return 0; |
409 } | 385 } |
410 | 386 |
411 // Upref since the Dart X509 object may outlive the SecurityContext. | 387 // Upref since the Dart X509 object may outlive the SecurityContext. |
412 if (certificate != NULL) { | 388 if (certificate != NULL) { |
413 X509_up_ref(certificate); | 389 X509_up_ref(certificate); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 // implementing List<int>, this class creates a scope in which a memory-backed | 453 // implementing List<int>, this class creates a scope in which a memory-backed |
478 // BIO is allocated. Leaving the scope cleans up the BIO and the buffer that | 454 // BIO is allocated. Leaving the scope cleans up the BIO and the buffer that |
479 // was used to create it. | 455 // was used to create it. |
480 // | 456 // |
481 // Do not make Dart_ API calls while in a ScopedMemBIO. | 457 // Do not make Dart_ API calls while in a ScopedMemBIO. |
482 // Do not call Dart_PropagateError while in a ScopedMemBIO. | 458 // Do not call Dart_PropagateError while in a ScopedMemBIO. |
483 class ScopedMemBIO { | 459 class ScopedMemBIO { |
484 public: | 460 public: |
485 explicit ScopedMemBIO(Dart_Handle object) { | 461 explicit ScopedMemBIO(Dart_Handle object) { |
486 if (!Dart_IsTypedData(object) && !Dart_IsList(object)) { | 462 if (!Dart_IsTypedData(object) && !Dart_IsList(object)) { |
487 Dart_ThrowException(DartUtils::NewDartArgumentError( | 463 Dart_ThrowException( |
488 "Argument is not a List<int>")); | 464 DartUtils::NewDartArgumentError("Argument is not a List<int>")); |
489 } | 465 } |
490 | 466 |
491 uint8_t* bytes = NULL; | 467 uint8_t* bytes = NULL; |
492 intptr_t bytes_len = 0; | 468 intptr_t bytes_len = 0; |
493 bool is_typed_data = false; | 469 bool is_typed_data = false; |
494 if (Dart_IsTypedData(object)) { | 470 if (Dart_IsTypedData(object)) { |
495 is_typed_data = true; | 471 is_typed_data = true; |
496 Dart_TypedData_Type typ; | 472 Dart_TypedData_Type typ; |
497 ThrowIfError(Dart_TypedDataAcquireData( | 473 ThrowIfError(Dart_TypedDataAcquireData( |
498 object, | 474 object, &typ, reinterpret_cast<void**>(&bytes), &bytes_len)); |
499 &typ, | |
500 reinterpret_cast<void**>(&bytes), | |
501 &bytes_len)); | |
502 } else { | 475 } else { |
503 ASSERT(Dart_IsList(object)); | 476 ASSERT(Dart_IsList(object)); |
504 ThrowIfError(Dart_ListLength(object, &bytes_len)); | 477 ThrowIfError(Dart_ListLength(object, &bytes_len)); |
505 bytes = Dart_ScopeAllocate(bytes_len); | 478 bytes = Dart_ScopeAllocate(bytes_len); |
506 ASSERT(bytes != NULL); | 479 ASSERT(bytes != NULL); |
507 ThrowIfError(Dart_ListGetAsBytes(object, 0, bytes, bytes_len)); | 480 ThrowIfError(Dart_ListGetAsBytes(object, 0, bytes, bytes_len)); |
508 } | 481 } |
509 | 482 |
510 object_ = object; | 483 object_ = object; |
511 bytes_ = bytes; | 484 bytes_ = bytes; |
(...skipping 22 matching lines...) Expand all Loading... |
534 Dart_Handle object_; | 507 Dart_Handle object_; |
535 uint8_t* bytes_; | 508 uint8_t* bytes_; |
536 intptr_t bytes_len_; | 509 intptr_t bytes_len_; |
537 BIO* bio_; | 510 BIO* bio_; |
538 bool is_typed_data_; | 511 bool is_typed_data_; |
539 | 512 |
540 DISALLOW_ALLOCATION(); | 513 DISALLOW_ALLOCATION(); |
541 DISALLOW_COPY_AND_ASSIGN(ScopedMemBIO); | 514 DISALLOW_COPY_AND_ASSIGN(ScopedMemBIO); |
542 }; | 515 }; |
543 | 516 |
544 template<typename T, void (*free_func)(T*)> | 517 template <typename T, void (*free_func)(T*)> |
545 class ScopedSSLType { | 518 class ScopedSSLType { |
546 public: | 519 public: |
547 explicit ScopedSSLType(T* obj) : obj_(obj) {} | 520 explicit ScopedSSLType(T* obj) : obj_(obj) {} |
548 | 521 |
549 ~ScopedSSLType() { | 522 ~ScopedSSLType() { |
550 if (obj_ != NULL) { | 523 if (obj_ != NULL) { |
551 free_func(obj_); | 524 free_func(obj_); |
552 } | 525 } |
553 } | 526 } |
554 | 527 |
555 T* get() { return obj_; } | 528 T* get() { return obj_; } |
556 const T* get() const { return obj_; } | 529 const T* get() const { return obj_; } |
557 | 530 |
558 T* release() { | 531 T* release() { |
559 T* result = obj_; | 532 T* result = obj_; |
560 obj_ = NULL; | 533 obj_ = NULL; |
561 return result; | 534 return result; |
562 } | 535 } |
563 | 536 |
564 private: | 537 private: |
565 T* obj_; | 538 T* obj_; |
566 | 539 |
567 DISALLOW_ALLOCATION(); | 540 DISALLOW_ALLOCATION(); |
568 DISALLOW_COPY_AND_ASSIGN(ScopedSSLType); | 541 DISALLOW_COPY_AND_ASSIGN(ScopedSSLType); |
569 }; | 542 }; |
570 | 543 |
571 template<typename T, typename E, void (*func)(E*)> | 544 template <typename T, typename E, void (*func)(E*)> |
572 class ScopedSSLStackType { | 545 class ScopedSSLStackType { |
573 public: | 546 public: |
574 explicit ScopedSSLStackType(T* obj) : obj_(obj) {} | 547 explicit ScopedSSLStackType(T* obj) : obj_(obj) {} |
575 | 548 |
576 ~ScopedSSLStackType() { | 549 ~ScopedSSLStackType() { |
577 if (obj_ != NULL) { | 550 if (obj_ != NULL) { |
578 sk_pop_free(reinterpret_cast<_STACK*>(obj_), | 551 sk_pop_free(reinterpret_cast<_STACK*>(obj_), |
579 reinterpret_cast<void (*)(void *)>(func)); | 552 reinterpret_cast<void (*)(void*)>(func)); |
580 } | 553 } |
581 } | 554 } |
582 | 555 |
583 T* get() { return obj_; } | 556 T* get() { return obj_; } |
584 const T* get() const { return obj_; } | 557 const T* get() const { return obj_; } |
585 | 558 |
586 T* release() { | 559 T* release() { |
587 T* result = obj_; | 560 T* result = obj_; |
588 obj_ = NULL; | 561 obj_ = NULL; |
589 return result; | 562 return result; |
(...skipping 17 matching lines...) Expand all Loading... |
607 } | 580 } |
608 | 581 |
609 | 582 |
610 static EVP_PKEY* GetPrivateKeyPKCS12(BIO* bio, const char* password) { | 583 static EVP_PKEY* GetPrivateKeyPKCS12(BIO* bio, const char* password) { |
611 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); | 584 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); |
612 if (p12.get() == NULL) { | 585 if (p12.get() == NULL) { |
613 return NULL; | 586 return NULL; |
614 } | 587 } |
615 | 588 |
616 EVP_PKEY* key = NULL; | 589 EVP_PKEY* key = NULL; |
617 X509 *cert = NULL; | 590 X509* cert = NULL; |
618 STACK_OF(X509) *ca_certs = NULL; | 591 STACK_OF(X509)* ca_certs = NULL; |
619 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); | 592 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); |
620 if (status == 0) { | 593 if (status == 0) { |
621 return NULL; | 594 return NULL; |
622 } | 595 } |
623 | 596 |
624 // We only care about the private key. | 597 // We only care about the private key. |
625 ScopedX509 delete_cert(cert); | 598 ScopedX509 delete_cert(cert); |
626 ScopedX509Stack delete_ca_certs(ca_certs); | 599 ScopedX509Stack delete_ca_certs(ca_certs); |
627 return key; | 600 return key; |
628 } | 601 } |
629 | 602 |
630 | 603 |
631 static EVP_PKEY* GetPrivateKey(BIO* bio, const char* password) { | 604 static EVP_PKEY* GetPrivateKey(BIO* bio, const char* password) { |
632 EVP_PKEY *key = PEM_read_bio_PrivateKey( | 605 EVP_PKEY* key = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallback, |
633 bio, NULL, PasswordCallback, const_cast<char*>(password)); | 606 const_cast<char*>(password)); |
634 if (key == NULL) { | 607 if (key == NULL) { |
635 // We try reading data as PKCS12 only if reading as PEM was unsuccessful and | 608 // We try reading data as PKCS12 only if reading as PEM was unsuccessful and |
636 // if there is no indication that the data is malformed PEM. We assume the | 609 // if there is no indication that the data is malformed PEM. We assume the |
637 // data is malformed PEM if it contains the start line, i.e. a line | 610 // data is malformed PEM if it contains the start line, i.e. a line |
638 // with ----- BEGIN. | 611 // with ----- BEGIN. |
639 if (NoPEMStartLine()) { | 612 if (NoPEMStartLine()) { |
640 // Reset the bio, and clear the error from trying to read as PEM. | 613 // Reset the bio, and clear the error from trying to read as PEM. |
641 ERR_clear_error(); | 614 ERR_clear_error(); |
642 BIO_reset(bio); | 615 BIO_reset(bio); |
643 | 616 |
644 // Try to decode as PKCS12. | 617 // Try to decode as PKCS12. |
645 key = GetPrivateKeyPKCS12(bio, password); | 618 key = GetPrivateKeyPKCS12(bio, password); |
646 } | 619 } |
647 } | 620 } |
648 return key; | 621 return key; |
649 } | 622 } |
650 | 623 |
651 | 624 |
652 static const char* GetPasswordArgument(Dart_NativeArguments args, | 625 static const char* GetPasswordArgument(Dart_NativeArguments args, |
653 intptr_t index) { | 626 intptr_t index) { |
654 Dart_Handle password_object = | 627 Dart_Handle password_object = |
655 ThrowIfError(Dart_GetNativeArgument(args, index)); | 628 ThrowIfError(Dart_GetNativeArgument(args, index)); |
656 const char* password = NULL; | 629 const char* password = NULL; |
657 if (Dart_IsString(password_object)) { | 630 if (Dart_IsString(password_object)) { |
658 ThrowIfError(Dart_StringToCString(password_object, &password)); | 631 ThrowIfError(Dart_StringToCString(password_object, &password)); |
659 if (strlen(password) > PEM_BUFSIZE - 1) { | 632 if (strlen(password) > PEM_BUFSIZE - 1) { |
660 Dart_ThrowException(DartUtils::NewDartArgumentError( | 633 Dart_ThrowException(DartUtils::NewDartArgumentError( |
661 "Password length is greater than 1023 (PEM_BUFSIZE)")); | 634 "Password length is greater than 1023 (PEM_BUFSIZE)")); |
662 } | 635 } |
663 } else if (Dart_IsNull(password_object)) { | 636 } else if (Dart_IsNull(password_object)) { |
664 password = ""; | 637 password = ""; |
665 } else { | 638 } else { |
666 Dart_ThrowException(DartUtils::NewDartArgumentError( | 639 Dart_ThrowException( |
667 "Password is not a String or null")); | 640 DartUtils::NewDartArgumentError("Password is not a String or null")); |
668 } | 641 } |
669 return password; | 642 return password; |
670 } | 643 } |
671 | 644 |
672 | 645 |
673 void FUNCTION_NAME(SecurityContext_UsePrivateKeyBytes)( | 646 void FUNCTION_NAME(SecurityContext_UsePrivateKeyBytes)( |
674 Dart_NativeArguments args) { | 647 Dart_NativeArguments args) { |
675 SSLContext* context = GetSecurityContext(args); | 648 SSLContext* context = GetSecurityContext(args); |
676 const char* password = GetPasswordArgument(args, 2); | 649 const char* password = GetPasswordArgument(args, 2); |
677 | 650 |
678 int status; | 651 int status; |
679 { | 652 { |
680 ScopedMemBIO bio(ThrowIfError(Dart_GetNativeArgument(args, 1))); | 653 ScopedMemBIO bio(ThrowIfError(Dart_GetNativeArgument(args, 1))); |
681 EVP_PKEY *key = GetPrivateKey(bio.bio(), password); | 654 EVP_PKEY* key = GetPrivateKey(bio.bio(), password); |
682 status = SSL_CTX_use_PrivateKey(context->context(), key); | 655 status = SSL_CTX_use_PrivateKey(context->context(), key); |
683 // SSL_CTX_use_PrivateKey increments the reference count of key on success, | 656 // SSL_CTX_use_PrivateKey increments the reference count of key on success, |
684 // so we have to call EVP_PKEY_free on both success and failure. | 657 // so we have to call EVP_PKEY_free on both success and failure. |
685 EVP_PKEY_free(key); | 658 EVP_PKEY_free(key); |
686 } | 659 } |
687 | 660 |
688 // TODO(24184): Handle different expected errors here - file missing, | 661 // TODO(24184): Handle different expected errors here - file missing, |
689 // incorrect password, file not a PEM, and throw exceptions. | 662 // incorrect password, file not a PEM, and throw exceptions. |
690 // CheckStatus should also throw an exception in uncaught cases. | 663 // CheckStatus should also throw an exception in uncaught cases. |
691 CheckStatus(status, "TlsException", "Failure in usePrivateKeyBytes"); | 664 CheckStatus(status, "TlsException", "Failure in usePrivateKeyBytes"); |
692 } | 665 } |
693 | 666 |
694 | 667 |
695 static int SetTrustedCertificatesBytesPKCS12(SSL_CTX* context, | 668 static int SetTrustedCertificatesBytesPKCS12(SSL_CTX* context, |
696 BIO* bio, | 669 BIO* bio, |
697 const char* password) { | 670 const char* password) { |
698 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); | 671 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); |
699 if (p12.get() == NULL) { | 672 if (p12.get() == NULL) { |
700 return 0; | 673 return 0; |
701 } | 674 } |
702 | 675 |
703 EVP_PKEY* key = NULL; | 676 EVP_PKEY* key = NULL; |
704 X509 *cert = NULL; | 677 X509* cert = NULL; |
705 STACK_OF(X509) *ca_certs = NULL; | 678 STACK_OF(X509)* ca_certs = NULL; |
706 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); | 679 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); |
707 if (status == 0) { | 680 if (status == 0) { |
708 return status; | 681 return status; |
709 } | 682 } |
710 | 683 |
711 ScopedX509Stack cert_stack(ca_certs); | 684 ScopedX509Stack cert_stack(ca_certs); |
712 X509_STORE* store = SSL_CTX_get_cert_store(context); | 685 X509_STORE* store = SSL_CTX_get_cert_store(context); |
713 status = X509_STORE_add_cert(store, cert); | 686 status = X509_STORE_add_cert(store, cert); |
714 // X509_STORE_add_cert increments the reference count of cert on success. | 687 // X509_STORE_add_cert increments the reference count of cert on success. |
715 X509_free(cert); | 688 X509_free(cert); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
772 } | 745 } |
773 | 746 |
774 | 747 |
775 void FUNCTION_NAME(SecurityContext_SetTrustedCertificatesBytes)( | 748 void FUNCTION_NAME(SecurityContext_SetTrustedCertificatesBytes)( |
776 Dart_NativeArguments args) { | 749 Dart_NativeArguments args) { |
777 SSLContext* context = GetSecurityContext(args); | 750 SSLContext* context = GetSecurityContext(args); |
778 const char* password = GetPasswordArgument(args, 2); | 751 const char* password = GetPasswordArgument(args, 2); |
779 int status; | 752 int status; |
780 { | 753 { |
781 ScopedMemBIO bio(ThrowIfError(Dart_GetNativeArgument(args, 1))); | 754 ScopedMemBIO bio(ThrowIfError(Dart_GetNativeArgument(args, 1))); |
782 status = SetTrustedCertificatesBytes( | 755 status = |
783 context->context(), bio.bio(), password); | 756 SetTrustedCertificatesBytes(context->context(), bio.bio(), password); |
784 } | 757 } |
785 CheckStatus(status, | 758 CheckStatus(status, "TlsException", "Failure in setTrustedCertificatesBytes"); |
786 "TlsException", | |
787 "Failure in setTrustedCertificatesBytes"); | |
788 } | 759 } |
789 | 760 |
790 | 761 |
791 void FUNCTION_NAME(SecurityContext_AlpnSupported)(Dart_NativeArguments args) { | 762 void FUNCTION_NAME(SecurityContext_AlpnSupported)(Dart_NativeArguments args) { |
792 Dart_SetReturnValue(args, Dart_NewBoolean(true)); | 763 Dart_SetReturnValue(args, Dart_NewBoolean(true)); |
793 } | 764 } |
794 | 765 |
795 | 766 |
796 static void AddCompiledInCerts(SSLContext* context) { | 767 static void AddCompiledInCerts(SSLContext* context) { |
797 if (root_certificates_pem == NULL) { | 768 if (root_certificates_pem == NULL) { |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
907 | 878 |
908 static int UseChainBytesPKCS12(SSL_CTX* context, | 879 static int UseChainBytesPKCS12(SSL_CTX* context, |
909 BIO* bio, | 880 BIO* bio, |
910 const char* password) { | 881 const char* password) { |
911 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); | 882 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); |
912 if (p12.get() == NULL) { | 883 if (p12.get() == NULL) { |
913 return 0; | 884 return 0; |
914 } | 885 } |
915 | 886 |
916 EVP_PKEY* key = NULL; | 887 EVP_PKEY* key = NULL; |
917 X509 *cert = NULL; | 888 X509* cert = NULL; |
918 STACK_OF(X509) *ca_certs = NULL; | 889 STACK_OF(X509)* ca_certs = NULL; |
919 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); | 890 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); |
920 if (status == 0) { | 891 if (status == 0) { |
921 return status; | 892 return status; |
922 } | 893 } |
923 | 894 |
924 ScopedX509 x509(cert); | 895 ScopedX509 x509(cert); |
925 ScopedX509Stack certs(ca_certs); | 896 ScopedX509Stack certs(ca_certs); |
926 status = SSL_CTX_use_certificate(context, x509.get()); | 897 status = SSL_CTX_use_certificate(context, x509.get()); |
927 if (ERR_peek_error() != 0) { | 898 if (ERR_peek_error() != 0) { |
928 // Key/certificate mismatch doesn't imply status is 0. | 899 // Key/certificate mismatch doesn't imply status is 0. |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1003 | 974 |
1004 void FUNCTION_NAME(SecurityContext_UseCertificateChainBytes)( | 975 void FUNCTION_NAME(SecurityContext_UseCertificateChainBytes)( |
1005 Dart_NativeArguments args) { | 976 Dart_NativeArguments args) { |
1006 SSLContext* context = GetSecurityContext(args); | 977 SSLContext* context = GetSecurityContext(args); |
1007 const char* password = GetPasswordArgument(args, 2); | 978 const char* password = GetPasswordArgument(args, 2); |
1008 int status; | 979 int status; |
1009 { | 980 { |
1010 ScopedMemBIO bio(ThrowIfError(Dart_GetNativeArgument(args, 1))); | 981 ScopedMemBIO bio(ThrowIfError(Dart_GetNativeArgument(args, 1))); |
1011 status = UseChainBytes(context->context(), bio.bio(), password); | 982 status = UseChainBytes(context->context(), bio.bio(), password); |
1012 } | 983 } |
1013 CheckStatus(status, | 984 CheckStatus(status, "TlsException", "Failure in useCertificateChainBytes"); |
1014 "TlsException", | |
1015 "Failure in useCertificateChainBytes"); | |
1016 } | 985 } |
1017 | 986 |
1018 | 987 |
1019 static int SetClientAuthoritiesPKCS12(SSL_CTX* context, | 988 static int SetClientAuthoritiesPKCS12(SSL_CTX* context, |
1020 BIO* bio, | 989 BIO* bio, |
1021 const char* password) { | 990 const char* password) { |
1022 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); | 991 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); |
1023 if (p12.get() == NULL) { | 992 if (p12.get() == NULL) { |
1024 return 0; | 993 return 0; |
1025 } | 994 } |
1026 | 995 |
1027 EVP_PKEY* key = NULL; | 996 EVP_PKEY* key = NULL; |
1028 X509 *cert = NULL; | 997 X509* cert = NULL; |
1029 STACK_OF(X509) *ca_certs = NULL; | 998 STACK_OF(X509)* ca_certs = NULL; |
1030 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); | 999 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); |
1031 if (status == 0) { | 1000 if (status == 0) { |
1032 return status; | 1001 return status; |
1033 } | 1002 } |
1034 | 1003 |
1035 ScopedX509Stack cert_stack(ca_certs); | 1004 ScopedX509Stack cert_stack(ca_certs); |
1036 status = SSL_CTX_add_client_CA(context, cert); | 1005 status = SSL_CTX_add_client_CA(context, cert); |
1037 // SSL_CTX_add_client_CA increments the reference count of cert on success. | 1006 // SSL_CTX_add_client_CA increments the reference count of cert on success. |
1038 X509_free(cert); | 1007 X509_free(cert); |
1039 if (status == 0) { | 1008 if (status == 0) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1090 Dart_NativeArguments args) { | 1059 Dart_NativeArguments args) { |
1091 SSLContext* context = GetSecurityContext(args); | 1060 SSLContext* context = GetSecurityContext(args); |
1092 const char* password = GetPasswordArgument(args, 2); | 1061 const char* password = GetPasswordArgument(args, 2); |
1093 | 1062 |
1094 int status; | 1063 int status; |
1095 { | 1064 { |
1096 ScopedMemBIO bio(ThrowIfError(Dart_GetNativeArgument(args, 1))); | 1065 ScopedMemBIO bio(ThrowIfError(Dart_GetNativeArgument(args, 1))); |
1097 status = SetClientAuthorities(context->context(), bio.bio(), password); | 1066 status = SetClientAuthorities(context->context(), bio.bio(), password); |
1098 } | 1067 } |
1099 | 1068 |
1100 CheckStatus(status, | 1069 CheckStatus(status, "TlsException", "Failure in setClientAuthoritiesBytes"); |
1101 "TlsException", | |
1102 "Failure in setClientAuthoritiesBytes"); | |
1103 } | 1070 } |
1104 | 1071 |
1105 | 1072 |
1106 void FUNCTION_NAME(SecurityContext_SetAlpnProtocols)( | 1073 void FUNCTION_NAME(SecurityContext_SetAlpnProtocols)( |
1107 Dart_NativeArguments args) { | 1074 Dart_NativeArguments args) { |
1108 SSLContext* context = GetSecurityContext(args); | 1075 SSLContext* context = GetSecurityContext(args); |
1109 Dart_Handle protocols_handle = | 1076 Dart_Handle protocols_handle = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
1110 ThrowIfError(Dart_GetNativeArgument(args, 1)); | 1077 Dart_Handle is_server_handle = ThrowIfError(Dart_GetNativeArgument(args, 2)); |
1111 Dart_Handle is_server_handle = | |
1112 ThrowIfError(Dart_GetNativeArgument(args, 2)); | |
1113 if (Dart_IsBoolean(is_server_handle)) { | 1078 if (Dart_IsBoolean(is_server_handle)) { |
1114 bool is_server = DartUtils::GetBooleanValue(is_server_handle); | 1079 bool is_server = DartUtils::GetBooleanValue(is_server_handle); |
1115 SetAlpnProtocolList(protocols_handle, NULL, context, is_server); | 1080 SetAlpnProtocolList(protocols_handle, NULL, context, is_server); |
1116 } else { | 1081 } else { |
1117 Dart_ThrowException(DartUtils::NewDartArgumentError( | 1082 Dart_ThrowException(DartUtils::NewDartArgumentError( |
1118 "Non-boolean is_server argument passed to SetAlpnProtocols")); | 1083 "Non-boolean is_server argument passed to SetAlpnProtocols")); |
1119 } | 1084 } |
1120 } | 1085 } |
1121 | 1086 |
1122 | 1087 |
1123 void FUNCTION_NAME(X509_Subject)( | 1088 void FUNCTION_NAME(X509_Subject)(Dart_NativeArguments args) { |
1124 Dart_NativeArguments args) { | |
1125 X509* certificate = GetX509Certificate(args); | 1089 X509* certificate = GetX509Certificate(args); |
1126 X509_NAME* subject = X509_get_subject_name(certificate); | 1090 X509_NAME* subject = X509_get_subject_name(certificate); |
1127 char* subject_string = X509_NAME_oneline(subject, NULL, 0); | 1091 char* subject_string = X509_NAME_oneline(subject, NULL, 0); |
1128 Dart_SetReturnValue(args, Dart_NewStringFromCString(subject_string)); | 1092 Dart_SetReturnValue(args, Dart_NewStringFromCString(subject_string)); |
1129 OPENSSL_free(subject_string); | 1093 OPENSSL_free(subject_string); |
1130 } | 1094 } |
1131 | 1095 |
1132 | 1096 |
1133 void FUNCTION_NAME(X509_Issuer)( | 1097 void FUNCTION_NAME(X509_Issuer)(Dart_NativeArguments args) { |
1134 Dart_NativeArguments args) { | |
1135 X509* certificate = GetX509Certificate(args); | 1098 X509* certificate = GetX509Certificate(args); |
1136 X509_NAME* issuer = X509_get_issuer_name(certificate); | 1099 X509_NAME* issuer = X509_get_issuer_name(certificate); |
1137 char* issuer_string = X509_NAME_oneline(issuer, NULL, 0); | 1100 char* issuer_string = X509_NAME_oneline(issuer, NULL, 0); |
1138 Dart_SetReturnValue(args, Dart_NewStringFromCString(issuer_string)); | 1101 Dart_SetReturnValue(args, Dart_NewStringFromCString(issuer_string)); |
1139 OPENSSL_free(issuer_string); | 1102 OPENSSL_free(issuer_string); |
1140 } | 1103 } |
1141 | 1104 |
1142 static Dart_Handle ASN1TimeToMilliseconds(ASN1_TIME* aTime) { | 1105 static Dart_Handle ASN1TimeToMilliseconds(ASN1_TIME* aTime) { |
1143 ASN1_UTCTIME* epoch_start = M_ASN1_UTCTIME_new(); | 1106 ASN1_UTCTIME* epoch_start = M_ASN1_UTCTIME_new(); |
1144 ASN1_UTCTIME_set_string(epoch_start, "700101000000Z"); | 1107 ASN1_UTCTIME_set_string(epoch_start, "700101000000Z"); |
1145 int days; | 1108 int days; |
1146 int seconds; | 1109 int seconds; |
1147 int result = ASN1_TIME_diff(&days, &seconds, epoch_start, aTime); | 1110 int result = ASN1_TIME_diff(&days, &seconds, epoch_start, aTime); |
1148 M_ASN1_UTCTIME_free(epoch_start); | 1111 M_ASN1_UTCTIME_free(epoch_start); |
1149 if (result != 1) { | 1112 if (result != 1) { |
1150 // TODO(whesse): Propagate an error to Dart. | 1113 // TODO(whesse): Propagate an error to Dart. |
1151 Log::PrintErr("ASN1Time error %d\n", result); | 1114 Log::PrintErr("ASN1Time error %d\n", result); |
1152 } | 1115 } |
1153 return Dart_NewInteger((86400LL * days + seconds) * 1000LL); | 1116 return Dart_NewInteger((86400LL * days + seconds) * 1000LL); |
1154 } | 1117 } |
1155 | 1118 |
1156 void FUNCTION_NAME(X509_StartValidity)( | 1119 void FUNCTION_NAME(X509_StartValidity)(Dart_NativeArguments args) { |
1157 Dart_NativeArguments args) { | |
1158 X509* certificate = GetX509Certificate(args); | 1120 X509* certificate = GetX509Certificate(args); |
1159 ASN1_TIME* not_before = X509_get_notBefore(certificate); | 1121 ASN1_TIME* not_before = X509_get_notBefore(certificate); |
1160 Dart_SetReturnValue(args, ASN1TimeToMilliseconds(not_before)); | 1122 Dart_SetReturnValue(args, ASN1TimeToMilliseconds(not_before)); |
1161 } | 1123 } |
1162 | 1124 |
1163 | 1125 |
1164 void FUNCTION_NAME(X509_EndValidity)( | 1126 void FUNCTION_NAME(X509_EndValidity)(Dart_NativeArguments args) { |
1165 Dart_NativeArguments args) { | |
1166 X509* certificate = GetX509Certificate(args); | 1127 X509* certificate = GetX509Certificate(args); |
1167 ASN1_TIME* not_after = X509_get_notAfter(certificate); | 1128 ASN1_TIME* not_after = X509_get_notAfter(certificate); |
1168 Dart_SetReturnValue(args, ASN1TimeToMilliseconds(not_after)); | 1129 Dart_SetReturnValue(args, ASN1TimeToMilliseconds(not_after)); |
1169 } | 1130 } |
1170 | 1131 |
1171 | 1132 |
1172 /** | 1133 /** |
1173 * Pushes data through the SSL filter, reading and writing from circular | 1134 * Pushes data through the SSL filter, reading and writing from circular |
1174 * buffers shared with Dart. | 1135 * buffers shared with Dart. |
1175 * | 1136 * |
(...skipping 21 matching lines...) Expand all Loading... |
1197 | 1158 |
1198 bool in_handshake = CObjectBool(request[1]).Value(); | 1159 bool in_handshake = CObjectBool(request[1]).Value(); |
1199 int starts[SSLFilter::kNumBuffers]; | 1160 int starts[SSLFilter::kNumBuffers]; |
1200 int ends[SSLFilter::kNumBuffers]; | 1161 int ends[SSLFilter::kNumBuffers]; |
1201 for (int i = 0; i < SSLFilter::kNumBuffers; ++i) { | 1162 for (int i = 0; i < SSLFilter::kNumBuffers; ++i) { |
1202 starts[i] = CObjectInt32(request[2 * i + 2]).Value(); | 1163 starts[i] = CObjectInt32(request[2 * i + 2]).Value(); |
1203 ends[i] = CObjectInt32(request[2 * i + 3]).Value(); | 1164 ends[i] = CObjectInt32(request[2 * i + 3]).Value(); |
1204 } | 1165 } |
1205 | 1166 |
1206 if (filter->ProcessAllBuffers(starts, ends, in_handshake)) { | 1167 if (filter->ProcessAllBuffers(starts, ends, in_handshake)) { |
1207 CObjectArray* result = new CObjectArray( | 1168 CObjectArray* result = |
1208 CObject::NewArray(SSLFilter::kNumBuffers * 2)); | 1169 new CObjectArray(CObject::NewArray(SSLFilter::kNumBuffers * 2)); |
1209 for (int i = 0; i < SSLFilter::kNumBuffers; ++i) { | 1170 for (int i = 0; i < SSLFilter::kNumBuffers; ++i) { |
1210 result->SetAt(2 * i, new CObjectInt32(CObject::NewInt32(starts[i]))); | 1171 result->SetAt(2 * i, new CObjectInt32(CObject::NewInt32(starts[i]))); |
1211 result->SetAt(2 * i + 1, new CObjectInt32(CObject::NewInt32(ends[i]))); | 1172 result->SetAt(2 * i + 1, new CObjectInt32(CObject::NewInt32(ends[i]))); |
1212 } | 1173 } |
1213 return result; | 1174 return result; |
1214 } else { | 1175 } else { |
1215 int32_t error_code = static_cast<int32_t>(ERR_peek_error()); | 1176 int32_t error_code = static_cast<int32_t>(ERR_peek_error()); |
1216 char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE]; | 1177 char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE]; |
1217 FetchErrorString(error_string, SSL_ERROR_MESSAGE_BUFFER_SIZE); | 1178 FetchErrorString(error_string, SSL_ERROR_MESSAGE_BUFFER_SIZE); |
1218 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); | 1179 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); |
(...skipping 19 matching lines...) Expand all Loading... |
1238 case kReadPlaintext: | 1199 case kReadPlaintext: |
1239 case kWriteEncrypted: | 1200 case kWriteEncrypted: |
1240 // Write data to the circular buffer's free space. If the buffer | 1201 // Write data to the circular buffer's free space. If the buffer |
1241 // is full, neither if statement is executed and nothing happens. | 1202 // is full, neither if statement is executed and nothing happens. |
1242 if (start <= end) { | 1203 if (start <= end) { |
1243 // If the free space may be split into two segments, | 1204 // If the free space may be split into two segments, |
1244 // then the first is [end, size), unless start == 0. | 1205 // then the first is [end, size), unless start == 0. |
1245 // Then, since the last free byte is at position start - 2, | 1206 // Then, since the last free byte is at position start - 2, |
1246 // the interval is [end, size - 1). | 1207 // the interval is [end, size - 1). |
1247 int buffer_end = (start == 0) ? size - 1 : size; | 1208 int buffer_end = (start == 0) ? size - 1 : size; |
1248 int bytes = (i == kReadPlaintext) ? | 1209 int bytes = (i == kReadPlaintext) |
1249 ProcessReadPlaintextBuffer(end, buffer_end) : | 1210 ? ProcessReadPlaintextBuffer(end, buffer_end) |
1250 ProcessWriteEncryptedBuffer(end, buffer_end); | 1211 : ProcessWriteEncryptedBuffer(end, buffer_end); |
1251 if (bytes < 0) return false; | 1212 if (bytes < 0) return false; |
1252 end += bytes; | 1213 end += bytes; |
1253 ASSERT(end <= size); | 1214 ASSERT(end <= size); |
1254 if (end == size) end = 0; | 1215 if (end == size) end = 0; |
1255 } | 1216 } |
1256 if (start > end + 1) { | 1217 if (start > end + 1) { |
1257 int bytes = (i == kReadPlaintext) ? | 1218 int bytes = (i == kReadPlaintext) |
1258 ProcessReadPlaintextBuffer(end, start - 1) : | 1219 ? ProcessReadPlaintextBuffer(end, start - 1) |
1259 ProcessWriteEncryptedBuffer(end, start - 1); | 1220 : ProcessWriteEncryptedBuffer(end, start - 1); |
1260 if (bytes < 0) return false; | 1221 if (bytes < 0) return false; |
1261 end += bytes; | 1222 end += bytes; |
1262 ASSERT(end < start); | 1223 ASSERT(end < start); |
1263 } | 1224 } |
1264 ends[i] = end; | 1225 ends[i] = end; |
1265 break; | 1226 break; |
1266 case kReadEncrypted: | 1227 case kReadEncrypted: |
1267 case kWritePlaintext: | 1228 case kWritePlaintext: |
1268 // Read/Write data from circular buffer. If the buffer is empty, | 1229 // Read/Write data from circular buffer. If the buffer is empty, |
1269 // neither if statement's condition is true. | 1230 // neither if statement's condition is true. |
1270 if (end < start) { | 1231 if (end < start) { |
1271 // Data may be split into two segments. In this case, | 1232 // Data may be split into two segments. In this case, |
1272 // the first is [start, size). | 1233 // the first is [start, size). |
1273 int bytes = (i == kReadEncrypted) ? | 1234 int bytes = (i == kReadEncrypted) |
1274 ProcessReadEncryptedBuffer(start, size) : | 1235 ? ProcessReadEncryptedBuffer(start, size) |
1275 ProcessWritePlaintextBuffer(start, size); | 1236 : ProcessWritePlaintextBuffer(start, size); |
1276 if (bytes < 0) return false; | 1237 if (bytes < 0) return false; |
1277 start += bytes; | 1238 start += bytes; |
1278 ASSERT(start <= size); | 1239 ASSERT(start <= size); |
1279 if (start == size) start = 0; | 1240 if (start == size) start = 0; |
1280 } | 1241 } |
1281 if (start < end) { | 1242 if (start < end) { |
1282 int bytes = (i == kReadEncrypted) ? | 1243 int bytes = (i == kReadEncrypted) |
1283 ProcessReadEncryptedBuffer(start, end) : | 1244 ? ProcessReadEncryptedBuffer(start, end) |
1284 ProcessWritePlaintextBuffer(start, end); | 1245 : ProcessWritePlaintextBuffer(start, end); |
1285 if (bytes < 0) return false; | 1246 if (bytes < 0) return false; |
1286 start += bytes; | 1247 start += bytes; |
1287 ASSERT(start <= end); | 1248 ASSERT(start <= end); |
1288 } | 1249 } |
1289 starts[i] = start; | 1250 starts[i] = start; |
1290 break; | 1251 break; |
1291 default: | 1252 default: |
1292 UNREACHABLE(); | 1253 UNREACHABLE(); |
1293 } | 1254 } |
1294 } | 1255 } |
(...skipping 23 matching lines...) Expand all Loading... |
1318 Dart_Handle SSLFilter::InitializeBuffers(Dart_Handle dart_this) { | 1279 Dart_Handle SSLFilter::InitializeBuffers(Dart_Handle dart_this) { |
1319 // Create SSLFilter buffers as ExternalUint8Array objects. | 1280 // Create SSLFilter buffers as ExternalUint8Array objects. |
1320 Dart_Handle buffers_string = DartUtils::NewString("buffers"); | 1281 Dart_Handle buffers_string = DartUtils::NewString("buffers"); |
1321 RETURN_IF_ERROR(buffers_string); | 1282 RETURN_IF_ERROR(buffers_string); |
1322 Dart_Handle dart_buffers_object = Dart_GetField(dart_this, buffers_string); | 1283 Dart_Handle dart_buffers_object = Dart_GetField(dart_this, buffers_string); |
1323 RETURN_IF_ERROR(dart_buffers_object); | 1284 RETURN_IF_ERROR(dart_buffers_object); |
1324 Dart_Handle secure_filter_impl_type = Dart_InstanceGetType(dart_this); | 1285 Dart_Handle secure_filter_impl_type = Dart_InstanceGetType(dart_this); |
1325 RETURN_IF_ERROR(secure_filter_impl_type); | 1286 RETURN_IF_ERROR(secure_filter_impl_type); |
1326 Dart_Handle size_string = DartUtils::NewString("SIZE"); | 1287 Dart_Handle size_string = DartUtils::NewString("SIZE"); |
1327 RETURN_IF_ERROR(size_string); | 1288 RETURN_IF_ERROR(size_string); |
1328 Dart_Handle dart_buffer_size = Dart_GetField( | 1289 Dart_Handle dart_buffer_size = |
1329 secure_filter_impl_type, size_string); | 1290 Dart_GetField(secure_filter_impl_type, size_string); |
1330 RETURN_IF_ERROR(dart_buffer_size); | 1291 RETURN_IF_ERROR(dart_buffer_size); |
1331 | 1292 |
1332 int64_t buffer_size = 0; | 1293 int64_t buffer_size = 0; |
1333 Dart_Handle err = Dart_IntegerToInt64(dart_buffer_size, &buffer_size); | 1294 Dart_Handle err = Dart_IntegerToInt64(dart_buffer_size, &buffer_size); |
1334 RETURN_IF_ERROR(err); | 1295 RETURN_IF_ERROR(err); |
1335 | 1296 |
1336 Dart_Handle encrypted_size_string = DartUtils::NewString("ENCRYPTED_SIZE"); | 1297 Dart_Handle encrypted_size_string = DartUtils::NewString("ENCRYPTED_SIZE"); |
1337 RETURN_IF_ERROR(encrypted_size_string); | 1298 RETURN_IF_ERROR(encrypted_size_string); |
1338 | 1299 |
1339 Dart_Handle dart_encrypted_buffer_size = Dart_GetField( | 1300 Dart_Handle dart_encrypted_buffer_size = |
1340 secure_filter_impl_type, encrypted_size_string); | 1301 Dart_GetField(secure_filter_impl_type, encrypted_size_string); |
1341 RETURN_IF_ERROR(dart_encrypted_buffer_size); | 1302 RETURN_IF_ERROR(dart_encrypted_buffer_size); |
1342 | 1303 |
1343 int64_t encrypted_buffer_size = 0; | 1304 int64_t encrypted_buffer_size = 0; |
1344 err = Dart_IntegerToInt64(dart_encrypted_buffer_size, &encrypted_buffer_size); | 1305 err = Dart_IntegerToInt64(dart_encrypted_buffer_size, &encrypted_buffer_size); |
1345 RETURN_IF_ERROR(err); | 1306 RETURN_IF_ERROR(err); |
1346 | 1307 |
1347 if (buffer_size <= 0 || buffer_size > 1 * MB) { | 1308 if (buffer_size <= 0 || buffer_size > 1 * MB) { |
1348 FATAL("Invalid buffer size in _ExternalBuffer"); | 1309 FATAL("Invalid buffer size in _ExternalBuffer"); |
1349 } | 1310 } |
1350 if (encrypted_buffer_size <= 0 || encrypted_buffer_size > 1 * MB) { | 1311 if (encrypted_buffer_size <= 0 || encrypted_buffer_size > 1 * MB) { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1422 | 1383 |
1423 | 1384 |
1424 Dart_Handle SSLFilter::PeerCertificate() { | 1385 Dart_Handle SSLFilter::PeerCertificate() { |
1425 // SSL_get_peer_certificate incs the refcount of certificate. X509_free is | 1386 // SSL_get_peer_certificate incs the refcount of certificate. X509_free is |
1426 // called by the finalizer set up by WrappedX509Certificate. | 1387 // called by the finalizer set up by WrappedX509Certificate. |
1427 X509* certificate = SSL_get_peer_certificate(ssl_); | 1388 X509* certificate = SSL_get_peer_certificate(ssl_); |
1428 return WrappedX509Certificate(certificate); | 1389 return WrappedX509Certificate(certificate); |
1429 } | 1390 } |
1430 | 1391 |
1431 | 1392 |
1432 int AlpnCallback(SSL *ssl, | 1393 int AlpnCallback(SSL* ssl, |
1433 const uint8_t **out, | 1394 const uint8_t** out, |
1434 uint8_t *outlen, | 1395 uint8_t* outlen, |
1435 const uint8_t *in, | 1396 const uint8_t* in, |
1436 unsigned int inlen, | 1397 unsigned int inlen, |
1437 void *arg) { | 1398 void* arg) { |
1438 // 'in' and 'arg' are sequences of (length, data) strings with 1-byte lengths. | 1399 // 'in' and 'arg' are sequences of (length, data) strings with 1-byte lengths. |
1439 // 'arg' is 0-terminated. Finds the first string in 'arg' that is in 'in'. | 1400 // 'arg' is 0-terminated. Finds the first string in 'arg' that is in 'in'. |
1440 uint8_t* server_list = static_cast<uint8_t*>(arg); | 1401 uint8_t* server_list = static_cast<uint8_t*>(arg); |
1441 while (*server_list != 0) { | 1402 while (*server_list != 0) { |
1442 uint8_t protocol_length = *server_list++; | 1403 uint8_t protocol_length = *server_list++; |
1443 const uint8_t* client_list = in; | 1404 const uint8_t* client_list = in; |
1444 while (client_list < in + inlen) { | 1405 while (client_list < in + inlen) { |
1445 uint8_t client_protocol_length = *client_list++; | 1406 uint8_t client_protocol_length = *client_list++; |
1446 if (client_protocol_length == protocol_length) { | 1407 if (client_protocol_length == protocol_length) { |
1447 if (0 == memcmp(server_list, client_list, protocol_length)) { | 1408 if (0 == memcmp(server_list, client_list, protocol_length)) { |
(...skipping 18 matching lines...) Expand all Loading... |
1466 bool is_server) { | 1427 bool is_server) { |
1467 // Enable ALPN (application layer protocol negotiation) if the caller provides | 1428 // Enable ALPN (application layer protocol negotiation) if the caller provides |
1468 // a valid list of supported protocols. | 1429 // a valid list of supported protocols. |
1469 Dart_TypedData_Type protocols_type; | 1430 Dart_TypedData_Type protocols_type; |
1470 uint8_t* protocol_string = NULL; | 1431 uint8_t* protocol_string = NULL; |
1471 uint8_t* protocol_string_copy = NULL; | 1432 uint8_t* protocol_string_copy = NULL; |
1472 intptr_t protocol_string_len = 0; | 1433 intptr_t protocol_string_len = 0; |
1473 int status; | 1434 int status; |
1474 | 1435 |
1475 Dart_Handle result = Dart_TypedDataAcquireData( | 1436 Dart_Handle result = Dart_TypedDataAcquireData( |
1476 protocols_handle, | 1437 protocols_handle, &protocols_type, |
1477 &protocols_type, | 1438 reinterpret_cast<void**>(&protocol_string), &protocol_string_len); |
1478 reinterpret_cast<void**>(&protocol_string), | |
1479 &protocol_string_len); | |
1480 if (Dart_IsError(result)) { | 1439 if (Dart_IsError(result)) { |
1481 Dart_PropagateError(result); | 1440 Dart_PropagateError(result); |
1482 } | 1441 } |
1483 | 1442 |
1484 if (protocols_type != Dart_TypedData_kUint8) { | 1443 if (protocols_type != Dart_TypedData_kUint8) { |
1485 Dart_TypedDataReleaseData(protocols_handle); | 1444 Dart_TypedDataReleaseData(protocols_handle); |
1486 Dart_PropagateError(Dart_NewApiError( | 1445 Dart_PropagateError(Dart_NewApiError( |
1487 "Unexpected type for protocols (expected valid Uint8List).")); | 1446 "Unexpected type for protocols (expected valid Uint8List).")); |
1488 } | 1447 } |
1489 | 1448 |
1490 if (protocol_string_len > 0) { | 1449 if (protocol_string_len > 0) { |
1491 if (is_server) { | 1450 if (is_server) { |
1492 // ALPN on server connections must be set on an SSL_CTX object, | 1451 // ALPN on server connections must be set on an SSL_CTX object, |
1493 // not on the SSL object of the individual connection. | 1452 // not on the SSL object of the individual connection. |
1494 ASSERT(context != NULL); | 1453 ASSERT(context != NULL); |
1495 ASSERT(ssl == NULL); | 1454 ASSERT(ssl == NULL); |
1496 // Because it must be passed as a single void*, terminate | 1455 // Because it must be passed as a single void*, terminate |
1497 // the list of (length, data) strings with a length 0 string. | 1456 // the list of (length, data) strings with a length 0 string. |
1498 protocol_string_copy = | 1457 protocol_string_copy = |
1499 static_cast<uint8_t*>(malloc(protocol_string_len + 1)); | 1458 static_cast<uint8_t*>(malloc(protocol_string_len + 1)); |
1500 memmove(protocol_string_copy, protocol_string, protocol_string_len); | 1459 memmove(protocol_string_copy, protocol_string, protocol_string_len); |
1501 protocol_string_copy[protocol_string_len] = '\0'; | 1460 protocol_string_copy[protocol_string_len] = '\0'; |
1502 SSL_CTX_set_alpn_select_cb( | 1461 SSL_CTX_set_alpn_select_cb(context->context(), AlpnCallback, |
1503 context->context(), AlpnCallback, protocol_string_copy); | 1462 protocol_string_copy); |
1504 context->set_alpn_protocol_string(protocol_string_copy); | 1463 context->set_alpn_protocol_string(protocol_string_copy); |
1505 } else { | 1464 } else { |
1506 // The function makes a local copy of protocol_string, which it owns. | 1465 // The function makes a local copy of protocol_string, which it owns. |
1507 if (ssl != NULL) { | 1466 if (ssl != NULL) { |
1508 ASSERT(context == NULL); | 1467 ASSERT(context == NULL); |
1509 status = SSL_set_alpn_protos(ssl, protocol_string, protocol_string_len); | 1468 status = SSL_set_alpn_protos(ssl, protocol_string, protocol_string_len); |
1510 } else { | 1469 } else { |
1511 ASSERT(context != NULL); | 1470 ASSERT(context != NULL); |
1512 ASSERT(ssl == NULL); | 1471 ASSERT(ssl == NULL); |
1513 status = SSL_CTX_set_alpn_protos( | 1472 status = SSL_CTX_set_alpn_protos(context->context(), protocol_string, |
1514 context->context(), protocol_string, protocol_string_len); | 1473 protocol_string_len); |
1515 } | 1474 } |
1516 ASSERT(status == 0); // The function returns a non-standard status. | 1475 ASSERT(status == 0); // The function returns a non-standard status. |
1517 } | 1476 } |
1518 } | 1477 } |
1519 Dart_TypedDataReleaseData(protocols_handle); | 1478 Dart_TypedDataReleaseData(protocols_handle); |
1520 } | 1479 } |
1521 | 1480 |
1522 | 1481 |
1523 void SSLFilter::Connect(const char* hostname, | 1482 void SSLFilter::Connect(const char* hostname, |
1524 SSL_CTX* context, | 1483 SSL_CTX* context, |
(...skipping 13 matching lines...) Expand all Loading... |
1538 CheckStatus(status, "TlsException", "BIO_new_bio_pair"); | 1497 CheckStatus(status, "TlsException", "BIO_new_bio_pair"); |
1539 | 1498 |
1540 assert(context != NULL); | 1499 assert(context != NULL); |
1541 ssl_ = SSL_new(context); | 1500 ssl_ = SSL_new(context); |
1542 SSL_set_bio(ssl_, ssl_side, ssl_side); | 1501 SSL_set_bio(ssl_, ssl_side, ssl_side); |
1543 SSL_set_mode(ssl_, SSL_MODE_AUTO_RETRY); // TODO(whesse): Is this right? | 1502 SSL_set_mode(ssl_, SSL_MODE_AUTO_RETRY); // TODO(whesse): Is this right? |
1544 SSL_set_ex_data(ssl_, filter_ssl_index, this); | 1503 SSL_set_ex_data(ssl_, filter_ssl_index, this); |
1545 | 1504 |
1546 if (is_server_) { | 1505 if (is_server_) { |
1547 int certificate_mode = | 1506 int certificate_mode = |
1548 request_client_certificate ? SSL_VERIFY_PEER : SSL_VERIFY_NONE; | 1507 request_client_certificate ? SSL_VERIFY_PEER : SSL_VERIFY_NONE; |
1549 if (require_client_certificate) { | 1508 if (require_client_certificate) { |
1550 certificate_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; | 1509 certificate_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
1551 } | 1510 } |
1552 SSL_set_verify(ssl_, certificate_mode, NULL); | 1511 SSL_set_verify(ssl_, certificate_mode, NULL); |
1553 } else { | 1512 } else { |
1554 SetAlpnProtocolList(protocols_handle, ssl_, NULL, false); | 1513 SetAlpnProtocolList(protocols_handle, ssl_, NULL, false); |
1555 status = SSL_set_tlsext_host_name(ssl_, hostname); | 1514 status = SSL_set_tlsext_host_name(ssl_, hostname); |
1556 CheckStatus(status, "TlsException", "Set SNI host name"); | 1515 CheckStatus(status, "TlsException", "Set SNI host name"); |
1557 // Sets the hostname in the certificate-checking object, so it is checked | 1516 // Sets the hostname in the certificate-checking object, so it is checked |
1558 // against the certificate presented by the server. | 1517 // against the certificate presented by the server. |
1559 X509_VERIFY_PARAM* certificate_checking_parameters = SSL_get0_param(ssl_); | 1518 X509_VERIFY_PARAM* certificate_checking_parameters = SSL_get0_param(ssl_); |
1560 hostname_ = strdup(hostname); | 1519 hostname_ = strdup(hostname); |
1561 X509_VERIFY_PARAM_set_flags(certificate_checking_parameters, | 1520 X509_VERIFY_PARAM_set_flags( |
1562 X509_V_FLAG_PARTIAL_CHAIN | | 1521 certificate_checking_parameters, |
1563 X509_V_FLAG_TRUSTED_FIRST); | 1522 X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_TRUSTED_FIRST); |
1564 X509_VERIFY_PARAM_set_hostflags(certificate_checking_parameters, 0); | 1523 X509_VERIFY_PARAM_set_hostflags(certificate_checking_parameters, 0); |
1565 status = X509_VERIFY_PARAM_set1_host(certificate_checking_parameters, | 1524 status = X509_VERIFY_PARAM_set1_host(certificate_checking_parameters, |
1566 hostname_, strlen(hostname_)); | 1525 hostname_, strlen(hostname_)); |
1567 CheckStatus(status, "TlsException", | 1526 CheckStatus(status, "TlsException", |
1568 "Set hostname for certificate checking"); | 1527 "Set hostname for certificate checking"); |
1569 } | 1528 } |
1570 // Make the connection: | 1529 // Make the connection: |
1571 if (is_server_) { | 1530 if (is_server_) { |
1572 status = SSL_accept(ssl_); | 1531 status = SSL_accept(ssl_); |
1573 if (SSL_LOG_STATUS) { | 1532 if (SSL_LOG_STATUS) { |
(...skipping 16 matching lines...) Expand all Loading... |
1590 error = SSL_get_error(ssl_, status); | 1549 error = SSL_get_error(ssl_, status); |
1591 if (SSL_LOG_STATUS) { | 1550 if (SSL_LOG_STATUS) { |
1592 Log::Print("SSL_connect error: %d\n", error); | 1551 Log::Print("SSL_connect error: %d\n", error); |
1593 } | 1552 } |
1594 } | 1553 } |
1595 } | 1554 } |
1596 Handshake(); | 1555 Handshake(); |
1597 } | 1556 } |
1598 | 1557 |
1599 | 1558 |
1600 int printErrorCallback(const char *str, size_t len, void *ctx) { | 1559 int printErrorCallback(const char* str, size_t len, void* ctx) { |
1601 Log::PrintErr("%.*s\n", static_cast<int>(len), str); | 1560 Log::PrintErr("%.*s\n", static_cast<int>(len), str); |
1602 return 1; | 1561 return 1; |
1603 } | 1562 } |
1604 | 1563 |
1605 void SSLFilter::Handshake() { | 1564 void SSLFilter::Handshake() { |
1606 // Try and push handshake along. | 1565 // Try and push handshake along. |
1607 int status; | 1566 int status; |
1608 status = SSL_do_handshake(ssl_); | 1567 status = SSL_do_handshake(ssl_); |
1609 if (callback_error != NULL) { | 1568 if (callback_error != NULL) { |
1610 // The SSL_do_handshake will try performing a handshake and might call | 1569 // The SSL_do_handshake will try performing a handshake and might call |
1611 // a CertificateCallback. If the certificate validation | 1570 // a CertificateCallback. If the certificate validation |
1612 // failed the 'callback_error" will be set by the certificateCallback | 1571 // failed the 'callback_error" will be set by the certificateCallback |
1613 // logic and we propagate the error" | 1572 // logic and we propagate the error" |
1614 Dart_PropagateError(callback_error); | 1573 Dart_PropagateError(callback_error); |
1615 } | 1574 } |
1616 if (SSL_want_write(ssl_) || SSL_want_read(ssl_)) { | 1575 if (SSL_want_write(ssl_) || SSL_want_read(ssl_)) { |
1617 in_handshake_ = true; | 1576 in_handshake_ = true; |
1618 return; | 1577 return; |
1619 } | 1578 } |
1620 CheckStatus(status, | 1579 CheckStatus(status, "HandshakeException", is_server_ |
1621 "HandshakeException", | 1580 ? "Handshake error in server" |
1622 is_server_ ? "Handshake error in server" : "Handshake error in client"); | 1581 : "Handshake error in client"); |
1623 // Handshake succeeded. | 1582 // Handshake succeeded. |
1624 if (in_handshake_) { | 1583 if (in_handshake_) { |
1625 // TODO(24071): Check return value of SSL_get_verify_result, this | 1584 // TODO(24071): Check return value of SSL_get_verify_result, this |
1626 // should give us the hostname check. | 1585 // should give us the hostname check. |
1627 int result = SSL_get_verify_result(ssl_); | 1586 int result = SSL_get_verify_result(ssl_); |
1628 if (SSL_LOG_STATUS) { | 1587 if (SSL_LOG_STATUS) { |
1629 Log::Print("Handshake verification status: %d\n", result); | 1588 Log::Print("Handshake verification status: %d\n", result); |
1630 X509* peer_certificate = SSL_get_peer_certificate(ssl_); | 1589 X509* peer_certificate = SSL_get_peer_certificate(ssl_); |
1631 if (peer_certificate == NULL) { | 1590 if (peer_certificate == NULL) { |
1632 Log::Print("No peer certificate received\n"); | 1591 Log::Print("No peer certificate received\n"); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1714 } | 1673 } |
1715 } | 1674 } |
1716 | 1675 |
1717 | 1676 |
1718 /* Read decrypted data from the filter to the circular buffer */ | 1677 /* Read decrypted data from the filter to the circular buffer */ |
1719 int SSLFilter::ProcessReadPlaintextBuffer(int start, int end) { | 1678 int SSLFilter::ProcessReadPlaintextBuffer(int start, int end) { |
1720 int length = end - start; | 1679 int length = end - start; |
1721 int bytes_processed = 0; | 1680 int bytes_processed = 0; |
1722 if (length > 0) { | 1681 if (length > 0) { |
1723 bytes_processed = SSL_read( | 1682 bytes_processed = SSL_read( |
1724 ssl_, | 1683 ssl_, reinterpret_cast<char*>((buffers_[kReadPlaintext] + start)), |
1725 reinterpret_cast<char*>((buffers_[kReadPlaintext] + start)), | |
1726 length); | 1684 length); |
1727 if (bytes_processed < 0) { | 1685 if (bytes_processed < 0) { |
1728 int error = SSL_get_error(ssl_, bytes_processed); | 1686 int error = SSL_get_error(ssl_, bytes_processed); |
1729 USE(error); | 1687 USE(error); |
1730 bytes_processed = 0; | 1688 bytes_processed = 0; |
1731 } | 1689 } |
1732 } | 1690 } |
1733 return bytes_processed; | 1691 return bytes_processed; |
1734 } | 1692 } |
1735 | 1693 |
1736 | 1694 |
1737 int SSLFilter::ProcessWritePlaintextBuffer(int start, int end) { | 1695 int SSLFilter::ProcessWritePlaintextBuffer(int start, int end) { |
1738 int length = end - start; | 1696 int length = end - start; |
1739 int bytes_processed = SSL_write( | 1697 int bytes_processed = |
1740 ssl_, buffers_[kWritePlaintext] + start, length); | 1698 SSL_write(ssl_, buffers_[kWritePlaintext] + start, length); |
1741 if (bytes_processed < 0) { | 1699 if (bytes_processed < 0) { |
1742 if (SSL_LOG_DATA) { | 1700 if (SSL_LOG_DATA) { |
1743 Log::Print("SSL_write returned error %d\n", bytes_processed); | 1701 Log::Print("SSL_write returned error %d\n", bytes_processed); |
1744 } | 1702 } |
1745 return 0; | 1703 return 0; |
1746 } | 1704 } |
1747 return bytes_processed; | 1705 return bytes_processed; |
1748 } | 1706 } |
1749 | 1707 |
1750 | 1708 |
1751 /* Read encrypted data from the circular buffer to the filter */ | 1709 /* Read encrypted data from the circular buffer to the filter */ |
1752 int SSLFilter::ProcessReadEncryptedBuffer(int start, int end) { | 1710 int SSLFilter::ProcessReadEncryptedBuffer(int start, int end) { |
1753 int length = end - start; | 1711 int length = end - start; |
1754 if (SSL_LOG_DATA) Log::Print( | 1712 if (SSL_LOG_DATA) |
1755 "Entering ProcessReadEncryptedBuffer with %d bytes\n", length); | 1713 Log::Print("Entering ProcessReadEncryptedBuffer with %d bytes\n", length); |
1756 int bytes_processed = 0; | 1714 int bytes_processed = 0; |
1757 if (length > 0) { | 1715 if (length > 0) { |
1758 bytes_processed = | 1716 bytes_processed = |
1759 BIO_write(socket_side_, buffers_[kReadEncrypted] + start, length); | 1717 BIO_write(socket_side_, buffers_[kReadEncrypted] + start, length); |
1760 if (bytes_processed <= 0) { | 1718 if (bytes_processed <= 0) { |
1761 bool retry = BIO_should_retry(socket_side_); | 1719 bool retry = BIO_should_retry(socket_side_); |
1762 if (!retry) { | 1720 if (!retry) { |
1763 if (SSL_LOG_DATA) Log::Print( | 1721 if (SSL_LOG_DATA) |
1764 "BIO_write failed in ReadEncryptedBuffer\n"); | 1722 Log::Print("BIO_write failed in ReadEncryptedBuffer\n"); |
1765 } | 1723 } |
1766 bytes_processed = 0; | 1724 bytes_processed = 0; |
1767 } | 1725 } |
1768 } | 1726 } |
1769 if (SSL_LOG_DATA) Log::Print( | 1727 if (SSL_LOG_DATA) |
1770 "Leaving ProcessReadEncryptedBuffer wrote %d bytes\n", bytes_processed); | 1728 Log::Print("Leaving ProcessReadEncryptedBuffer wrote %d bytes\n", |
| 1729 bytes_processed); |
1771 return bytes_processed; | 1730 return bytes_processed; |
1772 } | 1731 } |
1773 | 1732 |
1774 | 1733 |
1775 int SSLFilter::ProcessWriteEncryptedBuffer(int start, int end) { | 1734 int SSLFilter::ProcessWriteEncryptedBuffer(int start, int end) { |
1776 int length = end - start; | 1735 int length = end - start; |
1777 int bytes_processed = 0; | 1736 int bytes_processed = 0; |
1778 if (length > 0) { | 1737 if (length > 0) { |
1779 bytes_processed = BIO_read(socket_side_, | 1738 bytes_processed = |
1780 buffers_[kWriteEncrypted] + start, | 1739 BIO_read(socket_side_, buffers_[kWriteEncrypted] + start, length); |
1781 length); | |
1782 if (bytes_processed < 0) { | 1740 if (bytes_processed < 0) { |
1783 if (SSL_LOG_DATA) Log::Print( | 1741 if (SSL_LOG_DATA) |
1784 "WriteEncrypted BIO_read returned error %d\n", bytes_processed); | 1742 Log::Print("WriteEncrypted BIO_read returned error %d\n", |
| 1743 bytes_processed); |
1785 return 0; | 1744 return 0; |
1786 } else { | 1745 } else { |
1787 if (SSL_LOG_DATA) Log::Print( | 1746 if (SSL_LOG_DATA) |
1788 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); | 1747 Log::Print("WriteEncrypted BIO_read wrote %d bytes\n", |
| 1748 bytes_processed); |
1789 } | 1749 } |
1790 } | 1750 } |
1791 return bytes_processed; | 1751 return bytes_processed; |
1792 } | 1752 } |
1793 | 1753 |
1794 } // namespace bin | 1754 } // namespace bin |
1795 } // namespace dart | 1755 } // namespace dart |
1796 | 1756 |
1797 #endif // defined(TARGET_OS_LINUX) | 1757 #endif // defined(TARGET_OS_LINUX) |
1798 | 1758 |
1799 #endif // !defined(DART_IO_DISABLED) && | 1759 #endif // !defined(DART_IO_DISABLED) && |
1800 // !defined(DART_IO_SECURE_SOCKET_DISABLED) | 1760 // !defined(DART_IO_SECURE_SOCKET_DISABLED) |
OLD | NEW |