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) { |
| 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 = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
| 325 |
| 326 if (!Dart_IsList(certificate_object) || !Dart_IsString(trust_object)) { |
| 327 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 328 "Bad argument to SecureSocket.addCertificate")); |
| 329 } |
| 330 |
| 331 intptr_t length; |
| 332 ThrowIfError(Dart_ListLength(certificate_object, &length)); |
| 333 uint8_t* certificate = reinterpret_cast<uint8_t*>(malloc(length + 1)); |
| 334 if (certificate == NULL) { |
| 335 FATAL("Out of memory in SecureSocket.addCertificate"); |
| 336 } |
| 337 ThrowIfError(Dart_ListGetAsBytes( |
| 338 certificate_object, 0, certificate, length)); |
| 339 |
| 340 const char* trust_string; |
| 341 ThrowIfError(Dart_StringToCString(trust_object, |
| 342 &trust_string)); |
| 343 |
| 344 CERTCertificate* cert = CERT_DecodeCertFromPackage( |
| 345 reinterpret_cast<char*>(certificate), length); |
| 346 if (cert == NULL) { |
| 347 ThrowPRException("CertificateException", "Certificate cannot be decoded"); |
| 348 } |
| 349 CERTCertTrust trust; |
| 350 SECStatus status = CERT_DecodeTrustString(&trust, trust_string); |
| 351 if (status != SECSuccess) { |
| 352 ThrowPRException("CertificateException", "Trust string cannot be decoded"); |
| 353 } |
| 354 |
| 355 status = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), cert, &trust); |
| 356 if (status != SECSuccess) { |
| 357 ThrowPRException("CertificateException", "Cannot set trust attributes"); |
| 358 } |
| 359 |
| 360 Dart_SetReturnValue(args, X509FromCertificate(cert)); |
| 361 Dart_ExitScope(); |
| 362 return; |
| 363 } |
| 364 |
| 365 |
| 366 |
281 void FUNCTION_NAME(SecureSocket_PeerCertificate) | 367 void FUNCTION_NAME(SecureSocket_PeerCertificate) |
282 (Dart_NativeArguments args) { | 368 (Dart_NativeArguments args) { |
283 Dart_EnterScope(); | 369 Dart_EnterScope(); |
284 Dart_SetReturnValue(args, GetFilter(args)->PeerCertificate()); | 370 Dart_SetReturnValue(args, GetFilter(args)->PeerCertificate()); |
285 Dart_ExitScope(); | 371 Dart_ExitScope(); |
286 } | 372 } |
287 | 373 |
288 | 374 |
289 void FUNCTION_NAME(SecureSocket_FilterPointer)(Dart_NativeArguments args) { | 375 void FUNCTION_NAME(SecureSocket_FilterPointer)(Dart_NativeArguments args) { |
290 Dart_EnterScope(); | 376 Dart_EnterScope(); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 starts[i] = start; | 507 starts[i] = start; |
422 break; | 508 break; |
423 default: | 509 default: |
424 UNREACHABLE(); | 510 UNREACHABLE(); |
425 } | 511 } |
426 } | 512 } |
427 return true; | 513 return true; |
428 } | 514 } |
429 | 515 |
430 | 516 |
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) { | 517 void SSLFilter::Init(Dart_Handle dart_this) { |
470 if (!library_initialized_) { | 518 if (!library_initialized_) { |
471 InitializeLibrary(NULL, "", true, false); | 519 InitializeLibrary(NULL, "", true, false); |
472 } | 520 } |
473 ASSERT(string_start_ == NULL); | 521 ASSERT(string_start_ == NULL); |
474 string_start_ = Dart_NewPersistentHandle(DartUtils::NewString("start")); | 522 string_start_ = Dart_NewPersistentHandle(DartUtils::NewString("start")); |
475 ASSERT(string_start_ != NULL); | 523 ASSERT(string_start_ != NULL); |
476 ASSERT(string_length_ == NULL); | 524 ASSERT(string_length_ == NULL); |
477 string_length_ = Dart_NewPersistentHandle(DartUtils::NewString("length")); | 525 string_length_ = Dart_NewPersistentHandle(DartUtils::NewString("length")); |
478 ASSERT(string_length_ != NULL); | 526 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. | 1046 // Return a send port for the service port. |
999 Dart_Handle send_port = Dart_NewSendPort(service_port); | 1047 Dart_Handle send_port = Dart_NewSendPort(service_port); |
1000 Dart_SetReturnValue(args, send_port); | 1048 Dart_SetReturnValue(args, send_port); |
1001 } | 1049 } |
1002 Dart_ExitScope(); | 1050 Dart_ExitScope(); |
1003 } | 1051 } |
1004 | 1052 |
1005 | 1053 |
1006 } // namespace bin | 1054 } // namespace bin |
1007 } // namespace dart | 1055 } // namespace dart |
OLD | NEW |