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 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 filename, | 408 filename, |
409 SSL_FILETYPE_PEM); | 409 SSL_FILETYPE_PEM); |
410 // TODO(24184): Handle different expected errors here - file missing, | 410 // TODO(24184): Handle different expected errors here - file missing, |
411 // incorrect password, file not a PEM, and throw exceptions. | 411 // incorrect password, file not a PEM, and throw exceptions. |
412 // CheckStatus should also throw an exception in uncaught cases. | 412 // CheckStatus should also throw an exception in uncaught cases. |
413 CheckStatus(status, "TlsException", "Failure in usePrivateKey"); | 413 CheckStatus(status, "TlsException", "Failure in usePrivateKey"); |
414 SSL_CTX_set_default_passwd_cb_userdata(context, NULL); | 414 SSL_CTX_set_default_passwd_cb_userdata(context, NULL); |
415 } | 415 } |
416 | 416 |
417 | 417 |
| 418 void FUNCTION_NAME(SecurityContext_UsePrivateKeyBytes)( |
| 419 Dart_NativeArguments args) { |
| 420 SSL_CTX* context = GetSecurityContext(args); |
| 421 |
| 422 Dart_Handle key_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
| 423 if (!Dart_IsTypedData(key_object) && !Dart_IsList(key_object)) { |
| 424 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 425 "keyBytes argument to SecurityContext.usePrivateKey " |
| 426 "is not a List<int>")); |
| 427 } |
| 428 |
| 429 Dart_Handle password_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); |
| 430 const char* password = NULL; |
| 431 if (Dart_IsString(password_object)) { |
| 432 ThrowIfError(Dart_StringToCString(password_object, &password)); |
| 433 if (strlen(password) > PEM_BUFSIZE - 1) { |
| 434 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 435 "SecurityContext.usePrivateKey password length is greater than" |
| 436 " 1023 (PEM_BUFSIZE)")); |
| 437 } |
| 438 } else if (Dart_IsNull(password_object)) { |
| 439 password = ""; |
| 440 } else { |
| 441 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 442 "SecurityContext.usePrivateKey password is not a String or null")); |
| 443 } |
| 444 |
| 445 uint8_t* key_bytes = NULL; |
| 446 intptr_t key_bytes_len = 0; |
| 447 bool is_typed_data = false; |
| 448 if (Dart_IsTypedData(key_object)) { |
| 449 is_typed_data = true; |
| 450 Dart_TypedData_Type typ; |
| 451 ThrowIfError(Dart_TypedDataAcquireData( |
| 452 key_object, |
| 453 &typ, |
| 454 reinterpret_cast<void**>(&key_bytes), |
| 455 &key_bytes_len)); |
| 456 } else { |
| 457 ASSERT(Dart_IsList(key_object)); |
| 458 ThrowIfError(Dart_ListLength(key_object, &key_bytes_len)); |
| 459 key_bytes = new uint8_t[key_bytes_len]; |
| 460 ThrowIfError(Dart_ListGetAsBytes(key_object, 0, key_bytes, key_bytes_len)); |
| 461 } |
| 462 ASSERT(key_bytes != NULL); |
| 463 |
| 464 BIO* bio = BIO_new_mem_buf(key_bytes, key_bytes_len); |
| 465 EVP_PKEY *key = PEM_read_bio_PrivateKey( |
| 466 bio, NULL, PasswordCallback, const_cast<char*>(password)); |
| 467 int status = SSL_CTX_use_PrivateKey(context, key); |
| 468 BIO_free(bio); |
| 469 if (is_typed_data) { |
| 470 ThrowIfError(Dart_TypedDataReleaseData(key_object)); |
| 471 } else { |
| 472 delete key_bytes; |
| 473 } |
| 474 |
| 475 // TODO(24184): Handle different expected errors here - file missing, |
| 476 // incorrect password, file not a PEM, and throw exceptions. |
| 477 // CheckStatus should also throw an exception in uncaught cases. |
| 478 CheckStatus(status, "TlsException", "Failure in usePrivateKey"); |
| 479 } |
| 480 |
| 481 |
418 void FUNCTION_NAME(SecurityContext_SetTrustedCertificates)( | 482 void FUNCTION_NAME(SecurityContext_SetTrustedCertificates)( |
419 Dart_NativeArguments args) { | 483 Dart_NativeArguments args) { |
420 SSL_CTX* context = GetSecurityContext(args); | 484 SSL_CTX* context = GetSecurityContext(args); |
421 Dart_Handle filename_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); | 485 Dart_Handle filename_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
422 const char* filename = NULL; | 486 const char* filename = NULL; |
423 if (Dart_IsString(filename_object)) { | 487 if (Dart_IsString(filename_object)) { |
424 ThrowIfError(Dart_StringToCString(filename_object, &filename)); | 488 ThrowIfError(Dart_StringToCString(filename_object, &filename)); |
425 } | 489 } |
426 Dart_Handle directory_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); | 490 Dart_Handle directory_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); |
427 const char* directory = NULL; | 491 const char* directory = NULL; |
(...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1117 } else { | 1181 } else { |
1118 if (SSL_LOG_DATA) Log::Print( | 1182 if (SSL_LOG_DATA) Log::Print( |
1119 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); | 1183 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); |
1120 } | 1184 } |
1121 } | 1185 } |
1122 return bytes_processed; | 1186 return bytes_processed; |
1123 } | 1187 } |
1124 | 1188 |
1125 } // namespace bin | 1189 } // namespace bin |
1126 } // namespace dart | 1190 } // namespace dart |
OLD | NEW |