|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const crypto = require('crypto'); |
| 4 | +const { promisify } = require('util'); |
| 5 | + |
| 6 | +const { |
| 7 | + DataError, |
| 8 | + InvalidAccessError, |
| 9 | + NotSupportedError, |
| 10 | + OperationError |
| 11 | +} = require('../errors'); |
| 12 | +const { kKeyMaterial, CryptoKey } = require('../key'); |
| 13 | +const { limitUsages, opensslHashFunctionName, toBuffer } = require('../util'); |
| 14 | + |
| 15 | +const generateKeyPair = promisify(crypto.generateKeyPair); |
| 16 | + |
| 17 | +const curveBasePointOrderSizes = { |
| 18 | + 'P-256': 32, |
| 19 | + 'P-384': 48, |
| 20 | + 'P-521': 66 |
| 21 | +}; |
| 22 | + |
| 23 | +const byte = (b) => Buffer.from([b]); |
| 24 | + |
| 25 | +function convertSignatureToASN1(signature, n) { |
| 26 | + if (signature.length !== 2 * n) |
| 27 | + throw new OperationError(); |
| 28 | + |
| 29 | + const r = signature.slice(0, n); |
| 30 | + const s = signature.slice(n); |
| 31 | + |
| 32 | + function encodeLength(len) { |
| 33 | + // Short form. |
| 34 | + if (len < 128) |
| 35 | + return byte(len); |
| 36 | + |
| 37 | + // Long form. |
| 38 | + const buffer = Buffer.alloc(5); |
| 39 | + buffer.writeUInt32BE(len, 1); |
| 40 | + let offset = 1; |
| 41 | + while (buffer[offset] === 0) |
| 42 | + offset++; |
| 43 | + buffer[offset - 1] = 0x80 | (5 - offset); |
| 44 | + return buffer.slice(offset - 1); |
| 45 | + } |
| 46 | + |
| 47 | + function encodeUnsignedInteger(integer) { |
| 48 | + // ASN.1 integers are signed, so in order to encode unsigned integers, we |
| 49 | + // need to make sure that the MSB is not set. |
| 50 | + if (integer[0] & 0x80) { |
| 51 | + return Buffer.concat([ |
| 52 | + byte(0x02), |
| 53 | + encodeLength(integer.length + 1), |
| 54 | + byte(0x00), integer |
| 55 | + ]); |
| 56 | + } else { |
| 57 | + // If the MSB is not set, enforce a minimal representation of the integer. |
| 58 | + let i = 0; |
| 59 | + while (integer[i] === 0 && (integer[i + 1] & 0x80) === 0) |
| 60 | + i++; |
| 61 | + return Buffer.concat([ |
| 62 | + byte(0x02), |
| 63 | + encodeLength(integer.length - i), |
| 64 | + integer.slice(i) |
| 65 | + ]); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + const seq = Buffer.concat([ |
| 70 | + encodeUnsignedInteger(r), |
| 71 | + encodeUnsignedInteger(s) |
| 72 | + ]); |
| 73 | + |
| 74 | + return Buffer.concat([byte(0x30), encodeLength(seq.length), seq]); |
| 75 | +} |
| 76 | + |
| 77 | +function convertSignatureFromASN1(signature, n) { |
| 78 | + let offset = 2; |
| 79 | + if (signature[1] & 0x80) |
| 80 | + offset += signature[1] & ~0x80; |
| 81 | + |
| 82 | + function decodeUnsignedInteger() { |
| 83 | + let length = signature[offset + 1]; |
| 84 | + offset += 2; |
| 85 | + if (length & 0x80) { |
| 86 | + // Long form. |
| 87 | + const nBytes = length & ~0x80; |
| 88 | + length = 0; |
| 89 | + for (let i = 0; i < nBytes; i++) |
| 90 | + length = (length << 8) | signature[offset + 2 + i]; |
| 91 | + offset += nBytes; |
| 92 | + } |
| 93 | + |
| 94 | + // There may be exactly one leading zero (if the next byte's MSB is set). |
| 95 | + if (signature[offset] === 0) { |
| 96 | + offset++; |
| 97 | + length--; |
| 98 | + } |
| 99 | + |
| 100 | + const result = signature.slice(offset, offset + length); |
| 101 | + offset += length; |
| 102 | + return result; |
| 103 | + } |
| 104 | + |
| 105 | + const r = decodeUnsignedInteger(); |
| 106 | + const s = decodeUnsignedInteger(); |
| 107 | + |
| 108 | + const result = Buffer.alloc(2 * n, 0); |
| 109 | + r.copy(result, n - r.length); |
| 110 | + s.copy(result, 2 * n - s.length); |
| 111 | + return result; |
| 112 | +} |
| 113 | + |
| 114 | +// Spec: https://www.w3.org/TR/WebCryptoAPI/#ecdsa |
| 115 | +module.exports.ECDSA = { |
| 116 | + name: 'ECDSA', |
| 117 | + |
| 118 | + async generateKey(algorithm, extractable, usages) { |
| 119 | + limitUsages(usages, ['sign', 'verify']); |
| 120 | + const privateUsages = usages.includes('sign') ? ['sign'] : []; |
| 121 | + const publicUsages = usages.includes('verify') ? ['verify'] : []; |
| 122 | + |
| 123 | + const { namedCurve } = algorithm; |
| 124 | + if (!curveBasePointOrderSizes[namedCurve]) |
| 125 | + throw new NotSupportedError(); |
| 126 | + |
| 127 | + const { privateKey, publicKey } = await generateKeyPair('ec', { |
| 128 | + namedCurve |
| 129 | + }); |
| 130 | + |
| 131 | + const alg = { |
| 132 | + name: this.name, |
| 133 | + namedCurve |
| 134 | + }; |
| 135 | + |
| 136 | + return { |
| 137 | + privateKey: new CryptoKey('private', alg, extractable, privateUsages, |
| 138 | + privateKey), |
| 139 | + publicKey: new CryptoKey('public', alg, extractable, publicUsages, |
| 140 | + publicKey) |
| 141 | + }; |
| 142 | + }, |
| 143 | + |
| 144 | + importKey(keyFormat, keyData, params, extractable, keyUsages) { |
| 145 | + const { namedCurve } = params; |
| 146 | + |
| 147 | + const opts = { |
| 148 | + key: toBuffer(keyData), |
| 149 | + format: 'der', |
| 150 | + type: keyFormat |
| 151 | + }; |
| 152 | + |
| 153 | + let key; |
| 154 | + if (keyFormat === 'spki') { |
| 155 | + limitUsages(keyUsages, ['verify']); |
| 156 | + key = crypto.createPublicKey(opts); |
| 157 | + } else if (keyFormat === 'pkcs8') { |
| 158 | + limitUsages(keyUsages, ['sign']); |
| 159 | + key = crypto.createPrivateKey(opts); |
| 160 | + } else { |
| 161 | + throw new NotSupportedError(); |
| 162 | + } |
| 163 | + |
| 164 | + if (key.asymmetricKeyType !== 'ec') |
| 165 | + throw new DataError(); |
| 166 | + |
| 167 | + return new CryptoKey(key.type, { name: this.name, namedCurve }, |
| 168 | + extractable, keyUsages, key); |
| 169 | + }, |
| 170 | + |
| 171 | + exportKey(format, key) { |
| 172 | + if (format !== 'spki' && format !== 'pkcs8') |
| 173 | + throw new NotSupportedError(); |
| 174 | + |
| 175 | + if (format === 'spki' && key.type !== 'public' || |
| 176 | + format === 'pkcs8' && key.type !== 'private') |
| 177 | + throw new InvalidAccessError(); |
| 178 | + |
| 179 | + return key[kKeyMaterial].export({ |
| 180 | + format: 'der', |
| 181 | + type: format |
| 182 | + }); |
| 183 | + }, |
| 184 | + |
| 185 | + sign(algorithm, key, data) { |
| 186 | + if (key.type !== 'private') |
| 187 | + throw new InvalidAccessError(); |
| 188 | + |
| 189 | + const { hash } = algorithm; |
| 190 | + const hashFn = opensslHashFunctionName(hash); |
| 191 | + |
| 192 | + const asn1Sig = crypto.sign(hashFn, toBuffer(data), key[kKeyMaterial]); |
| 193 | + const n = curveBasePointOrderSizes[key.algorithm.namedCurve]; |
| 194 | + return convertSignatureFromASN1(asn1Sig, n); |
| 195 | + }, |
| 196 | + |
| 197 | + verify(algorithm, key, signature, data) { |
| 198 | + if (key.type !== 'public') |
| 199 | + throw new InvalidAccessError(); |
| 200 | + |
| 201 | + const n = curveBasePointOrderSizes[key.algorithm.namedCurve]; |
| 202 | + signature = convertSignatureToASN1(toBuffer(signature), n); |
| 203 | + |
| 204 | + const { hash } = algorithm; |
| 205 | + const hashFn = opensslHashFunctionName(hash); |
| 206 | + return crypto.verify(hashFn, data, key[kKeyMaterial], signature); |
| 207 | + } |
| 208 | +}; |
0 commit comments