Skip to content

Commit cfa4222

Browse files
committed
fix: throw on unsupported EC curves
1 parent ce03ea6 commit cfa4222

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/jwk/key/ec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { generateKeyPairSync, generateKeyPair: async } = require('crypto')
22
const { promisify } = require('util')
33

44
const { THUMBPRINT_MATERIAL, PUBLIC_MEMBERS, PRIVATE_MEMBERS, JWK_MEMBERS } = require('../../help/symbols')
5+
const errors = require('../../errors')
56

67
const Key = require('./base')
78

@@ -36,6 +37,9 @@ class ECKey extends Key {
3637
value: 'EC',
3738
enumerable: true
3839
})
40+
if (!this.crv) {
41+
throw new errors.JOSENotSupported('unsupported EC key curve')
42+
}
3943
}
4044

4145
static get [PUBLIC_MEMBERS] () {

test/jwk/ec.test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const test = require('ava')
2-
const { createPrivateKey, createPublicKey } = require('crypto')
2+
const { createPrivateKey, createPublicKey, generateKeyPairSync } = require('crypto')
33
const { hasProperty, hasNoProperties, hasProperties } = require('../macros')
44
const fixtures = require('../fixtures')
5+
const errors = require('../../lib/errors')
56

67
const ECKey = require('../../lib/jwk/key/ec')
78

@@ -10,6 +11,18 @@ test(`EC key .algorithms invalid operation`, t => {
1011
t.throws(() => key.algorithms('foo'), { instanceOf: TypeError, message: 'invalid key operation' })
1112
})
1213

14+
test('Unusable with unsupported curves', t => {
15+
const kp = generateKeyPairSync('ec', { namedCurve: 'secp224k1' })
16+
t.throws(
17+
() => new ECKey(kp.privateKey),
18+
{ instanceOf: errors.JOSENotSupported, code: 'ERR_JOSE_NOT_SUPPORTED', message: 'unsupported EC key curve' }
19+
)
20+
t.throws(
21+
() => new ECKey(kp.publicKey),
22+
{ instanceOf: errors.JOSENotSupported, code: 'ERR_JOSE_NOT_SUPPORTED', message: 'unsupported EC key curve' }
23+
)
24+
})
25+
1326
Object.entries({
1427
'P-256': [256, 'rDd6H6t9-nJUoz72nTpz8tInvypVWhE2iQoPznj8ZY8'],
1528
'P-256K': [256, 'zZYrH69YCAAihM7ZCoRj90VI55H5MmQscSpf-JuUS50'],

0 commit comments

Comments
 (0)