|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { AsyncWrap, Providers } = process.binding('async_wrap'); |
| 4 | +const { |
| 5 | + generateKeyPairRSA, |
| 6 | + generateKeyPairDSA, |
| 7 | + generateKeyPairEC, |
| 8 | + OPENSSL_EC_NAMED_CURVE, |
| 9 | + OPENSSL_EC_EXPLICIT_CURVE, |
| 10 | + PK_ENCODING_PKCS1, |
| 11 | + PK_ENCODING_PKCS8, |
| 12 | + PK_ENCODING_SPKI, |
| 13 | + PK_ENCODING_SEC1, |
| 14 | + PK_FORMAT_DER, |
| 15 | + PK_FORMAT_PEM |
| 16 | +} = process.binding('crypto'); |
| 17 | +const { isUint32 } = require('internal/validators'); |
| 18 | +const { |
| 19 | + ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS, |
| 20 | + ERR_INVALID_ARG_TYPE, |
| 21 | + ERR_INVALID_ARG_VALUE, |
| 22 | + ERR_INVALID_CALLBACK, |
| 23 | + ERR_INVALID_OPT_VALUE |
| 24 | +} = require('internal/errors').codes; |
| 25 | + |
| 26 | +function generateKeyPair(type, options, callback) { |
| 27 | + if (typeof options === 'function') { |
| 28 | + callback = options; |
| 29 | + options = undefined; |
| 30 | + } |
| 31 | + |
| 32 | + const impl = check(type, options); |
| 33 | + |
| 34 | + if (typeof callback !== 'function') |
| 35 | + throw new ERR_INVALID_CALLBACK(); |
| 36 | + |
| 37 | + const wrap = new AsyncWrap(Providers.KEYPAIRGENREQUEST); |
| 38 | + wrap.ondone = (ex, pubkey, privkey) => { |
| 39 | + if (ex) return callback.call(wrap, ex); |
| 40 | + callback.call(wrap, null, pubkey, privkey); |
| 41 | + }; |
| 42 | + |
| 43 | + handleError(impl, wrap); |
| 44 | +} |
| 45 | + |
| 46 | +function generateKeyPairSync(type, options) { |
| 47 | + const impl = check(type, options); |
| 48 | + return handleError(impl); |
| 49 | +} |
| 50 | + |
| 51 | +function handleError(impl, wrap) { |
| 52 | + const ret = impl(wrap); |
| 53 | + if (ret === undefined) |
| 54 | + return; // async |
| 55 | + |
| 56 | + const [err, publicKey, privateKey] = ret; |
| 57 | + if (err !== undefined) |
| 58 | + throw err; |
| 59 | + |
| 60 | + return { publicKey, privateKey }; |
| 61 | +} |
| 62 | + |
| 63 | +function parseKeyEncoding(keyType, options) { |
| 64 | + const { publicKeyEncoding, privateKeyEncoding } = options; |
| 65 | + |
| 66 | + if (publicKeyEncoding == null || typeof publicKeyEncoding !== 'object') |
| 67 | + throw new ERR_INVALID_OPT_VALUE('publicKeyEncoding', publicKeyEncoding); |
| 68 | + |
| 69 | + const { format: strPublicFormat, type: strPublicType } = publicKeyEncoding; |
| 70 | + |
| 71 | + let publicType; |
| 72 | + if (strPublicType === 'pkcs1') { |
| 73 | + if (keyType !== 'rsa') { |
| 74 | + throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS( |
| 75 | + strPublicType, 'can only be used for RSA keys'); |
| 76 | + } |
| 77 | + publicType = PK_ENCODING_PKCS1; |
| 78 | + } else if (strPublicType === 'spki') { |
| 79 | + publicType = PK_ENCODING_SPKI; |
| 80 | + } else { |
| 81 | + throw new ERR_INVALID_OPT_VALUE('publicKeyEncoding.type', strPublicType); |
| 82 | + } |
| 83 | + |
| 84 | + let publicFormat; |
| 85 | + if (strPublicFormat === 'der') { |
| 86 | + publicFormat = PK_FORMAT_DER; |
| 87 | + } else if (strPublicFormat === 'pem') { |
| 88 | + publicFormat = PK_FORMAT_PEM; |
| 89 | + } else { |
| 90 | + throw new ERR_INVALID_OPT_VALUE('publicKeyEncoding.format', |
| 91 | + strPublicFormat); |
| 92 | + } |
| 93 | + |
| 94 | + if (privateKeyEncoding == null || typeof privateKeyEncoding !== 'object') |
| 95 | + throw new ERR_INVALID_OPT_VALUE('privateKeyEncoding', privateKeyEncoding); |
| 96 | + |
| 97 | + const { |
| 98 | + cipher, |
| 99 | + passphrase, |
| 100 | + format: strPrivateFormat, |
| 101 | + type: strPrivateType |
| 102 | + } = privateKeyEncoding; |
| 103 | + |
| 104 | + let privateType; |
| 105 | + if (strPrivateType === 'pkcs1') { |
| 106 | + if (keyType !== 'rsa') { |
| 107 | + throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS( |
| 108 | + strPrivateType, 'can only be used for RSA keys'); |
| 109 | + } |
| 110 | + privateType = PK_ENCODING_PKCS1; |
| 111 | + } else if (strPrivateType === 'pkcs8') { |
| 112 | + privateType = PK_ENCODING_PKCS8; |
| 113 | + } else if (strPrivateType === 'sec1') { |
| 114 | + if (keyType !== 'ec') { |
| 115 | + throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS( |
| 116 | + strPrivateType, 'can only be used for EC keys'); |
| 117 | + } |
| 118 | + privateType = PK_ENCODING_SEC1; |
| 119 | + } else { |
| 120 | + throw new ERR_INVALID_OPT_VALUE('privateKeyEncoding.type', strPrivateType); |
| 121 | + } |
| 122 | + |
| 123 | + let privateFormat; |
| 124 | + if (strPrivateFormat === 'der') { |
| 125 | + privateFormat = PK_FORMAT_DER; |
| 126 | + } else if (strPrivateFormat === 'pem') { |
| 127 | + privateFormat = PK_FORMAT_PEM; |
| 128 | + } else { |
| 129 | + throw new ERR_INVALID_OPT_VALUE('privateKeyEncoding.format', |
| 130 | + strPrivateFormat); |
| 131 | + } |
| 132 | + |
| 133 | + if (cipher != null) { |
| 134 | + if (typeof cipher !== 'string') |
| 135 | + throw new ERR_INVALID_OPT_VALUE('privateKeyEncoding.cipher', cipher); |
| 136 | + if (privateType !== PK_ENCODING_PKCS8) { |
| 137 | + throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS( |
| 138 | + strPrivateType, 'does not support encryption'); |
| 139 | + } |
| 140 | + if (typeof passphrase !== 'string') { |
| 141 | + throw new ERR_INVALID_OPT_VALUE('privateKeyEncoding.passphrase', |
| 142 | + passphrase); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return { |
| 147 | + cipher, passphrase, publicType, publicFormat, privateType, privateFormat |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +function check(type, options, callback) { |
| 152 | + if (typeof type !== 'string') |
| 153 | + throw new ERR_INVALID_ARG_TYPE('type', 'string', type); |
| 154 | + if (options == null || typeof options !== 'object') |
| 155 | + throw new ERR_INVALID_ARG_TYPE('options', 'object', options); |
| 156 | + |
| 157 | + // These will be set after parsing the type and type-specific options to make |
| 158 | + // the order a bit more intuitive. |
| 159 | + let cipher, passphrase, publicType, publicFormat, privateType, privateFormat; |
| 160 | + |
| 161 | + let impl; |
| 162 | + switch (type) { |
| 163 | + case 'rsa': |
| 164 | + { |
| 165 | + const { modulusLength } = options; |
| 166 | + if (!isUint32(modulusLength)) |
| 167 | + throw new ERR_INVALID_OPT_VALUE('modulusLength', modulusLength); |
| 168 | + |
| 169 | + let { publicExponent } = options; |
| 170 | + if (publicExponent == null) { |
| 171 | + publicExponent = 0x10001; |
| 172 | + } else if (!isUint32(publicExponent)) { |
| 173 | + throw new ERR_INVALID_OPT_VALUE('publicExponent', publicExponent); |
| 174 | + } |
| 175 | + |
| 176 | + impl = (wrap) => generateKeyPairRSA(modulusLength, publicExponent, |
| 177 | + publicType, publicFormat, |
| 178 | + privateType, privateFormat, |
| 179 | + cipher, passphrase, wrap); |
| 180 | + } |
| 181 | + break; |
| 182 | + case 'dsa': |
| 183 | + { |
| 184 | + const { modulusLength } = options; |
| 185 | + if (!isUint32(modulusLength)) |
| 186 | + throw new ERR_INVALID_OPT_VALUE('modulusLength', modulusLength); |
| 187 | + |
| 188 | + let { divisorLength } = options; |
| 189 | + if (divisorLength == null) { |
| 190 | + divisorLength = -1; |
| 191 | + } else if (!isUint32(divisorLength)) { |
| 192 | + throw new ERR_INVALID_OPT_VALUE('divisorLength', divisorLength); |
| 193 | + } |
| 194 | + |
| 195 | + impl = (wrap) => generateKeyPairDSA(modulusLength, divisorLength, |
| 196 | + publicType, publicFormat, |
| 197 | + privateType, privateFormat, |
| 198 | + cipher, passphrase, wrap); |
| 199 | + } |
| 200 | + break; |
| 201 | + case 'ec': |
| 202 | + { |
| 203 | + const { namedCurve } = options; |
| 204 | + if (typeof namedCurve !== 'string') |
| 205 | + throw new ERR_INVALID_OPT_VALUE('namedCurve', namedCurve); |
| 206 | + let { paramEncoding } = options; |
| 207 | + if (paramEncoding == null || paramEncoding === 'named') |
| 208 | + paramEncoding = OPENSSL_EC_NAMED_CURVE; |
| 209 | + else if (paramEncoding === 'explicit') |
| 210 | + paramEncoding = OPENSSL_EC_EXPLICIT_CURVE; |
| 211 | + else |
| 212 | + throw new ERR_INVALID_OPT_VALUE('paramEncoding', paramEncoding); |
| 213 | + |
| 214 | + impl = (wrap) => generateKeyPairEC(namedCurve, paramEncoding, |
| 215 | + publicType, publicFormat, |
| 216 | + privateType, privateFormat, |
| 217 | + cipher, passphrase, wrap); |
| 218 | + } |
| 219 | + break; |
| 220 | + default: |
| 221 | + throw new ERR_INVALID_ARG_VALUE('type', type, |
| 222 | + "must be one of 'rsa', 'dsa', 'ec'"); |
| 223 | + } |
| 224 | + |
| 225 | + ({ |
| 226 | + cipher, |
| 227 | + passphrase, |
| 228 | + publicType, |
| 229 | + publicFormat, |
| 230 | + privateType, |
| 231 | + privateFormat |
| 232 | + } = parseKeyEncoding(type, options)); |
| 233 | + |
| 234 | + return impl; |
| 235 | +} |
| 236 | + |
| 237 | +module.exports = { generateKeyPair, generateKeyPairSync }; |
0 commit comments