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 #include "bin/secure_socket.h" | 5 #include "bin/secure_socket.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 start_validity_date, | 265 start_validity_date, |
266 end_validity_date }; | 266 end_validity_date }; |
267 return Dart_New(x509_class, Dart_Null(), 4, arguments); | 267 return Dart_New(x509_class, Dart_Null(), 4, arguments); |
268 } | 268 } |
269 | 269 |
270 | 270 |
271 void SSLFilter::Init(Dart_Handle dart_this) { | 271 void SSLFilter::Init(Dart_Handle dart_this) { |
272 if (!library_initialized_) { | 272 if (!library_initialized_) { |
273 InitializeLibrary(NULL, "", true, false); | 273 InitializeLibrary(NULL, "", true, false); |
274 } | 274 } |
275 string_start_ = ThrowIfError( | 275 ASSERT(string_start_ == NULL); |
276 Dart_NewPersistentHandle(DartUtils::NewString("start"))); | 276 string_start_ = Dart_NewPersistentHandle(DartUtils::NewString("start")); |
277 string_length_ = ThrowIfError( | 277 ASSERT(string_start_ != NULL); |
278 Dart_NewPersistentHandle(DartUtils::NewString("length"))); | 278 ASSERT(string_length_ == NULL); |
| 279 string_length_ = Dart_NewPersistentHandle(DartUtils::NewString("length")); |
| 280 ASSERT(string_length_ != NULL); |
| 281 ASSERT(bad_certificate_callback_ == NULL); |
| 282 bad_certificate_callback_ = Dart_NewPersistentHandle(Dart_Null()); |
| 283 ASSERT(bad_certificate_callback_ != NULL); |
279 | 284 |
280 InitializeBuffers(dart_this); | 285 InitializeBuffers(dart_this); |
281 filter_ = memio_CreateIOLayer(kMemioBufferSize); | 286 filter_ = memio_CreateIOLayer(kMemioBufferSize); |
282 } | 287 } |
283 | 288 |
284 | 289 |
285 void SSLFilter::InitializeBuffers(Dart_Handle dart_this) { | 290 void SSLFilter::InitializeBuffers(Dart_Handle dart_this) { |
286 // Create SSLFilter buffers as ExternalUint8Array objects. | 291 // Create SSLFilter buffers as ExternalUint8Array objects. |
287 Dart_Handle dart_buffers_object = ThrowIfError( | 292 Dart_Handle dart_buffers_object = ThrowIfError( |
288 Dart_GetField(dart_this, DartUtils::NewString("buffers"))); | 293 Dart_GetField(dart_this, DartUtils::NewString("buffers"))); |
(...skipping 17 matching lines...) Expand all Loading... |
306 Dart_ThrowException(DartUtils::NewString( | 311 Dart_ThrowException(DartUtils::NewString( |
307 "Invalid encrypted buffer size in _ExternalBuffer")); | 312 "Invalid encrypted buffer size in _ExternalBuffer")); |
308 } | 313 } |
309 buffer_size_ = static_cast<int>(buffer_size); | 314 buffer_size_ = static_cast<int>(buffer_size); |
310 encrypted_buffer_size_ = static_cast<int>(encrypted_buffer_size); | 315 encrypted_buffer_size_ = static_cast<int>(encrypted_buffer_size); |
311 | 316 |
312 | 317 |
313 Dart_Handle data_identifier = DartUtils::NewString("data"); | 318 Dart_Handle data_identifier = DartUtils::NewString("data"); |
314 for (int i = 0; i < kNumBuffers; ++i) { | 319 for (int i = 0; i < kNumBuffers; ++i) { |
315 int size = isEncrypted(i) ? encrypted_buffer_size_ : buffer_size_; | 320 int size = isEncrypted(i) ? encrypted_buffer_size_ : buffer_size_; |
316 dart_buffer_objects_[i] = ThrowIfError( | 321 dart_buffer_objects_[i] = |
317 Dart_NewPersistentHandle(Dart_ListGetAt(dart_buffers_object, i))); | 322 Dart_NewPersistentHandle(Dart_ListGetAt(dart_buffers_object, i)); |
| 323 ASSERT(dart_buffer_objects_[i] != NULL); |
318 buffers_[i] = new uint8_t[size]; | 324 buffers_[i] = new uint8_t[size]; |
319 Dart_Handle data = ThrowIfError( | 325 Dart_Handle data = ThrowIfError( |
320 Dart_NewExternalTypedData(kUint8, buffers_[i], size, NULL, NULL)); | 326 Dart_NewExternalTypedData(kUint8, buffers_[i], size)); |
321 ThrowIfError(Dart_SetField(dart_buffer_objects_[i], | 327 ThrowIfError( |
322 data_identifier, | 328 Dart_SetField(Dart_HandleFromPersistent(dart_buffer_objects_[i]), |
323 data)); | 329 data_identifier, |
| 330 data)); |
324 } | 331 } |
325 } | 332 } |
326 | 333 |
327 | 334 |
328 void SSLFilter::RegisterHandshakeCompleteCallback(Dart_Handle complete) { | 335 void SSLFilter::RegisterHandshakeCompleteCallback(Dart_Handle complete) { |
329 ASSERT(NULL == handshake_complete_); | 336 ASSERT(NULL == handshake_complete_); |
330 handshake_complete_ = ThrowIfError(Dart_NewPersistentHandle(complete)); | 337 handshake_complete_ = Dart_NewPersistentHandle(complete); |
| 338 ASSERT(handshake_complete_ != NULL); |
331 } | 339 } |
332 | 340 |
333 | 341 |
334 void SSLFilter::RegisterBadCertificateCallback(Dart_Handle callback) { | 342 void SSLFilter::RegisterBadCertificateCallback(Dart_Handle callback) { |
335 if (NULL != bad_certificate_callback_) { | 343 ASSERT(bad_certificate_callback_ != NULL); |
336 Dart_DeletePersistentHandle(bad_certificate_callback_); | 344 Dart_DeletePersistentHandle(bad_certificate_callback_); |
337 } | 345 bad_certificate_callback_ = Dart_NewPersistentHandle(callback); |
338 bad_certificate_callback_ = ThrowIfError(Dart_NewPersistentHandle(callback)); | 346 ASSERT(bad_certificate_callback_ != NULL); |
339 } | 347 } |
340 | 348 |
341 static const char* builtin_roots_module = | 349 static const char* builtin_roots_module = |
342 #if defined(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID) | 350 #if defined(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID) |
343 "name=\"Root Certs\" library=\"libnssckbi.so\""; | 351 "name=\"Root Certs\" library=\"libnssckbi.so\""; |
344 #elif defined(TARGET_OS_MACOS) | 352 #elif defined(TARGET_OS_MACOS) |
345 "name=\"Root Certs\" library=\"libnssckbi.dylib\""; | 353 "name=\"Root Certs\" library=\"libnssckbi.dylib\""; |
346 #elif defined(TARGET_OS_WINDOWS) | 354 #elif defined(TARGET_OS_WINDOWS) |
347 "name=\"Root Certs\" library=\"nssckbi.dll\""; | 355 "name=\"Root Certs\" library=\"nssckbi.dll\""; |
348 #else | 356 #else |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 if (!retry) { | 428 if (!retry) { |
421 return PL_strdup(static_cast<char*>(arg)); // Freed by NSS internals. | 429 return PL_strdup(static_cast<char*>(arg)); // Freed by NSS internals. |
422 } | 430 } |
423 return NULL; | 431 return NULL; |
424 } | 432 } |
425 | 433 |
426 | 434 |
427 SECStatus BadCertificateCallback(void* filter, PRFileDesc* fd) { | 435 SECStatus BadCertificateCallback(void* filter, PRFileDesc* fd) { |
428 SSLFilter* ssl_filter = static_cast<SSLFilter*>(filter); | 436 SSLFilter* ssl_filter = static_cast<SSLFilter*>(filter); |
429 Dart_Handle callback = ssl_filter->bad_certificate_callback(); | 437 Dart_Handle callback = ssl_filter->bad_certificate_callback(); |
430 if (callback == NULL || Dart_IsNull(callback)) return SECFailure; | 438 if (Dart_IsNull(callback)) return SECFailure; |
431 | 439 |
432 Dart_EnterScope(); | 440 Dart_EnterScope(); |
433 Dart_Handle x509_object = ssl_filter->PeerCertificate(); | 441 Dart_Handle x509_object = ssl_filter->PeerCertificate(); |
434 Dart_Handle result = | 442 Dart_Handle result = |
435 ThrowIfError(Dart_InvokeClosure(callback, 1, &x509_object)); | 443 ThrowIfError(Dart_InvokeClosure(callback, 1, &x509_object)); |
436 bool c_result = Dart_IsBoolean(result) && DartUtils::GetBooleanValue(result); | 444 bool c_result = Dart_IsBoolean(result) && DartUtils::GetBooleanValue(result); |
437 Dart_ExitScope(); | 445 Dart_ExitScope(); |
438 return c_result ? SECSuccess : SECFailure; | 446 return c_result ? SECSuccess : SECFailure; |
439 } | 447 } |
440 | 448 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 | 583 |
576 memio_SetPeerName(filter_, &host_address); | 584 memio_SetPeerName(filter_, &host_address); |
577 PR_FreeAddrInfo(info); | 585 PR_FreeAddrInfo(info); |
578 } | 586 } |
579 | 587 |
580 | 588 |
581 void SSLFilter::Handshake() { | 589 void SSLFilter::Handshake() { |
582 SECStatus status = SSL_ForceHandshake(filter_); | 590 SECStatus status = SSL_ForceHandshake(filter_); |
583 if (status == SECSuccess) { | 591 if (status == SECSuccess) { |
584 if (in_handshake_) { | 592 if (in_handshake_) { |
585 ThrowIfError(Dart_InvokeClosure(handshake_complete_, 0, NULL)); | 593 ThrowIfError(Dart_InvokeClosure( |
| 594 Dart_HandleFromPersistent(handshake_complete_), 0, NULL)); |
586 in_handshake_ = false; | 595 in_handshake_ = false; |
587 } | 596 } |
588 } else { | 597 } else { |
589 PRErrorCode error = PR_GetError(); | 598 PRErrorCode error = PR_GetError(); |
590 if (error == PR_WOULD_BLOCK_ERROR) { | 599 if (error == PR_WOULD_BLOCK_ERROR) { |
591 if (!in_handshake_) { | 600 if (!in_handshake_) { |
592 in_handshake_ = true; | 601 in_handshake_ = true; |
593 } | 602 } |
594 } else { | 603 } else { |
595 if (is_server_) { | 604 if (is_server_) { |
596 ThrowPRException("Unexpected handshake error in server"); | 605 ThrowPRException("Unexpected handshake error in server"); |
597 } else { | 606 } else { |
598 ThrowPRException("Unexpected handshake error in client"); | 607 ThrowPRException("Unexpected handshake error in client"); |
599 } | 608 } |
600 } | 609 } |
601 } | 610 } |
602 } | 611 } |
603 | 612 |
604 | 613 |
605 void SSLFilter::Destroy() { | 614 void SSLFilter::Destroy() { |
606 for (int i = 0; i < kNumBuffers; ++i) { | 615 for (int i = 0; i < kNumBuffers; ++i) { |
607 Dart_DeletePersistentHandle(dart_buffer_objects_[i]); | 616 Dart_DeletePersistentHandle(dart_buffer_objects_[i]); |
608 delete[] buffers_[i]; | 617 delete[] buffers_[i]; |
609 } | 618 } |
610 Dart_DeletePersistentHandle(string_start_); | 619 Dart_DeletePersistentHandle(string_start_); |
611 Dart_DeletePersistentHandle(string_length_); | 620 Dart_DeletePersistentHandle(string_length_); |
612 Dart_DeletePersistentHandle(handshake_complete_); | 621 Dart_DeletePersistentHandle(handshake_complete_); |
613 if (bad_certificate_callback_ != NULL) { | 622 Dart_DeletePersistentHandle(bad_certificate_callback_); |
614 Dart_DeletePersistentHandle(bad_certificate_callback_); | |
615 } | |
616 free(client_certificate_name_); | 623 free(client_certificate_name_); |
617 | 624 |
618 PR_Close(filter_); | 625 PR_Close(filter_); |
619 } | 626 } |
620 | 627 |
621 | 628 |
622 intptr_t SSLFilter::ProcessBuffer(int buffer_index) { | 629 intptr_t SSLFilter::ProcessBuffer(int buffer_index) { |
623 int size = isEncrypted(buffer_index) ? encrypted_buffer_size_ : buffer_size_; | 630 int size = isEncrypted(buffer_index) ? encrypted_buffer_size_ : buffer_size_; |
624 Dart_Handle buffer_object = dart_buffer_objects_[buffer_index]; | 631 Dart_Handle buffer_object = |
| 632 Dart_HandleFromPersistent(dart_buffer_objects_[buffer_index]); |
625 Dart_Handle start_object = ThrowIfError( | 633 Dart_Handle start_object = ThrowIfError( |
626 Dart_GetField(buffer_object, string_start_)); | 634 Dart_GetField(buffer_object, Dart_HandleFromPersistent(string_start_))); |
627 Dart_Handle length_object = ThrowIfError( | 635 Dart_Handle length_object = ThrowIfError( |
628 Dart_GetField(buffer_object, string_length_)); | 636 Dart_GetField(buffer_object, Dart_HandleFromPersistent(string_length_))); |
629 int64_t unsafe_start = DartUtils::GetIntegerValue(start_object); | 637 int64_t unsafe_start = DartUtils::GetIntegerValue(start_object); |
630 int64_t unsafe_length = DartUtils::GetIntegerValue(length_object); | 638 int64_t unsafe_length = DartUtils::GetIntegerValue(length_object); |
631 ASSERT(unsafe_start >= 0); | 639 ASSERT(unsafe_start >= 0); |
632 ASSERT(unsafe_start < size); | 640 ASSERT(unsafe_start < size); |
633 ASSERT(unsafe_length >= 0); | 641 ASSERT(unsafe_length >= 0); |
634 ASSERT(unsafe_length <= size); | 642 ASSERT(unsafe_length <= size); |
635 int start = static_cast<int>(unsafe_start); | 643 int start = static_cast<int>(unsafe_start); |
636 int length = static_cast<int>(unsafe_length); | 644 int length = static_cast<int>(unsafe_length); |
637 uint8_t* buffer = buffers_[buffer_index]; | 645 uint8_t* buffer = buffers_[buffer_index]; |
638 | 646 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
714 bytes_processed = 0; | 722 bytes_processed = 0; |
715 } | 723 } |
716 break; | 724 break; |
717 } | 725 } |
718 } | 726 } |
719 return bytes_processed; | 727 return bytes_processed; |
720 } | 728 } |
721 | 729 |
722 } // namespace bin | 730 } // namespace bin |
723 } // namespace dart | 731 } // namespace dart |
OLD | NEW |