GeezerVison is showing github.com — crypto: check EC coordinate conversion results · nodejs/node@be88858
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 +8-4Lines changed: 8 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6098,8 +6098,10 @@ bool ECKeyPointer::setPublicKeyRaw(const BignumPointer& x, | 6098 | 6098 | if (!buf) return false; | 6099 | 6099 | unsigned char* ptr = static_cast<unsigned char*>(buf.get()); | 6100 | 6100 | ptr[0] = POINT_CONVERSION_UNCOMPRESSED; | 6101 | | - x.encodePaddedInto(ptr + 1, field_len); | 6102 | | - y.encodePaddedInto(ptr + 1 + field_len, field_len); | | 6101 | + if (x.encodePaddedInto(ptr + 1, field_len) != field_len || | | 6102 | + y.encodePaddedInto(ptr + 1 + field_len, field_len) != field_len) { | | 6103 | + return false; | | 6104 | + } | 6103 | 6105 |
| 6104 | 6106 | auto point = ECPointPointer::New(group); | 6105 | 6107 | if (!point) return false; | @@ -6327,8 +6329,10 @@ bool ECKeyPointer::setPublicKeyRaw(const BignumPointer& x, | 6327 | 6329 | if (!buf) return false; | 6328 | 6330 | unsigned char* ptr = static_cast<unsigned char*>(buf.get()); | 6329 | 6331 | ptr[0] = POINT_CONVERSION_UNCOMPRESSED; | 6330 | | - x.encodePaddedInto(ptr + 1, field_len); | 6331 | | - y.encodePaddedInto(ptr + 1 + field_len, field_len); | | 6332 | + if (x.encodePaddedInto(ptr + 1, field_len) != field_len || | | 6333 | + y.encodePaddedInto(ptr + 1 + field_len, field_len) != field_len) { | | 6334 | + return false; | | 6335 | + } | 6332 | 6336 |
| 6333 | 6337 | auto point = ECPointPointer::New(group_.get()); | 6334 | 6338 | if (!point || !point.setFromBuffer({ptr, uncompressed_len}, group_.get())) { | |
Collapse file Copy file name to clipboard +53Lines changed: 53 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 { createPublicKey, subtle } = require('crypto'); | | 9 | + | | 10 | +// This is the valid P-256 point (0, sqrt(b)). A failed conversion of an | | 11 | +// oversized x coordinate must not silently replace it with zero. | | 12 | +const jwk = { | | 13 | + kty: 'EC', | | 14 | + crv: 'P-256', | | 15 | + x: Buffer.alloc(32).toString('base64url'), | | 16 | + y: 'ZkhceA4vg9ckM71dhKBrtlQcKvMdrocXKL-FahdPk_Q', | | 17 | +}; | | 18 | + | | 19 | +(async () => { | | 20 | + for (const field of ['x', 'y']) { | | 21 | + const invalid = { ...jwk, [field]: Buffer.alloc(33, 1).toString('base64url') }; | | 22 | + assert.throws(() => createPublicKey({ key: invalid, format: 'jwk' }), { | | 23 | + code: 'ERR_CRYPTO_INVALID_JWK', | | 24 | + }); | | 25 | + for (const name of ['ECDSA', 'ECDH']) { | | 26 | + await assert.rejects(subtle.importKey( | | 27 | + 'jwk', invalid, { name, namedCurve: 'P-256' }, true, | | 28 | + name === 'ECDSA' ? ['verify'] : []), { name: 'DataError' }); | | 29 | + } | | 30 | + } | | 31 | + | | 32 | + // Equivalent integer encodings remain accepted by the existing decoder. | | 33 | + for (const encoded of [ | | 34 | + jwk, | | 35 | + { ...jwk, x: Buffer.alloc(1).toString('base64url') }, | | 36 | + { | | 37 | + ...jwk, | | 38 | + x: Buffer.alloc(33).toString('base64url'), | | 39 | + y: Buffer.concat([Buffer.alloc(1), Buffer.from(jwk.y, 'base64url')]).toString('base64url'), | | 40 | + }, | | 41 | + ]) { | | 42 | + const publicKey = createPublicKey({ key: encoded, format: 'jwk' }); | | 43 | + assert.deepStrictEqual(publicKey.export({ format: 'jwk' }), jwk); | | 44 | + for (const name of ['ECDSA', 'ECDH']) { | | 45 | + const key = await subtle.importKey( | | 46 | + 'jwk', encoded, { name, namedCurve: 'P-256' }, true, | | 47 | + name === 'ECDSA' ? ['verify'] : []); | | 48 | + const exported = await subtle.exportKey('jwk', key); | | 49 | + assert.strictEqual(exported.x, jwk.x); | | 50 | + assert.strictEqual(exported.y, jwk.y); | | 51 | + } | | 52 | + } | | 53 | +})().then(common.mustCall()); |
|