1
1
'use strict' ;
2
2
3
3
const {
4
- Promise,
5
4
SafeSet,
6
5
Uint8Array,
7
6
} = primordials ;
@@ -49,6 +48,7 @@ const {
49
48
50
49
const {
51
50
lazyDOMException,
51
+ promisify,
52
52
} = require ( 'internal/util' ) ;
53
53
54
54
const {
@@ -64,14 +64,15 @@ const {
64
64
} = require ( 'internal/crypto/keys' ) ;
65
65
66
66
const {
67
- generateKeyPair,
67
+ generateKeyPair : _generateKeyPair ,
68
68
} = require ( 'internal/crypto/keygen' ) ;
69
69
70
70
const kRsaVariants = {
71
71
'RSASSA-PKCS1-v1_5' : kKeyVariantRSA_SSA_PKCS1_v1_5 ,
72
72
'RSA-PSS' : kKeyVariantRSA_PSS ,
73
73
'RSA-OAEP' : kKeyVariantRSA_OAEP ,
74
74
} ;
75
+ const generateKeyPair = promisify ( _generateKeyPair ) ;
75
76
76
77
function verifyAcceptableRsaKeyUse ( name , type , usages ) {
77
78
let checkSet ;
@@ -173,56 +174,53 @@ async function rsaKeyGenerate(
173
174
}
174
175
}
175
176
176
- return new Promise ( ( resolve , reject ) => {
177
- generateKeyPair ( 'rsa' , {
178
- modulusLength,
179
- publicExponent : publicExponentConverted ,
180
- } , ( err , pubKey , privKey ) => {
181
- if ( err ) {
182
- return reject ( lazyDOMException (
183
- 'The operation failed for an operation-specific reason' ,
184
- 'OperationError' ) ) ;
185
- }
177
+ const keypair = await generateKeyPair ( 'rsa' , {
178
+ modulusLength,
179
+ publicExponent : publicExponentConverted ,
180
+ } ) . catch ( ( err ) => {
181
+ // TODO(@panva): add err as cause to DOMException
182
+ throw lazyDOMException (
183
+ 'The operation failed for an operation-specific reason' ,
184
+ 'OperationError' ) ;
185
+ } ) ;
186
186
187
- const algorithm = {
188
- name,
189
- modulusLength,
190
- publicExponent,
191
- hash : { name : hash . name }
192
- } ;
193
-
194
- let publicUsages ;
195
- let privateUsages ;
196
- switch ( name ) {
197
- case 'RSA-OAEP' : {
198
- publicUsages = getUsagesUnion ( usageSet , 'encrypt' , 'wrapKey' ) ;
199
- privateUsages = getUsagesUnion ( usageSet , 'decrypt' , 'unwrapKey' ) ;
200
- break ;
201
- }
202
- default : {
203
- publicUsages = getUsagesUnion ( usageSet , 'verify' ) ;
204
- privateUsages = getUsagesUnion ( usageSet , 'sign' ) ;
205
- break ;
206
- }
207
- }
187
+ const keyAlgorithm = {
188
+ name,
189
+ modulusLength,
190
+ publicExponent,
191
+ hash : { name : hash . name }
192
+ } ;
208
193
209
- const publicKey =
210
- new InternalCryptoKey (
211
- pubKey ,
212
- algorithm ,
213
- publicUsages ,
214
- true ) ;
215
-
216
- const privateKey =
217
- new InternalCryptoKey (
218
- privKey ,
219
- algorithm ,
220
- privateUsages ,
221
- extractable ) ;
222
-
223
- resolve ( { publicKey, privateKey } ) ;
224
- } ) ;
225
- } ) ;
194
+ let publicUsages ;
195
+ let privateUsages ;
196
+ switch ( name ) {
197
+ case 'RSA-OAEP' : {
198
+ publicUsages = getUsagesUnion ( usageSet , 'encrypt' , 'wrapKey' ) ;
199
+ privateUsages = getUsagesUnion ( usageSet , 'decrypt' , 'unwrapKey' ) ;
200
+ break ;
201
+ }
202
+ default : {
203
+ publicUsages = getUsagesUnion ( usageSet , 'verify' ) ;
204
+ privateUsages = getUsagesUnion ( usageSet , 'sign' ) ;
205
+ break ;
206
+ }
207
+ }
208
+
209
+ const publicKey =
210
+ new InternalCryptoKey (
211
+ keypair . publicKey ,
212
+ keyAlgorithm ,
213
+ publicUsages ,
214
+ true ) ;
215
+
216
+ const privateKey =
217
+ new InternalCryptoKey (
218
+ keypair . privateKey ,
219
+ keyAlgorithm ,
220
+ privateUsages ,
221
+ extractable ) ;
222
+
223
+ return { publicKey, privateKey } ;
226
224
}
227
225
228
226
function rsaExportKey ( key , format ) {
0 commit comments