| 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 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 ThrowIfError(Dart_StringToCString(filename_object, &filename)); | 426 ThrowIfError(Dart_StringToCString(filename_object, &filename)); |
| 427 } | 427 } |
| 428 Dart_Handle directory_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); | 428 Dart_Handle directory_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); |
| 429 const char* directory = NULL; | 429 const char* directory = NULL; |
| 430 if (Dart_IsString(directory_object)) { | 430 if (Dart_IsString(directory_object)) { |
| 431 ThrowIfError(Dart_StringToCString(directory_object, &directory)); | 431 ThrowIfError(Dart_StringToCString(directory_object, &directory)); |
| 432 } else if (Dart_IsNull(directory_object)) { | 432 } else if (Dart_IsNull(directory_object)) { |
| 433 directory = NULL; | 433 directory = NULL; |
| 434 } else { | 434 } else { |
| 435 Dart_ThrowException(DartUtils::NewDartArgumentError( | 435 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 436 "Directory argument to SecurityContext.usePrivateKey is not " | 436 "Directory argument to SecurityContext.setTrustedCertificates is not " |
| 437 "a String or null")); | 437 "a String or null")); |
| 438 } | 438 } |
| 439 | 439 |
| 440 int status = SSL_CTX_load_verify_locations(context, filename, directory); | 440 int status = SSL_CTX_load_verify_locations(context, filename, directory); |
| 441 CheckStatus( | 441 CheckStatus( |
| 442 status, "TlsException", "SSL_CTX_load_verify_locations"); | 442 status, "TlsException", "SSL_CTX_load_verify_locations"); |
| 443 } | 443 } |
| 444 | 444 |
| 445 | 445 |
| 446 void FUNCTION_NAME(SecurityContext_TrustBuiltinRoots)( | 446 void FUNCTION_NAME(SecurityContext_TrustBuiltinRoots)( |
| (...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 status = BIO_new_bio_pair(&ssl_side, 10000, &socket_side_, 10000); | 903 status = BIO_new_bio_pair(&ssl_side, 10000, &socket_side_, 10000); |
| 904 CheckStatus(status, "TlsException", "BIO_new_bio_pair"); | 904 CheckStatus(status, "TlsException", "BIO_new_bio_pair"); |
| 905 | 905 |
| 906 assert(context != NULL); | 906 assert(context != NULL); |
| 907 ssl_ = SSL_new(context); | 907 ssl_ = SSL_new(context); |
| 908 SSL_set_bio(ssl_, ssl_side, ssl_side); | 908 SSL_set_bio(ssl_, ssl_side, ssl_side); |
| 909 SSL_set_mode(ssl_, SSL_MODE_AUTO_RETRY); // TODO(whesse): Is this right? | 909 SSL_set_mode(ssl_, SSL_MODE_AUTO_RETRY); // TODO(whesse): Is this right? |
| 910 SSL_set_ex_data(ssl_, filter_ssl_index, this); | 910 SSL_set_ex_data(ssl_, filter_ssl_index, this); |
| 911 | 911 |
| 912 if (is_server_) { | 912 if (is_server_) { |
| 913 // Do not request a client certificate. | 913 int certificate_mode = |
| 914 // TODO(24069): Allow server to request a client certificate, when desired. | 914 request_client_certificate ? SSL_VERIFY_PEER : SSL_VERIFY_NONE; |
| 915 SSL_set_verify(ssl_, SSL_VERIFY_NONE, NULL); | 915 if (require_client_certificate) { |
| 916 certificate_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 917 } |
| 918 SSL_set_verify(ssl_, certificate_mode, NULL); |
| 916 } else { | 919 } else { |
| 917 SetAlpnProtocolList(protocols_handle, ssl_, NULL, false); | 920 SetAlpnProtocolList(protocols_handle, ssl_, NULL, false); |
| 918 status = SSL_set_tlsext_host_name(ssl_, hostname); | 921 status = SSL_set_tlsext_host_name(ssl_, hostname); |
| 919 CheckStatus(status, "TlsException", "Set SNI host name"); | 922 CheckStatus(status, "TlsException", "Set SNI host name"); |
| 920 // Sets the hostname in the certificate-checking object, so it is checked | 923 // Sets the hostname in the certificate-checking object, so it is checked |
| 921 // against the certificate presented by the server. | 924 // against the certificate presented by the server. |
| 922 X509_VERIFY_PARAM* certificate_checking_parameters = SSL_get0_param(ssl_); | 925 X509_VERIFY_PARAM* certificate_checking_parameters = SSL_get0_param(ssl_); |
| 923 hostname_ = strdup(hostname); | 926 hostname_ = strdup(hostname); |
| 924 X509_VERIFY_PARAM_set_flags(certificate_checking_parameters, | 927 X509_VERIFY_PARAM_set_flags(certificate_checking_parameters, |
| 925 X509_V_FLAG_PARTIAL_CHAIN | | 928 X509_V_FLAG_PARTIAL_CHAIN | |
| (...skipping 15 matching lines...) Expand all Loading... |
| 941 } | 944 } |
| 942 } else { | 945 } else { |
| 943 status = SSL_connect(ssl_); | 946 status = SSL_connect(ssl_); |
| 944 if (SSL_LOG_STATUS) Log::Print("SSL_connect status: %d\n", status); | 947 if (SSL_LOG_STATUS) Log::Print("SSL_connect status: %d\n", status); |
| 945 if (status != 1) { | 948 if (status != 1) { |
| 946 // TODO(whesse): expect a needs-data error here. Handle other errors. | 949 // TODO(whesse): expect a needs-data error here. Handle other errors. |
| 947 error = SSL_get_error(ssl_, status); | 950 error = SSL_get_error(ssl_, status); |
| 948 if (SSL_LOG_STATUS) Log::Print("SSL_connect error: %d\n", error); | 951 if (SSL_LOG_STATUS) Log::Print("SSL_connect error: %d\n", error); |
| 949 } | 952 } |
| 950 } | 953 } |
| 951 if (is_server_) { | |
| 952 if (request_client_certificate) { | |
| 953 // TODO(24069): Handle client certificates on server side. | |
| 954 Dart_ThrowException(DartUtils::NewDartArgumentError( | |
| 955 "requestClientCertificate not implemented.")); | |
| 956 } | |
| 957 } else { // Client. | |
| 958 if (send_client_certificate) { | |
| 959 // TODO(24070): Handle client certificates on client side. | |
| 960 Dart_ThrowException(DartUtils::NewDartArgumentError( | |
| 961 "sendClientCertificate not implemented.")); | |
| 962 } | |
| 963 } | |
| 964 Handshake(); | 954 Handshake(); |
| 965 } | 955 } |
| 966 | 956 |
| 967 | 957 |
| 968 int printErrorCallback(const char *str, size_t len, void *ctx) { | 958 int printErrorCallback(const char *str, size_t len, void *ctx) { |
| 969 Log::PrintErr("%.*s\n", static_cast<int>(len), str); | 959 Log::PrintErr("%.*s\n", static_cast<int>(len), str); |
| 970 return 1; | 960 return 1; |
| 971 } | 961 } |
| 972 | 962 |
| 973 void SSLFilter::Handshake() { | 963 void SSLFilter::Handshake() { |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1130 } else { | 1120 } else { |
| 1131 if (SSL_LOG_DATA) Log::Print( | 1121 if (SSL_LOG_DATA) Log::Print( |
| 1132 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); | 1122 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); |
| 1133 } | 1123 } |
| 1134 } | 1124 } |
| 1135 return bytes_processed; | 1125 return bytes_processed; |
| 1136 } | 1126 } |
| 1137 | 1127 |
| 1138 } // namespace bin | 1128 } // namespace bin |
| 1139 } // namespace dart | 1129 } // namespace dart |
| OLD | NEW |