GeezerVison is showing github.com — crypto: include EC public keys in PKCS8 exports · nodejs/node@a0989d7
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 +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7540,6 +7540,24 @@ int Ec::getCurve() const { | 7540 | 7540 | return EC_GROUP_get_curve_name(getGroup()); | 7541 | 7541 | } | 7542 | 7542 |
| | 7543 | +BIOPointer Ec::ExportPrivatePkcs8(const EVPKeyPointer& key) { | | 7544 | + MarkPopErrorOnReturn mark_pop_error_on_return; | | 7545 | + if (!key || !key.isA(KeyAlgorithm::EC)) return {}; | | 7546 | + auto ec = ECKeyPointer(key).clone(); | | 7547 | + if (!ec) return {}; | | 7548 | +#if NCRYPTO_USE_LEGACY_KEY_TYPES | | 7549 | + // Decoding an ECPrivateKey without publicKey reconstructs the public point | | 7550 | + // but retains a flag that omits it from subsequent encodings. | | 7551 | + EC_KEY_set_enc_flags(ec.get(), | | 7552 | + EC_KEY_get_enc_flags(ec.get()) & ~EC_PKEY_NO_PUBKEY); | | 7553 | +#endif | | 7554 | + auto export_key = EVPKeyPointer::New(); | | 7555 | + if (!export_key || !export_key.set(ec)) return {}; | | 7556 | + auto encoded = export_key.writePrivateKey({}); | | 7557 | + if (!encoded) return {}; | | 7558 | + return std::move(encoded.value); | | 7559 | +} | | 7560 | + | 7543 | 7561 | DataPointer Ec::TryExportPublic(const EVPKeyPointer& key, | 7544 | 7562 | point_conversion_form_t form) { | 7545 | 7563 | if (!key || form != POINT_CONVERSION_UNCOMPRESSED) return {}; | |
Collapse file Copy file name to clipboardExpand all lines: deps/ncrypto/ncrypto.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -778,6 +778,7 @@ class Ec final { | 778 | 778 | static DataPointer TryExportPublic(const EVPKeyPointer& key, | 779 | 779 | point_conversion_form_t form); | 780 | 780 | static DataPointer ExportPrivate(const EVPKeyPointer& key); | | 781 | + static BIOPointer ExportPrivatePkcs8(const EVPKeyPointer& key); | 781 | 782 | static bool GetKeyComponents(const EVPKeyPointer& key, | 782 | 783 | BignumPointer* x, | 783 | 784 | BignumPointer* y, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/ec.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -119,7 +119,7 @@ function ecExportKey(key, format) { | 119 | 119 | } | 120 | 120 | case kWebCryptoKeyFormatPKCS8: { | 121 | 121 | return TypedArrayPrototypeGetBuffer( | 122 | | - handle.export(kKeyFormatDER, kWebCryptoKeyFormatPKCS8, null, null)); | | 122 | + handle.exportECPrivatePkcs8()); | 123 | 123 | } | 124 | 124 | default: | 125 | 125 | return undefined; | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_keys.cc +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1138,6 +1138,8 @@ Local<Function> KeyObjectHandle::Initialize(Environment* env) { | 1138 | 1138 | isolate, templ, "exportECPublicRaw", ExportECPublicRaw); | 1139 | 1139 | SetProtoMethodNoSideEffect( | 1140 | 1140 | isolate, templ, "exportECPrivateRaw", ExportECPrivateRaw); | | 1141 | + SetProtoMethodNoSideEffect( | | 1142 | + isolate, templ, "exportECPrivatePkcs8", ExportECPrivatePkcs8); | 1141 | 1143 | SetProtoMethod(isolate, templ, "keyDetail", GetKeyDetail); | 1142 | 1144 | SetProtoMethod(isolate, templ, "equals", Equals); | 1143 | 1145 |
| @@ -1161,6 +1163,7 @@ void KeyObjectHandle::RegisterExternalReferences( | 1161 | 1163 | registry->Register(RawSeed); | 1162 | 1164 | registry->Register(ExportECPublicRaw); | 1163 | 1165 | registry->Register(ExportECPrivateRaw); | | 1166 | + registry->Register(ExportECPrivatePkcs8); | 1164 | 1167 | registry->Register(GetKeyDetail); | 1165 | 1168 | registry->Register(Equals); | 1166 | 1169 | } | @@ -1577,6 +1580,24 @@ void KeyObjectHandle::ExportECPrivateRaw( | 1577 | 1580 | .FromMaybe(Local<Value>())); | 1578 | 1581 | } | 1579 | 1582 |
| | 1583 | +void KeyObjectHandle::ExportECPrivatePkcs8( | | 1584 | + const FunctionCallbackInfo<Value>& args) { | | 1585 | + Environment* env = Environment::GetCurrent(args); | | 1586 | + KeyObjectHandle* key; | | 1587 | + ASSIGN_OR_RETURN_UNWRAP(&key, args.This()); | | 1588 | + const KeyObjectData& data = key->Data(); | | 1589 | + CHECK_EQ(data.GetKeyType(), kKeyTypePrivate); | | 1590 | + Mutex::ScopedLock lock(data.mutex()); | | 1591 | + auto encoded = ncrypto::Ec::ExportPrivatePkcs8(data.GetAsymmetricKey()); | | 1592 | + if (!encoded) { | | 1593 | + return THROW_ERR_CRYPTO_OPERATION_FAILED(env, | | 1594 | + "Failed to export EC private key"); | | 1595 | + } | | 1596 | + const EVPKeyPointer::PrivateKeyEncodingConfig config; | | 1597 | + args.GetReturnValue().Set( | | 1598 | + ToV8Value(env, encoded, config).FromMaybe(Local<Value>())); | | 1599 | +} | | 1600 | + | 1580 | 1601 | void KeyObjectHandle::RawSeed(const v8::FunctionCallbackInfo<v8::Value>& args) { | 1581 | 1602 | Environment* env = Environment::GetCurrent(args); | 1582 | 1603 | KeyObjectHandle* key; | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_keys.h +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -181,6 +181,8 @@ class KeyObjectHandle : public BaseObject { | 181 | 181 | const v8::FunctionCallbackInfo<v8::Value>& args); | 182 | 182 | static void ExportECPrivateRaw( | 183 | 183 | const v8::FunctionCallbackInfo<v8::Value>& args); | | 184 | + static void ExportECPrivatePkcs8( | | 185 | + const v8::FunctionCallbackInfo<v8::Value>& args); | 184 | 186 | static void RawSeed(const v8::FunctionCallbackInfo<v8::Value>& args); | 185 | 187 |
| 186 | 188 | v8::MaybeLocal<v8::Value> ExportSecretKey() const; | |
Collapse file Copy file name to clipboard +49Lines changed: 49 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 assert = require('assert'); | | 8 | +const { createPrivateKey, KeyObject } = require('crypto'); | | 9 | +const fixtures = require('../common/fixtures'); | | 10 | +const { subtle } = globalThis.crypto; | | 11 | + | | 12 | +function der(tag, ...parts) { | | 13 | + const body = Buffer.concat(parts); | | 14 | + const length = body.length < 128 ? [body.length] : [0x81, body.length]; | | 15 | + return Buffer.concat([Buffer.from([tag, ...length]), body]); | | 16 | +} | | 17 | + | | 18 | +(async () => { | | 19 | + for (const [curve, oid] of [ | | 20 | + ['p256', '06082a8648ce3d030107'], | | 21 | + ['p384', '06052b81040022'], | | 22 | + ['p521', '06052b81040023'], | | 23 | + ]) { | | 24 | + const privateKey = createPrivateKey(fixtures.readKey(`ec_${curve}_private.pem`)); | | 25 | + const expected = privateKey.export({ type: 'pkcs8', format: 'der' }); | | 26 | + const jwk = privateKey.export({ format: 'jwk' }); | | 27 | + const curveOid = Buffer.from(oid, 'hex'); | | 28 | + const algorithmIdentifier = der( | | 29 | + 0x30, Buffer.from('06072a8648ce3d0201', 'hex'), curveOid); | | 30 | + | | 31 | + for (const includeParameters of [false, true]) { | | 32 | + const ecPrivateKey = der( | | 33 | + 0x30, Buffer.from('020101', 'hex'), der(0x04, Buffer.from(jwk.d, 'base64url')), | | 34 | + includeParameters ? der(0xa0, curveOid) : Buffer.alloc(0)); | | 35 | + const privateOnly = der( | | 36 | + 0x30, Buffer.from('020100', 'hex'), algorithmIdentifier, der(0x04, ecPrivateKey)); | | 37 | + for (const name of ['ECDSA', 'ECDH']) { | | 38 | + const key = await subtle.importKey( | | 39 | + 'pkcs8', privateOnly, { name, namedCurve: jwk.crv }, true, | | 40 | + name === 'ECDSA' ? ['sign'] : ['deriveBits']); | | 41 | + const original = KeyObject.from(key).export({ type: 'pkcs8', format: 'der' }); | | 42 | + const actual = Buffer.from(await subtle.exportKey('pkcs8', key)); | | 43 | + assert.deepStrictEqual(actual, expected); | | 44 | + assert.deepStrictEqual( | | 45 | + KeyObject.from(key).export({ type: 'pkcs8', format: 'der' }), original); | | 46 | + } | | 47 | + } | | 48 | + } | | 49 | +})().then(common.mustCall()); |
Collapse file Copy file name to clipboardExpand all lines: typings/internalBinding/crypto.d.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -583,6 +583,7 @@ declare namespace InternalCryptoBinding { | 583 | 583 | getAsymmetricKeyType(): string | undefined; | 584 | 584 | getSymmetricKeySize(): number; | 585 | 585 | checkEcKeyData(): boolean; | | 586 | + exportECPrivatePkcs8(): Buffer; | 586 | 587 | } | 587 | 588 |
| 588 | 589 | interface NativeKeyObject { | |
|