Skip to content

Commit abd7c94

Browse files
panvatargos
authored andcommitted
crypto: fix webcrypto ECDH JWK import
This fixes the importKey operation when importing a JWK for the ECDH algorithm. As per the Web Crypto API specification the JWK `alg` property is not checked (as opposed to ECDSA). PR-URL: #35855 Fixes: #35812 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 0580258 commit abd7c94

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

lib/internal/crypto/ec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ async function ecImportKey(
230230
throw lazyDOMException('JWK is not extractable', 'DataError');
231231
}
232232

233-
if (keyData.alg !== undefined) {
233+
if (algorithm.name === 'ECDSA' && keyData.alg !== undefined) {
234234
if (typeof keyData.alg !== 'string')
235235
throw lazyDOMException('Invalid alg', 'DataError');
236236
switch (keyData.alg) {

test/parallel/test-webcrypto-export-import-ec.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ if (!common.hasCrypto)
88
const assert = require('assert');
99
const { subtle } = require('crypto').webcrypto;
1010

11-
const curves = ['P-384', 'P-521'];
11+
const curves = ['P-256', 'P-384', 'P-521'];
1212

1313
const keyData = {
1414
'P-521': {
15+
jwsAlg: 'ES512',
1516
spki: Buffer.from(
1617
'30819b301006072a8648ce3d020106052b8104002303818600040156f479f8df' +
1718
'1e20a7ffc04ce420c3e154ae251996bee42f034b84d41b743f34e45f311b813a' +
@@ -40,6 +41,7 @@ const keyData = {
4041
}
4142
},
4243
'P-384': {
44+
jwsAlg: 'ES384',
4345
spki: Buffer.from(
4446
'3076301006072a8648ce3d020106052b8104002203620004219c14d66617b36e' +
4547
'c6d8856b385b73a74d344fd8ae75ef046435dda54e3b44bd5fbdebd1d08dd69e' +
@@ -60,6 +62,26 @@ const keyData = {
6062
d: 'RTe1mQeE08LSLpao-S-hqkku6HPldqQVguFEGDyYiNEOa560ztSyzEAS5KxeqEBz'
6163
}
6264
},
65+
'P-256': {
66+
jwsAlg: 'ES256',
67+
spki: Buffer.from(
68+
'3059301306072a8648ce3d020106082a8648ce3d03010703420004d6e8328a95' +
69+
'fe29afcdc30977b9251efbb219022807f6b14bb34695b6b4bdb93ee6684548a4' +
70+
'ad13c49d00433c45315e8274f3540f58f5d79ef7a1b184f4c21d17', 'hex'),
71+
pkcs8: Buffer.from(
72+
'308187020100301306072a8648ce3d020106082a8648ce3d030107046d306b02' +
73+
'010104202bc2eda265e46866efa8f8f99da993175b6c85c246e15dceaed7e307' +
74+
'0f13fbf8a14403420004d6e8328a95fe29afcdc30977b9251efbb219022807f6' +
75+
'b14bb34695b6b4bdb93ee6684548a4ad13c49d00433c45315e8274f3540f58f5' +
76+
'd79ef7a1b184f4c21d17', 'hex'),
77+
jwk: {
78+
kty: 'EC',
79+
crv: 'P-256',
80+
x: '1ugyipX-Ka_Nwwl3uSUe-7IZAigH9rFLs0aVtrS9uT4',
81+
y: '5mhFSKStE8SdAEM8RTFegnTzVA9Y9dee96GxhPTCHRc',
82+
d: 'K8LtomXkaGbvqPj5namTF1tshcJG4V3OrtfjBw8T-_g'
83+
}
84+
},
6385
};
6486

6587
const testVectors = [
@@ -168,6 +190,26 @@ async function testImportJwk(
168190
jwk,
169191
{ name, namedCurve },
170192
extractable,
193+
privateUsages),
194+
subtle.importKey(
195+
'jwk',
196+
{
197+
alg: name === 'ECDSA' ? keyData[namedCurve].jwsAlg : 'ECDH-ES',
198+
kty: jwk.kty,
199+
crv: jwk.crv,
200+
x: jwk.x,
201+
y: jwk.y,
202+
},
203+
{ name, namedCurve },
204+
extractable, publicUsages),
205+
subtle.importKey(
206+
'jwk',
207+
{
208+
...jwk,
209+
alg: name === 'ECDSA' ? keyData[namedCurve].jwsAlg : 'ECDH-ES',
210+
},
211+
{ name, namedCurve },
212+
extractable,
171213
privateUsages)
172214
]);
173215

0 commit comments

Comments
 (0)