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

Side by Side Diff: runtime/bin/tls_socket.cc

Issue 10916081: Add secure sockets to dart:io (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments, remove HandshakeStartHandler. Created 8 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « runtime/bin/tls_socket.h ('k') | runtime/bin/tls_socket_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 #include "bin/tls_socket.h"
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <nss.h>
14 #include <prerror.h>
15 #include <prinit.h>
16 #include <prnetdb.h>
17 #include <ssl.h>
18
19 #include "bin/builtin.h"
20 #include "bin/dartutils.h"
21 #include "bin/net/nss_memio.h"
22 #include "bin/thread.h"
23 #include "bin/utils.h"
24 #include "platform/utils.h"
25
26 #include "include/dart_api.h"
27
28 bool TlsFilter::library_initialized_ = false;
29 dart::Mutex TlsFilter::mutex_; // To protect library initialization.
30 static const int kTlsFilterNativeFieldIndex = 0;
31
32 static TlsFilter* GetTlsFilter(Dart_NativeArguments args) {
33 TlsFilter* filter;
34 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0));
35 ASSERT(Dart_IsInstance(dart_this));
36 ThrowIfError(Dart_GetNativeInstanceField(
37 dart_this,
38 kTlsFilterNativeFieldIndex,
39 reinterpret_cast<intptr_t*>(&filter)));
40 return filter;
41 }
42
43
44 static void SetTlsFilter(Dart_NativeArguments args, TlsFilter* filter) {
45 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0));
46 ASSERT(Dart_IsInstance(dart_this));
47 ThrowIfError(Dart_SetNativeInstanceField(
48 dart_this,
49 kTlsFilterNativeFieldIndex,
50 reinterpret_cast<intptr_t>(filter)));
51 }
52
53
54 void FUNCTION_NAME(TlsSocket_Init)(Dart_NativeArguments args) {
55 Dart_EnterScope();
56 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0));
57 TlsFilter* filter = new TlsFilter;
58 SetTlsFilter(args, filter);
59 filter->Init(dart_this);
60 Dart_ExitScope();
61 }
62
63
64 void FUNCTION_NAME(TlsSocket_Connect)(Dart_NativeArguments args) {
65 Dart_EnterScope();
66 Dart_Handle host_name = ThrowIfError(Dart_GetNativeArgument(args, 1));
67 Dart_Handle port_object = ThrowIfError(Dart_GetNativeArgument(args, 2));
68
69 const char* host_name_string = NULL;
70 // TODO(whesse): Is truncating a Dart string containing \0 what we want?
71 ThrowIfError(Dart_StringToCString(host_name, &host_name_string));
72
73 int64_t port;
74 if (!DartUtils::GetInt64Value(port_object, &port) ||
75 port < 0 || port > 65535) {
76 Dart_ThrowException(DartUtils::NewDartArgumentError(
77 "Illegal port parameter in TlsSocket"));
78 }
79
80 GetTlsFilter(args)->Connect(host_name_string, static_cast<int>(port));
81 Dart_ExitScope();
82 }
83
84
85 void FUNCTION_NAME(TlsSocket_Destroy)(Dart_NativeArguments args) {
86 Dart_EnterScope();
87 TlsFilter* filter = GetTlsFilter(args);
88 SetTlsFilter(args, NULL);
89 filter->Destroy();
90 delete filter;
91 Dart_ExitScope();
92 }
93
94
95 void FUNCTION_NAME(TlsSocket_Handshake)(Dart_NativeArguments args) {
96 Dart_EnterScope();
97 GetTlsFilter(args)->Handshake();
98 Dart_ExitScope();
99 }
100
101
102 void FUNCTION_NAME(TlsSocket_RegisterHandshakeCompleteCallback)(
103 Dart_NativeArguments args) {
104 Dart_EnterScope();
105 Dart_Handle handshake_complete =
106 ThrowIfError(Dart_GetNativeArgument(args, 1));
107 if (!Dart_IsClosure(handshake_complete)) {
108 Dart_ThrowException(DartUtils::NewDartArgumentError(
109 "Illegal argument to RegisterHandshakeCompleteCallback"));
110 }
111 GetTlsFilter(args)->RegisterHandshakeCompleteCallback(handshake_complete);
112 Dart_ExitScope();
113 }
114
115
116 void FUNCTION_NAME(TlsSocket_ProcessBuffer)(Dart_NativeArguments args) {
117 Dart_EnterScope();
118 Dart_Handle buffer_id_object = ThrowIfError(Dart_GetNativeArgument(args, 1));
119 int64_t buffer_id = DartUtils::GetIntegerValue(buffer_id_object);
120 if (buffer_id < 0 || buffer_id >= TlsFilter::kNumBuffers) {
121 Dart_ThrowException(DartUtils::NewDartArgumentError(
122 "Illegal argument to ProcessBuffer"));
123 }
124
125 intptr_t bytes_read =
126 GetTlsFilter(args)->ProcessBuffer(static_cast<int>(buffer_id));
127 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read));
128 Dart_ExitScope();
129 }
130
131
132 void FUNCTION_NAME(TlsSocket_SetCertificateDatabase)
133 (Dart_NativeArguments args) {
134 Dart_EnterScope();
135 Dart_Handle dart_pkcert_dir = ThrowIfError(Dart_GetNativeArgument(args, 0));
136 // Check that the type is string, and get the UTF-8 C string value from it.
137 if (Dart_IsString(dart_pkcert_dir)) {
138 const char* pkcert_dir = NULL;
139 ThrowIfError(Dart_StringToCString(dart_pkcert_dir, &pkcert_dir));
140 TlsFilter::InitializeLibrary(pkcert_dir);
141 } else {
142 Dart_ThrowException(DartUtils::NewDartArgumentError(
143 "Non-String argument to SetCertificateDatabase"));
144 }
145 Dart_ExitScope();
146 }
147
148
149 void TlsFilter::Init(Dart_Handle dart_this) {
150 string_start_ = ThrowIfError(
151 Dart_NewPersistentHandle(DartUtils::NewString("start")));
152 string_length_ = ThrowIfError(
153 Dart_NewPersistentHandle(DartUtils::NewString("length")));
154
155 InitializeBuffers(dart_this);
156 memio_ = memio_CreateIOLayer(kMemioBufferSize);
157 }
158
159
160 void TlsFilter::InitializeBuffers(Dart_Handle dart_this) {
161 // Create TlsFilter buffers as ExternalUint8Array objects.
162 Dart_Handle dart_buffers_object = ThrowIfError(
163 Dart_GetField(dart_this, DartUtils::NewString("buffers")));
164 Dart_Handle dart_buffer_object =
165 Dart_ListGetAt(dart_buffers_object, kReadPlaintext);
166 Dart_Handle tls_external_buffer_class =
167 Dart_InstanceGetClass(dart_buffer_object);
168 Dart_Handle dart_buffer_size = ThrowIfError(
169 Dart_GetField(tls_external_buffer_class, DartUtils::NewString("SIZE")));
170 buffer_size_ = DartUtils::GetIntegerValue(dart_buffer_size);
171 if (buffer_size_ <= 0 || buffer_size_ > 1024 * 1024) {
172 Dart_ThrowException(
173 DartUtils::NewString("Invalid buffer size in _TlsExternalBuffer"));
174 }
175
176 Dart_Handle data_identifier = DartUtils::NewString("data");
177 for (int i = 0; i < kNumBuffers; ++i) {
178 dart_buffer_objects_[i] = ThrowIfError(
179 Dart_NewPersistentHandle(Dart_ListGetAt(dart_buffers_object, i)));
180 buffers_[i] = new uint8_t[buffer_size_];
181 Dart_Handle data = ThrowIfError(
182 Dart_NewExternalByteArray(buffers_[i], buffer_size_, NULL, NULL));
183 ThrowIfError(Dart_SetField(dart_buffer_objects_[i],
184 data_identifier,
185 data));
186 }
187 }
188
189
190 void TlsFilter::RegisterHandshakeCompleteCallback(Dart_Handle complete) {
191 ASSERT(NULL == handshake_complete_);
192 handshake_complete_ = ThrowIfError(Dart_NewPersistentHandle(complete));
193 }
194
195
196 void TlsFilter::InitializeLibrary(const char* pkcert_database) {
197 MutexLocker locker(&mutex_);
198 if (!library_initialized_) {
199 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
200 // TODO(whesse): Verify there are no UTF-8 issues here.
201 SECStatus status = NSS_Init(pkcert_database);
202 if (status != SECSuccess) {
203 ThrowPRException("Unsuccessful NSS_Init call.");
204 }
205
206 status = NSS_SetDomesticPolicy();
207 if (status != SECSuccess) {
208 ThrowPRException("Unsuccessful NSS_SetDomesticPolicy call.");
209 }
210 } else {
211 ThrowException("Called TlsFilter::InitializeLibrary more than once");
212 }
213 }
214
215
216 void TlsFilter::Connect(const char* host, int port) {
217 if (in_handshake_) {
218 ThrowException("Connect called while already in handshake state.");
219 }
220 PRFileDesc* my_socket = memio_;
221
222 my_socket = SSL_ImportFD(NULL, my_socket);
223 if (my_socket == NULL) {
224 ThrowPRException("Unsuccessful SSL_ImportFD call");
225 }
226
227 if (SSL_SetURL(my_socket, host) == -1) {
228 ThrowPRException("Unsuccessful SetURL call");
229 }
230
231 SECStatus status = SSL_ResetHandshake(my_socket, PR_FALSE);
232 if (status != SECSuccess) {
233 ThrowPRException("Unsuccessful SSL_ResetHandshake call");
234 }
235
236 // SetPeerAddress
237 PRNetAddr host_address;
238 char host_entry_buffer[PR_NETDB_BUF_SIZE];
239 PRHostEnt host_entry;
240 PRStatus rv = PR_GetHostByName(host, host_entry_buffer,
241 PR_NETDB_BUF_SIZE, &host_entry);
242 if (rv != PR_SUCCESS) {
243 ThrowPRException("Unsuccessful PR_GetHostByName call");
244 }
245
246 int index = PR_EnumerateHostEnt(0, &host_entry, port, &host_address);
247 if (index == -1 || index == 0) {
248 ThrowPRException("Unsuccessful PR_EnumerateHostEnt call");
249 }
250
251 memio_SetPeerName(my_socket, &host_address);
252 memio_ = my_socket;
253 }
254
255
256 void TlsFilter::Handshake() {
257 SECStatus status = SSL_ForceHandshake(memio_);
258 if (status == SECSuccess) {
259 if (in_handshake_) {
260 ThrowIfError(Dart_InvokeClosure(handshake_complete_, 0, NULL));
261 in_handshake_ = false;
262 }
263 } else {
264 PRErrorCode error = PR_GetError();
265 if (error == PR_WOULD_BLOCK_ERROR) {
266 if (!in_handshake_) {
267 in_handshake_ = true;
268 }
269 } else {
270 ThrowPRException("Unexpected handshake error");
271 }
272 }
273 }
274
275
276 void TlsFilter::Destroy() {
277 for (int i = 0; i < kNumBuffers; ++i) {
278 Dart_DeletePersistentHandle(dart_buffer_objects_[i]);
279 delete[] buffers_[i];
280 }
281 Dart_DeletePersistentHandle(string_start_);
282 Dart_DeletePersistentHandle(string_length_);
283 Dart_DeletePersistentHandle(handshake_complete_);
284 // TODO(whesse): Free NSS objects here.
285 }
286
287
288 intptr_t TlsFilter::ProcessBuffer(int buffer_index) {
289 Dart_Handle buffer_object = dart_buffer_objects_[buffer_index];
290 Dart_Handle start_object = ThrowIfError(
291 Dart_GetField(buffer_object, string_start_));
292 Dart_Handle length_object = ThrowIfError(
293 Dart_GetField(buffer_object, string_length_));
294 int64_t unsafe_start = DartUtils::GetIntegerValue(start_object);
295 int64_t unsafe_length = DartUtils::GetIntegerValue(length_object);
296 ASSERT(unsafe_start >= 0);
297 ASSERT(unsafe_start < buffer_size_);
298 ASSERT(unsafe_length >= 0);
299 ASSERT(unsafe_length <= buffer_size_);
300 intptr_t start = static_cast<intptr_t>(unsafe_start);
301 intptr_t length = static_cast<intptr_t>(unsafe_length);
302 uint8_t* buffer = buffers_[buffer_index];
303
304 int bytes_processed = 0;
305 switch (buffer_index) {
306 case kReadPlaintext: {
307 int bytes_free = buffer_size_ - start - length;
308 bytes_processed = PR_Read(memio_,
309 buffer + start + length,
310 bytes_free);
311 if (bytes_processed < 0) {
312 ASSERT(bytes_processed == -1);
313 // TODO(whesse): Handle unexpected errors here.
314 PRErrorCode pr_error = PR_GetError();
315 if (PR_WOULD_BLOCK_ERROR != pr_error) {
316 ThrowPRException("Error reading plaintext from TlsFilter");
317 }
318 bytes_processed = 0;
319 }
320 break;
321 }
322
323 case kWriteEncrypted: {
324 const uint8_t* buf1;
325 const uint8_t* buf2;
326 unsigned int len1;
327 unsigned int len2;
328 int bytes_free = buffer_size_ - start - length;
329 memio_Private* secret = memio_GetSecret(memio_);
330 memio_GetWriteParams(secret, &buf1, &len1, &buf2, &len2);
331 int bytes_to_send =
332 dart::Utils::Minimum(len1, static_cast<unsigned>(bytes_free));
333 if (bytes_to_send > 0) {
334 memmove(buffer + start + length, buf1, bytes_to_send);
335 bytes_processed = bytes_to_send;
336 }
337 bytes_to_send = dart::Utils::Minimum(len2,
338 static_cast<unsigned>(bytes_free - bytes_processed));
339 if (bytes_to_send > 0) {
340 memmove(buffer + start + length + bytes_processed, buf2,
341 bytes_to_send);
342 bytes_processed += bytes_to_send;
343 }
344 if (bytes_processed > 0) {
345 memio_PutWriteResult(secret, bytes_processed);
346 }
347 break;
348 }
349
350 case kReadEncrypted: {
351 if (length > 0) {
352 bytes_processed = length;
353 memio_Private* secret = memio_GetSecret(memio_);
354 uint8_t* memio_buf;
355 int free_bytes = memio_GetReadParams(secret, &memio_buf);
356 if (free_bytes < bytes_processed) bytes_processed = free_bytes;
357 memmove(memio_buf,
358 buffer + start,
359 bytes_processed);
360 memio_PutReadResult(secret, bytes_processed);
361 }
362 break;
363 }
364
365 case kWritePlaintext: {
366 if (length > 0) {
367 bytes_processed = PR_Write(memio_,
368 buffer + start,
369 length);
370 }
371
372 if (bytes_processed < 0) {
373 ASSERT(bytes_processed == -1);
374 // TODO(whesse): Handle unexpected errors here.
375 PRErrorCode pr_error = PR_GetError();
376 if (PR_WOULD_BLOCK_ERROR != pr_error) {
377 ThrowPRException("Error reading plaintext from TlsFilter");
378 }
379 bytes_processed = 0;
380 }
381 break;
382 }
383 }
384 return bytes_processed;
385 }
OLDNEW
« no previous file with comments | « runtime/bin/tls_socket.h ('k') | runtime/bin/tls_socket_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698