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 #include "bin/secure_socket.h" | 5 #include "bin/secure_socket.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
271 } else { | 271 } else { |
272 Dart_ThrowException(DartUtils::NewDartArgumentError( | 272 Dart_ThrowException(DartUtils::NewDartArgumentError( |
273 "UseBuiltinRoots argument to SetCertificateDatabase is not a bool")); | 273 "UseBuiltinRoots argument to SetCertificateDatabase is not a bool")); |
274 } | 274 } |
275 | 275 |
276 SSLFilter::InitializeLibrary(certificate_database, password, builtin_roots); | 276 SSLFilter::InitializeLibrary(certificate_database, password, builtin_roots); |
277 Dart_ExitScope(); | 277 Dart_ExitScope(); |
278 } | 278 } |
279 | 279 |
280 | 280 |
281 static Dart_Handle X509FromCertificate(CERTCertificate* certificate) { | |
Bill Hesse
2013/07/29 11:22:14
This function is just moved from a later place in
| |
282 PRTime start_validity; | |
283 PRTime end_validity; | |
284 SECStatus status = | |
285 CERT_GetCertTimes(certificate, &start_validity, &end_validity); | |
286 if (status != SECSuccess) { | |
287 ThrowPRException("CertificateException", | |
288 "Cannot get validity times from certificate"); | |
289 } | |
290 int64_t start_epoch_ms = start_validity / PR_USEC_PER_MSEC; | |
291 int64_t end_epoch_ms = end_validity / PR_USEC_PER_MSEC; | |
292 Dart_Handle subject_name_object = | |
293 DartUtils::NewString(certificate->subjectName); | |
294 Dart_Handle issuer_name_object = | |
295 DartUtils::NewString(certificate->issuerName); | |
296 Dart_Handle start_epoch_ms_int = Dart_NewInteger(start_epoch_ms); | |
297 Dart_Handle end_epoch_ms_int = Dart_NewInteger(end_epoch_ms); | |
298 | |
299 Dart_Handle date_type = | |
300 DartUtils::GetDartType(DartUtils::kCoreLibURL, "DateTime"); | |
301 Dart_Handle from_milliseconds = | |
302 DartUtils::NewString("fromMillisecondsSinceEpoch"); | |
303 | |
304 Dart_Handle start_validity_date = | |
305 Dart_New(date_type, from_milliseconds, 1, &start_epoch_ms_int); | |
306 Dart_Handle end_validity_date = | |
307 Dart_New(date_type, from_milliseconds, 1, &end_epoch_ms_int); | |
308 | |
309 Dart_Handle x509_type = | |
310 DartUtils::GetDartType(DartUtils::kIOLibURL, "X509Certificate"); | |
311 Dart_Handle arguments[] = { subject_name_object, | |
312 issuer_name_object, | |
313 start_validity_date, | |
314 end_validity_date }; | |
315 return Dart_New(x509_type, Dart_Null(), 4, arguments); | |
316 } | |
317 | |
318 | |
319 void FUNCTION_NAME(SecureSocket_AddCertificate) | |
320 (Dart_NativeArguments args) { | |
321 Dart_EnterScope(); | |
322 Dart_Handle certificate_object = | |
323 ThrowIfError(Dart_GetNativeArgument(args, 0)); | |
324 Dart_Handle trust_object = | |
325 ThrowIfError(Dart_GetNativeArgument(args, 1)); | |
326 | |
327 // Check that the type is List, and get the bytes from it. | |
328 if (!Dart_IsList(certificate_object) || | |
329 !Dart_IsString(trust_object)) { | |
330 Dart_ThrowException(DartUtils::NewDartArgumentError( | |
331 "Bad argument to SecureSocket.addCertificate")); | |
332 } | |
333 | |
334 intptr_t length; | |
335 ThrowIfError(Dart_ListLength(certificate_object, &length)); | |
336 printf("certificate length: %d\n", length); | |
337 uint8_t* certificate = reinterpret_cast<uint8_t*>(malloc(length + 1)); | |
338 if (certificate == NULL) { | |
339 FATAL("Out of memory in SecureSocket.addCertificate"); | |
340 } | |
341 ThrowIfError(Dart_ListGetAsBytes( | |
342 certificate_object, 0, certificate, length)); | |
343 | |
344 const char* trust_string; | |
345 ThrowIfError(Dart_StringToCString(trust_object, | |
346 &trust_string)); | |
347 | |
348 CERTCertificate* cert = CERT_DecodeCertFromPackage( | |
349 reinterpret_cast<char*>(certificate), length); | |
350 if (cert == NULL) { | |
351 ThrowPRException("CertificateException", | |
352 "Certificate cannot be decoded"); | |
353 } else { | |
354 CERTCertTrust trust; | |
355 SECStatus status = CERT_DecodeTrustString(&trust, trust_string); | |
356 if (status != SECSuccess) { | |
357 ThrowPRException("CertificateException", | |
358 "Trust string cannot be decoded"); | |
359 } | |
360 | |
361 status = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), | |
362 cert, | |
363 &trust); | |
364 | |
365 if (status != SECSuccess) { | |
366 // Throw certificate exception printf("Can't change trust\n" | |
367 } | |
368 printf("Successfully decoded certificate\n"); | |
369 printf("SubjectName: %s\n", cert->subjectName); | |
370 printf("IssuerName: %s\n", cert->issuerName); | |
371 printf("nickname: %s\n", cert->nickname); | |
372 printf("trust: %s\n", trust_string); | |
373 printf("isperm: %d istemp: %d\n", cert->isperm, cert->istemp); | |
374 | |
375 Dart_SetReturnValue(args, X509FromCertificate(cert)); | |
376 } | |
377 Dart_ExitScope(); | |
378 return; | |
379 } | |
380 | |
381 | |
382 | |
281 void FUNCTION_NAME(SecureSocket_PeerCertificate) | 383 void FUNCTION_NAME(SecureSocket_PeerCertificate) |
282 (Dart_NativeArguments args) { | 384 (Dart_NativeArguments args) { |
283 Dart_EnterScope(); | 385 Dart_EnterScope(); |
284 Dart_SetReturnValue(args, GetFilter(args)->PeerCertificate()); | 386 Dart_SetReturnValue(args, GetFilter(args)->PeerCertificate()); |
285 Dart_ExitScope(); | 387 Dart_ExitScope(); |
286 } | 388 } |
287 | 389 |
288 | 390 |
289 void FUNCTION_NAME(SecureSocket_FilterPointer)(Dart_NativeArguments args) { | 391 void FUNCTION_NAME(SecureSocket_FilterPointer)(Dart_NativeArguments args) { |
290 Dart_EnterScope(); | 392 Dart_EnterScope(); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
421 starts[i] = start; | 523 starts[i] = start; |
422 break; | 524 break; |
423 default: | 525 default: |
424 UNREACHABLE(); | 526 UNREACHABLE(); |
425 } | 527 } |
426 } | 528 } |
427 return true; | 529 return true; |
428 } | 530 } |
429 | 531 |
430 | 532 |
431 static Dart_Handle X509FromCertificate(CERTCertificate* certificate) { | |
432 PRTime start_validity; | |
433 PRTime end_validity; | |
434 SECStatus status = | |
435 CERT_GetCertTimes(certificate, &start_validity, &end_validity); | |
436 if (status != SECSuccess) { | |
437 ThrowPRException("CertificateException", | |
438 "Cannot get validity times from certificate"); | |
439 } | |
440 int64_t start_epoch_ms = start_validity / PR_USEC_PER_MSEC; | |
441 int64_t end_epoch_ms = end_validity / PR_USEC_PER_MSEC; | |
442 Dart_Handle subject_name_object = | |
443 DartUtils::NewString(certificate->subjectName); | |
444 Dart_Handle issuer_name_object = | |
445 DartUtils::NewString(certificate->issuerName); | |
446 Dart_Handle start_epoch_ms_int = Dart_NewInteger(start_epoch_ms); | |
447 Dart_Handle end_epoch_ms_int = Dart_NewInteger(end_epoch_ms); | |
448 | |
449 Dart_Handle date_type = | |
450 DartUtils::GetDartType(DartUtils::kCoreLibURL, "DateTime"); | |
451 Dart_Handle from_milliseconds = | |
452 DartUtils::NewString("fromMillisecondsSinceEpoch"); | |
453 | |
454 Dart_Handle start_validity_date = | |
455 Dart_New(date_type, from_milliseconds, 1, &start_epoch_ms_int); | |
456 Dart_Handle end_validity_date = | |
457 Dart_New(date_type, from_milliseconds, 1, &end_epoch_ms_int); | |
458 | |
459 Dart_Handle x509_type = | |
460 DartUtils::GetDartType(DartUtils::kIOLibURL, "X509Certificate"); | |
461 Dart_Handle arguments[] = { subject_name_object, | |
462 issuer_name_object, | |
463 start_validity_date, | |
464 end_validity_date }; | |
465 return Dart_New(x509_type, Dart_Null(), 4, arguments); | |
466 } | |
467 | |
468 | |
469 void SSLFilter::Init(Dart_Handle dart_this) { | 533 void SSLFilter::Init(Dart_Handle dart_this) { |
470 if (!library_initialized_) { | 534 if (!library_initialized_) { |
471 InitializeLibrary(NULL, "", true, false); | 535 InitializeLibrary(NULL, "", true, false); |
472 } | 536 } |
473 ASSERT(string_start_ == NULL); | 537 ASSERT(string_start_ == NULL); |
474 string_start_ = Dart_NewPersistentHandle(DartUtils::NewString("start")); | 538 string_start_ = Dart_NewPersistentHandle(DartUtils::NewString("start")); |
475 ASSERT(string_start_ != NULL); | 539 ASSERT(string_start_ != NULL); |
476 ASSERT(string_length_ == NULL); | 540 ASSERT(string_length_ == NULL); |
477 string_length_ = Dart_NewPersistentHandle(DartUtils::NewString("length")); | 541 string_length_ = Dart_NewPersistentHandle(DartUtils::NewString("length")); |
478 ASSERT(string_length_ != NULL); | 542 ASSERT(string_length_ != NULL); |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
998 // Return a send port for the service port. | 1062 // Return a send port for the service port. |
999 Dart_Handle send_port = Dart_NewSendPort(service_port); | 1063 Dart_Handle send_port = Dart_NewSendPort(service_port); |
1000 Dart_SetReturnValue(args, send_port); | 1064 Dart_SetReturnValue(args, send_port); |
1001 } | 1065 } |
1002 Dart_ExitScope(); | 1066 Dart_ExitScope(); |
1003 } | 1067 } |
1004 | 1068 |
1005 | 1069 |
1006 } // namespace bin | 1070 } // namespace bin |
1007 } // namespace dart | 1071 } // namespace dart |
OLD | NEW |