Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(517)

Side by Side Diff: runtime/bin/secure_socket.cc

Issue 21716004: dart:io | Add SecureSocket.importPrivateCertificates, that reads a PKCS#12 file. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add more API functions, tests Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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>
11 #include <string.h> 11 #include <string.h>
12 12
13 #include <certdb.h>
13 #include <key.h> 14 #include <key.h>
14 #include <keyt.h> 15 #include <keyt.h>
15 #include <nss.h> 16 #include <nss.h>
17 #include <p12.h>
18 #include <p12plcy.h>
16 #include <pk11pub.h> 19 #include <pk11pub.h>
17 #include <prerror.h> 20 #include <prerror.h>
18 #include <prinit.h> 21 #include <prinit.h>
19 #include <prnetdb.h> 22 #include <prnetdb.h>
20 #include <secmod.h> 23 #include <secmod.h>
21 #include <ssl.h> 24 #include <ssl.h>
22 #include <sslproto.h> 25 #include <sslproto.h>
23 26
24 #include "bin/builtin.h" 27 #include "bin/builtin.h"
25 #include "bin/dartutils.h" 28 #include "bin/dartutils.h"
26 #include "bin/net/nss_memio.h" 29 #include "bin/net/nss_memio.h"
27 #include "bin/socket.h" 30 #include "bin/socket.h"
28 #include "bin/thread.h" 31 #include "bin/thread.h"
29 #include "bin/utils.h" 32 #include "bin/utils.h"
30 #include "platform/utils.h" 33 #include "platform/utils.h"
31 34
32 #include "include/dart_api.h" 35 #include "include/dart_api.h"
33 36
34 37
35 namespace dart { 38 namespace dart {
36 namespace bin { 39 namespace bin {
37 40
38 bool SSLFilter::library_initialized_ = false; 41 bool SSLFilter::library_initialized_ = false;
39 // To protect library initialization. 42 // To protect library initialization.
40 dart::Mutex* SSLFilter::mutex_ = new dart::Mutex(); 43 dart::Mutex* SSLFilter::mutex_ = new dart::Mutex();
41 // The password is needed when creating secure server sockets. It can 44 // The password is needed when creating secure server sockets. It can
42 // be null if only secure client sockets are used. 45 // be null if only secure client sockets are used.
43 const char* SSLFilter::password_ = NULL; 46 char* SSLFilter::password_ = NULL;
44 47
45 // Forward declaration. 48 // Forward declaration.
46 static void ProcessFilter(Dart_Port dest_port_id, 49 static void ProcessFilter(Dart_Port dest_port_id,
47 Dart_Port reply_port_id, 50 Dart_Port reply_port_id,
48 Dart_CObject* message); 51 Dart_CObject* message);
49 52
50 NativeService SSLFilter::filter_service_("FilterService", ProcessFilter, 16); 53 NativeService SSLFilter::filter_service_("FilterService", ProcessFilter, 16);
51 54
52 static const int kSSLFilterNativeFieldIndex = 0; 55 static const int kSSLFilterNativeFieldIndex = 0;
53 56
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 static void SetFilter(Dart_NativeArguments args, SSLFilter* filter) { 101 static void SetFilter(Dart_NativeArguments args, SSLFilter* filter) {
99 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); 102 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0));
100 ASSERT(Dart_IsInstance(dart_this)); 103 ASSERT(Dart_IsInstance(dart_this));
101 ThrowIfError(Dart_SetNativeInstanceField( 104 ThrowIfError(Dart_SetNativeInstanceField(
102 dart_this, 105 dart_this,
103 kSSLFilterNativeFieldIndex, 106 kSSLFilterNativeFieldIndex,
104 reinterpret_cast<intptr_t>(filter))); 107 reinterpret_cast<intptr_t>(filter)));
105 } 108 }
106 109
107 110
111 char* PasswordCallback(PK11SlotInfo* slot, PRBool retry, void* arg) {
Bill Hesse 2013/08/06 16:47:59 Moved here from later in the source file.
112 if (!retry) {
113 return PL_strdup(static_cast<char*>(arg)); // Freed by NSS internals.
114 }
115 return NULL;
116 }
117
118
108 void FUNCTION_NAME(SecureSocket_Init)(Dart_NativeArguments args) { 119 void FUNCTION_NAME(SecureSocket_Init)(Dart_NativeArguments args) {
109 Dart_EnterScope(); 120 Dart_EnterScope();
110 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); 121 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0));
111 SSLFilter* filter = new SSLFilter; 122 SSLFilter* filter = new SSLFilter;
112 SetFilter(args, filter); 123 SetFilter(args, filter);
113 filter->Init(dart_this); 124 filter->Init(dart_this);
114 Dart_ExitScope(); 125 Dart_ExitScope();
115 } 126 }
116 127
117 128
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 "Password argument to SetCertificateDatabase is not a String or null")); 273 "Password argument to SetCertificateDatabase is not a String or null"));
263 } 274 }
264 275
265 Dart_Handle builtin_roots_object = 276 Dart_Handle builtin_roots_object =
266 ThrowIfError(Dart_GetNativeArgument(args, 2)); 277 ThrowIfError(Dart_GetNativeArgument(args, 2));
267 // Check that the type is boolean, and get the boolean value from it. 278 // Check that the type is boolean, and get the boolean value from it.
268 bool builtin_roots = true; 279 bool builtin_roots = true;
269 if (Dart_IsBoolean(builtin_roots_object)) { 280 if (Dart_IsBoolean(builtin_roots_object)) {
270 ThrowIfError(Dart_BooleanValue(builtin_roots_object, &builtin_roots)); 281 ThrowIfError(Dart_BooleanValue(builtin_roots_object, &builtin_roots));
271 } else { 282 } else {
272 Dart_ThrowException(DartUtils::NewDartArgumentError( 283 Dart_ThrowException(DartUtils::NewDartArgumentError("Non-Boolean value for"
273 "UseBuiltinRoots argument to SetCertificateDatabase is not a bool")); 284 " argument useBuiltinRoots in SetCertificateDatabase"));
274 } 285 }
275 286
276 SSLFilter::InitializeLibrary(certificate_database, password, builtin_roots); 287 Dart_Handle read_only_object =
288 ThrowIfError(Dart_GetNativeArgument(args, 3));
289 // Check that the type is boolean, and get the boolean value from it.
290 bool read_only = true;
291 if (Dart_IsBoolean(read_only_object)) {
292 ThrowIfError(Dart_BooleanValue(read_only_object, &read_only));
293 } else {
294 Dart_ThrowException(DartUtils::NewDartArgumentError(
295 "Non-Boolean value for argument readOnly in SetCertificateDatabase"));
296 }
297
298 SSLFilter::InitializeLibrary(
299 certificate_database, password, builtin_roots, read_only);
277 Dart_ExitScope(); 300 Dart_ExitScope();
278 } 301 }
279 302
280 303
281 static Dart_Handle X509FromCertificate(CERTCertificate* certificate) { 304 static Dart_Handle X509FromCertificate(CERTCertificate* certificate) {
282 PRTime start_validity; 305 PRTime start_validity;
283 PRTime end_validity; 306 PRTime end_validity;
284 SECStatus status = 307 SECStatus status =
285 CERT_GetCertTimes(certificate, &start_validity, &end_validity); 308 CERT_GetCertTimes(certificate, &start_validity, &end_validity);
286 if (status != SECSuccess) { 309 if (status != SECSuccess) {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 SECStatus status = CERT_DecodeTrustString(&trust, trust_string); 373 SECStatus status = CERT_DecodeTrustString(&trust, trust_string);
351 if (status != SECSuccess) { 374 if (status != SECSuccess) {
352 ThrowPRException("CertificateException", "Trust string cannot be decoded"); 375 ThrowPRException("CertificateException", "Trust string cannot be decoded");
353 } 376 }
354 377
355 status = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), cert, &trust); 378 status = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), cert, &trust);
356 if (status != SECSuccess) { 379 if (status != SECSuccess) {
357 ThrowPRException("CertificateException", "Cannot set trust attributes"); 380 ThrowPRException("CertificateException", "Cannot set trust attributes");
358 } 381 }
359 382
383 status = SEC_PKCS12DecoderImportBags(NULL);
384
360 Dart_SetReturnValue(args, X509FromCertificate(cert)); 385 Dart_SetReturnValue(args, X509FromCertificate(cert));
361 Dart_ExitScope(); 386 Dart_ExitScope();
362 return; 387 return;
363 } 388 }
364 389
365 390
391 SECItem* nickname_collision_callback(SECItem *old_nickname,
392 PRBool *cancel,
393 void *arg) {
394 printf("Nickname collision callback hit\n");
395 return NULL;
396 }
397
398
399 void FUNCTION_NAME(SecureSocket_ImportPrivateCertificates)
400 (Dart_NativeArguments args) {
401 Dart_EnterScope();
402 Dart_Handle pk12_object =
403 ThrowIfError(Dart_GetNativeArgument(args, 0));
404 if (!Dart_IsList(pk12_object)) {
405 Dart_ThrowException(DartUtils::NewDartArgumentError(
406 "SecureSocket.importPrivateCertificates"));
407 }
408
409 intptr_t length;
410 ThrowIfError(Dart_ListLength(pk12_object, &length));
411 uint8_t* pk12 = reinterpret_cast<uint8_t*>(malloc(length + 1));
412 if (pk12 == NULL) {
413 FATAL("Out of memory in SecureSocket.addCertificate");
414 }
415 ThrowIfError(Dart_ListGetAsBytes(pk12_object, 0, pk12, length));
416
417 Dart_Handle password_object = ThrowIfError(Dart_GetNativeArgument(args, 1));
418 if (!Dart_IsString(password_object)) {
419 Dart_ThrowException(DartUtils::NewDartArgumentError(
420 "SecureSocket.importPrivateCertificates"));
421 }
422
423 // A big-endian Unicode (UTF16) password.
424 intptr_t password_length;
425 ThrowIfError(Dart_StringLength(password_object, &password_length));
426 password_length++;
427 uint16_t* password = reinterpret_cast<uint16_t*>(malloc(2 * password_length));
428 if (password == NULL) {
429 FATAL("Out of memory in SecureSocket.addCertificate");
430 }
431 intptr_t returned_length = password_length;
432 ThrowIfError(Dart_StringToUTF16(password_object, password, &returned_length));
433 ASSERT(password_length == returned_length + 1);
434 password[password_length - 1] = 0;
435 for (int i = 0; i < password_length; ++i) {
436 password[i] = Utils::HostToBigEndian16(password[i]);
437 }
438 SECItem p12_password;
439 p12_password.type = siBuffer;
440 p12_password.data = (unsigned char*)password;
441 p12_password.len = 2 * password_length;
442
443 Dart_SetReturnValue(args, Dart_Null());
444 PK11_SetPasswordFunc(PasswordCallback);
445 PK11SlotInfo* slot = PK11_GetInternalKeySlot();
446 SECStatus status = PK11_Authenticate(slot, PR_TRUE, SSLFilter::GetPassword());
447 if (status == SECFailure) {
448 printf("Failed PK11_Authenticate\n");
449 Dart_ExitScope();
450 return;
451 }
452
453 SEC_PKCS12DecoderContext* context = SEC_PKCS12DecoderStart(
454 &p12_password,
455 slot,
456 SSLFilter::GetPassword(),
457 NULL,
458 NULL,
459 NULL,
460 NULL,
461 NULL);
462 if (!context) {
463 Dart_ExitScope();
464 return;
465 }
466
467 if (SECSuccess == SEC_PKCS12DecoderUpdate(
468 context,
469 pk12,
470 length) &&
471 SECSuccess == SEC_PKCS12DecoderVerify(context) &&
472 SECSuccess ==
473 SEC_PKCS12DecoderValidateBags(context, nickname_collision_callback) &&
474 SECSuccess == SEC_PKCS12DecoderImportBags(context)) {
475 SEC_PKCS12DecoderFinish(context);
476 Dart_ExitScope();
477 return;
478 } else {
479 SEC_PKCS12DecoderFinish(context);
480 ThrowPRException("CertificateException",
481 "Could not import PKCS#12 file");
482 }
483 }
484
485
486 void FUNCTION_NAME(SecureSocket_ChangeTrust)(Dart_NativeArguments args) {
487 Dart_EnterScope();
488 Dart_Handle nickname_object = ThrowIfError(Dart_GetNativeArgument(args, 0));
489 Dart_Handle trust_object = ThrowIfError(Dart_GetNativeArgument(args, 1));
490
491 if (!Dart_IsString(nickname_object) || !Dart_IsString(trust_object)) {
492 Dart_ThrowException(DartUtils::NewDartArgumentError(
493 "Bad argument to SecureSocket.changeTrust"));
494 }
495 const char* nickname;
496 ThrowIfError(Dart_StringToCString(nickname_object, &nickname));
497 const char* trust_string;
498 ThrowIfError(Dart_StringToCString(trust_object, &trust_string));
499
500 CERTCertificate* certificate =
501 PK11_FindCertFromNickname(nickname, SSLFilter::GetPassword());
502 if (certificate == NULL) {
503 ThrowCertificateException("Cannot find certificate by nickname: %s",
504 nickname);
505 }
506 CERTCertTrust trust;
507 SECStatus status = CERT_DecodeTrustString(&trust, trust_string);
508 if (status != SECSuccess) {
509 ThrowPRException("CertificateException", "Trust string cannot be decoded");
510 }
511 status = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), certificate, &trust);
512 if (status != SECSuccess) {
513 ThrowCertificateException("Cannot set trust on certificate %s", nickname);
514 }
515
516 CERT_DestroyCertificate(certificate);
517 Dart_ExitScope();
518 }
519
520
521 void FUNCTION_NAME(SecureSocket_RemoveCertificate)(Dart_NativeArguments args) {
522 Dart_EnterScope();
523 Dart_Handle nickname_object =
524 ThrowIfError(Dart_GetNativeArgument(args, 0));
525 if (!Dart_IsString(nickname_object)) {
526 Dart_ThrowException(DartUtils::NewDartArgumentError(
527 "Bad argument to SecureSocket.removeCertificate"));
528 }
529 const char* nickname;
530 ThrowIfError(Dart_StringToCString(nickname_object, &nickname));
531
532 CERTCertificate* certificate =
533 PK11_FindCertFromNickname(nickname, SSLFilter::GetPassword());
534 if (certificate == NULL) {
535 ThrowCertificateException("Cannot find certificate by nickname: %s",
536 nickname);
537 }
538
539 SECKEYPrivateKey* key =
540 PK11_FindKeyByAnyCert(certificate, SSLFilter::GetPassword());
541 if (key == NULL) {
542 SECStatus status = SEC_DeletePermCertificate(certificate);
543 if (status == SECFailure) {
544 CERT_DestroyCertificate(certificate); // get-set prerror
545 ThrowCertificateException("Cannot remove certificate %s",
546 nickname);
547 }
548 } else {
549 SECStatus status =
550 PK11_DeleteTokenCertAndKey(certificate, SSLFilter::GetPassword());
551 if (status == SECFailure) {
552 CERT_DestroyCertificate(certificate); // get-set prerror
553 ThrowCertificateException("Cannot remove certificate %s",
554 nickname);
555 }
556 }
557 Dart_ExitScope();
558 }
559
366 560
367 void FUNCTION_NAME(SecureSocket_PeerCertificate) 561 void FUNCTION_NAME(SecureSocket_PeerCertificate)
368 (Dart_NativeArguments args) { 562 (Dart_NativeArguments args) {
369 Dart_EnterScope(); 563 Dart_EnterScope();
370 Dart_SetReturnValue(args, GetFilter(args)->PeerCertificate()); 564 Dart_SetReturnValue(args, GetFilter(args)->PeerCertificate());
371 Dart_ExitScope(); 565 Dart_ExitScope();
372 } 566 }
373 567
374 568
375 void FUNCTION_NAME(SecureSocket_FilterPointer)(Dart_NativeArguments args) { 569 void FUNCTION_NAME(SecureSocket_FilterPointer)(Dart_NativeArguments args) {
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 default: 703 default:
510 UNREACHABLE(); 704 UNREACHABLE();
511 } 705 }
512 } 706 }
513 return true; 707 return true;
514 } 708 }
515 709
516 710
517 void SSLFilter::Init(Dart_Handle dart_this) { 711 void SSLFilter::Init(Dart_Handle dart_this) {
518 if (!library_initialized_) { 712 if (!library_initialized_) {
519 InitializeLibrary(NULL, "", true, false); 713 InitializeLibrary(NULL, "", true, true, false);
520 } 714 }
521 ASSERT(string_start_ == NULL); 715 ASSERT(string_start_ == NULL);
522 string_start_ = Dart_NewPersistentHandle(DartUtils::NewString("start")); 716 string_start_ = Dart_NewPersistentHandle(DartUtils::NewString("start"));
523 ASSERT(string_start_ != NULL); 717 ASSERT(string_start_ != NULL);
524 ASSERT(string_length_ == NULL); 718 ASSERT(string_length_ == NULL);
525 string_length_ = Dart_NewPersistentHandle(DartUtils::NewString("length")); 719 string_length_ = Dart_NewPersistentHandle(DartUtils::NewString("length"));
526 ASSERT(string_length_ != NULL); 720 ASSERT(string_length_ != NULL);
527 ASSERT(bad_certificate_callback_ == NULL); 721 ASSERT(bad_certificate_callback_ == NULL);
528 bad_certificate_callback_ = Dart_NewPersistentHandle(Dart_Null()); 722 bad_certificate_callback_ = Dart_NewPersistentHandle(Dart_Null());
529 ASSERT(bad_certificate_callback_ != NULL); 723 ASSERT(bad_certificate_callback_ != NULL);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 776
583 777
584 void SSLFilter::RegisterBadCertificateCallback(Dart_Handle callback) { 778 void SSLFilter::RegisterBadCertificateCallback(Dart_Handle callback) {
585 ASSERT(bad_certificate_callback_ != NULL); 779 ASSERT(bad_certificate_callback_ != NULL);
586 Dart_DeletePersistentHandle(bad_certificate_callback_); 780 Dart_DeletePersistentHandle(bad_certificate_callback_);
587 bad_certificate_callback_ = Dart_NewPersistentHandle(callback); 781 bad_certificate_callback_ = Dart_NewPersistentHandle(callback);
588 ASSERT(bad_certificate_callback_ != NULL); 782 ASSERT(bad_certificate_callback_ != NULL);
589 } 783 }
590 784
591 785
592 char* PasswordCallback(PK11SlotInfo* slot, PRBool retry, void* arg) {
593 if (!retry) {
594 return PL_strdup(static_cast<char*>(arg)); // Freed by NSS internals.
595 }
596 return NULL;
597 }
598
599
600 static const char* builtin_roots_module = 786 static const char* builtin_roots_module =
601 #if defined(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID) 787 #if defined(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID)
602 "name=\"Root Certs\" library=\"libnssckbi.so\""; 788 "name=\"Root Certs\" library=\"libnssckbi.so\"";
603 #elif defined(TARGET_OS_MACOS) 789 #elif defined(TARGET_OS_MACOS)
604 "name=\"Root Certs\" library=\"libnssckbi.dylib\""; 790 "name=\"Root Certs\" library=\"libnssckbi.dylib\"";
605 #elif defined(TARGET_OS_WINDOWS) 791 #elif defined(TARGET_OS_WINDOWS)
606 "name=\"Root Certs\" library=\"nssckbi.dll\""; 792 "name=\"Root Certs\" library=\"nssckbi.dll\"";
607 #else 793 #else
608 #error Automatic target os detection failed. 794 #error Automatic target os detection failed.
609 #endif 795 #endif
610 796
611 797
612 798
613 void SSLFilter::InitializeLibrary(const char* certificate_database, 799 void SSLFilter::InitializeLibrary(const char* certificate_database,
614 const char* password, 800 const char* password,
615 bool use_builtin_root_certificates, 801 bool use_builtin_root_certificates,
802 bool read_only,
616 bool report_duplicate_initialization) { 803 bool report_duplicate_initialization) {
617 MutexLocker locker(mutex_); 804 MutexLocker locker(mutex_);
618 SECStatus status; 805 SECStatus status;
619 if (!library_initialized_) { 806 if (!library_initialized_) {
620 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); 807 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
621 // TODO(whesse): Verify there are no UTF-8 issues here. 808 // TODO(whesse): Verify there are no UTF-8 issues here.
622 if (certificate_database == NULL || certificate_database[0] == '\0') { 809 if (certificate_database == NULL || certificate_database[0] == '\0') {
623 status = NSS_NoDB_Init(NULL); 810 status = NSS_NoDB_Init(NULL);
624 if (status != SECSuccess) { 811 if (status != SECSuccess) {
625 mutex_->Unlock(); // MutexLocker destructor not called when throwing. 812 mutex_->Unlock(); // MutexLocker destructor not called when throwing.
626 ThrowPRException("TlsException", 813 ThrowPRException("TlsException",
627 "Failed NSS_NoDB_Init call."); 814 "Failed NSS_NoDB_Init call.");
628 } 815 }
629 if (use_builtin_root_certificates) { 816 if (use_builtin_root_certificates) {
630 SECMODModule* module = SECMOD_LoadUserModule( 817 SECMODModule* module = SECMOD_LoadUserModule(
631 const_cast<char*>(builtin_roots_module), NULL, PR_FALSE); 818 const_cast<char*>(builtin_roots_module), NULL, PR_FALSE);
632 if (!module) { 819 if (!module) {
633 mutex_->Unlock(); // MutexLocker destructor not called when throwing. 820 mutex_->Unlock(); // MutexLocker destructor not called when throwing.
634 ThrowPRException("TlsException", 821 ThrowPRException("TlsException",
635 "Failed to load builtin root certificates."); 822 "Failed to load builtin root certificates.");
636 } 823 }
637 } 824 }
638 } else { 825 } else {
639 PRUint32 init_flags = NSS_INIT_READONLY; 826 PRUint32 init_flags = read_only ? NSS_INIT_READONLY : 0;
640 if (!use_builtin_root_certificates) { 827 if (!use_builtin_root_certificates) {
641 init_flags |= NSS_INIT_NOMODDB; 828 init_flags |= NSS_INIT_NOMODDB;
642 } 829 }
643 status = NSS_Initialize(certificate_database, 830 status = NSS_Initialize(certificate_database,
644 "", 831 "",
645 "", 832 "",
646 SECMOD_DB, 833 SECMOD_DB,
647 init_flags); 834 init_flags);
648 if (status != SECSuccess) { 835 if (status != SECSuccess) {
649 mutex_->Unlock(); // MutexLocker destructor not called when throwing. 836 mutex_->Unlock(); // MutexLocker destructor not called when throwing.
650 ThrowPRException("TlsException", 837 ThrowPRException("TlsException",
651 "Failed NSS_Init call."); 838 "Failed NSS_Init call.");
652 } 839 }
653 password_ = strdup(password); // This one copy persists until Dart exits. 840 password_ = strdup(password); // This one copy persists until Dart exits.
654 PK11_SetPasswordFunc(PasswordCallback); 841 PK11_SetPasswordFunc(PasswordCallback);
655 } 842 }
656 library_initialized_ = true; 843 library_initialized_ = true;
657 844
658 status = NSS_SetDomesticPolicy(); 845 status = NSS_SetDomesticPolicy();
659 if (status != SECSuccess) { 846 if (status != SECSuccess) {
660 mutex_->Unlock(); // MutexLocker destructor not called when throwing. 847 mutex_->Unlock(); // MutexLocker destructor not called when throwing.
661 ThrowPRException("TlsException", 848 ThrowPRException("TlsException",
662 "Failed NSS_SetDomesticPolicy call."); 849 "Failed NSS_SetDomesticPolicy call.");
663 } 850 }
851
852 // Allow encoding and decoding of private keys in PKCS#12 files (.pk files).
853 SEC_PKCS12EnableCipher(PKCS12_RC4_40, 1);
854 SEC_PKCS12EnableCipher(PKCS12_RC4_128, 1);
855 SEC_PKCS12EnableCipher(PKCS12_RC2_CBC_40, 1);
856 SEC_PKCS12EnableCipher(PKCS12_RC2_CBC_128, 1);
857 SEC_PKCS12EnableCipher(PKCS12_DES_56, 1);
858 SEC_PKCS12EnableCipher(PKCS12_DES_EDE3_168, 1);
859 SEC_PKCS12SetPreferredCipher(PKCS12_DES_EDE3_168, 1);
860
664 // Enable TLS, as well as SSL3 and SSL2. 861 // Enable TLS, as well as SSL3 and SSL2.
665 status = SSL_OptionSetDefault(SSL_ENABLE_TLS, PR_TRUE); 862 status = SSL_OptionSetDefault(SSL_ENABLE_TLS, PR_TRUE);
666 if (status != SECSuccess) { 863 if (status != SECSuccess) {
667 mutex_->Unlock(); // MutexLocker destructor not called when throwing. 864 mutex_->Unlock(); // MutexLocker destructor not called when throwing.
668 ThrowPRException("TlsException", 865 ThrowPRException("TlsException",
669 "Failed SSL_OptionSetDefault enable TLS call."); 866 "Failed SSL_OptionSetDefault enable TLS call.");
670 } 867 }
671 status = SSL_ConfigServerSessionIDCache(0, 0, 0, NULL); 868 status = SSL_ConfigServerSessionIDCache(0, 0, 0, NULL);
672 if (status != SECSuccess) { 869 if (status != SECSuccess) {
673 mutex_->Unlock(); // MutexLocker destructor not called when throwing. 870 mutex_->Unlock(); // MutexLocker destructor not called when throwing.
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
750 certificate = CERT_FindCertByNameString(certificate_database, 947 certificate = CERT_FindCertByNameString(certificate_database,
751 const_cast<char*>(certificate_name)); 948 const_cast<char*>(certificate_name));
752 if (certificate == NULL) { 949 if (certificate == NULL) {
753 ThrowCertificateException( 950 ThrowCertificateException(
754 "Cannot find server certificate by distinguished name: %s", 951 "Cannot find server certificate by distinguished name: %s",
755 certificate_name); 952 certificate_name);
756 } 953 }
757 } else { 954 } else {
758 // Look up certificate using the nickname certificate_name. 955 // Look up certificate using the nickname certificate_name.
759 certificate = PK11_FindCertFromNickname( 956 certificate = PK11_FindCertFromNickname(
760 const_cast<char*>(certificate_name), 957 const_cast<char*>(certificate_name), GetPassword());
761 static_cast<void*>(const_cast<char*>(password_))); 958 // static_cast<void*>(const_cast<char*>(password_)));
762 if (certificate == NULL) { 959 if (certificate == NULL) {
763 ThrowCertificateException( 960 ThrowCertificateException(
764 "Cannot find server certificate by nickname: %s", 961 "Cannot find server certificate by nickname: %s",
765 certificate_name); 962 certificate_name);
766 } 963 }
767 } 964 }
768 SECKEYPrivateKey* key = PK11_FindKeyByAnyCert( 965 SECKEYPrivateKey* key = PK11_FindKeyByAnyCert(
769 certificate, 966 certificate,
770 static_cast<void*>(const_cast<char*>(password_))); 967 static_cast<void*>(const_cast<char*>(password_)));
771 if (key == NULL) { 968 if (key == NULL) {
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
1046 // Return a send port for the service port. 1243 // Return a send port for the service port.
1047 Dart_Handle send_port = Dart_NewSendPort(service_port); 1244 Dart_Handle send_port = Dart_NewSendPort(service_port);
1048 Dart_SetReturnValue(args, send_port); 1245 Dart_SetReturnValue(args, send_port);
1049 } 1246 }
1050 Dart_ExitScope(); 1247 Dart_ExitScope();
1051 } 1248 }
1052 1249
1053 1250
1054 } // namespace bin 1251 } // namespace bin
1055 } // namespace dart 1252 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698