Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: runtime/bin/secure_socket_macos.h

Issue 2903743002: Porting SecureSocket to use BoringSSL on OSX (Closed)
Patch Set: Fixed issues on non-Macos platforms Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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_MACOS_H_ 5 #ifndef RUNTIME_BIN_SECURE_SOCKET_MACOS_H_
6 #define RUNTIME_BIN_SECURE_SOCKET_MACOS_H_ 6 #define RUNTIME_BIN_SECURE_SOCKET_MACOS_H_
7 7
8 #if !defined(RUNTIME_BIN_SECURE_SOCKET_H_) 8 #if !defined(RUNTIME_BIN_SECURE_SOCKET_H_)
9 #error Do not include secure_socket_macos.h directly. Use secure_socket.h. 9 #error Do not include secure_socket_macos.h directly. Use secure_socket.h.
10 #endif 10 #endif
11 11
12 #include <stdlib.h> 12 #include <stdlib.h>
13 #include <string.h> 13 #include <string.h>
14 #include <stdio.h> 14 #include <stdio.h>
15 #include <sys/types.h> 15 #include <sys/types.h>
16 16
17 #include <CoreFoundation/CoreFoundation.h> 17 #include <CoreFoundation/CoreFoundation.h>
18 #include <Security/SecureTransport.h> 18 #include <Security/SecureTransport.h>
19 #include <Security/Security.h> 19 #include <Security/Security.h>
20 20
21 #include <openssl/bio.h>
22 #include <openssl/err.h>
23 #include <openssl/ssl.h>
24 #include <openssl/x509.h>
25
21 #include "bin/builtin.h" 26 #include "bin/builtin.h"
22 #include "bin/dartutils.h" 27 #include "bin/dartutils.h"
23 #include "bin/lockers.h" 28 #include "bin/lockers.h"
24 #include "bin/reference_counting.h" 29 #include "bin/reference_counting.h"
25 #include "bin/socket.h" 30 #include "bin/socket.h"
26 #include "bin/thread.h" 31 #include "bin/thread.h"
27 #include "bin/utils.h" 32 #include "bin/utils.h"
28 33
29 namespace dart { 34 namespace dart {
30 namespace bin { 35 namespace bin {
31 36
37 // Forward declaration
38 class SSLFilter;
39
32 // SSLCertContext wraps the certificates needed for a SecureTransport 40 // SSLCertContext wraps the certificates needed for a SecureTransport
33 // connection. Fields are protected by the mutex_ field, and may only be set 41 // connection. Fields are protected by the mutex_ field, and may only be set
34 // once. This is to allow access by both the Dart thread and the IOService 42 // once. This is to allow access by both the Dart thread and the IOService
35 // thread. Setters return false if the field was already set. 43 // thread. Setters return false if the field was already set.
36 class SSLCertContext : public ReferenceCounted<SSLCertContext> { 44 class SSLCertContext : public ReferenceCounted<SSLCertContext> {
37 public: 45 public:
38 SSLCertContext() 46 static const intptr_t kApproximateSize;
47 static const int kSecurityContextNativeFieldIndex = 0;
48 static const int kX509NativeFieldIndex = 0;
49
50 explicit SSLCertContext(SSL_CTX* context)
39 : ReferenceCounted(), 51 : ReferenceCounted(),
40 mutex_(new Mutex()), 52 context_(context),
41 private_key_(NULL), 53 alpn_protocol_string_(NULL),
42 keychain_(NULL),
43 cert_chain_(NULL), 54 cert_chain_(NULL),
44 trusted_certs_(NULL), 55 trusted_certs_(NULL),
45 cert_authorities_(NULL), 56 cert_authorities_(NULL),
46 trust_builtin_(false) {} 57 trust_builtin_(false) {}
47 58
48 ~SSLCertContext() { 59 ~SSLCertContext() {
49 { 60 SSL_CTX_free(context_);
50 MutexLocker m(mutex_); 61 if (alpn_protocol_string_ != NULL) {
51 if (private_key_ != NULL) { 62 free(alpn_protocol_string_);
52 CFRelease(private_key_);
53 }
54 if (keychain_ != NULL) {
55 SecKeychainDelete(keychain_);
56 CFRelease(keychain_);
57 }
58 if (cert_chain_ != NULL) {
59 CFRelease(cert_chain_);
60 }
61 if (trusted_certs_ != NULL) {
62 CFRelease(trusted_certs_);
63 }
64 if (cert_authorities_ != NULL) {
65 CFRelease(cert_authorities_);
66 }
67 } 63 }
68 delete mutex_; 64 if (cert_chain_ != NULL) {
65 CFRelease(cert_chain_);
66 }
67 if (trusted_certs_ != NULL) {
68 CFRelease(trusted_certs_);
69 }
70 if (cert_authorities_ != NULL) {
71 CFRelease(cert_authorities_);
72 }
69 } 73 }
70 74
71 SecKeyRef private_key() { 75 static SSLCertContext* GetSecurityContext(Dart_NativeArguments args);
72 MutexLocker m(mutex_); 76 static const char* GetPasswordArgument(Dart_NativeArguments args,
73 return private_key_; 77 intptr_t index);
74 } 78
75 bool set_private_key(SecKeyRef private_key) { 79 SSL_CTX* context() const { return context_; }
76 MutexLocker m(mutex_); 80
77 if (private_key_ != NULL) { 81 uint8_t* alpn_protocol_string() const { return alpn_protocol_string_; }
78 return false; 82
83 void set_alpn_protocol_string(uint8_t* protocol_string) {
84 if (alpn_protocol_string_ != NULL) {
85 free(alpn_protocol_string_);
79 } 86 }
80 private_key_ = private_key; 87 alpn_protocol_string_ = protocol_string;
81 return true;
82 } 88 }
83 89
84 SecKeychainRef keychain() { 90 CFArrayRef cert_chain() const { return cert_chain_; }
85 MutexLocker m(mutex_);
86 return keychain_;
87 }
88 bool set_keychain(SecKeychainRef keychain) {
89 MutexLocker m(mutex_);
90 if (keychain_ != NULL) {
91 return false;
92 }
93 keychain_ = keychain;
94 return true;
95 }
96 91
97 CFArrayRef cert_chain() {
98 MutexLocker m(mutex_);
99 return cert_chain_;
100 }
101 bool set_cert_chain(CFArrayRef cert_chain) { 92 bool set_cert_chain(CFArrayRef cert_chain) {
102 MutexLocker m(mutex_);
103 if (cert_chain_ != NULL) { 93 if (cert_chain_ != NULL) {
104 return false; 94 return false;
105 } 95 }
106 cert_chain_ = cert_chain; 96 cert_chain_ = cert_chain;
107 return true; 97 return true;
108 } 98 }
109 99
110 CFArrayRef trusted_certs() { 100 CFArrayRef trusted_certs() const { return trusted_certs_; }
111 MutexLocker m(mutex_); 101
112 return trusted_certs_;
113 }
114 bool set_trusted_certs(CFArrayRef trusted_certs) { 102 bool set_trusted_certs(CFArrayRef trusted_certs) {
115 MutexLocker m(mutex_);
116 if (trusted_certs_ != NULL) { 103 if (trusted_certs_ != NULL) {
117 return false; 104 return false;
118 } 105 }
119 trusted_certs_ = trusted_certs; 106 trusted_certs_ = trusted_certs;
120 return true; 107 return true;
121 } 108 }
122 109
123 CFArrayRef cert_authorities() { 110 CFArrayRef cert_authorities() const { return cert_authorities_; }
124 MutexLocker m(mutex_); 111
125 return cert_authorities_;
126 }
127 bool set_cert_authorities(CFArrayRef cert_authorities) { 112 bool set_cert_authorities(CFArrayRef cert_authorities) {
128 MutexLocker m(mutex_);
129 if (cert_authorities_ != NULL) { 113 if (cert_authorities_ != NULL) {
130 return false; 114 return false;
131 } 115 }
132 cert_authorities_ = cert_authorities; 116 cert_authorities_ = cert_authorities;
133 return true; 117 return true;
134 } 118 }
135 119
136 bool trust_builtin() { 120 bool trust_builtin() const { return trust_builtin_; }
137 MutexLocker m(mutex_); 121
138 return trust_builtin_;
139 }
140 void set_trust_builtin(bool trust_builtin) { 122 void set_trust_builtin(bool trust_builtin) {
141 MutexLocker m(mutex_);
142 trust_builtin_ = trust_builtin; 123 trust_builtin_ = trust_builtin;
143 } 124 }
144 125
126 void RegisterCallbacks(SSLFilter* filter);
127
145 private: 128 private:
146 // The context is accessed both by Dart code and the IOService. This mutex 129 SSL_CTX* context_;
147 // protects all fields. 130 uint8_t* alpn_protocol_string_;
148 Mutex* mutex_;
149
150 SecKeyRef private_key_;
151 SecKeychainRef keychain_;
152 131
153 // CFArrays of SecCertificateRef. 132 // CFArrays of SecCertificateRef.
154 CFArrayRef cert_chain_; 133 CFArrayRef cert_chain_;
155 CFArrayRef trusted_certs_; 134 CFArrayRef trusted_certs_;
156 CFArrayRef cert_authorities_; 135 CFArrayRef cert_authorities_;
157 136
158 bool trust_builtin_; 137 bool trust_builtin_;
159 138
160 DISALLOW_COPY_AND_ASSIGN(SSLCertContext); 139 DISALLOW_COPY_AND_ASSIGN(SSLCertContext);
161 }; 140 };
162 141
163 // SSLFilter encapsulates the SecureTransport code in a filter that communicates
164 // with the containing _SecureFilterImpl Dart object through four shared
165 // ExternalByteArray buffers, for reading and writing plaintext, and
166 // reading and writing encrypted text. The filter handles handshaking
167 // and certificate verification.
168 class SSLFilter : public ReferenceCounted<SSLFilter> {
169 public:
170 // These enums must agree with those in sdk/lib/io/secure_socket.dart.
171 enum BufferIndex {
172 kReadPlaintext,
173 kWritePlaintext,
174 kReadEncrypted,
175 kWriteEncrypted,
176 kNumBuffers,
177 kFirstEncrypted = kReadEncrypted
178 };
179
180 SSLFilter()
181 : ReferenceCounted(),
182 cert_context_(NULL),
183 ssl_context_(NULL),
184 peer_certs_(NULL),
185 string_start_(NULL),
186 string_length_(NULL),
187 handshake_complete_(NULL),
188 bad_certificate_callback_(NULL),
189 in_handshake_(false),
190 connected_(false),
191 bad_cert_(false),
192 is_server_(false),
193 hostname_(NULL) {}
194
195 ~SSLFilter();
196
197 // Callback called by the IOService.
198 static CObject* ProcessFilterRequest(const CObjectArray& request);
199
200 Dart_Handle Init(Dart_Handle dart_this);
201 void Connect(Dart_Handle dart_this,
202 const char* hostname,
203 SSLCertContext* context,
204 bool is_server,
205 bool request_client_certificate,
206 bool require_client_certificate);
207 void Destroy();
208 OSStatus CheckHandshake();
209 void Renegotiate(bool use_session_cache,
210 bool request_client_certificate,
211 bool require_client_certificate);
212 void RegisterHandshakeCompleteCallback(Dart_Handle handshake_complete);
213 void RegisterBadCertificateCallback(Dart_Handle callback);
214 Dart_Handle PeerCertificate();
215
216 private:
217 static OSStatus SSLReadCallback(SSLConnectionRef connection,
218 void* data,
219 size_t* data_length);
220 static OSStatus SSLWriteCallback(SSLConnectionRef connection,
221 const void* data,
222 size_t* data_length);
223
224 static bool isBufferEncrypted(intptr_t i) {
225 return static_cast<BufferIndex>(i) >= kFirstEncrypted;
226 }
227 Dart_Handle InitializeBuffers(Dart_Handle dart_this);
228
229 intptr_t GetBufferStart(intptr_t idx) const;
230 intptr_t GetBufferEnd(intptr_t idx) const;
231 void SetBufferStart(intptr_t idx, intptr_t value);
232 void SetBufferEnd(intptr_t idx, intptr_t value);
233
234 OSStatus ProcessAllBuffers(intptr_t starts[kNumBuffers],
235 intptr_t ends[kNumBuffers],
236 bool in_handshake);
237 OSStatus ProcessReadPlaintextBuffer(intptr_t start,
238 intptr_t end,
239 intptr_t* bytes_processed);
240 OSStatus ProcessWritePlaintextBuffer(intptr_t start,
241 intptr_t end,
242 intptr_t* bytes_processed);
243
244 // These calls can block on IO, and should only be invoked from
245 // from ProcessAllBuffers from ProcessFilterRequest.
246 OSStatus EvaluatePeerTrust();
247 OSStatus Handshake();
248 Dart_Handle InvokeBadCertCallback(SecCertificateRef peer_cert);
249
250 RetainedPointer<SSLCertContext> cert_context_;
251 SSLContextRef ssl_context_;
252 CFArrayRef peer_certs_;
253
254 // starts and ends filled in at the start of ProcessAllBuffers.
255 // If these are NULL, then try to get the pointers out of
256 // dart_buffer_objects_.
257 uint8_t* buffers_[kNumBuffers];
258 intptr_t* buffer_starts_[kNumBuffers];
259 intptr_t* buffer_ends_[kNumBuffers];
260 intptr_t buffer_size_;
261 intptr_t encrypted_buffer_size_;
262 Dart_PersistentHandle string_start_;
263 Dart_PersistentHandle string_length_;
264 Dart_PersistentHandle dart_buffer_objects_[kNumBuffers];
265 Dart_PersistentHandle handshake_complete_;
266 Dart_PersistentHandle bad_certificate_callback_;
267 bool in_handshake_;
268 bool connected_;
269 bool bad_cert_;
270 bool is_server_;
271 char* hostname_;
272
273 DISALLOW_COPY_AND_ASSIGN(SSLFilter);
274 };
275
276 } // namespace bin 142 } // namespace bin
277 } // namespace dart 143 } // namespace dart
278 144
279 #endif // RUNTIME_BIN_SECURE_SOCKET_MACOS_H_ 145 #endif // RUNTIME_BIN_SECURE_SOCKET_MACOS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698