@@ -36,9 +36,14 @@ var JoseJWS = {};
36
36
* Set crypto provider to use (window.crypto, node-webcrypto-ossl, node-webcrypto-pkcs11 etc.).
37
37
*/
38
38
exports . setCrypto = function ( cp ) {
39
- crypto = cp ;
39
+ Jose . crypto = cp ;
40
40
} ;
41
41
42
+ /**
43
+ * Default to the global "crypto" variable
44
+ */
45
+ exports . setCrypto ( crypto ) ;
46
+
42
47
/**
43
48
* Use Node versions of atob, btoa functions outside the browser
44
49
*/
@@ -93,17 +98,17 @@ Jose.caniuse = function() {
93
98
r = r && ( typeof Promise . all == "function" ) ;
94
99
95
100
// Crypto (http://www.w3.org/TR/WebCryptoAPI/)
96
- r = r && ( typeof crypto == "object" ) ;
97
- r = r && ( typeof crypto . subtle == "object" ) ;
98
- r = r && ( typeof crypto . getRandomValues == "function" ) ;
99
- r = r && ( typeof crypto . subtle . importKey == "function" ) ;
100
- r = r && ( typeof crypto . subtle . generateKey == "function" ) ;
101
- r = r && ( typeof crypto . subtle . exportKey == "function" ) ;
102
- r = r && ( typeof crypto . subtle . wrapKey == "function" ) ;
103
- r = r && ( typeof crypto . subtle . unwrapKey == "function" ) ;
104
- r = r && ( typeof crypto . subtle . encrypt == "function" ) ;
105
- r = r && ( typeof crypto . subtle . decrypt == "function" ) ;
106
- r = r && ( typeof crypto . subtle . sign == "function" ) ;
101
+ r = r && ( typeof Jose . crypto == "object" ) ;
102
+ r = r && ( typeof Jose . crypto . subtle == "object" ) ;
103
+ r = r && ( typeof Jose . crypto . getRandomValues == "function" ) ;
104
+ r = r && ( typeof Jose . crypto . subtle . importKey == "function" ) ;
105
+ r = r && ( typeof Jose . crypto . subtle . generateKey == "function" ) ;
106
+ r = r && ( typeof Jose . crypto . subtle . exportKey == "function" ) ;
107
+ r = r && ( typeof Jose . crypto . subtle . wrapKey == "function" ) ;
108
+ r = r && ( typeof Jose . crypto . subtle . unwrapKey == "function" ) ;
109
+ r = r && ( typeof Jose . crypto . subtle . encrypt == "function" ) ;
110
+ r = r && ( typeof Jose . crypto . subtle . decrypt == "function" ) ;
111
+ r = r && ( typeof Jose . crypto . subtle . sign == "function" ) ;
107
112
108
113
// ArrayBuffer (http://people.mozilla.org/~jorendorff/es6-draft.html#sec-arraybuffer-constructor)
109
114
r = r && ( typeof ArrayBuffer == "function" ) ;
@@ -213,7 +218,7 @@ WebCryptographer.prototype.getContentSignAlgorithm = function() {
213
218
*/
214
219
WebCryptographer . prototype . createIV = function ( ) {
215
220
var iv = new Uint8Array ( new Array ( this . content_encryption . iv_bytes ) ) ;
216
- return crypto . getRandomValues ( iv ) ;
221
+ return Jose . crypto . getRandomValues ( iv ) ;
217
222
} ;
218
223
219
224
/**
@@ -224,19 +229,19 @@ WebCryptographer.prototype.createIV = function() {
224
229
*/
225
230
WebCryptographer . prototype . createCek = function ( ) {
226
231
var hack = getCekWorkaround ( this . content_encryption ) ;
227
- return crypto . subtle . generateKey ( hack . id , true , hack . enc_op ) ;
232
+ return Jose . crypto . subtle . generateKey ( hack . id , true , hack . enc_op ) ;
228
233
} ;
229
234
230
235
WebCryptographer . prototype . wrapCek = function ( cek , key ) {
231
- return crypto . subtle . wrapKey ( "raw" , cek , key , this . key_encryption . id ) ;
236
+ return Jose . crypto . subtle . wrapKey ( "raw" , cek , key , this . key_encryption . id ) ;
232
237
} ;
233
238
234
239
WebCryptographer . prototype . unwrapCek = function ( cek , key ) {
235
240
var hack = getCekWorkaround ( this . content_encryption ) ;
236
241
var extractable = ( this . content_encryption . specific_cek_bytes > 0 ) ;
237
242
var key_encryption = this . key_encryption . id ;
238
243
239
- return crypto . subtle . unwrapKey ( "raw" , cek , key , key_encryption , hack . id , extractable , hack . dec_op ) ;
244
+ return Jose . crypto . subtle . unwrapKey ( "raw" , cek , key , key_encryption , hack . id , extractable , hack . dec_op ) ;
240
245
} ;
241
246
242
247
/**
@@ -291,7 +296,7 @@ WebCryptographer.prototype.encrypt = function(iv, aad, cek_promise, plain_text)
291
296
} ;
292
297
293
298
return cek_promise . then ( function ( cek ) {
294
- return crypto . subtle . encrypt ( enc , cek , plain_text ) . then ( function ( cipher_text ) {
299
+ return Jose . crypto . subtle . encrypt ( enc , cek , plain_text ) . then ( function ( cipher_text ) {
295
300
var offset = cipher_text . byteLength - tag_bytes ;
296
301
return {
297
302
cipher : cipher_text . slice ( 0 , offset ) ,
@@ -310,7 +315,7 @@ WebCryptographer.prototype.encrypt = function(iv, aad, cek_promise, plain_text)
310
315
name : config . id . name ,
311
316
iv : iv
312
317
} ;
313
- return crypto . subtle . encrypt ( enc , enc_key , plain_text ) ;
318
+ return Jose . crypto . subtle . encrypt ( enc , enc_key , plain_text ) ;
314
319
} ) ;
315
320
316
321
// compute MAC
@@ -355,8 +360,8 @@ WebCryptographer.prototype.decrypt = function(cek_promise, aad, iv, cipher_text,
355
360
Jose . assert ( arr2 instanceof Uint8Array , "compare: invalid input" ) ;
356
361
357
362
return mac_key_promise . then ( function ( mac_key ) {
358
- var hash1 = crypto . subtle . sign ( config . auth . id , mac_key , arr1 ) ;
359
- var hash2 = crypto . subtle . sign ( config . auth . id , mac_key , arr2 ) ;
363
+ var hash1 = Jose . crypto . subtle . sign ( config . auth . id , mac_key , arr1 ) ;
364
+ var hash2 = Jose . crypto . subtle . sign ( config . auth . id , mac_key , arr2 ) ;
360
365
return Promise . all ( [ hash1 , hash2 ] ) . then ( function ( all ) {
361
366
var hash1 = new Uint8Array ( all [ 0 ] ) ;
362
367
var hash2 = new Uint8Array ( all [ 1 ] ) ;
@@ -388,7 +393,7 @@ WebCryptographer.prototype.decrypt = function(cek_promise, aad, iv, cipher_text,
388
393
389
394
return cek_promise . then ( function ( cek ) {
390
395
var buf = Utils . arrayBufferConcat ( cipher_text , tag ) ;
391
- return crypto . subtle . decrypt ( dec , cek , buf ) ;
396
+ return Jose . crypto . subtle . decrypt ( dec , cek , buf ) ;
392
397
} ) ;
393
398
} else {
394
399
var keys = splitKey ( config , cek_promise , [ "decrypt" ] ) ;
@@ -412,7 +417,7 @@ WebCryptographer.prototype.decrypt = function(cek_promise, aad, iv, cipher_text,
412
417
name : config . id . name ,
413
418
iv : iv
414
419
} ;
415
- return crypto . subtle . decrypt ( dec , enc_key , cipher_text ) ;
420
+ return Jose . crypto . subtle . decrypt ( dec , enc_key , cipher_text ) ;
416
421
} ) . catch ( function ( err ) {
417
422
return Promise . reject ( Error ( "decryptCiphertext: MAC failed." ) ) ;
418
423
} ) ;
@@ -437,7 +442,7 @@ WebCryptographer.prototype.sign = function(aad, payload, key_promise) {
437
442
438
443
// Encrypt the plain text
439
444
return key_promise . then ( function ( key ) {
440
- return crypto . subtle . sign ( config . id , key , Utils . arrayFromString ( Utils . Base64Url . encode ( JSON . stringify ( aad ) ) + '.' + Utils . Base64Url . encodeArray ( payload ) ) ) ;
445
+ return Jose . crypto . subtle . sign ( config . id , key , Utils . arrayFromString ( Utils . Base64Url . encode ( JSON . stringify ( aad ) ) + '.' + Utils . Base64Url . encodeArray ( payload ) ) ) ;
441
446
} ) ;
442
447
} ;
443
448
@@ -456,7 +461,7 @@ WebCryptographer.prototype.verify = function(aad, payload, signature, key_promis
456
461
457
462
return key_promise . then ( function ( key ) {
458
463
config = getSignConfig ( getJwaNameForSignKey ( key ) ) ;
459
- return crypto . subtle . verify ( config . id , key , signature , Utils . arrayFromString ( aad + "." + payload ) ) . then ( function ( res ) {
464
+ return Jose . crypto . subtle . verify ( config . id , key , signature , Utils . arrayFromString ( aad + "." + payload ) ) . then ( function ( res ) {
460
465
return { kid : key_id , verified : res } ;
461
466
} ) ;
462
467
} ) ;
@@ -481,21 +486,21 @@ Jose.WebCryptographer.keyId = function(rsa_key) {
481
486
var splitKey = function ( config , cek_promise , purpose ) {
482
487
// We need to split the CEK key into a MAC and ENC keys
483
488
var cek_bytes_promise = cek_promise . then ( function ( cek ) {
484
- return crypto . subtle . exportKey ( "raw" , cek ) ;
489
+ return Jose . crypto . subtle . exportKey ( "raw" , cek ) ;
485
490
} ) ;
486
491
var mac_key_promise = cek_bytes_promise . then ( function ( cek_bytes ) {
487
492
if ( cek_bytes . byteLength * 8 != config . id . length + config . auth . key_bytes * 8 ) {
488
493
return Promise . reject ( Error ( "encryptPlainText: incorrect cek length" ) ) ;
489
494
}
490
495
var bytes = cek_bytes . slice ( 0 , config . auth . key_bytes ) ;
491
- return crypto . subtle . importKey ( "raw" , bytes , config . auth . id , false , [ "sign" ] ) ;
496
+ return Jose . crypto . subtle . importKey ( "raw" , bytes , config . auth . id , false , [ "sign" ] ) ;
492
497
} ) ;
493
498
var enc_key_promise = cek_bytes_promise . then ( function ( cek_bytes ) {
494
499
if ( cek_bytes . byteLength * 8 != config . id . length + config . auth . key_bytes * 8 ) {
495
500
return Promise . reject ( Error ( "encryptPlainText: incorrect cek length" ) ) ;
496
501
}
497
502
var bytes = cek_bytes . slice ( config . auth . key_bytes ) ;
498
- return crypto . subtle . importKey ( "raw" , bytes , config . id , false , purpose ) ;
503
+ return Jose . crypto . subtle . importKey ( "raw" , bytes , config . id , false , purpose ) ;
499
504
} ) ;
500
505
return [ mac_key_promise , enc_key_promise ] ;
501
506
} ;
@@ -601,7 +606,7 @@ var truncatedMac = function(config, mac_key_promise, aad, iv, cipher_text) {
601
606
var al_full = new Uint8Array ( 8 ) ;
602
607
al_full . set ( al , 4 ) ;
603
608
var buf = Utils . arrayBufferConcat ( aad , iv , cipher_text , al_full ) ;
604
- return crypto . subtle . sign ( config . auth . id , mac_key , buf ) . then ( function ( bytes ) {
609
+ return Jose . crypto . subtle . sign ( config . auth . id , mac_key , buf ) . then ( function ( bytes ) {
605
610
return bytes . slice ( 0 , config . auth . truncated_bytes ) ;
606
611
} ) ;
607
612
} ) ;
@@ -805,7 +810,7 @@ Jose.Utils.importRsaPublicKey = function(rsa_key, alg) {
805
810
jwk = Utils . convertRsaKey ( rk , [ "n" , "e" ] ) ;
806
811
jwk . ext = true ;
807
812
}
808
- return crypto . subtle . importKey ( "jwk" , jwk , config . id , false , [ usage . publicKey ] ) ;
813
+ return Jose . crypto . subtle . importKey ( "jwk" , jwk , config . id , false , [ usage . publicKey ] ) ;
809
814
} ;
810
815
811
816
/**
@@ -845,7 +850,7 @@ Jose.Utils.importRsaPrivateKey = function(rsa_key, alg) {
845
850
jwk = Utils . convertRsaKey ( rk , [ "n" , "e" , "d" , "p" , "q" , "dp" , "dq" , "qi" ] ) ;
846
851
jwk . ext = true ;
847
852
}
848
- return crypto . subtle . importKey ( "jwk" , jwk , config . id , false , [ usage . privateKey ] ) ;
853
+ return Jose . crypto . subtle . importKey ( "jwk" , jwk , config . id , false , [ usage . privateKey ] ) ;
849
854
} ;
850
855
851
856
// Private functions
@@ -1109,15 +1114,17 @@ Utils.sha256 = function(str) {
1109
1114
// Browser docs indicate the first parameter to crypto.subtle.digest to be a
1110
1115
// DOMString. This was initially implemented as an object and continues to be
1111
1116
// supported, so we favor the older form for backwards compatibility.
1112
- return crypto . subtle . digest ( { name : "SHA-256" } , Utils . arrayFromString ( str ) ) . then ( function ( hash ) {
1117
+ return Jose . crypto . subtle . digest ( { name : "SHA-256" } , Utils . arrayFromString ( str ) ) . then ( function ( hash ) {
1113
1118
return Utils . Base64Url . encodeArray ( hash ) ;
1114
1119
} ) ;
1115
1120
} ;
1116
1121
1117
1122
Utils . isCryptoKey = function ( rsa_key ) {
1118
1123
// Some browsers don't expose the CryptoKey as an object, so we need to check
1119
1124
// the constructor's name.
1120
- return rsa_key . constructor . name == 'CryptoKey' ;
1125
+ if ( rsa_key . constructor . name == 'CryptoKey' ) {
1126
+ return true ;
1127
+ }
1121
1128
} ;
1122
1129
1123
1130
/*-
0 commit comments