MCPcopy Index your code
hub / github.com/nodejs/node / ecExportKey

Function ecExportKey

lib/internal/crypto/ec.js:90–132  ·  view source on GitHub ↗
(key, format)

Source from the content-addressed store, hash-verified

88}
89
90function ecExportKey(key, format) {
91 try {
92 const handle = getCryptoKeyHandle(key);
93 switch (format) {
94 case kWebCryptoKeyFormatRaw: {
95 return TypedArrayPrototypeGetBuffer(
96 handle.exportECPublicRaw(POINT_CONVERSION_UNCOMPRESSED));
97 }
98 case kWebCryptoKeyFormatSPKI: {
99 let spki = handle.export(kKeyFormatDER, kWebCryptoKeyFormatSPKI);
100 // WebCrypto requires uncompressed point format for SPKI exports.
101 // This is a very rare edge case dependent on the imported key
102 // using compressed point format.
103 // Expected SPKI DER byte lengths with uncompressed points:
104 // P-256: 91 = 26 bytes of SPKI ASN.1 + 65-byte uncompressed point.
105 // P-384: 120 = 23 bytes of SPKI ASN.1 + 97-byte uncompressed point.
106 // P-521: 158 = 25 bytes of SPKI ASN.1 + 133-byte uncompressed point.
107 // Difference in initial SPKI ASN.1 is caused by OIDs and length encoding.
108 const { namedCurve } = getCryptoKeyAlgorithm(key);
109 if (TypedArrayPrototypeGetByteLength(spki) !== {
110 '__proto__': null, 'P-256': 91, 'P-384': 120, 'P-521': 158,
111 }[namedCurve]) {
112 const raw = handle.exportECPublicRaw(POINT_CONVERSION_UNCOMPRESSED);
113 const tmp = new KeyObjectHandle();
114 tmp.init(kKeyTypePublic, raw, kKeyFormatRawPublic,
115 'ec', null, namedCurve);
116 spki = tmp.export(kKeyFormatDER, kWebCryptoKeyFormatSPKI);
117 }
118 return TypedArrayPrototypeGetBuffer(spki);
119 }
120 case kWebCryptoKeyFormatPKCS8: {
121 return TypedArrayPrototypeGetBuffer(
122 handle.export(kKeyFormatDER, kWebCryptoKeyFormatPKCS8, null, null));
123 }
124 default:
125 return undefined;
126 }
127 } catch (err) {
128 throw lazyDOMException(
129 'The operation failed for an operation-specific reason',
130 { name: 'OperationError', cause: err });
131 }
132}
133
134function ecImportKey(
135 format,

Callers

nothing calls this directly

Calls 5

getCryptoKeyHandleFunction · 0.85
getCryptoKeyAlgorithmFunction · 0.85
lazyDOMExceptionFunction · 0.85
exportMethod · 0.45
initMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…