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

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

Powered by Google App Engine
This is Rietveld 408576698