@@ -36,9 +36,14 @@ var JoseJWS = {};
3636 * Set crypto provider to use (window.crypto, node-webcrypto-ossl, node-webcrypto-pkcs11 etc.). 
3737 */ 
3838exports . setCrypto  =  function  ( cp )  { 
39-   crypto  =  cp ; 
39+   Jose . crypto  =  cp ; 
4040} ; 
4141
42+ /** 
43+  * Default to the global "crypto" variable 
44+  */ 
45+ exports . setCrypto ( crypto ) ; 
46+ 
4247/** 
4348 * Use Node versions of atob, btoa functions outside the browser 
4449 */ 
@@ -93,17 +98,17 @@ Jose.caniuse = function() {
9398  r  =  r  &&  ( typeof  Promise . all  ==  "function" ) ; 
9499
95100  // 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" ) ; 
107112
108113  // ArrayBuffer (http://people.mozilla.org/~jorendorff/es6-draft.html#sec-arraybuffer-constructor) 
109114  r  =  r  &&  ( typeof  ArrayBuffer  ==  "function" ) ; 
@@ -213,7 +218,7 @@ WebCryptographer.prototype.getContentSignAlgorithm = function() {
213218 */ 
214219WebCryptographer . prototype . createIV  =  function ( )  { 
215220  var  iv  =  new  Uint8Array ( new  Array ( this . content_encryption . iv_bytes ) ) ; 
216-   return  crypto . getRandomValues ( iv ) ; 
221+   return  Jose . crypto . getRandomValues ( iv ) ; 
217222} ; 
218223
219224/** 
@@ -224,19 +229,19 @@ WebCryptographer.prototype.createIV = function() {
224229 */ 
225230WebCryptographer . prototype . createCek  =  function ( )  { 
226231  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 ) ; 
228233} ; 
229234
230235WebCryptographer . 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 ) ; 
232237} ; 
233238
234239WebCryptographer . prototype . unwrapCek  =  function ( cek ,  key )  { 
235240  var  hack  =  getCekWorkaround ( this . content_encryption ) ; 
236241  var  extractable  =  ( this . content_encryption . specific_cek_bytes  >  0 ) ; 
237242  var  key_encryption  =  this . key_encryption . id ; 
238243
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 ) ; 
240245} ; 
241246
242247/** 
@@ -291,7 +296,7 @@ WebCryptographer.prototype.encrypt = function(iv, aad, cek_promise, plain_text)
291296    } ; 
292297
293298    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 )  { 
295300        var  offset  =  cipher_text . byteLength  -  tag_bytes ; 
296301        return  { 
297302          cipher : cipher_text . slice ( 0 ,  offset ) , 
@@ -310,7 +315,7 @@ WebCryptographer.prototype.encrypt = function(iv, aad, cek_promise, plain_text)
310315        name : config . id . name , 
311316        iv : iv 
312317      } ; 
313-       return  crypto . subtle . encrypt ( enc ,  enc_key ,  plain_text ) ; 
318+       return  Jose . crypto . subtle . encrypt ( enc ,  enc_key ,  plain_text ) ; 
314319    } ) ; 
315320
316321    // compute MAC 
@@ -355,8 +360,8 @@ WebCryptographer.prototype.decrypt = function(cek_promise, aad, iv, cipher_text,
355360    Jose . assert ( arr2  instanceof  Uint8Array ,  "compare: invalid input" ) ; 
356361
357362    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 ) ; 
360365      return  Promise . all ( [ hash1 ,  hash2 ] ) . then ( function ( all )  { 
361366        var  hash1  =  new  Uint8Array ( all [ 0 ] ) ; 
362367        var  hash2  =  new  Uint8Array ( all [ 1 ] ) ; 
@@ -388,7 +393,7 @@ WebCryptographer.prototype.decrypt = function(cek_promise, aad, iv, cipher_text,
388393
389394    return  cek_promise . then ( function ( cek )  { 
390395      var  buf  =  Utils . arrayBufferConcat ( cipher_text ,  tag ) ; 
391-       return  crypto . subtle . decrypt ( dec ,  cek ,  buf ) ; 
396+       return  Jose . crypto . subtle . decrypt ( dec ,  cek ,  buf ) ; 
392397    } ) ; 
393398  }  else  { 
394399    var  keys  =  splitKey ( config ,  cek_promise ,  [ "decrypt" ] ) ; 
@@ -412,7 +417,7 @@ WebCryptographer.prototype.decrypt = function(cek_promise, aad, iv, cipher_text,
412417          name : config . id . name , 
413418          iv : iv 
414419        } ; 
415-         return  crypto . subtle . decrypt ( dec ,  enc_key ,  cipher_text ) ; 
420+         return  Jose . crypto . subtle . decrypt ( dec ,  enc_key ,  cipher_text ) ; 
416421      } ) . catch ( function ( err )  { 
417422        return  Promise . reject ( Error ( "decryptCiphertext: MAC failed." ) ) ; 
418423      } ) ; 
@@ -437,7 +442,7 @@ WebCryptographer.prototype.sign = function(aad, payload, key_promise) {
437442
438443  // Encrypt the plain text 
439444  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 ) ) ) ; 
441446  } ) ; 
442447} ; 
443448
@@ -456,7 +461,7 @@ WebCryptographer.prototype.verify = function(aad, payload, signature, key_promis
456461
457462  return  key_promise . then ( function ( key )  { 
458463    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 )  { 
460465      return  { kid : key_id ,  verified : res } ; 
461466    } ) ; 
462467  } ) ; 
@@ -481,21 +486,21 @@ Jose.WebCryptographer.keyId = function(rsa_key) {
481486var  splitKey  =  function ( config ,  cek_promise ,  purpose )  { 
482487  // We need to split the CEK key into a MAC and ENC keys 
483488  var  cek_bytes_promise  =  cek_promise . then ( function ( cek )  { 
484-     return  crypto . subtle . exportKey ( "raw" ,  cek ) ; 
489+     return  Jose . crypto . subtle . exportKey ( "raw" ,  cek ) ; 
485490  } ) ; 
486491  var  mac_key_promise  =  cek_bytes_promise . then ( function ( cek_bytes )  { 
487492    if  ( cek_bytes . byteLength  *  8  !=  config . id . length  +  config . auth . key_bytes  *  8 )  { 
488493      return  Promise . reject ( Error ( "encryptPlainText: incorrect cek length" ) ) ; 
489494    } 
490495    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" ] ) ; 
492497  } ) ; 
493498  var  enc_key_promise  =  cek_bytes_promise . then ( function ( cek_bytes )  { 
494499    if  ( cek_bytes . byteLength  *  8  !=  config . id . length  +  config . auth . key_bytes  *  8 )  { 
495500      return  Promise . reject ( Error ( "encryptPlainText: incorrect cek length" ) ) ; 
496501    } 
497502    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 ) ; 
499504  } ) ; 
500505  return  [ mac_key_promise ,  enc_key_promise ] ; 
501506} ; 
@@ -601,7 +606,7 @@ var truncatedMac = function(config, mac_key_promise, aad, iv, cipher_text) {
601606    var  al_full  =  new  Uint8Array ( 8 ) ; 
602607    al_full . set ( al ,  4 ) ; 
603608    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 )  { 
605610      return  bytes . slice ( 0 ,  config . auth . truncated_bytes ) ; 
606611    } ) ; 
607612  } ) ; 
@@ -805,7 +810,7 @@ Jose.Utils.importRsaPublicKey = function(rsa_key, alg) {
805810    jwk  =  Utils . convertRsaKey ( rk ,  [ "n" ,  "e" ] ) ; 
806811    jwk . ext  =  true ; 
807812  } 
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 ] ) ; 
809814} ; 
810815
811816/** 
@@ -845,7 +850,7 @@ Jose.Utils.importRsaPrivateKey = function(rsa_key, alg) {
845850    jwk  =  Utils . convertRsaKey ( rk ,  [ "n" ,  "e" ,  "d" ,  "p" ,  "q" ,  "dp" ,  "dq" ,  "qi" ] ) ; 
846851    jwk . ext  =  true ; 
847852  } 
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 ] ) ; 
849854} ; 
850855
851856// Private functions 
@@ -1109,15 +1114,17 @@ Utils.sha256 = function(str) {
11091114  // Browser docs indicate the first parameter to crypto.subtle.digest to be a 
11101115  // DOMString. This was initially implemented as an object and continues to be 
11111116  // 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 )  { 
11131118    return  Utils . Base64Url . encodeArray ( hash ) ; 
11141119  } ) ; 
11151120} ; 
11161121
11171122Utils . isCryptoKey  =  function ( rsa_key )  { 
11181123  // Some browsers don't expose the CryptoKey as an object, so we need to check 
11191124  // the constructor's name. 
1120-   return  rsa_key . constructor . name  ==  'CryptoKey' ; 
1125+   if  ( rsa_key . constructor . name  ==  'CryptoKey' )  { 
1126+     return  true ; 
1127+   } 
11211128} ; 
11221129
11231130/*- 
0 commit comments