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

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

Issue 1616073004: Adds SecurityContext.usePrivateKeyBytes (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comments Created 4 years, 10 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
« no previous file with comments | « runtime/bin/io_natives.cc ('k') | runtime/bin/secure_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
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 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 int error = ERR_get_error(); 369 int error = ERR_get_error();
370 Log::PrintErr("Failed: %s status %d", message, status); 370 Log::PrintErr("Failed: %s status %d", message, status);
371 char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE]; 371 char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE];
372 ERR_error_string_n(error, error_string, SSL_ERROR_MESSAGE_BUFFER_SIZE); 372 ERR_error_string_n(error, error_string, SSL_ERROR_MESSAGE_BUFFER_SIZE);
373 Log::PrintErr("ERROR: %d %s\n", error, error_string); 373 Log::PrintErr("ERROR: %d %s\n", error, error_string);
374 } 374 }
375 ThrowIOException(status, type, message); 375 ThrowIOException(status, type, message);
376 } 376 }
377 377
378 378
379 void FUNCTION_NAME(SecurityContext_UsePrivateKey)(Dart_NativeArguments args) { 379 void FUNCTION_NAME(SecurityContext_UsePrivateKeyAsBytes)(
380 Dart_NativeArguments args) {
380 SSL_CTX* context = GetSecurityContext(args); 381 SSL_CTX* context = GetSecurityContext(args);
381 Dart_Handle filename_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); 382
382 const char* filename = NULL; 383 Dart_Handle key_object = ThrowIfError(Dart_GetNativeArgument(args, 1));
383 if (Dart_IsString(filename_object)) { 384 if (!Dart_IsTypedData(key_object) && !Dart_IsList(key_object)) {
384 ThrowIfError(Dart_StringToCString(filename_object, &filename));
385 } else {
386 Dart_ThrowException(DartUtils::NewDartArgumentError( 385 Dart_ThrowException(DartUtils::NewDartArgumentError(
387 "File argument to SecurityContext.usePrivateKey is not a String")); 386 "keyBytes argument to SecurityContext.usePrivateKey "
387 "is not a List<int>"));
388 } 388 }
389
389 Dart_Handle password_object = ThrowIfError(Dart_GetNativeArgument(args, 2)); 390 Dart_Handle password_object = ThrowIfError(Dart_GetNativeArgument(args, 2));
390 const char* password = NULL; 391 const char* password = NULL;
391 if (Dart_IsString(password_object)) { 392 if (Dart_IsString(password_object)) {
392 ThrowIfError(Dart_StringToCString(password_object, &password)); 393 ThrowIfError(Dart_StringToCString(password_object, &password));
393 if (strlen(password) > PEM_BUFSIZE - 1) { 394 if (strlen(password) > PEM_BUFSIZE - 1) {
394 Dart_ThrowException(DartUtils::NewDartArgumentError( 395 Dart_ThrowException(DartUtils::NewDartArgumentError(
395 "SecurityContext.usePrivateKey password length is greater than" 396 "SecurityContext.usePrivateKey password length is greater than"
396 " 1023 (PEM_BUFSIZE)")); 397 " 1023 (PEM_BUFSIZE)"));
397 } 398 }
398 } else if (Dart_IsNull(password_object)) { 399 } else if (Dart_IsNull(password_object)) {
399 password = ""; 400 password = "";
400 } else { 401 } else {
401 Dart_ThrowException(DartUtils::NewDartArgumentError( 402 Dart_ThrowException(DartUtils::NewDartArgumentError(
402 "SecurityContext.usePrivateKey password is not a String or null")); 403 "SecurityContext.usePrivateKey password is not a String or null"));
403 } 404 }
404 405
405 SSL_CTX_set_default_passwd_cb(context, PasswordCallback); 406 uint8_t* key_bytes = NULL;
406 SSL_CTX_set_default_passwd_cb_userdata(context, const_cast<char*>(password)); 407 intptr_t key_bytes_len = 0;
407 int status = SSL_CTX_use_PrivateKey_file(context, 408 bool is_typed_data = false;
408 filename, 409 if (Dart_IsTypedData(key_object)) {
409 SSL_FILETYPE_PEM); 410 is_typed_data = true;
411 Dart_TypedData_Type typ;
412 ThrowIfError(Dart_TypedDataAcquireData(
413 key_object,
414 &typ,
415 reinterpret_cast<void**>(&key_bytes),
416 &key_bytes_len));
417 } else {
418 ASSERT(Dart_IsList(key_object));
419 ThrowIfError(Dart_ListLength(key_object, &key_bytes_len));
420 key_bytes = new uint8_t[key_bytes_len];
421 Dart_Handle err =
422 Dart_ListGetAsBytes(key_object, 0, key_bytes, key_bytes_len);
423 if (Dart_IsError(err)) {
424 delete[] key_bytes;
425 Dart_PropagateError(err);
426 }
427 }
428 ASSERT(key_bytes != NULL);
429
430 BIO* bio = BIO_new_mem_buf(key_bytes, key_bytes_len);
431 EVP_PKEY *key = PEM_read_bio_PrivateKey(
432 bio, NULL, PasswordCallback, const_cast<char*>(password));
433 int status = SSL_CTX_use_PrivateKey(context, key);
434 BIO_free(bio);
435 if (is_typed_data) {
436 ThrowIfError(Dart_TypedDataReleaseData(key_object));
437 } else {
438 delete[] key_bytes;
439 }
440
410 // TODO(24184): Handle different expected errors here - file missing, 441 // TODO(24184): Handle different expected errors here - file missing,
411 // incorrect password, file not a PEM, and throw exceptions. 442 // incorrect password, file not a PEM, and throw exceptions.
412 // CheckStatus should also throw an exception in uncaught cases. 443 // CheckStatus should also throw an exception in uncaught cases.
413 CheckStatus(status, "TlsException", "Failure in usePrivateKey"); 444 CheckStatus(status, "TlsException", "Failure in usePrivateKey");
414 SSL_CTX_set_default_passwd_cb_userdata(context, NULL);
415 } 445 }
416 446
417 447
418 void FUNCTION_NAME(SecurityContext_SetTrustedCertificates)( 448 void FUNCTION_NAME(SecurityContext_SetTrustedCertificates)(
419 Dart_NativeArguments args) { 449 Dart_NativeArguments args) {
420 SSL_CTX* context = GetSecurityContext(args); 450 SSL_CTX* context = GetSecurityContext(args);
421 Dart_Handle filename_object = ThrowIfError(Dart_GetNativeArgument(args, 1)); 451 Dart_Handle filename_object = ThrowIfError(Dart_GetNativeArgument(args, 1));
422 const char* filename = NULL; 452 const char* filename = NULL;
423 if (Dart_IsString(filename_object)) { 453 if (Dart_IsString(filename_object)) {
424 ThrowIfError(Dart_StringToCString(filename_object, &filename)); 454 ThrowIfError(Dart_StringToCString(filename_object, &filename));
(...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after
1117 } else { 1147 } else {
1118 if (SSL_LOG_DATA) Log::Print( 1148 if (SSL_LOG_DATA) Log::Print(
1119 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed); 1149 "WriteEncrypted BIO_read wrote %d bytes\n", bytes_processed);
1120 } 1150 }
1121 } 1151 }
1122 return bytes_processed; 1152 return bytes_processed;
1123 } 1153 }
1124 1154
1125 } // namespace bin 1155 } // namespace bin
1126 } // namespace dart 1156 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/io_natives.cc ('k') | runtime/bin/secure_socket_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698