| 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | 93 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
| 94 ASSERT(Dart_IsInstance(dart_this)); | 94 ASSERT(Dart_IsInstance(dart_this)); |
| 95 ThrowIfError(Dart_GetNativeInstanceField( | 95 ThrowIfError(Dart_GetNativeInstanceField( |
| 96 dart_this, | 96 dart_this, |
| 97 kSecurityContextNativeFieldIndex, | 97 kSecurityContextNativeFieldIndex, |
| 98 reinterpret_cast<intptr_t*>(&context))); | 98 reinterpret_cast<intptr_t*>(&context))); |
| 99 return context; | 99 return context; |
| 100 } | 100 } |
| 101 | 101 |
| 102 | 102 |
| 103 static void FreeSecurityContext( |
| 104 void* isolate_data, |
| 105 Dart_WeakPersistentHandle handle, |
| 106 void* context_pointer) { |
| 107 SSL_CTX* context = static_cast<SSL_CTX*>(context_pointer); |
| 108 SSL_CTX_free(context); |
| 109 } |
| 110 |
| 111 |
| 103 static void SetSecurityContext(Dart_NativeArguments args, | 112 static void SetSecurityContext(Dart_NativeArguments args, |
| 104 SSL_CTX* context) { | 113 SSL_CTX* context) { |
| 114 const int approximate_size_of_context = 1500; |
| 105 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | 115 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
| 106 ASSERT(Dart_IsInstance(dart_this)); | 116 ASSERT(Dart_IsInstance(dart_this)); |
| 107 ThrowIfError(Dart_SetNativeInstanceField( | 117 ThrowIfError(Dart_SetNativeInstanceField( |
| 108 dart_this, | 118 dart_this, |
| 109 kSecurityContextNativeFieldIndex, | 119 kSecurityContextNativeFieldIndex, |
| 110 reinterpret_cast<intptr_t>(context))); | 120 reinterpret_cast<intptr_t>(context))); |
| 121 Dart_NewWeakPersistentHandle(dart_this, |
| 122 context, |
| 123 approximate_size_of_context, |
| 124 FreeSecurityContext); |
| 111 } | 125 } |
| 112 | 126 |
| 113 | 127 |
| 114 static X509* GetX509Certificate(Dart_NativeArguments args) { | 128 static X509* GetX509Certificate(Dart_NativeArguments args) { |
| 115 X509* certificate; | 129 X509* certificate; |
| 116 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | 130 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
| 117 ASSERT(Dart_IsInstance(dart_this)); | 131 ASSERT(Dart_IsInstance(dart_this)); |
| 118 ThrowIfError(Dart_GetNativeInstanceField( | 132 ThrowIfError(Dart_GetNativeInstanceField( |
| 119 dart_this, | 133 dart_this, |
| 120 kX509NativeFieldIndex, | 134 kX509NativeFieldIndex, |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 | 320 |
| 307 | 321 |
| 308 void FUNCTION_NAME(SecurityContext_Allocate)(Dart_NativeArguments args) { | 322 void FUNCTION_NAME(SecurityContext_Allocate)(Dart_NativeArguments args) { |
| 309 SSLFilter::InitializeLibrary(); | 323 SSLFilter::InitializeLibrary(); |
| 310 SSL_CTX* context = SSL_CTX_new(TLS_method()); | 324 SSL_CTX* context = SSL_CTX_new(TLS_method()); |
| 311 SSL_CTX_set_verify(context, SSL_VERIFY_PEER, CertificateCallback); | 325 SSL_CTX_set_verify(context, SSL_VERIFY_PEER, CertificateCallback); |
| 312 SSL_CTX_set_min_version(context, TLS1_VERSION); | 326 SSL_CTX_set_min_version(context, TLS1_VERSION); |
| 313 SSL_CTX_set_cipher_list(context, "HIGH:MEDIUM"); | 327 SSL_CTX_set_cipher_list(context, "HIGH:MEDIUM"); |
| 314 SSL_CTX_set_cipher_list_tls11(context, "HIGH:MEDIUM"); | 328 SSL_CTX_set_cipher_list_tls11(context, "HIGH:MEDIUM"); |
| 315 SetSecurityContext(args, context); | 329 SetSecurityContext(args, context); |
| 316 // TODO(whesse): Use WeakPersistentHandle to free the SSL_CTX | |
| 317 // when the object is GC'd. Also free the alpn_select_cb data pointer, | |
| 318 // if non-null (allocated in SetAlpnProtocolList). | |
| 319 } | 330 } |
| 320 | 331 |
| 321 | 332 |
| 322 int PasswordCallback(char* buf, int size, int rwflag, void* userdata) { | 333 int PasswordCallback(char* buf, int size, int rwflag, void* userdata) { |
| 323 char* password = static_cast<char*>(userdata); | 334 char* password = static_cast<char*>(userdata); |
| 324 if (static_cast<size_t>(size) < strlen(password) + 1) { | 335 if (static_cast<size_t>(size) < strlen(password) + 1) { |
| 325 Log::PrintErr("Password buffer too small.\n"); | 336 Log::PrintErr("Password buffer too small.\n"); |
| 326 exit(1); | 337 exit(1); |
| 327 // TODO(24182): Find the actual value of size passed in here, and | 338 // TODO(24182): Find the actual value of size passed in here, and |
| 328 // check for password length longer than this in the Dart function | 339 // check for password length longer than this in the Dart function |
| (...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 // Because it must be passed as a single void*, terminate | 829 // Because it must be passed as a single void*, terminate |
| 819 // the list of (length, data) strings with a length 0 string. | 830 // the list of (length, data) strings with a length 0 string. |
| 820 protocol_string_copy = | 831 protocol_string_copy = |
| 821 static_cast<uint8_t*>(malloc(protocol_string_len + 1)); | 832 static_cast<uint8_t*>(malloc(protocol_string_len + 1)); |
| 822 memmove(protocol_string_copy, protocol_string, protocol_string_len); | 833 memmove(protocol_string_copy, protocol_string, protocol_string_len); |
| 823 protocol_string_copy[protocol_string_len] = '\0'; | 834 protocol_string_copy[protocol_string_len] = '\0'; |
| 824 SSL_CTX_set_alpn_select_cb(context, AlpnCallback, protocol_string_copy); | 835 SSL_CTX_set_alpn_select_cb(context, AlpnCallback, protocol_string_copy); |
| 825 // TODO(whesse): If this function is called again, free the previous | 836 // TODO(whesse): If this function is called again, free the previous |
| 826 // protocol_string_copy. It may be better to keep this as a native | 837 // protocol_string_copy. It may be better to keep this as a native |
| 827 // field on the Dart object, since fetching it from the structure is | 838 // field on the Dart object, since fetching it from the structure is |
| 828 // not in the public api. Also free this when the context is destroyed. | 839 // not in the public api. |
| 840 // Also free protocol_string_copy when the context is destroyed, |
| 841 // in FreeSecurityContext() |
| 829 } else { | 842 } else { |
| 830 // The function makes a local copy of protocol_string, which it owns. | 843 // The function makes a local copy of protocol_string, which it owns. |
| 831 if (ssl != NULL) { | 844 if (ssl != NULL) { |
| 832 ASSERT(context == NULL); | 845 ASSERT(context == NULL); |
| 833 status = SSL_set_alpn_protos(ssl, protocol_string, protocol_string_len); | 846 status = SSL_set_alpn_protos(ssl, protocol_string, protocol_string_len); |
| 834 } else { | 847 } else { |
| 835 ASSERT(context != NULL); | 848 ASSERT(context != NULL); |
| 836 ASSERT(ssl == NULL); | 849 ASSERT(ssl == NULL); |
| 837 status = SSL_CTX_set_alpn_protos( | 850 status = SSL_CTX_set_alpn_protos( |
| 838 context, protocol_string, protocol_string_len); | 851 context, protocol_string, protocol_string_len); |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 // TODO(24070, 24069): Implement setting the client certificate parameters, | 1028 // TODO(24070, 24069): Implement setting the client certificate parameters, |
| 1016 // and triggering rehandshake. | 1029 // and triggering rehandshake. |
| 1017 } | 1030 } |
| 1018 | 1031 |
| 1019 | 1032 |
| 1020 void SSLFilter::Destroy() { | 1033 void SSLFilter::Destroy() { |
| 1021 if (ssl_ != NULL) { | 1034 if (ssl_ != NULL) { |
| 1022 SSL_free(ssl_); | 1035 SSL_free(ssl_); |
| 1023 ssl_ = NULL; | 1036 ssl_ = NULL; |
| 1024 } | 1037 } |
| 1038 if (socket_side_ != NULL) { |
| 1039 BIO_free(socket_side_); |
| 1040 socket_side_ = NULL; |
| 1041 } |
| 1025 for (int i = 0; i < kNumBuffers; ++i) { | 1042 for (int i = 0; i < kNumBuffers; ++i) { |
| 1026 Dart_DeletePersistentHandle(dart_buffer_objects_[i]); | 1043 Dart_DeletePersistentHandle(dart_buffer_objects_[i]); |
| 1027 delete[] buffers_[i]; | 1044 delete[] buffers_[i]; |
| 1028 } | 1045 } |
| 1029 Dart_DeletePersistentHandle(string_start_); | 1046 Dart_DeletePersistentHandle(string_start_); |
| 1030 Dart_DeletePersistentHandle(string_length_); | 1047 Dart_DeletePersistentHandle(string_length_); |
| 1031 Dart_DeletePersistentHandle(handshake_complete_); | 1048 Dart_DeletePersistentHandle(handshake_complete_); |
| 1032 Dart_DeletePersistentHandle(bad_certificate_callback_); | 1049 Dart_DeletePersistentHandle(bad_certificate_callback_); |
| 1033 } | 1050 } |
| 1034 | 1051 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1104 } else { | 1121 } else { |
| 1105 if (SSL_LOG_DATA) Log::Print( | 1122 if (SSL_LOG_DATA) Log::Print( |
| 1106 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); | 1123 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); |
| 1107 } | 1124 } |
| 1108 } | 1125 } |
| 1109 return bytes_processed; | 1126 return bytes_processed; |
| 1110 } | 1127 } |
| 1111 | 1128 |
| 1112 } // namespace bin | 1129 } // namespace bin |
| 1113 } // namespace dart | 1130 } // namespace dart |
| OLD | NEW |