Skip to content

Commit

Permalink
fix: throw on unsupported EC curves
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Mar 27, 2019
1 parent ce03ea6 commit cfa4222
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/jwk/key/ec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { generateKeyPairSync, generateKeyPair: async } = require('crypto')
const { promisify } = require('util')

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

const Key = require('./base')

Expand Down Expand Up @@ -36,6 +37,9 @@ class ECKey extends Key {
value: 'EC',
enumerable: true
})
if (!this.crv) {
throw new errors.JOSENotSupported('unsupported EC key curve')
}
}

static get [PUBLIC_MEMBERS] () {
Expand Down
15 changes: 14 additions & 1 deletion test/jwk/ec.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const test = require('ava')
const { createPrivateKey, createPublicKey } = require('crypto')
const { createPrivateKey, createPublicKey, generateKeyPairSync } = require('crypto')
const { hasProperty, hasNoProperties, hasProperties } = require('../macros')
const fixtures = require('../fixtures')
const errors = require('../../lib/errors')

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

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

test('Unusable with unsupported curves', t => {
const kp = generateKeyPairSync('ec', { namedCurve: 'secp224k1' })
t.throws(
() => new ECKey(kp.privateKey),
{ instanceOf: errors.JOSENotSupported, code: 'ERR_JOSE_NOT_SUPPORTED', message: 'unsupported EC key curve' }
)
t.throws(
() => new ECKey(kp.publicKey),
{ instanceOf: errors.JOSENotSupported, code: 'ERR_JOSE_NOT_SUPPORTED', message: 'unsupported EC key curve' }
)
})

Object.entries({
'P-256': [256, 'rDd6H6t9-nJUoz72nTpz8tInvypVWhE2iQoPznj8ZY8'],
'P-256K': [256, 'zZYrH69YCAAihM7ZCoRj90VI55H5MmQscSpf-JuUS50'],
Expand Down

0 comments on commit cfa4222

Please sign in to comment.