| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2017, 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 #if !defined(DART_IO_DISABLED) && !defined(DART_IO_SECURE_SOCKET_DISABLED) |
| 6 |
| 7 #include "bin/security_context.h" |
| 8 |
| 9 #include <openssl/bio.h> |
| 10 #include <openssl/err.h> |
| 11 #include <openssl/pkcs12.h> |
| 12 #include <openssl/ssl.h> |
| 13 #include <openssl/x509.h> |
| 14 |
| 15 #include "platform/globals.h" |
| 16 |
| 17 #include "bin/directory.h" |
| 18 #include "bin/file.h" |
| 19 #include "bin/log.h" |
| 20 #include "bin/secure_socket.h" |
| 21 #include "bin/secure_socket_utils.h" |
| 22 |
| 23 // Return the error from the containing function if handle is an error handle. |
| 24 #define RETURN_IF_ERROR(handle) \ |
| 25 { \ |
| 26 Dart_Handle __handle = handle; \ |
| 27 if (Dart_IsError((__handle))) { \ |
| 28 return __handle; \ |
| 29 } \ |
| 30 } |
| 31 |
| 32 namespace dart { |
| 33 namespace bin { |
| 34 |
| 35 SSLCertContext* SSLCertContext::GetSecurityContext(Dart_NativeArguments args) { |
| 36 SSLCertContext* context; |
| 37 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
| 38 ASSERT(Dart_IsInstance(dart_this)); |
| 39 ThrowIfError(Dart_GetNativeInstanceField( |
| 40 dart_this, SSLCertContext::kSecurityContextNativeFieldIndex, |
| 41 reinterpret_cast<intptr_t*>(&context))); |
| 42 return context; |
| 43 } |
| 44 |
| 45 |
| 46 static void DeleteSecurityContext(void* isolate_data, |
| 47 Dart_WeakPersistentHandle handle, |
| 48 void* context_pointer) { |
| 49 SSLCertContext* context = static_cast<SSLCertContext*>(context_pointer); |
| 50 context->Release(); |
| 51 } |
| 52 |
| 53 |
| 54 static Dart_Handle SetSecurityContext(Dart_NativeArguments args, |
| 55 SSLCertContext* context) { |
| 56 Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); |
| 57 RETURN_IF_ERROR(dart_this); |
| 58 ASSERT(Dart_IsInstance(dart_this)); |
| 59 Dart_Handle err = Dart_SetNativeInstanceField( |
| 60 dart_this, SSLCertContext::kSecurityContextNativeFieldIndex, |
| 61 reinterpret_cast<intptr_t>(context)); |
| 62 RETURN_IF_ERROR(err); |
| 63 Dart_NewWeakPersistentHandle(dart_this, context, |
| 64 SSLCertContext::kApproximateSize, |
| 65 DeleteSecurityContext); |
| 66 return Dart_Null(); |
| 67 } |
| 68 |
| 69 |
| 70 static int SetTrustedCertificatesBytesPKCS12(SSL_CTX* context, |
| 71 BIO* bio, |
| 72 const char* password) { |
| 73 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); |
| 74 if (p12.get() == NULL) { |
| 75 return 0; |
| 76 } |
| 77 |
| 78 EVP_PKEY* key = NULL; |
| 79 X509* cert = NULL; |
| 80 STACK_OF(X509)* ca_certs = NULL; |
| 81 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); |
| 82 if (status == 0) { |
| 83 return status; |
| 84 } |
| 85 |
| 86 ScopedX509Stack cert_stack(ca_certs); |
| 87 X509_STORE* store = SSL_CTX_get_cert_store(context); |
| 88 status = X509_STORE_add_cert(store, cert); |
| 89 // X509_STORE_add_cert increments the reference count of cert on success. |
| 90 X509_free(cert); |
| 91 if (status == 0) { |
| 92 return status; |
| 93 } |
| 94 |
| 95 X509* ca; |
| 96 while ((ca = sk_X509_shift(cert_stack.get())) != NULL) { |
| 97 status = X509_STORE_add_cert(store, ca); |
| 98 // X509_STORE_add_cert increments the reference count of cert on success. |
| 99 X509_free(ca); |
| 100 if (status == 0) { |
| 101 return status; |
| 102 } |
| 103 } |
| 104 |
| 105 return status; |
| 106 } |
| 107 |
| 108 |
| 109 static int SetTrustedCertificatesBytesPEM(SSL_CTX* context, BIO* bio) { |
| 110 X509_STORE* store = SSL_CTX_get_cert_store(context); |
| 111 |
| 112 int status = 0; |
| 113 X509* cert = NULL; |
| 114 while ((cert = PEM_read_bio_X509(bio, NULL, NULL, NULL)) != NULL) { |
| 115 status = X509_STORE_add_cert(store, cert); |
| 116 // X509_STORE_add_cert increments the reference count of cert on success. |
| 117 X509_free(cert); |
| 118 if (status == 0) { |
| 119 return status; |
| 120 } |
| 121 } |
| 122 |
| 123 // If no PEM start line is found, it means that we read to the end of the |
| 124 // file, or that the file isn't PEM. In the first case, status will be |
| 125 // non-zero indicating success. In the second case, status will be 0, |
| 126 // indicating that we should try to read as PKCS12. If there is some other |
| 127 // error, we return it up to the caller. |
| 128 return SecureSocketUtils::NoPEMStartLine() ? status : 0; |
| 129 } |
| 130 |
| 131 |
| 132 void SSLCertContext::SetTrustedCertificatesBytes(Dart_Handle cert_bytes, |
| 133 const char* password) { |
| 134 ScopedMemBIO bio(cert_bytes); |
| 135 int status = SetTrustedCertificatesBytesPEM(context(), bio.bio()); |
| 136 if (status == 0) { |
| 137 if (SecureSocketUtils::NoPEMStartLine()) { |
| 138 ERR_clear_error(); |
| 139 BIO_reset(bio.bio()); |
| 140 status = |
| 141 SetTrustedCertificatesBytesPKCS12(context(), bio.bio(), password); |
| 142 } |
| 143 } else { |
| 144 // The PEM file was successfully parsed. |
| 145 ERR_clear_error(); |
| 146 } |
| 147 |
| 148 SecureSocketUtils::CheckStatus(status, "TlsException", |
| 149 "Failure trusting builtin roots"); |
| 150 } |
| 151 |
| 152 |
| 153 static int SetClientAuthoritiesPKCS12(SSL_CTX* context, |
| 154 BIO* bio, |
| 155 const char* password) { |
| 156 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); |
| 157 if (p12.get() == NULL) { |
| 158 return 0; |
| 159 } |
| 160 |
| 161 EVP_PKEY* key = NULL; |
| 162 X509* cert = NULL; |
| 163 STACK_OF(X509)* ca_certs = NULL; |
| 164 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); |
| 165 if (status == 0) { |
| 166 return status; |
| 167 } |
| 168 |
| 169 ScopedX509Stack cert_stack(ca_certs); |
| 170 status = SSL_CTX_add_client_CA(context, cert); |
| 171 // SSL_CTX_add_client_CA increments the reference count of cert on success. |
| 172 X509_free(cert); |
| 173 if (status == 0) { |
| 174 return status; |
| 175 } |
| 176 |
| 177 X509* ca; |
| 178 while ((ca = sk_X509_shift(cert_stack.get())) != NULL) { |
| 179 status = SSL_CTX_add_client_CA(context, ca); |
| 180 // SSL_CTX_add_client_CA increments the reference count of ca on success. |
| 181 X509_free(ca); // The name has been extracted. |
| 182 if (status == 0) { |
| 183 return status; |
| 184 } |
| 185 } |
| 186 |
| 187 return status; |
| 188 } |
| 189 |
| 190 |
| 191 static int SetClientAuthoritiesPEM(SSL_CTX* context, BIO* bio) { |
| 192 int status = 0; |
| 193 X509* cert = NULL; |
| 194 while ((cert = PEM_read_bio_X509(bio, NULL, NULL, NULL)) != NULL) { |
| 195 status = SSL_CTX_add_client_CA(context, cert); |
| 196 X509_free(cert); // The name has been extracted. |
| 197 if (status == 0) { |
| 198 return status; |
| 199 } |
| 200 } |
| 201 return SecureSocketUtils::NoPEMStartLine() ? status : 0; |
| 202 } |
| 203 |
| 204 |
| 205 static int SetClientAuthorities(SSL_CTX* context, |
| 206 BIO* bio, |
| 207 const char* password) { |
| 208 int status = SetClientAuthoritiesPEM(context, bio); |
| 209 if (status == 0) { |
| 210 if (SecureSocketUtils::NoPEMStartLine()) { |
| 211 ERR_clear_error(); |
| 212 BIO_reset(bio); |
| 213 status = SetClientAuthoritiesPKCS12(context, bio, password); |
| 214 } |
| 215 } else { |
| 216 // The PEM file was successfully parsed. |
| 217 ERR_clear_error(); |
| 218 } |
| 219 return status; |
| 220 } |
| 221 |
| 222 |
| 223 void SSLCertContext::SetClientAuthoritiesBytes( |
| 224 Dart_Handle client_authorities_bytes, |
| 225 const char* password) { |
| 226 int status; |
| 227 { |
| 228 ScopedMemBIO bio(client_authorities_bytes); |
| 229 status = SetClientAuthorities(context(), bio.bio(), password); |
| 230 } |
| 231 |
| 232 SecureSocketUtils::CheckStatus(status, "TlsException", |
| 233 "Failure in setClientAuthoritiesBytes"); |
| 234 } |
| 235 |
| 236 void SSLCertContext::LoadRootCertFile(const char* file) { |
| 237 if (SSL_LOG_STATUS) { |
| 238 Log::Print("Looking for trusted roots in %s\n", file); |
| 239 } |
| 240 if (!File::Exists(file)) { |
| 241 SecureSocketUtils::ThrowIOException(-1, "TlsException", |
| 242 "Failed to find root cert file", NULL); |
| 243 } |
| 244 int status = SSL_CTX_load_verify_locations(context(), file, NULL); |
| 245 SecureSocketUtils::CheckStatus(status, "TlsException", |
| 246 "Failure trusting builtin roots"); |
| 247 if (SSL_LOG_STATUS) { |
| 248 Log::Print("Trusting roots from: %s\n", file); |
| 249 } |
| 250 } |
| 251 |
| 252 |
| 253 void SSLCertContext::LoadRootCertCache(const char* cache) { |
| 254 if (SSL_LOG_STATUS) { |
| 255 Log::Print("Looking for trusted roots in %s\n", cache); |
| 256 } |
| 257 if (Directory::Exists(cache) != Directory::EXISTS) { |
| 258 SecureSocketUtils::ThrowIOException(-1, "TlsException", |
| 259 "Failed to find root cert cache", NULL); |
| 260 } |
| 261 int status = SSL_CTX_load_verify_locations(context(), NULL, cache); |
| 262 SecureSocketUtils::CheckStatus(status, "TlsException", |
| 263 "Failure trusting builtin roots"); |
| 264 if (SSL_LOG_STATUS) { |
| 265 Log::Print("Trusting roots from: %s\n", cache); |
| 266 } |
| 267 } |
| 268 |
| 269 |
| 270 int PasswordCallback(char* buf, int size, int rwflag, void* userdata) { |
| 271 char* password = static_cast<char*>(userdata); |
| 272 ASSERT(size == PEM_BUFSIZE); |
| 273 strncpy(buf, password, size); |
| 274 return strlen(password); |
| 275 } |
| 276 |
| 277 |
| 278 static EVP_PKEY* GetPrivateKeyPKCS12(BIO* bio, const char* password) { |
| 279 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); |
| 280 if (p12.get() == NULL) { |
| 281 return NULL; |
| 282 } |
| 283 |
| 284 EVP_PKEY* key = NULL; |
| 285 X509* cert = NULL; |
| 286 STACK_OF(X509)* ca_certs = NULL; |
| 287 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); |
| 288 if (status == 0) { |
| 289 return NULL; |
| 290 } |
| 291 |
| 292 // We only care about the private key. |
| 293 ScopedX509 delete_cert(cert); |
| 294 ScopedX509Stack delete_ca_certs(ca_certs); |
| 295 return key; |
| 296 } |
| 297 |
| 298 |
| 299 static EVP_PKEY* GetPrivateKey(BIO* bio, const char* password) { |
| 300 EVP_PKEY* key = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallback, |
| 301 const_cast<char*>(password)); |
| 302 if (key == NULL) { |
| 303 // We try reading data as PKCS12 only if reading as PEM was unsuccessful and |
| 304 // if there is no indication that the data is malformed PEM. We assume the |
| 305 // data is malformed PEM if it contains the start line, i.e. a line |
| 306 // with ----- BEGIN. |
| 307 if (SecureSocketUtils::NoPEMStartLine()) { |
| 308 // Reset the bio, and clear the error from trying to read as PEM. |
| 309 ERR_clear_error(); |
| 310 BIO_reset(bio); |
| 311 |
| 312 // Try to decode as PKCS12. |
| 313 key = GetPrivateKeyPKCS12(bio, password); |
| 314 } |
| 315 } |
| 316 return key; |
| 317 } |
| 318 |
| 319 |
| 320 const char* SSLCertContext::GetPasswordArgument(Dart_NativeArguments args, |
| 321 intptr_t index) { |
| 322 Dart_Handle password_object = |
| 323 ThrowIfError(Dart_GetNativeArgument(args, index)); |
| 324 const char* password = NULL; |
| 325 if (Dart_IsString(password_object)) { |
| 326 ThrowIfError(Dart_StringToCString(password_object, &password)); |
| 327 if (strlen(password) > PEM_BUFSIZE - 1) { |
| 328 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 329 "Password length is greater than 1023 (PEM_BUFSIZE)")); |
| 330 } |
| 331 } else if (Dart_IsNull(password_object)) { |
| 332 password = ""; |
| 333 } else { |
| 334 Dart_ThrowException( |
| 335 DartUtils::NewDartArgumentError("Password is not a String or null")); |
| 336 } |
| 337 return password; |
| 338 } |
| 339 |
| 340 |
| 341 int AlpnCallback(SSL* ssl, |
| 342 const uint8_t** out, |
| 343 uint8_t* outlen, |
| 344 const uint8_t* in, |
| 345 unsigned int inlen, |
| 346 void* arg) { |
| 347 // 'in' and 'arg' are sequences of (length, data) strings with 1-byte lengths. |
| 348 // 'arg' is 0-terminated. Finds the first string in 'arg' that is in 'in'. |
| 349 uint8_t* server_list = static_cast<uint8_t*>(arg); |
| 350 while (*server_list != 0) { |
| 351 uint8_t protocol_length = *server_list++; |
| 352 const uint8_t* client_list = in; |
| 353 while (client_list < in + inlen) { |
| 354 uint8_t client_protocol_length = *client_list++; |
| 355 if (client_protocol_length == protocol_length) { |
| 356 if (0 == memcmp(server_list, client_list, protocol_length)) { |
| 357 *out = client_list; |
| 358 *outlen = client_protocol_length; |
| 359 return SSL_TLSEXT_ERR_OK; // Success |
| 360 } |
| 361 } |
| 362 client_list += client_protocol_length; |
| 363 } |
| 364 server_list += protocol_length; |
| 365 } |
| 366 // TODO(23580): Make failure send a fatal alert instead of ignoring ALPN. |
| 367 return SSL_TLSEXT_ERR_NOACK; |
| 368 } |
| 369 |
| 370 |
| 371 // Sets the protocol list for ALPN on a SSL object or a context. |
| 372 void SSLCertContext::SetAlpnProtocolList(Dart_Handle protocols_handle, |
| 373 SSL* ssl, |
| 374 SSLCertContext* context, |
| 375 bool is_server) { |
| 376 // Enable ALPN (application layer protocol negotiation) if the caller provides |
| 377 // a valid list of supported protocols. |
| 378 Dart_TypedData_Type protocols_type; |
| 379 uint8_t* protocol_string = NULL; |
| 380 uint8_t* protocol_string_copy = NULL; |
| 381 intptr_t protocol_string_len = 0; |
| 382 int status; |
| 383 |
| 384 Dart_Handle result = Dart_TypedDataAcquireData( |
| 385 protocols_handle, &protocols_type, |
| 386 reinterpret_cast<void**>(&protocol_string), &protocol_string_len); |
| 387 if (Dart_IsError(result)) { |
| 388 Dart_PropagateError(result); |
| 389 } |
| 390 |
| 391 if (protocols_type != Dart_TypedData_kUint8) { |
| 392 Dart_TypedDataReleaseData(protocols_handle); |
| 393 Dart_PropagateError(Dart_NewApiError( |
| 394 "Unexpected type for protocols (expected valid Uint8List).")); |
| 395 } |
| 396 |
| 397 if (protocol_string_len > 0) { |
| 398 if (is_server) { |
| 399 // ALPN on server connections must be set on an SSL_CTX object, |
| 400 // not on the SSL object of the individual connection. |
| 401 ASSERT(context != NULL); |
| 402 ASSERT(ssl == NULL); |
| 403 // Because it must be passed as a single void*, terminate |
| 404 // the list of (length, data) strings with a length 0 string. |
| 405 protocol_string_copy = |
| 406 static_cast<uint8_t*>(malloc(protocol_string_len + 1)); |
| 407 memmove(protocol_string_copy, protocol_string, protocol_string_len); |
| 408 protocol_string_copy[protocol_string_len] = '\0'; |
| 409 SSL_CTX_set_alpn_select_cb(context->context(), AlpnCallback, |
| 410 protocol_string_copy); |
| 411 context->set_alpn_protocol_string(protocol_string_copy); |
| 412 } else { |
| 413 // The function makes a local copy of protocol_string, which it owns. |
| 414 if (ssl != NULL) { |
| 415 ASSERT(context == NULL); |
| 416 status = SSL_set_alpn_protos(ssl, protocol_string, protocol_string_len); |
| 417 } else { |
| 418 ASSERT(context != NULL); |
| 419 ASSERT(ssl == NULL); |
| 420 status = SSL_CTX_set_alpn_protos(context->context(), protocol_string, |
| 421 protocol_string_len); |
| 422 } |
| 423 ASSERT(status == 0); // The function returns a non-standard status. |
| 424 } |
| 425 } |
| 426 Dart_TypedDataReleaseData(protocols_handle); |
| 427 } |
| 428 |
| 429 |
| 430 static int UseChainBytesPKCS12(SSL_CTX* context, |
| 431 BIO* bio, |
| 432 const char* password) { |
| 433 ScopedPKCS12 p12(d2i_PKCS12_bio(bio, NULL)); |
| 434 if (p12.get() == NULL) { |
| 435 return 0; |
| 436 } |
| 437 |
| 438 EVP_PKEY* key = NULL; |
| 439 X509* cert = NULL; |
| 440 STACK_OF(X509)* ca_certs = NULL; |
| 441 int status = PKCS12_parse(p12.get(), password, &key, &cert, &ca_certs); |
| 442 if (status == 0) { |
| 443 return status; |
| 444 } |
| 445 |
| 446 ScopedX509 x509(cert); |
| 447 ScopedX509Stack certs(ca_certs); |
| 448 status = SSL_CTX_use_certificate(context, x509.get()); |
| 449 if (ERR_peek_error() != 0) { |
| 450 // Key/certificate mismatch doesn't imply status is 0. |
| 451 status = 0; |
| 452 } |
| 453 if (status == 0) { |
| 454 return status; |
| 455 } |
| 456 |
| 457 SSL_CTX_clear_chain_certs(context); |
| 458 |
| 459 X509* ca; |
| 460 while ((ca = sk_X509_shift(certs.get())) != NULL) { |
| 461 status = SSL_CTX_add0_chain_cert(context, ca); |
| 462 // SSL_CTX_add0_chain_cert does not inc ref count, so don't free unless the |
| 463 // call fails. |
| 464 if (status == 0) { |
| 465 X509_free(ca); |
| 466 return status; |
| 467 } |
| 468 } |
| 469 |
| 470 return status; |
| 471 } |
| 472 |
| 473 |
| 474 static int UseChainBytesPEM(SSL_CTX* context, BIO* bio) { |
| 475 int status = 0; |
| 476 ScopedX509 x509(PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL)); |
| 477 if (x509.get() == NULL) { |
| 478 return 0; |
| 479 } |
| 480 |
| 481 status = SSL_CTX_use_certificate(context, x509.get()); |
| 482 if (ERR_peek_error() != 0) { |
| 483 // Key/certificate mismatch doesn't imply status is 0. |
| 484 status = 0; |
| 485 } |
| 486 if (status == 0) { |
| 487 return status; |
| 488 } |
| 489 |
| 490 SSL_CTX_clear_chain_certs(context); |
| 491 |
| 492 X509* ca; |
| 493 while ((ca = PEM_read_bio_X509(bio, NULL, NULL, NULL)) != NULL) { |
| 494 status = SSL_CTX_add0_chain_cert(context, ca); |
| 495 // SSL_CTX_add0_chain_cert does not inc ref count, so don't free unless the |
| 496 // call fails. |
| 497 if (status == 0) { |
| 498 X509_free(ca); |
| 499 return status; |
| 500 } |
| 501 // Note that we must not free `ca` if it was successfully added to the |
| 502 // chain. We must free the main certificate x509, though since its reference |
| 503 // count is increased by SSL_CTX_use_certificate. |
| 504 } |
| 505 |
| 506 return SecureSocketUtils::NoPEMStartLine() ? status : 0; |
| 507 } |
| 508 |
| 509 |
| 510 static int UseChainBytes(SSL_CTX* context, BIO* bio, const char* password) { |
| 511 int status = UseChainBytesPEM(context, bio); |
| 512 if (status == 0) { |
| 513 if (SecureSocketUtils::NoPEMStartLine()) { |
| 514 ERR_clear_error(); |
| 515 BIO_reset(bio); |
| 516 status = UseChainBytesPKCS12(context, bio, password); |
| 517 } |
| 518 } else { |
| 519 // The PEM file was successfully read. |
| 520 ERR_clear_error(); |
| 521 } |
| 522 return status; |
| 523 } |
| 524 |
| 525 |
| 526 int SSLCertContext::UseCertificateChainBytes(Dart_Handle cert_chain_bytes, |
| 527 const char* password) { |
| 528 ScopedMemBIO bio(cert_chain_bytes); |
| 529 return UseChainBytes(context(), bio.bio(), password); |
| 530 } |
| 531 |
| 532 |
| 533 static X509* GetX509Certificate(Dart_NativeArguments args) { |
| 534 X509* certificate = NULL; |
| 535 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
| 536 ASSERT(Dart_IsInstance(dart_this)); |
| 537 ThrowIfError(Dart_GetNativeInstanceField( |
| 538 dart_this, SSLCertContext::kX509NativeFieldIndex, |
| 539 reinterpret_cast<intptr_t*>(&certificate))); |
| 540 return certificate; |
| 541 } |
| 542 |
| 543 |
| 544 Dart_Handle X509Helper::GetSubject(Dart_NativeArguments args) { |
| 545 X509* certificate = GetX509Certificate(args); |
| 546 X509_NAME* subject = X509_get_subject_name(certificate); |
| 547 char* subject_string = X509_NAME_oneline(subject, NULL, 0); |
| 548 if (subject_string == NULL) { |
| 549 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 550 "X509.subject failed to find subject's common name.")); |
| 551 } |
| 552 Dart_Handle subject_handle = Dart_NewStringFromCString(subject_string); |
| 553 OPENSSL_free(subject_string); |
| 554 return subject_handle; |
| 555 } |
| 556 |
| 557 |
| 558 Dart_Handle X509Helper::GetIssuer(Dart_NativeArguments args) { |
| 559 fprintf(stdout, "Getting issuer!\n"); |
| 560 X509* certificate = GetX509Certificate(args); |
| 561 X509_NAME* issuer = X509_get_issuer_name(certificate); |
| 562 char* issuer_string = X509_NAME_oneline(issuer, NULL, 0); |
| 563 if (issuer_string == NULL) { |
| 564 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 565 "X509.issuer failed to find issuer's common name.")); |
| 566 } |
| 567 Dart_Handle issuer_handle = Dart_NewStringFromCString(issuer_string); |
| 568 OPENSSL_free(issuer_string); |
| 569 return issuer_handle; |
| 570 } |
| 571 |
| 572 |
| 573 static Dart_Handle ASN1TimeToMilliseconds(ASN1_TIME* aTime) { |
| 574 ASN1_UTCTIME* epoch_start = M_ASN1_UTCTIME_new(); |
| 575 ASN1_UTCTIME_set_string(epoch_start, "700101000000Z"); |
| 576 int days; |
| 577 int seconds; |
| 578 int result = ASN1_TIME_diff(&days, &seconds, epoch_start, aTime); |
| 579 M_ASN1_UTCTIME_free(epoch_start); |
| 580 if (result != 1) { |
| 581 // TODO(whesse): Propagate an error to Dart. |
| 582 Log::PrintErr("ASN1Time error %d\n", result); |
| 583 } |
| 584 return Dart_NewInteger((86400LL * days + seconds) * 1000LL); |
| 585 } |
| 586 |
| 587 |
| 588 Dart_Handle X509Helper::GetStartValidity(Dart_NativeArguments args) { |
| 589 X509* certificate = GetX509Certificate(args); |
| 590 ASN1_TIME* not_before = X509_get_notBefore(certificate); |
| 591 return ASN1TimeToMilliseconds(not_before); |
| 592 } |
| 593 |
| 594 |
| 595 Dart_Handle X509Helper::GetEndValidity(Dart_NativeArguments args) { |
| 596 X509* certificate = GetX509Certificate(args); |
| 597 ASN1_TIME* not_after = X509_get_notAfter(certificate); |
| 598 return ASN1TimeToMilliseconds(not_after); |
| 599 } |
| 600 |
| 601 void FUNCTION_NAME(SecurityContext_UsePrivateKeyBytes)( |
| 602 Dart_NativeArguments args) { |
| 603 SSLCertContext* context = SSLCertContext::GetSecurityContext(args); |
| 604 const char* password = SSLCertContext::GetPasswordArgument(args, 2); |
| 605 |
| 606 int status; |
| 607 { |
| 608 ScopedMemBIO bio(ThrowIfError(Dart_GetNativeArgument(args, 1))); |
| 609 EVP_PKEY* key = GetPrivateKey(bio.bio(), password); |
| 610 status = SSL_CTX_use_PrivateKey(context->context(), key); |
| 611 // SSL_CTX_use_PrivateKey increments the reference count of key on success, |
| 612 // so we have to call EVP_PKEY_free on both success and failure. |
| 613 EVP_PKEY_free(key); |
| 614 } |
| 615 |
| 616 // TODO(24184): Handle different expected errors here - file missing, |
| 617 // incorrect password, file not a PEM, and throw exceptions. |
| 618 // SecureSocketUtils::CheckStatus should also throw an exception in uncaught |
| 619 // cases. |
| 620 SecureSocketUtils::CheckStatus(status, "TlsException", |
| 621 "Failure in usePrivateKeyBytes"); |
| 622 } |
| 623 |
| 624 |
| 625 void FUNCTION_NAME(SecurityContext_Allocate)(Dart_NativeArguments args) { |
| 626 SSLFilter::InitializeLibrary(); |
| 627 SSL_CTX* ctx = SSL_CTX_new(TLS_method()); |
| 628 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, CertificateCallback); |
| 629 SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 630 SSL_CTX_set_cipher_list(ctx, "HIGH:MEDIUM"); |
| 631 SSLCertContext* context = new SSLCertContext(ctx); |
| 632 Dart_Handle err = SetSecurityContext(args, context); |
| 633 if (Dart_IsError(err)) { |
| 634 delete context; |
| 635 Dart_PropagateError(err); |
| 636 } |
| 637 } |
| 638 |
| 639 |
| 640 void FUNCTION_NAME(SecurityContext_SetTrustedCertificatesBytes)( |
| 641 Dart_NativeArguments args) { |
| 642 SSLCertContext* context = SSLCertContext::GetSecurityContext(args); |
| 643 Dart_Handle cert_bytes = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
| 644 const char* password = SSLCertContext::GetPasswordArgument(args, 2); |
| 645 |
| 646 ASSERT(context != NULL); |
| 647 ASSERT(password != NULL); |
| 648 context->SetTrustedCertificatesBytes(cert_bytes, password); |
| 649 } |
| 650 |
| 651 |
| 652 void FUNCTION_NAME(SecurityContext_SetClientAuthoritiesBytes)( |
| 653 Dart_NativeArguments args) { |
| 654 SSLCertContext* context = SSLCertContext::GetSecurityContext(args); |
| 655 Dart_Handle client_authorities_bytes = |
| 656 ThrowIfError(Dart_GetNativeArgument(args, 1)); |
| 657 const char* password = SSLCertContext::GetPasswordArgument(args, 2); |
| 658 |
| 659 ASSERT(context != NULL); |
| 660 ASSERT(password != NULL); |
| 661 |
| 662 context->SetClientAuthoritiesBytes(client_authorities_bytes, password); |
| 663 } |
| 664 |
| 665 |
| 666 void FUNCTION_NAME(SecurityContext_UseCertificateChainBytes)( |
| 667 Dart_NativeArguments args) { |
| 668 SSLCertContext* context = SSLCertContext::GetSecurityContext(args); |
| 669 Dart_Handle cert_chain_bytes = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
| 670 const char* password = SSLCertContext::GetPasswordArgument(args, 2); |
| 671 |
| 672 ASSERT(context != NULL); |
| 673 ASSERT(password != NULL); |
| 674 |
| 675 int status = context->UseCertificateChainBytes(cert_chain_bytes, password); |
| 676 |
| 677 SecureSocketUtils::CheckStatus(status, "TlsException", |
| 678 "Failure in useCertificateChainBytes"); |
| 679 } |
| 680 |
| 681 |
| 682 void FUNCTION_NAME(SecurityContext_AlpnSupported)(Dart_NativeArguments args) { |
| 683 Dart_SetReturnValue(args, Dart_NewBoolean(true)); |
| 684 } |
| 685 |
| 686 |
| 687 void FUNCTION_NAME(SecurityContext_TrustBuiltinRoots)( |
| 688 Dart_NativeArguments args) { |
| 689 SSLCertContext* context = SSLCertContext::GetSecurityContext(args); |
| 690 |
| 691 ASSERT(context != NULL); |
| 692 |
| 693 context->TrustBuiltinRoots(); |
| 694 } |
| 695 |
| 696 |
| 697 void FUNCTION_NAME(X509_Subject)(Dart_NativeArguments args) { |
| 698 Dart_SetReturnValue(args, X509Helper::GetSubject(args)); |
| 699 } |
| 700 |
| 701 |
| 702 void FUNCTION_NAME(X509_Issuer)(Dart_NativeArguments args) { |
| 703 Dart_SetReturnValue(args, X509Helper::GetIssuer(args)); |
| 704 } |
| 705 |
| 706 |
| 707 void FUNCTION_NAME(X509_StartValidity)(Dart_NativeArguments args) { |
| 708 Dart_SetReturnValue(args, X509Helper::GetStartValidity(args)); |
| 709 } |
| 710 |
| 711 |
| 712 void FUNCTION_NAME(X509_EndValidity)(Dart_NativeArguments args) { |
| 713 Dart_SetReturnValue(args, X509Helper::GetEndValidity(args)); |
| 714 } |
| 715 |
| 716 |
| 717 void FUNCTION_NAME(SecurityContext_SetAlpnProtocols)( |
| 718 Dart_NativeArguments args) { |
| 719 SSLCertContext* context = SSLCertContext::GetSecurityContext(args); |
| 720 Dart_Handle protocols_handle = ThrowIfError(Dart_GetNativeArgument(args, 1)); |
| 721 Dart_Handle is_server_handle = ThrowIfError(Dart_GetNativeArgument(args, 2)); |
| 722 if (Dart_IsBoolean(is_server_handle)) { |
| 723 bool is_server = DartUtils::GetBooleanValue(is_server_handle); |
| 724 SSLCertContext::SetAlpnProtocolList(protocols_handle, NULL, context, |
| 725 is_server); |
| 726 } else { |
| 727 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 728 "Non-boolean is_server argument passed to SetAlpnProtocols")); |
| 729 } |
| 730 } |
| 731 |
| 732 } // namespace bin |
| 733 } // namespace dart |
| 734 #endif |
| OLD | NEW |