GeezerVison is showing github.com — crypto: fix ncrypto provider behavior · nodejs/node@1db6022
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: deps/ncrypto/ncrypto.cc +42-12Lines changed: 42 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -727,6 +727,8 @@ BignumPointer BignumPointer::clone() { | 727 | 727 | int BignumPointer::isPrime(int nchecks, | 728 | 728 | BignumPointer::PrimeCheckCallback innerCb) const { | 729 | 729 | BignumCtxPointer ctx(BN_CTX_new()); | | 730 | + if (!ctx) [[unlikely]] | | 731 | + return -1; | 730 | 732 | BignumGenCallbackPointer cb(nullptr); | 731 | 733 | if (innerCb != nullptr) { | 732 | 734 | cb = BignumGenCallbackPointer(BN_GENCB_new()); | @@ -744,7 +746,17 @@ int BignumPointer::isPrime(int nchecks, | 744 | 746 | &innerCb); | 745 | 747 | } | 746 | 748 | #if NCRYPTO_USE_OPENSSL3_PROVIDER | 747 | | - return BN_check_prime(get(), ctx.get(), cb.get()); | | 749 | + const int minimum_checks = GetBitCount(get()) > 2048 ? 128 : 64; | | 750 | + const int repetitions = | | 751 | + nchecks > minimum_checks ? 1 + (nchecks - 1) / minimum_checks : 1; | | 752 | + for (int i = 0; i < repetitions; i++) { | | 753 | + // BN_check_prime() may return without invoking the callback (e.g. small | | 754 | + // primes resolved by trial division), so check cancellation here too. | | 755 | + if (i > 0 && innerCb != nullptr && !innerCb(1, -1)) return -1; | | 756 | + const int result = BN_check_prime(get(), ctx.get(), cb.get()); | | 757 | + if (result != 1) return result; | | 758 | + } | | 759 | + return 1; | 748 | 760 | #elif NCRYPTO_USE_BORINGSSL | 749 | 761 | int is_probably_prime = 0; | 750 | 762 | if (BN_primality_test( | @@ -4294,14 +4306,17 @@ int WriteDERView(const void* x, unsigned char** out) { | 4294 | 4306 |
| 4295 | 4307 | bool WriteEncryptedTraditionalPEM(BIO* bio, | 4296 | 4308 | const EVP_PKEY* pkey, | | 4309 | + int selection, | | 4310 | + const char* structure, | | 4311 | + const char* label, | 4297 | 4312 | const EVP_CIPHER* cipher, | 4298 | 4313 | Buffer<char> passphrase) { | 4299 | 4314 | if (passphrase.len > static_cast<size_t>(INT_MAX)) return false; | 4300 | 4315 |
| 4301 | 4316 | unsigned char* der = nullptr; | 4302 | 4317 | size_t der_len = 0; | 4303 | 4318 | OSSLEncoderCtxPointer ctx(OSSL_ENCODER_CTX_new_for_pkey( | 4304 | | - pkey, OSSL_KEYMGMT_SELECT_KEYPAIR, "DER", "pkcs1", nullptr)); | | 4319 | + pkey, selection, "DER", structure, nullptr)); | 4305 | 4320 | if (!ctx) return false; | 4306 | 4321 |
| 4307 | 4322 | const int result = OSSL_ENCODER_to_data(ctx.get(), &der, &der_len); | @@ -4311,7 +4326,7 @@ bool WriteEncryptedTraditionalPEM(BIO* bio, | 4311 | 4326 | DERView der_view{der_storage.get<const unsigned char>(), der_len}; | 4312 | 4327 | return PEM_ASN1_write_bio( | 4313 | 4328 | WriteDERView, | 4314 | | - PEM_STRING_RSA, | | 4329 | + label, | 4315 | 4330 | bio, | 4316 | 4331 | &der_view, | 4317 | 4332 | cipher, | @@ -4522,8 +4537,13 @@ Result<BIOPointer, bool> EVPKeyPointer::writePrivateKey( | 4522 | 4537 | const EVP_CIPHER* cipher = | 4523 | 4538 | config.format == PKFormatType::PEM ? config.cipher.get() : nullptr; | 4524 | 4539 | if (cipher != nullptr && passphrase.len == 0) { | 4525 | | - err = | 4526 | | - !WriteEncryptedTraditionalPEM(bio.get(), get(), cipher, passphrase); | | 4540 | + err = !WriteEncryptedTraditionalPEM(bio.get(), | | 4541 | + get(), | | 4542 | + OSSL_KEYMGMT_SELECT_KEYPAIR, | | 4543 | + "pkcs1", | | 4544 | + PEM_STRING_RSA, | | 4545 | + cipher, | | 4546 | + passphrase); | 4527 | 4547 | } else { | 4528 | 4548 | err = !WriteEncodedPKey(bio.get(), | 4529 | 4549 | get(), | @@ -4618,13 +4638,23 @@ Result<BIOPointer, bool> EVPKeyPointer::writePrivateKey( | 4618 | 4638 | #if NCRYPTO_USE_OPENSSL3_PROVIDER | 4619 | 4639 | const EVP_CIPHER* cipher = | 4620 | 4640 | config.format == PKFormatType::PEM ? config.cipher.get() : nullptr; | 4621 | | - err = !WriteEncodedPKey(bio.get(), | 4622 | | - get(), | 4623 | | - OSSL_KEYMGMT_SELECT_ALL, | 4624 | | - config.format, | 4625 | | - "type-specific", | 4626 | | - cipher, | 4627 | | - passphrase); | | 4641 | + if (cipher != nullptr && passphrase.len == 0) { | | 4642 | + err = !WriteEncryptedTraditionalPEM(bio.get(), | | 4643 | + get(), | | 4644 | + OSSL_KEYMGMT_SELECT_ALL, | | 4645 | + "type-specific", | | 4646 | + PEM_STRING_ECPRIVATEKEY, | | 4647 | + cipher, | | 4648 | + passphrase); | | 4649 | + } else { | | 4650 | + err = !WriteEncodedPKey(bio.get(), | | 4651 | + get(), | | 4652 | + OSSL_KEYMGMT_SELECT_ALL, | | 4653 | + config.format, | | 4654 | + "type-specific", | | 4655 | + cipher, | | 4656 | + passphrase); | | 4657 | + } | 4628 | 4658 | #else | 4629 | 4659 | #if OPENSSL_VERSION_MAJOR >= 3 | 4630 | 4660 | const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(get()); | |
Collapse file Copy file name to clipboardExpand all lines: test/cctest/test_node_crypto.cc +76Lines changed: 76 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -437,6 +437,82 @@ TEST(NodeCrypto, RsaPrimeProduct) { | 437 | 437 | EXPECT_FALSE(rsa.checkPrimeProduct()); | 438 | 438 | } | 439 | 439 |
| | 440 | +#if NCRYPTO_USE_OPENSSL3_PROVIDER | | 441 | +namespace { | | 442 | +constexpr int kMinimumPrimeChecks = 64; | | 443 | + | | 444 | +bool CheckMersennePrime(int exponent, int requested_checks) { | | 445 | + auto candidate = ncrypto::BignumPointer::New(); | | 446 | + if (!candidate || BN_set_bit(candidate.get(), exponent) != 1 || | | 447 | + BN_sub_word(candidate.get(), 1) != 1) { | | 448 | + return false; | | 449 | + } | | 450 | + | | 451 | + int completed_checks = 0; | | 452 | + const int result = candidate.isPrime( | | 453 | + requested_checks, [&completed_checks](int event, int iteration) { | | 454 | + if (event == 1 && iteration >= 0) completed_checks++; | | 455 | + return true; | | 456 | + }); | | 457 | + return result == 1 && completed_checks >= requested_checks; | | 458 | +} | | 459 | +} // namespace | | 460 | + | | 461 | +TEST(NodeCrypto, BignumPrimeChecksHonorRequestedCount) { | | 462 | + EXPECT_TRUE(CheckMersennePrime(127, kMinimumPrimeChecks + 1)); | | 463 | + EXPECT_TRUE(CheckMersennePrime(2203, 129)); | | 464 | +} | | 465 | + | | 466 | +TEST(NodeCrypto, BignumPrimeCheckCancellation) { | | 467 | + auto candidate = ncrypto::BignumPointer::New(); | | 468 | + ASSERT_TRUE(candidate); | | 469 | + ASSERT_EQ(BN_set_bit(candidate.get(), 127), 1); | | 470 | + ASSERT_EQ(BN_sub_word(candidate.get(), 1), 1); | | 471 | + | | 472 | + int invocations = 0; | | 473 | + int completed_checks = 0; | | 474 | + bool valid_events = true; | | 475 | + const int result = | | 476 | + candidate.isPrime(kMinimumPrimeChecks + 1, [&](int event, int iteration) { | | 477 | + if (event != 1) { | | 478 | + valid_events = false; | | 479 | + return false; | | 480 | + } | | 481 | + if (iteration == -1) { | | 482 | + invocations++; | | 483 | + return true; | | 484 | + } | | 485 | + if (iteration != completed_checks % kMinimumPrimeChecks) { | | 486 | + valid_events = false; | | 487 | + return false; | | 488 | + } | | 489 | + if (completed_checks == kMinimumPrimeChecks) return false; | | 490 | + completed_checks++; | | 491 | + return true; | | 492 | + }); | | 493 | + EXPECT_EQ(result, -1); | | 494 | + EXPECT_TRUE(valid_events); | | 495 | + EXPECT_EQ(invocations, 3); | | 496 | + EXPECT_EQ(completed_checks, kMinimumPrimeChecks); | | 497 | + ERR_clear_error(); | | 498 | +} | | 499 | + | | 500 | +TEST(NodeCrypto, BignumPrimeCheckCancellationSmallPrime) { | | 501 | + // 311 is resolved by trial division, so OpenSSL never invokes the callback. | | 502 | + auto candidate = ncrypto::BignumPointer::New(); | | 503 | + ASSERT_TRUE(candidate); | | 504 | + ASSERT_EQ(BN_set_word(candidate.get(), 311), 1); | | 505 | + | | 506 | + int invocations = 0; | | 507 | + const int result = candidate.isPrime(INT_MAX, [&](int, int) { | | 508 | + invocations++; | | 509 | + return false; | | 510 | + }); | | 511 | + EXPECT_EQ(result, -1); | | 512 | + EXPECT_EQ(invocations, 1); | | 513 | +} | | 514 | +#endif | | 515 | + | 440 | 516 | TEST(NodeCrypto, EcKeyComponents) { | 441 | 517 | ncrypto::ClearErrorOnReturn clear_errors; | 442 | 518 | ncrypto::BignumPointer x; | |
Collapse file Copy file name to clipboard +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | + | | 7 | +const { hasFIPS, hasOpenSSL } = require('../common/crypto'); | | 8 | + | | 9 | +if (!hasOpenSSL(3)) | | 10 | + common.skip('OpenSSL 3 provider encoders are required'); | | 11 | +if (hasFIPS(3)) | | 12 | + common.skip('SEC1 private key encoding is not supported in FIPS mode'); | | 13 | + | | 14 | +const assert = require('assert'); | | 15 | +const { | | 16 | + createPrivateKey, | | 17 | + generateKeyPairSync, | | 18 | +} = require('crypto'); | | 19 | + | | 20 | +const { privateKey } = generateKeyPairSync('ec', { | | 21 | + namedCurve: 'prime256v1', | | 22 | +}); | | 23 | + | | 24 | +for (const passphrase of [ | | 25 | + Buffer.alloc(0), | | 26 | + Buffer.from([0x62, 0x69, 0x00, 0x6e, 0x61, 0x72, 0x79]), | | 27 | +]) { | | 28 | + const pem = privateKey.export({ | | 29 | + type: 'sec1', | | 30 | + format: 'pem', | | 31 | + cipher: 'aes-128-cbc', | | 32 | + passphrase, | | 33 | + }); | | 34 | + assert.match(pem.toString(), /^-----BEGIN EC PRIVATE KEY-----\n/); | | 35 | + assert.strictEqual(createPrivateKey({ key: pem, passphrase }) | | 36 | + .asymmetricKeyType, 'ec'); | | 37 | + // A wrong CBC password can occasionally yield valid padding, in which case | | 38 | + // ASN.1 decoding fails instead. | | 39 | + assert.throws(() => createPrivateKey({ | | 40 | + key: pem, | | 41 | + passphrase: Buffer.from('wrong'), | | 42 | + }), { code: /^ERR_OSSL_(BAD_DECRYPT|UNSUPPORTED)$/ }); | | 43 | + assert.throws(() => createPrivateKey({ key: pem }), { | | 44 | + code: 'ERR_OSSL_CRYPTO_INTERRUPTED_OR_CANCELLED', | | 45 | + }); | | 46 | +} |
|