forked from unjs/unenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keygen.ts
187 lines (169 loc) · 5.98 KB
/
keygen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import type nodeCrypto from "node:crypto";
import { isArrayBufferView } from "../util/types";
export const generateKeyPair: typeof nodeCrypto.generateKeyPair = (
type,
options,
callback
) => {
if (typeof options === 'function') {
callback = options;
options = undefined;
}
const job = createJob('kCryptoJobAsync', type, options);
job.ondone = (error, result) => {
if (error) {
return FunctionPrototypeCall(callback, job, error);
}
// If no encoding was chosen, return key objects instead.
let { 0: pubkey, 1: privkey } = result;
pubkey = wrapKey(pubkey, PublicKeyObject);
privkey = wrapKey(privkey, PrivateKeyObject);
FunctionPrototypeCall(callback, job, null, pubkey, privkey);
};
job.run();
}
function createJob(mode:('kCryptoJobAsync' | 'kCryptoJobSync'), type:string, options) {
const encoding = new SafeArrayIterator(parseKeyEncoding(type, options));
switch (type) {
case 'rsa':
case 'rsa-pss':
{
const { modulusLength } = options;
let { publicExponent } = options;
if (publicExponent == null) {
publicExponent = 0x1_00_01;
}
if (type === 'rsa') {
return new RsaKeyPairGenJob(
mode,
kKeyVariantRSA_SSA_PKCS1_v1_5, // Used also for RSA-OAEP
modulusLength,
publicExponent,
...encoding);
}
const {
hash, mgf1Hash, hashAlgorithm, mgf1HashAlgorithm, saltLength,
} = options;
if (hash !== undefined) {
process.emitWarning(
'"options.hash" is deprecated, ' +
'use "options.hashAlgorithm" instead.',
'DeprecationWarning',
'DEP0154');
if (hashAlgorithm && hash !== hashAlgorithm) {
throw new ERR_INVALID_ARG_VALUE('options.hash', hash);
}
}
if (mgf1Hash !== undefined) {
process.emitWarning(
'"options.mgf1Hash" is deprecated, ' +
'use "options.mgf1HashAlgorithm" instead.',
'DeprecationWarning',
'DEP0154');
if (mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm) {
throw new ERR_INVALID_ARG_VALUE('options.mgf1Hash', mgf1Hash);
}
}
return new RsaKeyPairGenJob(
mode,
kKeyVariantRSA_PSS,
modulusLength,
publicExponent,
hashAlgorithm || hash,
mgf1HashAlgorithm || mgf1Hash,
saltLength,
...encoding);
}
case 'dsa':
{
const { modulusLength } = options;
let { divisorLength } = options;
if (divisorLength == null) {
divisorLength = -1;
}
return new DsaKeyPairGenJob(
mode,
modulusLength,
divisorLength,
...encoding);
}
case 'ec':
{
const { namedCurve } = options;
let { paramEncoding } = options;
if (paramEncoding == null || paramEncoding === 'named')
{ paramEncoding = OPENSSL_EC_NAMED_CURVE; }
else if (paramEncoding === 'explicit')
{ paramEncoding = OPENSSL_EC_EXPLICIT_CURVE; }
else
{ throw new ERR_INVALID_ARG_VALUE('options.paramEncoding', paramEncoding); }
return new EcKeyPairGenJob(
mode,
namedCurve,
paramEncoding,
...encoding);
}
case 'ed25519':
case 'ed448':
case 'x25519':
case 'x448':
{
let id;
switch (type) {
case 'ed25519':
id = EVP_PKEY_ED25519;
break;
case 'ed448':
id = EVP_PKEY_ED448;
break;
case 'x25519':
id = EVP_PKEY_X25519;
break;
case 'x448':
id = EVP_PKEY_X448;
break;
}
return new NidKeyPairGenJob(mode, id, ...encoding);
}
case 'dh':
{
const { group, primeLength, prime, generator } = options;
if (group != null) {
if (prime != null)
{ throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'prime'); }
if (primeLength != null)
{ throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'primeLength'); }
if (generator != null)
{ throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'generator'); }
return new DhKeyPairGenJob(mode, group, ...encoding);
}
if (prime != null) {
if (primeLength != null)
{ throw new ERR_INCOMPATIBLE_OPTION_PAIR('prime', 'primeLength'); }
} else if (primeLength == null) {
throw new ERR_MISSING_OPTION(
'At least one of the group, prime, or primeLength options');
}
return new DhKeyPairGenJob(
mode,
prime == null ? primeLength : prime,
generator == null ? 2 : generator,
...encoding);
}
default:
// Fall through
}
throw new ERR_INVALID_ARG_VALUE('type', type, 'must be a supported key type');
}
function isJwk(obj:any) {
return obj != null && obj.kty !== undefined;
}
function wrapKey(key:any, ctor:any) {
if (typeof key === 'string' ||
isArrayBufferView(key) ||
isJwk(key)) {
return key;
}
// eslint-disable-next-line new-cap
return new ctor(key);
}