GeezerVison is showing github.com — crypto: remove key export and metadata locks · nodejs/node@14bd819
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: src/crypto/crypto_ec.cc -4Lines changed: 0 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -485,7 +485,6 @@ WebCryptoKeyExportStatus EC_Raw_Export(const KeyObjectData& key_data, | 485 | 485 | ByteSource* out) { | 486 | 486 | const auto& m_pkey = key_data.GetAsymmetricKey(); | 487 | 487 | CHECK(m_pkey); | 488 | | - Mutex::ScopedLock lock(key_data.mutex()); | 489 | 488 |
| 490 | 489 | if (!m_pkey.isA(KeyAlgorithm::EC)) { | 491 | 490 | switch (key_data.GetKeyType()) { | @@ -573,7 +572,6 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport( | 573 | 572 | // the header but it is a somewhat silly hoop to jump through because | 574 | 573 | // the header is for all practical purposes a static 26 byte sequence | 575 | 574 | // where only the second byte changes. | 576 | | - Mutex::ScopedLock lock(key_data.mutex()); | 577 | 575 | ECKeyPointer ec_key(m_pkey); | 578 | 576 | if (!ec_key) return WebCryptoKeyExportStatus::FAILED; | 579 | 577 | const auto group = ec_key.getGroup(); | @@ -617,7 +615,6 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport( | 617 | 615 | bool ExportJWKEcKey(Environment* env, | 618 | 616 | const KeyObjectData& key, | 619 | 617 | Local<Object> target) { | 620 | | - Mutex::ScopedLock lock(key.mutex()); | 621 | 618 | const auto& m_pkey = key.GetAsymmetricKey(); | 622 | 619 | DCHECK(m_pkey.isA(KeyAlgorithm::EC)); | 623 | 620 |
| @@ -762,7 +759,6 @@ KeyObjectData ImportJWKEcKey(Environment* env, Local<Object> jwk) { | 762 | 759 | bool GetEcKeyDetail(Environment* env, | 763 | 760 | const KeyObjectData& key, | 764 | 761 | Local<Object> target) { | 765 | | - Mutex::ScopedLock lock(key.mutex()); | 766 | 762 | const auto& m_pkey = key.GetAsymmetricKey(); | 767 | 763 | DCHECK(m_pkey.isA(KeyAlgorithm::EC)); | 768 | 764 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_keys.cc +14-21Lines changed: 14 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -188,7 +188,6 @@ KeyObjectData ImportJWKSecretKey(Environment* env, Local<Object> jwk) { | 188 | 188 | static bool ExportJWKRawKey(Environment* env, | 189 | 189 | const KeyObjectData& key, | 190 | 190 | Local<Object> target) { | 191 | | - Mutex::ScopedLock lock(key.mutex()); | 192 | 191 | auto result = | 193 | 192 | key.GetAsymmetricKey().exportRawJwk(key.GetKeyType() == kKeyTypePrivate); | 194 | 193 | if (!result) { | @@ -417,7 +416,6 @@ bool KeyObjectData::ToEncodedPublicKey( | 417 | 416 | return ExportJWKInner( | 418 | 417 | env, addRefWithType(KeyType::kKeyTypePublic), *out, false); | 419 | 418 | } else if (config.format == EVPKeyPointer::PKFormatType::RAW_PUBLIC) { | 420 | | - Mutex::ScopedLock lock(mutex()); | 421 | 419 | const auto& pkey = GetAsymmetricKey(); | 422 | 420 | const auto* algorithm = pkey.getAlgorithm(); | 423 | 421 | if (algorithm == &KeyAlgorithm::EC) { | @@ -472,7 +470,6 @@ bool KeyObjectData::ToEncodedPrivateKey( | 472 | 470 | return ExportJWKInner( | 473 | 471 | env, addRefWithType(KeyType::kKeyTypePrivate), *out, false); | 474 | 472 | } else if (config.format == EVPKeyPointer::PKFormatType::RAW_PRIVATE) { | 475 | | - Mutex::ScopedLock lock(mutex()); | 476 | 473 | const auto& pkey = GetAsymmetricKey(); | 477 | 474 | const auto* algorithm = pkey.getAlgorithm(); | 478 | 475 | if (algorithm == &KeyAlgorithm::EC) { | @@ -498,7 +495,6 @@ bool KeyObjectData::ToEncodedPrivateKey( | 498 | 495 | return Buffer::Copy(env, raw_data.get<const char>(), raw_data.size()) | 499 | 496 | .ToLocal(out); | 500 | 497 | } else if (config.format == EVPKeyPointer::PKFormatType::RAW_SEED) { | 501 | | - Mutex::ScopedLock lock(mutex()); | 502 | 498 | const auto& pkey = GetAsymmetricKey(); | 503 | 499 | auto raw_data = pkey.rawSeed(); | 504 | 500 | if (!raw_data) { | @@ -1057,9 +1053,7 @@ KeyObjectData::KeyObjectData(ByteSource symmetric_key) | 1057 | 1053 | data_(std::make_shared<Data>(std::move(symmetric_key))) {} | 1058 | 1054 |
| 1059 | 1055 | KeyObjectData::KeyObjectData(KeyType type, EVPKeyPointer&& pkey) | 1060 | | - : key_type_(type), | 1061 | | - mutex_(std::make_shared<Mutex>()), | 1062 | | - data_(std::make_shared<Data>(std::move(pkey))) {} | | 1056 | + : key_type_(type), data_(std::make_shared<Data>(std::move(pkey))) {} | 1063 | 1057 |
| 1064 | 1058 | void KeyObjectData::Data::MemoryInfo(MemoryTracker* tracker) const { | 1065 | 1059 | if (asymmetric_key) { | @@ -1076,11 +1070,6 @@ void KeyObjectData::MemoryInfo(MemoryTracker* tracker) const { | 1076 | 1070 | tracker->TrackField("data", data_); | 1077 | 1071 | } | 1078 | 1072 |
| 1079 | | -Mutex& KeyObjectData::mutex() const { | 1080 | | - if (!mutex_) mutex_ = std::make_shared<Mutex>(); | 1081 | | - return *mutex_.get(); | 1082 | | -} | 1083 | | - | 1084 | 1073 | KeyObjectData KeyObjectData::CreateSecret(ByteSource key) { | 1085 | 1074 | return KeyObjectData(std::move(key)); | 1086 | 1075 | } | @@ -1475,7 +1464,6 @@ void KeyObjectHandle::RawPublicKey( | 1475 | 1464 | const KeyObjectData& data = key->Data(); | 1476 | 1465 | CHECK_NE(data.GetKeyType(), kKeyTypeSecret); | 1477 | 1466 |
| 1478 | | - Mutex::ScopedLock lock(data.mutex()); | 1479 | 1467 | const auto& pkey = data.GetAsymmetricKey(); | 1480 | 1468 |
| 1481 | 1469 | const bool is_raw_supported = pkey.supportsRawPublic(); | @@ -1503,7 +1491,6 @@ void KeyObjectHandle::RawPrivateKey( | 1503 | 1491 | const KeyObjectData& data = key->Data(); | 1504 | 1492 | CHECK_EQ(data.GetKeyType(), kKeyTypePrivate); | 1505 | 1493 |
| 1506 | | - Mutex::ScopedLock lock(data.mutex()); | 1507 | 1494 | const auto& pkey = data.GetAsymmetricKey(); | 1508 | 1495 |
| 1509 | 1496 | const bool is_raw_supported = pkey.supportsRawPrivate(); | @@ -1531,7 +1518,6 @@ void KeyObjectHandle::ExportECPublicRaw( | 1531 | 1518 | const KeyObjectData& data = key->Data(); | 1532 | 1519 | CHECK_NE(data.GetKeyType(), kKeyTypeSecret); | 1533 | 1520 |
| 1534 | | - Mutex::ScopedLock lock(data.mutex()); | 1535 | 1521 | const auto& m_pkey = data.GetAsymmetricKey(); | 1536 | 1522 | if (!m_pkey.isA(KeyAlgorithm::EC)) { | 1537 | 1523 | return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env); | @@ -1570,7 +1556,6 @@ void KeyObjectHandle::ExportECPrivateRaw( | 1570 | 1556 | const KeyObjectData& data = key->Data(); | 1571 | 1557 | CHECK_EQ(data.GetKeyType(), kKeyTypePrivate); | 1572 | 1558 |
| 1573 | | - Mutex::ScopedLock lock(data.mutex()); | 1574 | 1559 | const auto& m_pkey = data.GetAsymmetricKey(); | 1575 | 1560 | if (!m_pkey.isA(KeyAlgorithm::EC)) { | 1576 | 1561 | return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(env); | @@ -1593,7 +1578,6 @@ void KeyObjectHandle::ExportECPrivatePkcs8( | 1593 | 1578 | ASSIGN_OR_RETURN_UNWRAP(&key, args.This()); | 1594 | 1579 | const KeyObjectData& data = key->Data(); | 1595 | 1580 | CHECK_EQ(data.GetKeyType(), kKeyTypePrivate); | 1596 | | - Mutex::ScopedLock lock(data.mutex()); | 1597 | 1581 | auto encoded = ncrypto::Ec::ExportPrivatePkcs8(data.GetAsymmetricKey()); | 1598 | 1582 | if (!encoded) { | 1599 | 1583 | return THROW_ERR_CRYPTO_OPERATION_FAILED(env, | @@ -1612,7 +1596,6 @@ void KeyObjectHandle::RawSeed(const v8::FunctionCallbackInfo<v8::Value>& args) { | 1612 | 1596 | const KeyObjectData& data = key->Data(); | 1613 | 1597 | CHECK_EQ(data.GetKeyType(), kKeyTypePrivate); | 1614 | 1598 |
| 1615 | | - Mutex::ScopedLock lock(data.mutex()); | 1616 | 1599 | const auto& pkey = data.GetAsymmetricKey(); | 1617 | 1600 |
| 1618 | 1601 | auto raw_data = pkey.rawSeed(); | @@ -1800,7 +1783,6 @@ std::unique_ptr<worker::TransferData> NativeKeyObject::CloneForMessaging() | 1800 | 1783 | WebCryptoKeyExportStatus PKEY_SPKI_Export(const KeyObjectData& key_data, | 1801 | 1784 | ByteSource* out) { | 1802 | 1785 | CHECK_EQ(key_data.GetKeyType(), kKeyTypePublic); | 1803 | | - Mutex::ScopedLock lock(key_data.mutex()); | 1804 | 1786 | auto bio = key_data.GetAsymmetricKey().derPublicKey(); | 1805 | 1787 | if (!bio) return WebCryptoKeyExportStatus::FAILED; | 1806 | 1788 | *out = ByteSource::FromBIO(bio); | @@ -1810,12 +1792,23 @@ WebCryptoKeyExportStatus PKEY_SPKI_Export(const KeyObjectData& key_data, | 1810 | 1792 | WebCryptoKeyExportStatus PKEY_PKCS8_Export(const KeyObjectData& key_data, | 1811 | 1793 | ByteSource* out) { | 1812 | 1794 | CHECK_EQ(key_data.GetKeyType(), kKeyTypePrivate); | 1813 | | - Mutex::ScopedLock lock(key_data.mutex()); | 1814 | 1795 | const auto& m_pkey = key_data.GetAsymmetricKey(); | | 1796 | + EVP_PKEY* export_key = m_pkey.get(); | | 1797 | +#if NCRYPTO_USE_OPENSSL3_PROVIDER | | 1798 | + // OpenSSL's provider EC PKCS8 encoders temporarily change encoding flags. | | 1799 | + // Use an independent key so concurrent exports do not change the source. | | 1800 | + EVPKeyPointer key_copy; | | 1801 | + if ((m_pkey.isA(KeyAlgorithm::EC) || m_pkey.isA(KeyAlgorithm::SM2)) && | | 1802 | + EVP_PKEY_get0_provider(m_pkey.get()) != nullptr) { | | 1803 | + key_copy.reset(EVP_PKEY_dup(m_pkey.get())); | | 1804 | + if (!key_copy) return WebCryptoKeyExportStatus::FAILED; | | 1805 | + export_key = key_copy.get(); | | 1806 | + } | | 1807 | +#endif | 1815 | 1808 |
| 1816 | 1809 | auto bio = BIOPointer::NewMem(); | 1817 | 1810 | CHECK(bio); | 1818 | | - PKCS8Pointer p8inf(EVP_PKEY2PKCS8(m_pkey.get())); | | 1811 | + PKCS8Pointer p8inf(EVP_PKEY2PKCS8(export_key)); | 1819 | 1812 | if (!i2d_PKCS8_PRIV_KEY_INFO_bio(bio.get(), p8inf.get())) | 1820 | 1813 | return WebCryptoKeyExportStatus::FAILED; | 1821 | 1814 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_keys.h +6-11Lines changed: 6 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,8 +54,8 @@ class KeyObjectData final : public MemoryRetainer { | 54 | 54 |
| 55 | 55 | KeyType GetKeyType() const; | 56 | 56 |
| 57 | | - // These functions allow unprotected access to the raw key material and should | 58 | | - // only be used to implement cryptographic operations requiring the key. | | 57 | + // The key material is immutable and can be used concurrently by operations | | 58 | + // with separate contexts. | 59 | 59 | const ncrypto::EVPKeyPointer& GetAsymmetricKey() const; | 60 | 60 | const char* GetSymmetricKey() const; | 61 | 61 | size_t GetSymmetricKeySize() const; | @@ -64,8 +64,6 @@ class KeyObjectData final : public MemoryRetainer { | 64 | 64 | SET_MEMORY_INFO_NAME(KeyObjectData) | 65 | 65 | SET_SELF_SIZE(KeyObjectData) | 66 | 66 |
| 67 | | - Mutex& mutex() const; | 68 | | - | 69 | 67 | static v8::Maybe<ncrypto::EVPKeyPointer::PublicKeyEncodingConfig> | 70 | 68 | GetPublicKeyEncodingFromJs(const v8::FunctionCallbackInfo<v8::Value>& args, | 71 | 69 | unsigned int* offset, | @@ -97,11 +95,11 @@ class KeyObjectData final : public MemoryRetainer { | 97 | 95 | v8::Local<v8::Value>* out); | 98 | 96 |
| 99 | 97 | inline KeyObjectData addRef() const { | 100 | | - return KeyObjectData(key_type_, mutex_, data_); | | 98 | + return KeyObjectData(key_type_, data_); | 101 | 99 | } | 102 | 100 |
| 103 | 101 | inline KeyObjectData addRefWithType(KeyType type) const { | 104 | | - return KeyObjectData(type, mutex_, data_); | | 102 | + return KeyObjectData(type, data_); | 105 | 103 | } | 106 | 104 |
| 107 | 105 | private: | @@ -115,7 +113,6 @@ class KeyObjectData final : public MemoryRetainer { | 115 | 113 | const char* default_msg); | 116 | 114 |
| 117 | 115 | KeyType key_type_; | 118 | | - mutable std::shared_ptr<Mutex> mutex_; | 119 | 116 |
| 120 | 117 | struct Data final : public MemoryRetainer { | 121 | 118 | const ByteSource symmetric_key; | @@ -131,10 +128,8 @@ class KeyObjectData final : public MemoryRetainer { | 131 | 128 | }; | 132 | 129 | std::shared_ptr<Data> data_; | 133 | 130 |
| 134 | | - KeyObjectData(KeyType type, | 135 | | - std::shared_ptr<Mutex> mutex, | 136 | | - std::shared_ptr<Data> data) | 137 | | - : key_type_(type), mutex_(std::move(mutex)), data_(std::move(data)) {} | | 131 | + KeyObjectData(KeyType type, std::shared_ptr<Data> data) | | 132 | + : key_type_(type), data_(std::move(data)) {} | 138 | 133 | }; | 139 | 134 |
| 140 | 135 | class KeyObjectHandle : public BaseObject { | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_rsa.cc -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -340,7 +340,6 @@ WebCryptoCipherStatus RSACipherTraits::DoCipher(Environment* env, | 340 | 340 | bool ExportJWKRsaKey(Environment* env, | 341 | 341 | const KeyObjectData& key, | 342 | 342 | Local<Object> target) { | 343 | | - Mutex::ScopedLock lock(key.mutex()); | 344 | 343 | const auto& m_pkey = key.GetAsymmetricKey(); | 345 | 344 |
| 346 | 345 | const ncrypto::Rsa rsa = m_pkey; | @@ -568,7 +567,6 @@ KeyObjectData ImportJWKRsaKey(Environment* env, Local<Object> jwk) { | 568 | 567 | bool GetRsaKeyDetail(Environment* env, | 569 | 568 | const KeyObjectData& key, | 570 | 569 | Local<Object> target) { | 571 | | - Mutex::ScopedLock lock(key.mutex()); | 572 | 570 | const auto& m_pkey = key.GetAsymmetricKey(); | 573 | 571 |
| 574 | 572 | const auto rsa = ncrypto::Rsa::PublicOnly(m_pkey); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-key-reuse-concurrent.js +83-3Lines changed: 83 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Flags: --expose-internals | 1 | 2 | 'use strict'; | 2 | 3 |
| 3 | 4 | const common = require('../common'); | @@ -18,6 +19,17 @@ const { once } = require('events'); | 18 | 19 | const { promisify } = require('util'); | 19 | 20 | const { Worker, parentPort, workerData } = require('worker_threads'); | 20 | 21 | const fixtures = require('../common/fixtures'); | | 22 | +const { internalBinding } = require('internal/test/binding'); | | 23 | +const { getKeyObjectHandle } = require('internal/crypto/keys'); | | 24 | +const { | | 25 | + ECKeyExportJob, | | 26 | + RSAKeyExportJob, | | 27 | + kCryptoJobWebCrypto, | | 28 | + kKeyVariantRSA_SSA_PKCS1_v1_5, | | 29 | + kWebCryptoKeyFormatRaw, | | 30 | + kWebCryptoKeyFormatPKCS8, | | 31 | + kWebCryptoKeyFormatSPKI, | | 32 | +} = internalBinding('crypto'); | 21 | 33 | const { subtle } = globalThis.crypto; | 22 | 34 | const signAsync = promisify(sign); | 23 | 35 | const verifyAsync = promisify(verify); | @@ -28,6 +40,14 @@ const sec1 = { type: 'sec1', format: 'der' }; | 28 | 40 | const spki = { type: 'spki', format: 'der' }; | 29 | 41 | const iterations = 16; | 30 | 42 |
| | 43 | +function exportLegacyKey(Job, format, key, expected, ...params) { | | 44 | + return new Job( | | 45 | + kCryptoJobWebCrypto, format, getKeyObjectHandle(key), ...params) | | 46 | + .run().then((encoded) => { | | 47 | + assert.deepStrictEqual(Buffer.from(encoded), Buffer.from(expected)); | | 48 | + }); | | 49 | +} | | 50 | + | 31 | 51 | async function exercise({ keys, peer, expected }) { | 32 | 52 | // Check generated signatures and exports with independent imports that do | 33 | 53 | // not warm the shared key's provider caches before its first operation. | @@ -37,6 +57,9 @@ async function exercise({ keys, peer, expected }) { | 37 | 57 | const referencePublic = createPublicKey({ key: expected.public, ...spki }); | 38 | 58 | const referencePem = referencePrivate.export(pkcs8Pem); | 39 | 59 | const referenceSec1 = referencePrivate.export(sec1); | | 60 | + const referenceRsaPublic = createPublicKey({ | | 61 | + key: expected.rsa.public, ...spki, | | 62 | + }); | 40 | 63 |
| 41 | 64 | for (let i = 0; i < iterations; i++) { | 42 | 65 | const data = Buffer.from(`shared key operation ${i}`); | @@ -76,6 +99,40 @@ async function exercise({ keys, peer, expected }) { | 76 | 99 | ); | 77 | 100 | } | 78 | 101 |
| | 102 | + // Public Web Crypto exports use direct bindings. Exercise the older | | 103 | + // export jobs explicitly while the same keys are used by other jobs. | | 104 | + pending.push( | | 105 | + exportLegacyKey(ECKeyExportJob, kWebCryptoKeyFormatRaw, | | 106 | + keys.publicKey, expected.rawPublic), | | 107 | + exportLegacyKey(ECKeyExportJob, kWebCryptoKeyFormatSPKI, | | 108 | + keys.publicKey, expected.public), | | 109 | + exportLegacyKey(ECKeyExportJob, kWebCryptoKeyFormatPKCS8, | | 110 | + keys.privateKey, expected.originalPrivate), | | 111 | + exportLegacyKey(RSAKeyExportJob, kWebCryptoKeyFormatSPKI, | | 112 | + keys.rsa.publicKey, expected.rsa.public, | | 113 | + kKeyVariantRSA_SSA_PKCS1_v1_5), | | 114 | + exportLegacyKey(RSAKeyExportJob, kWebCryptoKeyFormatPKCS8, | | 115 | + keys.rsa.privateKey, expected.rsa.private, | | 116 | + kKeyVariantRSA_SSA_PKCS1_v1_5), | | 117 | + assert.rejects(new ECKeyExportJob( | | 118 | + kCryptoJobWebCrypto, kWebCryptoKeyFormatPKCS8, | | 119 | + getKeyObjectHandle(keys.publicKey)).run(), (err) => { | | 120 | + assert.strictEqual(err.name, 'OperationError'); | | 121 | + assert.strictEqual( | | 122 | + err.message, 'The operation failed for an operation-specific reason'); | | 123 | + assert.strictEqual(err.cause?.message, 'Invalid key type'); | | 124 | + return true; | | 125 | + }), | | 126 | + signAsync('sha256', data, keys.rsa.privateKey).then((signature) => { | | 127 | + assert.strictEqual( | | 128 | + verify('sha256', data, referenceRsaPublic, signature), true); | | 129 | + return verifyAsync('sha256', data, keys.rsa.publicKey, signature) | | 130 | + .then((valid) => { | | 131 | + assert.strictEqual(valid, true); | | 132 | + }); | | 133 | + }), | | 134 | + ); | | 135 | + | 79 | 136 | // Export, compare, and query metadata while jobs use this same native key | 80 | 137 | // in the threadpool and other Workers. Fresh wrappers also exercise the | 81 | 138 | // native metadata getters on every iteration rather than their JS caches. | @@ -106,7 +163,8 @@ async function exercise({ keys, peer, expected }) { | 106 | 163 | format: 'raw-public', type: 'compressed', | 107 | 164 | }), Buffer.from(expected.compressedPublic)); | 108 | 165 | assert.deepStrictEqual( | 109 | | - publicKey.export(spki), Buffer.from(expected.public)); | | 166 | + publicKey.export(spki), | | 167 | + Buffer.from(expected.originalPublic ?? expected.public)); | 110 | 168 | assert.deepStrictEqual( | 111 | 169 | privateKey.export(pkcs8), Buffer.from(expected.originalPrivate)); | 112 | 170 | assert.strictEqual(privateKey.export(pkcs8Pem), referencePem); | @@ -161,8 +219,15 @@ if (workerData?.sharedKeyTest) { | 161 | 219 | // exercising concurrent first use of the shared key. CryptoKey conversion | 162 | 220 | // validates the key before its first concurrent sign/derive/export calls. | 163 | 221 | const privateKey = createPrivateKey({ key: input, ...pkcs8 }); | 164 | | - const publicKey = createPublicKey(privateKey); | 165 | | - const keys = { privateKey, publicKey }; | | 222 | + const publicKey = expected.originalPublic === undefined ? | | 223 | + createPublicKey(privateKey) : | | 224 | + createPublicKey({ key: expected.originalPublic, ...spki }); | | 225 | + const rsaPrivate = createPrivateKey({ key: expected.rsa.private, ...pkcs8 }); | | 226 | + const keys = { | | 227 | + privateKey, | | 228 | + publicKey, | | 229 | + rsa: { privateKey: rsaPrivate, publicKey: createPublicKey(rsaPrivate) }, | | 230 | + }; | 166 | 231 | if (webcrypto) { | 167 | 232 | keys.ecdsaPrivate = privateKey.toCryptoKey({ | 168 | 233 | name: 'ECDSA', namedCurve: 'P-256', | @@ -198,6 +263,8 @@ if (workerData?.sharedKeyTest) { | 198 | 263 | privateKey.export(pkcs8), Buffer.from(expected.originalPrivate)); | 199 | 264 | assert.deepStrictEqual(privateKey.export(sec1), | 200 | 265 | createPrivateKey({ key: input, ...pkcs8 }).export(sec1)); | | 266 | + assert.deepStrictEqual(publicKey.export(spki), | | 267 | + Buffer.from(expected.originalPublic ?? expected.public)); | 201 | 268 | } | 202 | 269 |
| 203 | 270 | (async () => { | @@ -207,6 +274,7 @@ if (workerData?.sharedKeyTest) { | 207 | 274 | namedCurve: 'prime256v1', | 208 | 275 | }); | 209 | 276 | const privateJwk = reference.export({ format: 'jwk' }); | | 277 | + const rsaPrivate = createPrivateKey(fixtures.readKey('rsa_private_2048.pem')); | 210 | 278 | const expected = { | 211 | 279 | private: reference.export(pkcs8), | 212 | 280 | public: publicKey.export(spki), | @@ -224,6 +292,10 @@ if (workerData?.sharedKeyTest) { | 224 | 292 | dsaEncoding: 'ieee-p1363', | 225 | 293 | }); | 226 | 294 | }), | | 295 | + rsa: { | | 296 | + private: rsaPrivate.export(pkcs8), | | 297 | + public: createPublicKey(rsaPrivate).export(spki), | | 298 | + }, | 227 | 299 | }; | 228 | 300 | const cryptoPeer = peer.toCryptoKey({ | 229 | 301 | name: 'ECDH', namedCurve: 'P-256', | @@ -237,6 +309,14 @@ if (workerData?.sharedKeyTest) { | 237 | 309 |
| 238 | 310 | const algorithmIdentifier = der(0x30, Buffer.from( | 239 | 311 | '06072a8648ce3d020106082a8648ce3d030107', 'hex')); | | 312 | + const compressedPublic = der( | | 313 | + 0x30, algorithmIdentifier, | | 314 | + der(0x03, Buffer.from([0]), expected.compressedPublic)); | | 315 | + await run(expected.private, cryptoPeer, { | | 316 | + ...expected, | | 317 | + originalPrivate: expected.private, | | 318 | + originalPublic: compressedPublic, | | 319 | + }); | 240 | 320 | const ecPrivateKey = der( | 241 | 321 | 0x30, Buffer.from('020101', 'hex'), | 242 | 322 | der(0x04, Buffer.from(privateJwk.d, 'base64url'))); | |
|