| 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 #ifndef RUNTIME_BIN_SECURE_SOCKET_H_ | 5 #ifndef RUNTIME_BIN_SECURE_SOCKET_H_ |
| 6 #define RUNTIME_BIN_SECURE_SOCKET_H_ | 6 #define RUNTIME_BIN_SECURE_SOCKET_H_ |
| 7 | 7 |
| 8 #if defined(DART_IO_DISABLED) || defined(DART_IO_SECURE_SOCKET_DISABLED) | 8 #if defined(DART_IO_DISABLED) || defined(DART_IO_SECURE_SOCKET_DISABLED) |
| 9 #error "secure_socket.h can only be included on builds with SSL enabled" | 9 #error "secure_socket.h can only be included on builds with SSL enabled" |
| 10 #endif | 10 #endif |
| 11 | 11 |
| 12 #include "platform/globals.h" | 12 #include "platform/globals.h" |
| 13 |
| 13 #if defined(HOST_OS_ANDROID) || defined(HOST_OS_LINUX) || \ | 14 #if defined(HOST_OS_ANDROID) || defined(HOST_OS_LINUX) || \ |
| 14 defined(HOST_OS_WINDOWS) || defined(HOST_OS_FUCHSIA) | 15 defined(HOST_OS_WINDOWS) || defined(HOST_OS_FUCHSIA) |
| 15 #include "bin/secure_socket_boringssl.h" | 16 #include "bin/secure_socket_boringssl.h" |
| 16 #elif defined(HOST_OS_MACOS) | 17 #elif defined(HOST_OS_MACOS) |
| 17 #if HOST_OS_IOS | 18 #if HOST_OS_IOS |
| 18 #include "bin/secure_socket_ios.h" | 19 #include "bin/secure_socket_ios.h" |
| 19 #else // HOST_OS_IOS | 20 #else // HOST_OS_IOS |
| 20 #include "bin/secure_socket_macos.h" | 21 #include "bin/secure_socket_macos.h" |
| 21 #endif // HOST_OS_IOS | 22 #endif // HOST_OS_IOS |
| 22 #else | 23 #else |
| 23 #error Unknown target os. | 24 #error Unknown target os. |
| 24 #endif | 25 #endif |
| 25 | 26 |
| 27 #include <openssl/bio.h> |
| 28 #include <openssl/err.h> |
| 29 #include <openssl/ssl.h> |
| 30 #include <openssl/x509.h> |
| 31 |
| 32 namespace dart { |
| 33 namespace bin { |
| 34 |
| 35 /* These are defined in root_certificates.cc. */ |
| 36 extern const unsigned char* root_certificates_pem; |
| 37 extern unsigned int root_certificates_pem_length; |
| 38 |
| 39 const bool SSL_LOG_STATUS = false; |
| 40 const bool SSL_LOG_DATA = false; |
| 41 const bool SSL_LOG_CERTS = false; |
| 42 |
| 43 // Defined in secure_socket_{macos, ios, boringssl}.cc. |
| 44 int CertificateCallback(int preverify_ok, X509_STORE_CTX* store_ctx); |
| 45 |
| 46 class SSLFilter : public ReferenceCounted<SSLFilter> { |
| 47 public: |
| 48 // These enums must agree with those in sdk/lib/io/secure_socket.dart. |
| 49 enum BufferIndex { |
| 50 kReadPlaintext, |
| 51 kWritePlaintext, |
| 52 kReadEncrypted, |
| 53 kWriteEncrypted, |
| 54 kNumBuffers, |
| 55 kFirstEncrypted = kReadEncrypted |
| 56 }; |
| 57 |
| 58 static const intptr_t kApproximateSize; |
| 59 static const int kSSLFilterNativeFieldIndex = 0; |
| 60 |
| 61 SSLFilter() |
| 62 : callback_error(NULL), |
| 63 ssl_(NULL), |
| 64 socket_side_(NULL), |
| 65 string_start_(NULL), |
| 66 string_length_(NULL), |
| 67 handshake_complete_(NULL), |
| 68 bad_certificate_callback_(NULL), |
| 69 in_handshake_(false), |
| 70 hostname_(NULL) {} |
| 71 |
| 72 ~SSLFilter(); |
| 73 |
| 74 Dart_Handle Init(Dart_Handle dart_this); |
| 75 void Connect(const char* hostname, |
| 76 SSLCertContext* context, |
| 77 bool is_server, |
| 78 bool request_client_certificate, |
| 79 bool require_client_certificate, |
| 80 Dart_Handle protocols_handle); |
| 81 void Destroy(); |
| 82 void FreeResources(); |
| 83 void Handshake(); |
| 84 void GetSelectedProtocol(Dart_NativeArguments args); |
| 85 void Renegotiate(bool use_session_cache, |
| 86 bool request_client_certificate, |
| 87 bool require_client_certificate); |
| 88 void RegisterHandshakeCompleteCallback(Dart_Handle handshake_complete); |
| 89 void RegisterBadCertificateCallback(Dart_Handle callback); |
| 90 Dart_Handle bad_certificate_callback() { |
| 91 return Dart_HandleFromPersistent(bad_certificate_callback_); |
| 92 } |
| 93 int ProcessReadPlaintextBuffer(int start, int end); |
| 94 int ProcessWritePlaintextBuffer(int start, int end); |
| 95 int ProcessReadEncryptedBuffer(int start, int end); |
| 96 int ProcessWriteEncryptedBuffer(int start, int end); |
| 97 bool ProcessAllBuffers(int starts[kNumBuffers], |
| 98 int ends[kNumBuffers], |
| 99 bool in_handshake); |
| 100 Dart_Handle PeerCertificate(); |
| 101 static void InitializeLibrary(); |
| 102 Dart_Handle callback_error; |
| 103 |
| 104 static CObject* ProcessFilterRequest(const CObjectArray& request); |
| 105 |
| 106 // The index of the external data field in _ssl that points to the SSLFilter. |
| 107 static int filter_ssl_index; |
| 108 |
| 109 private: |
| 110 void RegisterCallbacks(SSLCertContext* cert_ctx); |
| 111 int CertificateCallback(int preverify_ok, X509_STORE_CTX* store_ctx); |
| 112 |
| 113 static const intptr_t kInternalBIOSize; |
| 114 static bool library_initialized_; |
| 115 static Mutex* mutex_; // To protect library initialization. |
| 116 |
| 117 SSL* ssl_; |
| 118 BIO* socket_side_; |
| 119 |
| 120 uint8_t* buffers_[kNumBuffers]; |
| 121 int buffer_size_; |
| 122 int encrypted_buffer_size_; |
| 123 Dart_PersistentHandle string_start_; |
| 124 Dart_PersistentHandle string_length_; |
| 125 Dart_PersistentHandle dart_buffer_objects_[kNumBuffers]; |
| 126 Dart_PersistentHandle handshake_complete_; |
| 127 Dart_PersistentHandle bad_certificate_callback_; |
| 128 bool in_handshake_; |
| 129 bool is_server_; |
| 130 char* hostname_; |
| 131 |
| 132 static bool IsBufferEncrypted(int i) { |
| 133 return static_cast<BufferIndex>(i) >= kFirstEncrypted; |
| 134 } |
| 135 Dart_Handle InitializeBuffers(Dart_Handle dart_this); |
| 136 void InitializePlatformData(); |
| 137 |
| 138 DISALLOW_COPY_AND_ASSIGN(SSLFilter); |
| 139 }; |
| 140 |
| 141 } // namespace bin |
| 142 } // namespace dart |
| 143 |
| 26 #endif // RUNTIME_BIN_SECURE_SOCKET_H_ | 144 #endif // RUNTIME_BIN_SECURE_SOCKET_H_ |
| OLD | NEW |