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

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

Issue 1665433002: Adds SecurityContext.setTrustedCertificatesBytes (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « runtime/bin/io_natives.cc ('k') | runtime/bin/secure_socket_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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>
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 int error = ERR_get_error(); 369 int error = ERR_get_error();
370 Log::PrintErr("Failed: %s status %d", message, status); 370 Log::PrintErr("Failed: %s status %d", message, status);
371 char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE]; 371 char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE];
372 ERR_error_string_n(error, error_string, SSL_ERROR_MESSAGE_BUFFER_SIZE); 372 ERR_error_string_n(error, error_string, SSL_ERROR_MESSAGE_BUFFER_SIZE);
373 Log::PrintErr("ERROR: %d %s\n", error, error_string); 373 Log::PrintErr("ERROR: %d %s\n", error, error_string);
374 } 374 }
375 ThrowIOException(status, type, message); 375 ThrowIOException(status, type, message);
376 } 376 }
377 377
378 378
379 // Where the argument to the constructor is the handle for an object
380 // implementing List<int>, this class creates a scope in which a memory-backed
381 // BIO is allocated. Leaving the scope cleans up the BIO and the buffer that
382 // was used to create it.
383 //
384 // Do not make Dart_ API calls while in a MemBIOScope.
385 // Do not call Dart_PropagateError while in a MemBIOScope.
386 class MemBIOScope {
387 public:
388 explicit MemBIOScope(Dart_Handle object) {
389 if (!Dart_IsTypedData(object) && !Dart_IsList(object)) {
390 Dart_ThrowException(DartUtils::NewDartArgumentError(
391 "Argument is not a List<int>"));
392 }
393
394 uint8_t* bytes = NULL;
395 intptr_t bytes_len = 0;
396 bool is_typed_data = false;
397 if (Dart_IsTypedData(object)) {
398 is_typed_data = true;
399 Dart_TypedData_Type typ;
400 ThrowIfError(Dart_TypedDataAcquireData(
401 object,
402 &typ,
403 reinterpret_cast<void**>(&bytes),
404 &bytes_len));
405 } else {
406 ASSERT(Dart_IsList(object));
407 ThrowIfError(Dart_ListLength(object, &bytes_len));
408 bytes = Dart_ScopeAllocate(bytes_len);
409 ASSERT(bytes != NULL);
410 ThrowIfError(Dart_ListGetAsBytes(object, 0, bytes, bytes_len));
411 }
412
413 object_ = object;
414 bytes_ = bytes;
415 bytes_len_ = bytes_len_;
416 bio_ = BIO_new_mem_buf(bytes, bytes_len);
417 ASSERT(bio_ != NULL);
418 is_typed_data_ = is_typed_data;
419 }
420
421 ~MemBIOScope() {
422 ASSERT(bio_ != NULL);
423 if (is_typed_data_) {
424 BIO_free(bio_);
425 ThrowIfError(Dart_TypedDataReleaseData(object_));
426 } else {
427 BIO_free(bio_);
428 }
429 }
430
431 BIO* bio() {
432 ASSERT(bio_ != NULL);
433 return bio_;
434 }
435
436 private:
437 Dart_Handle object_;
438 uint8_t* bytes_;
439 intptr_t bytes_len_;
440 BIO* bio_;
441 bool is_typed_data_;
442
443 DISALLOW_ALLOCATION();
444 DISALLOW_COPY_AND_ASSIGN(MemBIOScope);
445 };
446
447
379 void FUNCTION_NAME(SecurityContext_UsePrivateKeyBytes)( 448 void FUNCTION_NAME(SecurityContext_UsePrivateKeyBytes)(
380 Dart_NativeArguments args) { 449 Dart_NativeArguments args) {
381 SSL_CTX* context = GetSecurityContext(args); 450 SSL_CTX* context = GetSecurityContext(args);
382 451
383 Dart_Handle key_object = ThrowIfError(Dart_GetNativeArgument(args, 1));
384 if (!Dart_IsTypedData(key_object) && !Dart_IsList(key_object)) {
385 Dart_ThrowException(DartUtils::NewDartArgumentError(
386 "keyBytes argument to SecurityContext.usePrivateKeyBytes "
387 "is not a List<int>"));
388 }
389
390 Dart_Handle password_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); 452 Dart_Handle password_object = ThrowIfError(Dart_GetNativeArgument(args, 2));
391 const char* password = NULL; 453 const char* password = NULL;
392 if (Dart_IsString(password_object)) { 454 if (Dart_IsString(password_object)) {
393 ThrowIfError(Dart_StringToCString(password_object, &password)); 455 ThrowIfError(Dart_StringToCString(password_object, &password));
394 if (strlen(password) > PEM_BUFSIZE - 1) { 456 if (strlen(password) > PEM_BUFSIZE - 1) {
395 Dart_ThrowException(DartUtils::NewDartArgumentError( 457 Dart_ThrowException(DartUtils::NewDartArgumentError(
396 "SecurityContext.usePrivateKey password length is greater than" 458 "SecurityContext.usePrivateKey password length is greater than"
397 " 1023 (PEM_BUFSIZE)")); 459 " 1023 (PEM_BUFSIZE)"));
398 } 460 }
399 } else if (Dart_IsNull(password_object)) { 461 } else if (Dart_IsNull(password_object)) {
400 password = ""; 462 password = "";
401 } else { 463 } else {
402 Dart_ThrowException(DartUtils::NewDartArgumentError( 464 Dart_ThrowException(DartUtils::NewDartArgumentError(
403 "SecurityContext.usePrivateKey password is not a String or null")); 465 "SecurityContext.usePrivateKey password is not a String or null"));
404 } 466 }
405 467
406 uint8_t* key_bytes = NULL; 468 int status;
407 intptr_t key_bytes_len = 0; 469 {
408 bool is_typed_data = false; 470 MemBIOScope bio(ThrowIfError(Dart_GetNativeArgument(args, 1)));
409 if (Dart_IsTypedData(key_object)) { 471 EVP_PKEY *key = PEM_read_bio_PrivateKey(
410 is_typed_data = true; 472 bio.bio(), NULL, PasswordCallback, const_cast<char*>(password));
411 Dart_TypedData_Type typ; 473 status = SSL_CTX_use_PrivateKey(context, key);
412 ThrowIfError(Dart_TypedDataAcquireData(
413 key_object,
414 &typ,
415 reinterpret_cast<void**>(&key_bytes),
416 &key_bytes_len));
417 } else {
418 ASSERT(Dart_IsList(key_object));
419 ThrowIfError(Dart_ListLength(key_object, &key_bytes_len));
420 key_bytes = new uint8_t[key_bytes_len];
421 Dart_Handle err =
422 Dart_ListGetAsBytes(key_object, 0, key_bytes, key_bytes_len);
423 if (Dart_IsError(err)) {
424 delete[] key_bytes;
425 Dart_PropagateError(err);
426 }
427 }
428 ASSERT(key_bytes != NULL);
429
430 BIO* bio = BIO_new_mem_buf(key_bytes, key_bytes_len);
431 EVP_PKEY *key = PEM_read_bio_PrivateKey(
432 bio, NULL, PasswordCallback, const_cast<char*>(password));
433 int status = SSL_CTX_use_PrivateKey(context, key);
434 BIO_free(bio);
435 if (is_typed_data) {
436 ThrowIfError(Dart_TypedDataReleaseData(key_object));
437 } else {
438 delete[] key_bytes;
439 } 474 }
440 475
441 // TODO(24184): Handle different expected errors here - file missing, 476 // TODO(24184): Handle different expected errors here - file missing,
442 // incorrect password, file not a PEM, and throw exceptions. 477 // incorrect password, file not a PEM, and throw exceptions.
443 // CheckStatus should also throw an exception in uncaught cases. 478 // CheckStatus should also throw an exception in uncaught cases.
444 CheckStatus(status, "TlsException", "Failure in usePrivateKeyBytes"); 479 CheckStatus(status, "TlsException", "Failure in usePrivateKeyBytes");
445 } 480 }
446 481
447 482
448 void FUNCTION_NAME(SecurityContext_SetTrustedCertificates)( 483 static int SetTrustedCertificatesBytes(SSL_CTX* context, BIO* bio) {
449 Dart_NativeArguments args) { 484 X509_STORE* store = SSL_CTX_get_cert_store(context);
450 SSL_CTX* context = GetSecurityContext(args); 485
451 Dart_Handle filename_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); 486 int status = 0;
452 const char* filename = NULL; 487 X509* cert = NULL;
453 if (Dart_IsString(filename_object)) { 488 while ((cert = PEM_read_bio_X509(bio, NULL, NULL, NULL)) != NULL) {
454 ThrowIfError(Dart_StringToCString(filename_object, &filename)); 489 status = X509_STORE_add_cert(store, cert);
455 } 490 if (status == 0) {
456 Dart_Handle directory_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); 491 X509_free(cert);
457 const char* directory = NULL; 492 return status;
458 if (Dart_IsString(directory_object)) { 493 }
459 ThrowIfError(Dart_StringToCString(directory_object, &directory));
460 } else if (Dart_IsNull(directory_object)) {
461 directory = NULL;
462 } else {
463 Dart_ThrowException(DartUtils::NewDartArgumentError(
464 "Directory argument to SecurityContext.setTrustedCertificates is not "
465 "a String or null"));
466 } 494 }
467 495
468 int status = SSL_CTX_load_verify_locations(context, filename, directory); 496 uint32_t err = ERR_peek_last_error();
469 CheckStatus( 497 if ((ERR_GET_LIB(err) == ERR_LIB_PEM) &&
470 status, "TlsException", "SSL_CTX_load_verify_locations"); 498 (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
499 // Reached the end of the buffer.
500 ERR_clear_error();
501 } else {
502 // Some real error happened.
503 status = 0;
504 }
505
506 return status;
471 } 507 }
472 508
473 509
510 void FUNCTION_NAME(SecurityContext_SetTrustedCertificatesBytes)(
511 Dart_NativeArguments args) {
512 SSL_CTX* context = GetSecurityContext(args);
513 int status;
514 {
515 MemBIOScope bio(ThrowIfError(Dart_GetNativeArgument(args, 1)));
516 status = SetTrustedCertificatesBytes(context, bio.bio());
517 }
518 CheckStatus(status,
519 "TlsException",
520 "Failure in setTrustedCertificatesBytes");
521 }
522
523
474 void FUNCTION_NAME(SecurityContext_TrustBuiltinRoots)( 524 void FUNCTION_NAME(SecurityContext_TrustBuiltinRoots)(
475 Dart_NativeArguments args) { 525 Dart_NativeArguments args) {
476 SSL_CTX* context = GetSecurityContext(args); 526 SSL_CTX* context = GetSecurityContext(args);
477 X509_STORE* store = SSL_CTX_get_cert_store(context); 527 X509_STORE* store = SSL_CTX_get_cert_store(context);
478 BIO* roots_bio = 528 BIO* roots_bio =
479 BIO_new_mem_buf(const_cast<unsigned char*>(root_certificates_pem), 529 BIO_new_mem_buf(const_cast<unsigned char*>(root_certificates_pem),
480 root_certificates_pem_length); 530 root_certificates_pem_length);
481 X509* root_cert; 531 X509* root_cert;
482 // PEM_read_bio_X509 reads PEM-encoded certificates from a bio (in our case, 532 // PEM_read_bio_X509 reads PEM-encoded certificates from a bio (in our case,
483 // backed by a memory buffer), and returns X509 objects, one by one. 533 // backed by a memory buffer), and returns X509 objects, one by one.
484 // When the end of the bio is reached, it returns null. 534 // When the end of the bio is reached, it returns null.
485 while ((root_cert = PEM_read_bio_X509(roots_bio, NULL, NULL, NULL))) { 535 while ((root_cert = PEM_read_bio_X509(roots_bio, NULL, NULL, NULL))) {
486 X509_STORE_add_cert(store, root_cert); 536 X509_STORE_add_cert(store, root_cert);
487 } 537 }
488 BIO_free(roots_bio); 538 BIO_free(roots_bio);
489 } 539 }
490 540
491 541
492 static int UseChainBytes( 542 static int UseChainBytes(SSL_CTX* context, BIO* bio) {
493 SSL_CTX* context, uint8_t* chain_bytes, intptr_t chain_bytes_len) {
494 int status = 0; 543 int status = 0;
495 BIO* bio = BIO_new_mem_buf(chain_bytes, chain_bytes_len); 544 X509* x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL);
496 if (bio == NULL) { 545 if (x509 == NULL) {
497 return 0; 546 return 0;
498 } 547 }
499 548
500 X509* x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL);
501 if (x509 == NULL) {
502 BIO_free(bio);
503 return 0;
504 }
505
506 status = SSL_CTX_use_certificate(context, x509); 549 status = SSL_CTX_use_certificate(context, x509);
507 if (ERR_peek_error() != 0) { 550 if (ERR_peek_error() != 0) {
508 // Key/certificate mismatch doesn't imply status is 0. 551 // Key/certificate mismatch doesn't imply status is 0.
509 status = 0; 552 status = 0;
510 } 553 }
511 if (status == 0) { 554 if (status == 0) {
512 X509_free(x509); 555 X509_free(x509);
513 BIO_free(bio);
514 return status; 556 return status;
515 } 557 }
516 558
517 SSL_CTX_clear_chain_certs(context); 559 SSL_CTX_clear_chain_certs(context);
518 560
519 while (true) { 561 while (true) {
520 X509* ca = PEM_read_bio_X509(bio, NULL, NULL, NULL); 562 X509* ca = PEM_read_bio_X509(bio, NULL, NULL, NULL);
521 if (ca == NULL) { 563 if (ca == NULL) {
522 break; 564 break;
523 } 565 }
524 status = SSL_CTX_add0_chain_cert(context, ca); 566 status = SSL_CTX_add0_chain_cert(context, ca);
525 if (status == 0) { 567 if (status == 0) {
526 X509_free(ca); 568 X509_free(ca);
527 X509_free(x509); 569 X509_free(x509);
528 BIO_free(bio);
529 return status; 570 return status;
530 } 571 }
531 // Note that we must not free `ca` if it was successfully added to the 572 // Note that we must not free `ca` if it was successfully added to the
532 // chain. We must free the main certificate x509, though since its reference 573 // chain. We must free the main certificate x509, though since its reference
533 // count is increased by SSL_CTX_use_certificate. 574 // count is increased by SSL_CTX_use_certificate.
534 } 575 }
535 576
536 uint32_t err = ERR_peek_last_error(); 577 uint32_t err = ERR_peek_last_error();
537 if ((ERR_GET_LIB(err) == ERR_LIB_PEM) && 578 if ((ERR_GET_LIB(err) == ERR_LIB_PEM) &&
538 (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { 579 (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
539 // Reached the end of the buffer. 580 // Reached the end of the buffer.
540 ERR_clear_error(); 581 ERR_clear_error();
541 } else { 582 } else {
542 // Some real error happened. 583 // Some real error happened.
543 status = 0; 584 status = 0;
544 } 585 }
545 586
546 X509_free(x509); 587 X509_free(x509);
547 BIO_free(bio);
548 return status; 588 return status;
549 } 589 }
550 590
551 591
552 void FUNCTION_NAME(SecurityContext_UseCertificateChainBytes)( 592 void FUNCTION_NAME(SecurityContext_UseCertificateChainBytes)(
553 Dart_NativeArguments args) { 593 Dart_NativeArguments args) {
554 SSL_CTX* context = GetSecurityContext(args); 594 SSL_CTX* context = GetSecurityContext(args);
555 595 int status;
556 Dart_Handle chain_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); 596 {
557 if (!Dart_IsTypedData(chain_object) && !Dart_IsList(chain_object)) { 597 MemBIOScope bio(ThrowIfError(Dart_GetNativeArgument(args, 1)));
558 Dart_ThrowException(DartUtils::NewDartArgumentError( 598 status = UseChainBytes(context, bio.bio());
559 "chainBytes argument to SecurityContext.useCertificateChainBytes "
560 "is not a List<int>"));
561 }
562
563 uint8_t* chain_bytes = NULL;
564 intptr_t chain_bytes_len = 0;
565 bool is_typed_data = false;
566 if (Dart_IsTypedData(chain_object)) {
567 is_typed_data = true;
568 Dart_TypedData_Type typ;
569 ThrowIfError(Dart_TypedDataAcquireData(
570 chain_object,
571 &typ,
572 reinterpret_cast<void**>(&chain_bytes),
573 &chain_bytes_len));
574 } else {
575 ASSERT(Dart_IsList(chain_object));
576 ThrowIfError(Dart_ListLength(chain_object, &chain_bytes_len));
577 chain_bytes = new uint8_t[chain_bytes_len];
578 Dart_Handle err =
579 Dart_ListGetAsBytes(chain_object, 0, chain_bytes, chain_bytes_len);
580 if (Dart_IsError(err)) {
581 delete[] chain_bytes;
582 Dart_PropagateError(err);
583 }
584 }
585 ASSERT(chain_bytes != NULL);
586
587 int status = UseChainBytes(context, chain_bytes, chain_bytes_len);
588
589 if (is_typed_data) {
590 ThrowIfError(Dart_TypedDataReleaseData(chain_object));
591 } else {
592 delete[] chain_bytes;
593 } 599 }
594 CheckStatus(status, 600 CheckStatus(status,
595 "TlsException", 601 "TlsException",
596 "Failure in useCertificateChainBytes"); 602 "Failure in useCertificateChainBytes");
597 } 603 }
598 604
599 605
600 void FUNCTION_NAME(SecurityContext_SetClientAuthorities)( 606 static STACK_OF(X509_NAME)* GetCertificateNames(BIO* bio) {
607 STACK_OF(X509_NAME)* result = sk_X509_NAME_new_null();
608 if (result == NULL) {
609 return NULL;
610 }
611
612 while (true) {
613 X509* x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
614 if (x509 == NULL) {
615 break;
616 }
617
618 X509_NAME* x509_name = X509_get_subject_name(x509);
619 if (x509_name == NULL) {
620 sk_X509_NAME_pop_free(result, X509_NAME_free);
621 X509_free(x509);
622 return NULL;
623 }
624
625 // Duplicate the name to put it on the stack.
626 x509_name = X509_NAME_dup(x509_name);
627 if (x509_name == NULL) {
628 sk_X509_NAME_pop_free(result, X509_NAME_free);
629 X509_free(x509);
630 return NULL;
631 }
632 sk_X509_NAME_push(result, x509_name);
633 X509_free(x509);
634 }
635
636 return result;
637 }
638
639
640 void FUNCTION_NAME(SecurityContext_SetClientAuthoritiesBytes)(
601 Dart_NativeArguments args) { 641 Dart_NativeArguments args) {
602 SSL_CTX* context = GetSecurityContext(args); 642 SSL_CTX* context = GetSecurityContext(args);
603 Dart_Handle filename_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); 643 STACK_OF(X509_NAME)* certificate_names;
604 const char* filename = NULL; 644
605 if (Dart_IsString(filename_object)) { 645 {
606 ThrowIfError(Dart_StringToCString(filename_object, &filename)); 646 MemBIOScope bio(ThrowIfError(Dart_GetNativeArgument(args, 1)));
607 } else { 647 certificate_names = GetCertificateNames(bio.bio());
608 Dart_ThrowException(DartUtils::NewDartArgumentError(
609 "file argument in SecurityContext.setClientAuthorities"
610 " is not a String"));
611 } 648 }
612 STACK_OF(X509_NAME)* certificate_names; 649
613 certificate_names = SSL_load_client_CA_file(filename);
614 if (certificate_names != NULL) { 650 if (certificate_names != NULL) {
615 SSL_CTX_set_client_CA_list(context, certificate_names); 651 SSL_CTX_set_client_CA_list(context, certificate_names);
616 } else { 652 } else {
617 Dart_ThrowException(DartUtils::NewDartArgumentError( 653 Dart_ThrowException(DartUtils::NewDartArgumentError(
618 "Could not load certificate names from file in SetClientAuthorities")); 654 "Could not load certificate names from file in SetClientAuthorities"));
619 } 655 }
620 } 656 }
621 657
622 658
623 void FUNCTION_NAME(SecurityContext_SetAlpnProtocols)( 659 void FUNCTION_NAME(SecurityContext_SetAlpnProtocols)(
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after
1236 } else { 1272 } else {
1237 if (SSL_LOG_DATA) Log::Print( 1273 if (SSL_LOG_DATA) Log::Print(
1238 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); 1274 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed);
1239 } 1275 }
1240 } 1276 }
1241 return bytes_processed; 1277 return bytes_processed;
1242 } 1278 }
1243 1279
1244 } // namespace bin 1280 } // namespace bin
1245 } // namespace dart 1281 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/io_natives.cc ('k') | runtime/bin/secure_socket_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698