test_jwcrypto.js (1655B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 ChromeUtils.defineESModuleGetters(this, { 7 jwcrypto: "moz-src:///services/crypto/modules/jwcrypto.sys.mjs", 8 }); 9 10 // Enable logging from jwcrypto.sys.mjs. 11 Services.prefs.setStringPref("services.crypto.jwcrypto.log.level", "Debug"); 12 13 add_task(async function test_jwe_roundtrip_ecdh_es_encryption() { 14 const plaintext = crypto.getRandomValues(new Uint8Array(123)); 15 const remoteKey = await crypto.subtle.generateKey( 16 { 17 name: "ECDH", 18 namedCurve: "P-256", 19 }, 20 true, 21 ["deriveKey"] 22 ); 23 const remoteJWK = await crypto.subtle.exportKey("jwk", remoteKey.publicKey); 24 delete remoteJWK.key_ops; 25 const jwe = await jwcrypto.generateJWE(remoteJWK, plaintext); 26 const decrypted = await jwcrypto.decryptJWE(jwe, remoteKey.privateKey); 27 Assert.deepEqual(plaintext, decrypted); 28 }); 29 30 add_task(async function test_jwe_header_includes_key_id() { 31 const plaintext = crypto.getRandomValues(new Uint8Array(123)); 32 const remoteKey = await crypto.subtle.generateKey( 33 { 34 name: "ECDH", 35 namedCurve: "P-256", 36 }, 37 true, 38 ["deriveKey"] 39 ); 40 const remoteJWK = await crypto.subtle.exportKey("jwk", remoteKey.publicKey); 41 delete remoteJWK.key_ops; 42 remoteJWK.kid = "key identifier"; 43 const jwe = await jwcrypto.generateJWE(remoteJWK, plaintext); 44 let [header /* other items deliberately ignored */] = jwe.split("."); 45 header = JSON.parse( 46 new TextDecoder().decode( 47 ChromeUtils.base64URLDecode(header, { padding: "reject" }) 48 ) 49 ); 50 Assert.equal(header.kid, "key identifier"); 51 });