Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 #ifndef RUNTIME_BIN_SECURE_SOCKET_BORINGSSL_H_ | 5 #ifndef RUNTIME_BIN_SECURE_SOCKET_FILTER_H_ |
| 6 #define RUNTIME_BIN_SECURE_SOCKET_BORINGSSL_H_ | 6 #define RUNTIME_BIN_SECURE_SOCKET_FILTER_H_ |
| 7 | |
| 8 #if !defined(RUNTIME_BIN_SECURE_SOCKET_H_) | |
| 9 #error Do not include secure_socket_boringssl.h directly. Use secure_socket.h. | |
| 10 #endif | |
| 11 | |
| 12 #include <stdio.h> | |
| 13 #include <stdlib.h> | |
| 14 #include <string.h> | |
| 15 #include <sys/types.h> | |
| 16 | 7 |
| 17 #include <openssl/bio.h> | 8 #include <openssl/bio.h> |
| 18 #include <openssl/err.h> | |
| 19 #include <openssl/ssl.h> | 9 #include <openssl/ssl.h> |
| 20 #include <openssl/x509.h> | 10 #include <openssl/x509.h> |
| 21 | 11 |
| 12 #include "platform/globals.h" | |
|
zra
2017/06/02 22:56:24
Is this header file needed?
bkonyi
2017/06/05 20:25:51
Apparently not. Removed.
| |
| 13 | |
| 22 #include "bin/builtin.h" | 14 #include "bin/builtin.h" |
| 23 #include "bin/dartutils.h" | |
| 24 #include "bin/reference_counting.h" | 15 #include "bin/reference_counting.h" |
| 25 #include "bin/socket.h" | 16 #include "bin/secure_socket.h" |
| 26 #include "bin/thread.h" | 17 #include "bin/security_context.h" |
| 27 #include "bin/utils.h" | 18 #include "platform/utils.h" |
| 28 | 19 |
| 29 namespace dart { | 20 namespace dart { |
| 30 namespace bin { | 21 namespace bin { |
| 31 | 22 |
| 32 /* These are defined in root_certificates.cc. */ | 23 /* These are defined in root_certificates.cc. */ |
| 33 extern const unsigned char* root_certificates_pem; | 24 extern const unsigned char* root_certificates_pem; |
| 34 extern unsigned int root_certificates_pem_length; | 25 extern unsigned int root_certificates_pem_length; |
| 35 | 26 |
| 36 class SSLContext { | 27 int CertificateCallback(int preverify_ok, X509_STORE_CTX* store_ctx); |
| 37 public: | |
| 38 static const intptr_t kApproximateSize; | |
| 39 | 28 |
| 40 explicit SSLContext(SSL_CTX* context) | |
| 41 : context_(context), alpn_protocol_string_(NULL) {} | |
| 42 | |
| 43 ~SSLContext() { | |
| 44 SSL_CTX_free(context_); | |
| 45 if (alpn_protocol_string_ != NULL) { | |
| 46 free(alpn_protocol_string_); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 SSL_CTX* context() const { return context_; } | |
| 51 | |
| 52 uint8_t* alpn_protocol_string() const { return alpn_protocol_string_; } | |
| 53 void set_alpn_protocol_string(uint8_t* protocol_string) { | |
| 54 if (alpn_protocol_string_ != NULL) { | |
| 55 free(alpn_protocol_string_); | |
| 56 } | |
| 57 alpn_protocol_string_ = protocol_string; | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 SSL_CTX* context_; | |
| 62 uint8_t* alpn_protocol_string_; | |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(SSLContext); | |
| 65 }; | |
| 66 | |
| 67 /* | |
| 68 * SSLFilter encapsulates the SSL(TLS) code in a filter, that communicates | |
| 69 * with the containing _SecureFilterImpl Dart object through four shared | |
| 70 * ExternalByteArray buffers, for reading and writing plaintext, and | |
| 71 * reading and writing encrypted text. The filter handles handshaking | |
| 72 * and certificate verification. | |
| 73 */ | |
| 74 class SSLFilter : public ReferenceCounted<SSLFilter> { | 29 class SSLFilter : public ReferenceCounted<SSLFilter> { |
| 75 public: | 30 public: |
| 76 // These enums must agree with those in sdk/lib/io/secure_socket.dart. | 31 // These enums must agree with those in sdk/lib/io/secure_socket.dart. |
| 77 enum BufferIndex { | 32 enum BufferIndex { |
| 78 kReadPlaintext, | 33 kReadPlaintext, |
| 79 kWritePlaintext, | 34 kWritePlaintext, |
| 80 kReadEncrypted, | 35 kReadEncrypted, |
| 81 kWriteEncrypted, | 36 kWriteEncrypted, |
| 82 kNumBuffers, | 37 kNumBuffers, |
| 83 kFirstEncrypted = kReadEncrypted | 38 kFirstEncrypted = kReadEncrypted |
| 84 }; | 39 }; |
| 85 | 40 |
| 86 static const intptr_t kApproximateSize; | 41 static const intptr_t kApproximateSize; |
| 42 static const int kSSLFilterNativeFieldIndex = 0; | |
| 43 | |
| 44 static Dart_Handle WrappedX509Certificate(X509* certificate); | |
| 87 | 45 |
| 88 SSLFilter() | 46 SSLFilter() |
| 89 : callback_error(NULL), | 47 : callback_error(NULL), |
| 90 ssl_(NULL), | 48 ssl_(NULL), |
| 91 socket_side_(NULL), | 49 socket_side_(NULL), |
| 92 string_start_(NULL), | 50 string_start_(NULL), |
| 93 string_length_(NULL), | 51 string_length_(NULL), |
| 94 handshake_complete_(NULL), | 52 handshake_complete_(NULL), |
| 95 bad_certificate_callback_(NULL), | 53 bad_certificate_callback_(NULL), |
| 96 in_handshake_(false), | 54 in_handshake_(false), |
| 97 hostname_(NULL) {} | 55 hostname_(NULL) {} |
| 98 | 56 |
| 99 ~SSLFilter(); | 57 ~SSLFilter(); |
| 100 | 58 |
| 101 Dart_Handle Init(Dart_Handle dart_this); | 59 Dart_Handle Init(Dart_Handle dart_this); |
| 102 void Connect(const char* hostname, | 60 void Connect(const char* hostname, |
| 103 SSL_CTX* context, | 61 SSLCertContext* context, |
| 104 bool is_server, | 62 bool is_server, |
| 105 bool request_client_certificate, | 63 bool request_client_certificate, |
| 106 bool require_client_certificate, | 64 bool require_client_certificate, |
| 107 Dart_Handle protocols_handle); | 65 Dart_Handle protocols_handle); |
| 108 void Destroy(); | 66 void Destroy(); |
| 109 void FreeResources(); | 67 void FreeResources(); |
| 110 void Handshake(); | 68 void Handshake(); |
| 111 void GetSelectedProtocol(Dart_NativeArguments args); | 69 void GetSelectedProtocol(Dart_NativeArguments args); |
| 112 void Renegotiate(bool use_session_cache, | 70 void Renegotiate(bool use_session_cache, |
| 113 bool request_client_certificate, | 71 bool request_client_certificate, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 126 bool in_handshake); | 84 bool in_handshake); |
| 127 Dart_Handle PeerCertificate(); | 85 Dart_Handle PeerCertificate(); |
| 128 static void InitializeLibrary(); | 86 static void InitializeLibrary(); |
| 129 Dart_Handle callback_error; | 87 Dart_Handle callback_error; |
| 130 | 88 |
| 131 static CObject* ProcessFilterRequest(const CObjectArray& request); | 89 static CObject* ProcessFilterRequest(const CObjectArray& request); |
| 132 | 90 |
| 133 // The index of the external data field in _ssl that points to the SSLFilter. | 91 // The index of the external data field in _ssl that points to the SSLFilter. |
| 134 static int filter_ssl_index; | 92 static int filter_ssl_index; |
| 135 | 93 |
| 136 // TODO(whesse): make private: | 94 private: |
| 137 SSL* ssl_; | 95 void RegisterCallbacks(SSLCertContext* cert_ctx); |
|
zra
2017/06/02 22:56:24
Can this be a public method on SSLCertContext? You
bkonyi
2017/06/05 20:25:51
Done.
| |
| 138 BIO* socket_side_; | 96 int CertificateCallback(int preverify_ok, X509_STORE_CTX* store_ctx); |
| 139 | 97 |
| 140 private: | |
| 141 static const intptr_t kInternalBIOSize; | 98 static const intptr_t kInternalBIOSize; |
| 142 static bool library_initialized_; | 99 static bool library_initialized_; |
| 143 static Mutex* mutex_; // To protect library initialization. | 100 static Mutex* mutex_; // To protect library initialization. |
| 144 | 101 |
| 102 SSL* ssl_; | |
| 103 BIO* socket_side_; | |
| 104 | |
| 145 uint8_t* buffers_[kNumBuffers]; | 105 uint8_t* buffers_[kNumBuffers]; |
| 146 int buffer_size_; | 106 int buffer_size_; |
| 147 int encrypted_buffer_size_; | 107 int encrypted_buffer_size_; |
| 148 Dart_PersistentHandle string_start_; | 108 Dart_PersistentHandle string_start_; |
| 149 Dart_PersistentHandle string_length_; | 109 Dart_PersistentHandle string_length_; |
| 150 Dart_PersistentHandle dart_buffer_objects_[kNumBuffers]; | 110 Dart_PersistentHandle dart_buffer_objects_[kNumBuffers]; |
| 151 Dart_PersistentHandle handshake_complete_; | 111 Dart_PersistentHandle handshake_complete_; |
| 152 Dart_PersistentHandle bad_certificate_callback_; | 112 Dart_PersistentHandle bad_certificate_callback_; |
| 153 bool in_handshake_; | 113 bool in_handshake_; |
| 154 bool is_server_; | 114 bool is_server_; |
| 155 char* hostname_; | 115 char* hostname_; |
| 156 | 116 |
| 157 static bool isBufferEncrypted(int i) { | 117 static bool IsBufferEncrypted(int i) { |
| 158 return static_cast<BufferIndex>(i) >= kFirstEncrypted; | 118 return static_cast<BufferIndex>(i) >= kFirstEncrypted; |
| 159 } | 119 } |
| 160 Dart_Handle InitializeBuffers(Dart_Handle dart_this); | 120 Dart_Handle InitializeBuffers(Dart_Handle dart_this); |
| 161 void InitializePlatformData(); | 121 void InitializePlatformData(); |
| 162 | 122 |
| 163 DISALLOW_COPY_AND_ASSIGN(SSLFilter); | 123 DISALLOW_COPY_AND_ASSIGN(SSLFilter); |
| 164 }; | 124 }; |
| 165 | 125 |
| 166 } // namespace bin | 126 } // namespace bin |
| 167 } // namespace dart | 127 } // namespace dart |
| 168 | 128 |
| 169 #endif // RUNTIME_BIN_SECURE_SOCKET_BORINGSSL_H_ | 129 #endif // RUNTIME_BIN_SECURE_SOCKET_FILTER_H_ |
| OLD | NEW |