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 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 ThrowIfError(Dart_TypedDataReleaseData(chain_object)); | 590 ThrowIfError(Dart_TypedDataReleaseData(chain_object)); |
591 } else { | 591 } else { |
592 delete[] chain_bytes; | 592 delete[] chain_bytes; |
593 } | 593 } |
594 CheckStatus(status, | 594 CheckStatus(status, |
595 "TlsException", | 595 "TlsException", |
596 "Failure in useCertificateChainBytes"); | 596 "Failure in useCertificateChainBytes"); |
597 } | 597 } |
598 | 598 |
599 | 599 |
600 void FUNCTION_NAME(SecurityContext_SetClientAuthorities)( | 600 static STACK_OF(X509_NAME)* GetCertificateNames( |
| 601 uint8_t* certs_bytes, intptr_t certs_bytes_len) { |
| 602 BIO* bio = BIO_new_mem_buf(certs_bytes, certs_bytes_len); |
| 603 if (bio == NULL) { |
| 604 return NULL; |
| 605 } |
| 606 |
| 607 STACK_OF(X509_NAME)* result = sk_X509_NAME_new_null(); |
| 608 if (result == NULL) { |
| 609 BIO_free(bio); |
| 610 return NULL; |
| 611 } |
| 612 |
| 613 while (true) { |
| 614 X509* x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL); |
| 615 if (x509 == NULL) { |
| 616 break; |
| 617 } |
| 618 |
| 619 X509_NAME* x509_name = X509_get_subject_name(x509); |
| 620 if (x509_name == NULL) { |
| 621 sk_X509_NAME_pop_free(result, X509_NAME_free); |
| 622 BIO_free(bio); |
| 623 X509_free(x509); |
| 624 return NULL; |
| 625 } |
| 626 |
| 627 // Duplicate the name to put it on the stack. |
| 628 x509_name = X509_NAME_dup(x509_name); |
| 629 if (x509_name == NULL) { |
| 630 sk_X509_NAME_pop_free(result, X509_NAME_free); |
| 631 BIO_free(bio); |
| 632 X509_free(x509); |
| 633 return NULL; |
| 634 } |
| 635 sk_X509_NAME_push(result, x509_name); |
| 636 X509_free(x509); |
| 637 } |
| 638 |
| 639 BIO_free(bio); |
| 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 |
604 const char* filename = NULL; | 648 Dart_Handle certs_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
605 if (Dart_IsString(filename_object)) { | 649 if (!Dart_IsTypedData(certs_object) && !Dart_IsList(certs_object)) { |
606 ThrowIfError(Dart_StringToCString(filename_object, &filename)); | 650 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 651 "authCertBytes argument to SecurityContext.setClientAuthoritiesBytes " |
| 652 "is not a List<int>")); |
| 653 } |
| 654 |
| 655 uint8_t* certs_bytes = NULL; |
| 656 intptr_t certs_bytes_len = 0; |
| 657 bool is_typed_data = false; |
| 658 if (Dart_IsTypedData(certs_object)) { |
| 659 is_typed_data = true; |
| 660 Dart_TypedData_Type typ; |
| 661 ThrowIfError(Dart_TypedDataAcquireData( |
| 662 certs_object, |
| 663 &typ, |
| 664 reinterpret_cast<void**>(&certs_bytes), |
| 665 &certs_bytes_len)); |
607 } else { | 666 } else { |
608 Dart_ThrowException(DartUtils::NewDartArgumentError( | 667 ASSERT(Dart_IsList(certs_object)); |
609 "file argument in SecurityContext.setClientAuthorities" | 668 ThrowIfError(Dart_ListLength(certs_object, &certs_bytes_len)); |
610 " is not a String")); | 669 certs_bytes = new uint8_t[certs_bytes_len]; |
| 670 Dart_Handle err = |
| 671 Dart_ListGetAsBytes(certs_object, 0, certs_bytes, certs_bytes_len); |
| 672 if (Dart_IsError(err)) { |
| 673 delete[] certs_bytes; |
| 674 Dart_PropagateError(err); |
| 675 } |
611 } | 676 } |
612 STACK_OF(X509_NAME)* certificate_names; | 677 ASSERT(certs_bytes != NULL); |
613 certificate_names = SSL_load_client_CA_file(filename); | 678 |
| 679 STACK_OF(X509_NAME)* certificate_names = |
| 680 GetCertificateNames(certs_bytes, certs_bytes_len); |
| 681 |
| 682 if (is_typed_data) { |
| 683 ThrowIfError(Dart_TypedDataReleaseData(certs_object)); |
| 684 } else { |
| 685 delete[] certs_bytes; |
| 686 } |
| 687 |
614 if (certificate_names != NULL) { | 688 if (certificate_names != NULL) { |
615 SSL_CTX_set_client_CA_list(context, certificate_names); | 689 SSL_CTX_set_client_CA_list(context, certificate_names); |
616 } else { | 690 } else { |
617 Dart_ThrowException(DartUtils::NewDartArgumentError( | 691 Dart_ThrowException(DartUtils::NewDartArgumentError( |
618 "Could not load certificate names from file in SetClientAuthorities")); | 692 "Could not load certificate names from file in SetClientAuthorities")); |
619 } | 693 } |
620 } | 694 } |
621 | 695 |
622 | 696 |
623 void FUNCTION_NAME(SecurityContext_SetAlpnProtocols)( | 697 void FUNCTION_NAME(SecurityContext_SetAlpnProtocols)( |
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1236 } else { | 1310 } else { |
1237 if (SSL_LOG_DATA) Log::Print( | 1311 if (SSL_LOG_DATA) Log::Print( |
1238 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); | 1312 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); |
1239 } | 1313 } |
1240 } | 1314 } |
1241 return bytes_processed; | 1315 return bytes_processed; |
1242 } | 1316 } |
1243 | 1317 |
1244 } // namespace bin | 1318 } // namespace bin |
1245 } // namespace dart | 1319 } // namespace dart |
OLD | NEW |