GeezerVison is showing github.com — crypto: fix ncrypto provider behavior · nodejs/node@2987e12
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 |
|---|
@@ -736,6 +736,8 @@ BignumPointer BignumPointer::clone() { | 736 | 736 | int BignumPointer::isPrime(int nchecks, | 737 | 737 | BignumPointer::PrimeCheckCallback innerCb) const { | 738 | 738 | BignumCtxPointer ctx(BN_CTX_new()); | | 739 | + if (!ctx) [[unlikely]] | | 740 | + return -1; | 739 | 741 | BignumGenCallbackPointer cb(nullptr); | 740 | 742 | if (innerCb != nullptr) { | 741 | 743 | cb = BignumGenCallbackPointer(BN_GENCB_new()); | @@ -753,7 +755,17 @@ int BignumPointer::isPrime(int nchecks, | 753 | 755 | &innerCb); | 754 | 756 | } | 755 | 757 | #if NCRYPTO_USE_OPENSSL3_PROVIDER | 756 | | - return BN_check_prime(get(), ctx.get(), cb.get()); | | 758 | + const int minimum_checks = GetBitCount(get()) > 2048 ? 128 : 64; | | 759 | + const int repetitions = | | 760 | + nchecks > minimum_checks ? 1 + (nchecks - 1) / minimum_checks : 1; | | 761 | + for (int i = 0; i < repetitions; i++) { | | 762 | + // BN_check_prime() may return without invoking the callback (e.g. small | | 763 | + // primes resolved by trial division), so check cancellation here too. | | 764 | + if (i > 0 && innerCb != nullptr && !innerCb(1, -1)) return -1; | | 765 | + const int result = BN_check_prime(get(), ctx.get(), cb.get()); | | 766 | + if (result != 1) return result; | | 767 | + } | | 768 | + return 1; | 757 | 769 | #elif NCRYPTO_USE_BORINGSSL | 758 | 770 | int is_probably_prime = 0; | 759 | 771 | if (BN_primality_test( | @@ -4472,14 +4484,17 @@ int WriteDERView(const void* x, unsigned char** out) { | 4472 | 4484 |
| 4473 | 4485 | bool WriteEncryptedTraditionalPEM(BIO* bio, | 4474 | 4486 | const EVP_PKEY* pkey, | | 4487 | + int selection, | | 4488 | + const char* structure, | | 4489 | + const char* label, | 4475 | 4490 | const EVP_CIPHER* cipher, | 4476 | 4491 | Buffer<char> passphrase) { | 4477 | 4492 | if (passphrase.len > static_cast<size_t>(INT_MAX)) return false; | 4478 | 4493 |
| 4479 | 4494 | unsigned char* der = nullptr; | 4480 | 4495 | size_t der_len = 0; | 4481 | 4496 | OSSLEncoderCtxPointer ctx(OSSL_ENCODER_CTX_new_for_pkey( | 4482 | | - pkey, OSSL_KEYMGMT_SELECT_KEYPAIR, "DER", "pkcs1", nullptr)); | | 4497 | + pkey, selection, "DER", structure, nullptr)); | 4483 | 4498 | if (!ctx) return false; | 4484 | 4499 |
| 4485 | 4500 | const int result = OSSL_ENCODER_to_data(ctx.get(), &der, &der_len); | @@ -4489,7 +4504,7 @@ bool WriteEncryptedTraditionalPEM(BIO* bio, | 4489 | 4504 | DERView der_view{der_storage.get<const unsigned char>(), der_len}; | 4490 | 4505 | return PEM_ASN1_write_bio( | 4491 | 4506 | WriteDERView, | 4492 | | - PEM_STRING_RSA, | | 4507 | + label, | 4493 | 4508 | bio, | 4494 | 4509 | &der_view, | 4495 | 4510 | cipher, | @@ -4701,8 +4716,13 @@ Result<BIOPointer, bool> EVPKeyPointer::writePrivateKey( | 4701 | 4716 | const EVP_CIPHER* cipher = | 4702 | 4717 | config.format == PKFormatType::PEM ? config.cipher.get() : nullptr; | 4703 | 4718 | if (cipher != nullptr && passphrase.len == 0) { | 4704 | | - err = | 4705 | | - !WriteEncryptedTraditionalPEM(bio.get(), get(), cipher, passphrase); | | 4719 | + err = !WriteEncryptedTraditionalPEM(bio.get(), | | 4720 | + get(), | | 4721 | + OSSL_KEYMGMT_SELECT_KEYPAIR, | | 4722 | + "pkcs1", | | 4723 | + PEM_STRING_RSA, | | 4724 | + cipher, | | 4725 | + passphrase); | 4706 | 4726 | } else { | 4707 | 4727 | err = !WriteEncodedPKey(bio.get(), | 4708 | 4728 | get(), | @@ -4797,13 +4817,23 @@ Result<BIOPointer, bool> EVPKeyPointer::writePrivateKey( | 4797 | 4817 | #if NCRYPTO_USE_OPENSSL3_PROVIDER | 4798 | 4818 | const EVP_CIPHER* cipher = | 4799 | 4819 | config.format == PKFormatType::PEM ? config.cipher.get() : nullptr; | 4800 | | - err = !WriteEncodedPKey(bio.get(), | 4801 | | - get(), | 4802 | | - OSSL_KEYMGMT_SELECT_ALL, | 4803 | | - config.format, | 4804 | | - "type-specific", | 4805 | | - cipher, | 4806 | | - passphrase); | | 4820 | + if (cipher != nullptr && passphrase.len == 0) { | | 4821 | + err = !WriteEncryptedTraditionalPEM(bio.get(), | | 4822 | + get(), | | 4823 | + OSSL_KEYMGMT_SELECT_ALL, | | 4824 | + "type-specific", | | 4825 | + PEM_STRING_ECPRIVATEKEY, | | 4826 | + cipher, | | 4827 | + passphrase); | | 4828 | + } else { | | 4829 | + err = !WriteEncodedPKey(bio.get(), | | 4830 | + get(), | | 4831 | + OSSL_KEYMGMT_SELECT_ALL, | | 4832 | + config.format, | | 4833 | + "type-specific", | | 4834 | + cipher, | | 4835 | + passphrase); | | 4836 | + } | 4807 | 4837 | #else | 4808 | 4838 | #if OPENSSL_VERSION_MAJOR >= 3 | 4809 | 4839 | 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 |
|---|
@@ -491,6 +491,82 @@ TEST(NodeCrypto, RsaPrimeProduct) { | 491 | 491 | EXPECT_FALSE(rsa.checkPrimeProduct()); | 492 | 492 | } | 493 | 493 |
| | 494 | +#if NCRYPTO_USE_OPENSSL3_PROVIDER | | 495 | +namespace { | | 496 | +constexpr int kMinimumPrimeChecks = 64; | | 497 | + | | 498 | +bool CheckMersennePrime(int exponent, int requested_checks) { | | 499 | + auto candidate = ncrypto::BignumPointer::New(); | | 500 | + if (!candidate || BN_set_bit(candidate.get(), exponent) != 1 || | | 501 | + BN_sub_word(candidate.get(), 1) != 1) { | | 502 | + return false; | | 503 | + } | | 504 | + | | 505 | + int completed_checks = 0; | | 506 | + const int result = candidate.isPrime( | | 507 | + requested_checks, [&completed_checks](int event, int iteration) { | | 508 | + if (event == 1 && iteration >= 0) completed_checks++; | | 509 | + return true; | | 510 | + }); | | 511 | + return result == 1 && completed_checks >= requested_checks; | | 512 | +} | | 513 | +} // namespace | | 514 | + | | 515 | +TEST(NodeCrypto, BignumPrimeChecksHonorRequestedCount) { | | 516 | + EXPECT_TRUE(CheckMersennePrime(127, kMinimumPrimeChecks + 1)); | | 517 | + EXPECT_TRUE(CheckMersennePrime(2203, 129)); | | 518 | +} | | 519 | + | | 520 | +TEST(NodeCrypto, BignumPrimeCheckCancellation) { | | 521 | + auto candidate = ncrypto::BignumPointer::New(); | | 522 | + ASSERT_TRUE(candidate); | | 523 | + ASSERT_EQ(BN_set_bit(candidate.get(), 127), 1); | | 524 | + ASSERT_EQ(BN_sub_word(candidate.get(), 1), 1); | | 525 | + | | 526 | + int invocations = 0; | | 527 | + int completed_checks = 0; | | 528 | + bool valid_events = true; | | 529 | + const int result = | | 530 | + candidate.isPrime(kMinimumPrimeChecks + 1, [&](int event, int iteration) { | | 531 | + if (event != 1) { | | 532 | + valid_events = false; | | 533 | + return false; | | 534 | + } | | 535 | + if (iteration == -1) { | | 536 | + invocations++; | | 537 | + return true; | | 538 | + } | | 539 | + if (iteration != completed_checks % kMinimumPrimeChecks) { | | 540 | + valid_events = false; | | 541 | + return false; | | 542 | + } | | 543 | + if (completed_checks == kMinimumPrimeChecks) return false; | | 544 | + completed_checks++; | | 545 | + return true; | | 546 | + }); | | 547 | + EXPECT_EQ(result, -1); | | 548 | + EXPECT_TRUE(valid_events); | | 549 | + EXPECT_EQ(invocations, 3); | | 550 | + EXPECT_EQ(completed_checks, kMinimumPrimeChecks); | | 551 | + ERR_clear_error(); | | 552 | +} | | 553 | + | | 554 | +TEST(NodeCrypto, BignumPrimeCheckCancellationSmallPrime) { | | 555 | + // 311 is resolved by trial division, so OpenSSL never invokes the callback. | | 556 | + auto candidate = ncrypto::BignumPointer::New(); | | 557 | + ASSERT_TRUE(candidate); | | 558 | + ASSERT_EQ(BN_set_word(candidate.get(), 311), 1); | | 559 | + | | 560 | + int invocations = 0; | | 561 | + const int result = candidate.isPrime(INT_MAX, [&](int, int) { | | 562 | + invocations++; | | 563 | + return false; | | 564 | + }); | | 565 | + EXPECT_EQ(result, -1); | | 566 | + EXPECT_EQ(invocations, 1); | | 567 | +} | | 568 | +#endif | | 569 | + | 494 | 570 | TEST(NodeCrypto, EcKeyComponents) { | 495 | 571 | ncrypto::ClearErrorOnReturn clear_errors; | 496 | 572 | 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 | +} |
|