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

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

Issue 2903743002: Porting SecureSocket to use BoringSSL on OSX (Closed)
Patch Set: General cleanup Created 3 years, 7 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_IOS_H_ 5 #ifndef RUNTIME_BIN_SECURE_SOCKET_IOS_H_
6 #define RUNTIME_BIN_SECURE_SOCKET_IOS_H_ 6 #define RUNTIME_BIN_SECURE_SOCKET_IOS_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_ios.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
34
29 namespace dart { 35 namespace dart {
30 namespace bin { 36 namespace bin {
31 37
38 // Forward declaration
39 class SSLFilter;
40
32 // SSLCertContext wraps the certificates needed for a SecureTransport 41 // SSLCertContext wraps the certificates needed for a SecureTransport
33 // connection. Fields are protected by the mutex_ field, and may only be set 42 // 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 43 // 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. 44 // thread. Setters return false if the field was already set.
36 class SSLCertContext : public ReferenceCounted<SSLCertContext> { 45 class SSLCertContext : public ReferenceCounted<SSLCertContext> {
37 public: 46 public:
38 SSLCertContext() 47 static const intptr_t kApproximateSize;
48
49 explicit SSLCertContext(SSL_CTX* context)
39 : ReferenceCounted(), 50 : ReferenceCounted(),
40 mutex_(new Mutex()), 51 context_(context),
52 alpn_protocol_string_(NULL),
53 cert_chain_(NULL),
41 trusted_certs_(NULL), 54 trusted_certs_(NULL),
42 identity_(NULL), 55 cert_authorities_(NULL),
43 cert_chain_(NULL),
44 trust_builtin_(false) {} 56 trust_builtin_(false) {}
45 57
46 ~SSLCertContext() { 58 ~SSLCertContext() {
47 { 59 SSL_CTX_free(context_);
48 MutexLocker m(mutex_); 60 if (alpn_protocol_string_ != NULL) {
49 if (trusted_certs_ != NULL) { 61 free(alpn_protocol_string_);
50 CFRelease(trusted_certs_);
51 }
52 if (identity_ != NULL) {
53 CFRelease(identity_);
54 }
55 if (cert_chain_ != NULL) {
56 CFRelease(cert_chain_);
57 }
58 } 62 }
59 delete mutex_; 63 if (cert_chain_ != NULL) {
64 CFRelease(cert_chain_);
65 }
66 if (trusted_certs_ != NULL) {
67 CFRelease(trusted_certs_);
68 }
69 if (cert_authorities_ != NULL) {
70 CFRelease(cert_authorities_);
71 }
60 } 72 }
61 73
62 CFMutableArrayRef trusted_certs() { 74 static SSLCertContext* GetSecurityContext(Dart_NativeArguments args);
63 MutexLocker m(mutex_); 75 static const char* GetPasswordArgument(Dart_NativeArguments args,
64 return trusted_certs_; 76 intptr_t index);
65 } 77
66 void add_trusted_cert(SecCertificateRef trusted_cert) { 78 SSL_CTX* context() const { return context_; }
67 // Takes ownership of trusted_cert. 79
68 MutexLocker m(mutex_); 80 uint8_t* alpn_protocol_string() const { return alpn_protocol_string_; }
69 if (trusted_certs_ == NULL) { 81 void set_alpn_protocol_string(uint8_t* protocol_string) {
70 trusted_certs_ = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); 82 if (alpn_protocol_string_ != NULL) {
83 free(alpn_protocol_string_);
71 } 84 }
72 CFArrayAppendValue(trusted_certs_, trusted_cert); 85 alpn_protocol_string_ = protocol_string;
73 CFRelease(trusted_cert); // trusted_cert is retained by the array.
74 } 86 }
75 87
76 SecIdentityRef identity() { 88 CFArrayRef cert_chain() { return cert_chain_; }
zra 2017/05/26 18:11:12 const
bkonyi 2017/05/26 23:35:30 Done.
77 MutexLocker m(mutex_); 89
78 return identity_; 90 bool set_cert_chain(CFArrayRef cert_chain) {
79 } 91 if (cert_chain_ != NULL) {
80 bool set_identity(SecIdentityRef identity) { 92 return false;
81 MutexLocker m(mutex_);
82 if (identity_ == NULL) {
83 identity_ = identity;
84 return true;
85 } 93 }
86 return false; 94 cert_chain_ = cert_chain;
95 return true;
87 } 96 }
88 97
89 CFArrayRef cert_chain() { 98 CFArrayRef trusted_certs() { return trusted_certs_; }
zra 2017/05/26 18:11:13 const
bkonyi 2017/05/26 23:35:30 Done.
90 MutexLocker m(mutex_); 99
91 return cert_chain_; 100 bool set_trusted_certs(CFArrayRef trusted_certs) {
101 if (trusted_certs_ != NULL) {
102 return false;
103 }
104 trusted_certs_ = trusted_certs;
105 return true;
92 } 106 }
93 bool set_cert_chain(CFArrayRef cert_chain) { 107
94 MutexLocker m(mutex_); 108 CFArrayRef cert_authorities() { return cert_authorities_; }
zra 2017/05/26 18:11:13 const
bkonyi 2017/05/26 23:35:30 Done.
95 if (cert_chain_ == NULL) { 109
96 cert_chain_ = cert_chain; 110 bool set_cert_authorities(CFArrayRef cert_authorities) {
97 return true; 111 if (cert_authorities_ != NULL) {
112 return false;
98 } 113 }
99 return false; 114 cert_authorities_ = cert_authorities;
115 return true;
100 } 116 }
101 117
102 bool trust_builtin() { 118 bool trust_builtin() {
zra 2017/05/26 18:11:13 const
bkonyi 2017/05/26 23:35:30 Done.
103 MutexLocker m(mutex_);
104 return trust_builtin_; 119 return trust_builtin_;
105 } 120 }
106 void set_trust_builtin(bool trust_builtin) { 121 void set_trust_builtin(bool trust_builtin) {
107 MutexLocker m(mutex_);
108 trust_builtin_ = trust_builtin; 122 trust_builtin_ = trust_builtin;
109 } 123 }
110 124
125 void RegisterCallbacks(SSLFilter* filter);
126
111 private: 127 private:
112 // The context is accessed both by Dart code and the IOService. This mutex 128 SSL_CTX* context_;
113 // protects all fields. 129 uint8_t* alpn_protocol_string_;
114 Mutex* mutex_; 130
115 CFMutableArrayRef trusted_certs_; 131 // CFArrays of SecCertificateRef.
116 SecIdentityRef identity_;
117 CFArrayRef cert_chain_; 132 CFArrayRef cert_chain_;
133 CFArrayRef trusted_certs_;
134 CFArrayRef cert_authorities_;
135
118 bool trust_builtin_; 136 bool trust_builtin_;
119 137
120 DISALLOW_COPY_AND_ASSIGN(SSLCertContext); 138 DISALLOW_COPY_AND_ASSIGN(SSLCertContext);
121 }; 139 };
122 140
123 // SSLFilter encapsulates the SecureTransport code in a filter that communicates
124 // with the containing _SecureFilterImpl Dart object through four shared
125 // ExternalByteArray buffers, for reading and writing plaintext, and
126 // reading and writing encrypted text. The filter handles handshaking
127 // and certificate verification.
128 class SSLFilter : public ReferenceCounted<SSLFilter> {
129 public:
130 // These enums must agree with those in sdk/lib/io/secure_socket.dart.
131 enum BufferIndex {
132 kReadPlaintext,
133 kWritePlaintext,
134 kReadEncrypted,
135 kWriteEncrypted,
136 kNumBuffers,
137 kFirstEncrypted = kReadEncrypted
138 };
139
140 SSLFilter()
141 : ReferenceCounted(),
142 cert_context_(NULL),
143 ssl_context_(NULL),
144 peer_certs_(NULL),
145 string_start_(NULL),
146 string_length_(NULL),
147 handshake_complete_(NULL),
148 bad_certificate_callback_(NULL),
149 in_handshake_(false),
150 connected_(false),
151 bad_cert_(false),
152 is_server_(false),
153 hostname_(NULL) {}
154
155 ~SSLFilter();
156
157 // Callback called by the IOService.
158 static CObject* ProcessFilterRequest(const CObjectArray& request);
159
160 Dart_Handle Init(Dart_Handle dart_this);
161 void Connect(Dart_Handle dart_this,
162 const char* hostname,
163 SSLCertContext* context,
164 bool is_server,
165 bool request_client_certificate,
166 bool require_client_certificate);
167 void Destroy();
168 OSStatus CheckHandshake();
169 void Renegotiate(bool use_session_cache,
170 bool request_client_certificate,
171 bool require_client_certificate);
172 void RegisterHandshakeCompleteCallback(Dart_Handle handshake_complete);
173 void RegisterBadCertificateCallback(Dart_Handle callback);
174 Dart_Handle PeerCertificate();
175
176 private:
177 static OSStatus SSLReadCallback(SSLConnectionRef connection,
178 void* data,
179 size_t* data_length);
180 static OSStatus SSLWriteCallback(SSLConnectionRef connection,
181 const void* data,
182 size_t* data_length);
183
184 static bool isBufferEncrypted(intptr_t i) {
185 return static_cast<BufferIndex>(i) >= kFirstEncrypted;
186 }
187 Dart_Handle InitializeBuffers(Dart_Handle dart_this);
188
189 intptr_t GetBufferStart(intptr_t idx) const;
190 intptr_t GetBufferEnd(intptr_t idx) const;
191 void SetBufferStart(intptr_t idx, intptr_t value);
192 void SetBufferEnd(intptr_t idx, intptr_t value);
193
194 OSStatus ProcessAllBuffers(intptr_t starts[kNumBuffers],
195 intptr_t ends[kNumBuffers],
196 bool in_handshake);
197 OSStatus ProcessReadPlaintextBuffer(intptr_t start,
198 intptr_t end,
199 intptr_t* bytes_processed);
200 OSStatus ProcessWritePlaintextBuffer(intptr_t start,
201 intptr_t end,
202 intptr_t* bytes_processed);
203
204 // These calls can block on IO, and should only be invoked from
205 // from ProcessAllBuffers from ProcessFilterRequest.
206 OSStatus EvaluatePeerTrust();
207 OSStatus Handshake();
208 Dart_Handle InvokeBadCertCallback(SecCertificateRef peer_cert);
209
210 RetainedPointer<SSLCertContext> cert_context_;
211 SSLContextRef ssl_context_;
212 CFArrayRef peer_certs_;
213
214 // starts and ends filled in at the start of ProcessAllBuffers.
215 // If these are NULL, then try to get the pointers out of
216 // dart_buffer_objects_.
217 uint8_t* buffers_[kNumBuffers];
218 intptr_t* buffer_starts_[kNumBuffers];
219 intptr_t* buffer_ends_[kNumBuffers];
220 intptr_t buffer_size_;
221 intptr_t encrypted_buffer_size_;
222 Dart_PersistentHandle string_start_;
223 Dart_PersistentHandle string_length_;
224 Dart_PersistentHandle dart_buffer_objects_[kNumBuffers];
225 Dart_PersistentHandle handshake_complete_;
226 Dart_PersistentHandle bad_certificate_callback_;
227 bool in_handshake_;
228 bool connected_;
229 bool bad_cert_;
230 bool is_server_;
231 char* hostname_;
232
233 DISALLOW_COPY_AND_ASSIGN(SSLFilter);
234 };
235
236 } // namespace bin 141 } // namespace bin
237 } // namespace dart 142 } // namespace dart
238 143
239 #endif // RUNTIME_BIN_SECURE_SOCKET_IOS_H_ 144 #endif // RUNTIME_BIN_SECURE_SOCKET_IOS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698