11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciif (!common.hasCrypto) 61cb0ef41Sopenharmony_ci common.skip('missing crypto'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst assert = require('assert'); 91cb0ef41Sopenharmony_ciconst { webcrypto } = require('crypto'); 101cb0ef41Sopenharmony_ciconst { subtle } = webcrypto; 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci{ 131cb0ef41Sopenharmony_ci async function test() { 141cb0ef41Sopenharmony_ci const keyData = webcrypto.getRandomValues(new Uint8Array(32)); 151cb0ef41Sopenharmony_ci await Promise.all([1, null, undefined, {}, []].map((format) => 161cb0ef41Sopenharmony_ci assert.rejects( 171cb0ef41Sopenharmony_ci subtle.importKey(format, keyData, {}, false, ['wrapKey']), { 181cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE' 191cb0ef41Sopenharmony_ci }) 201cb0ef41Sopenharmony_ci )); 211cb0ef41Sopenharmony_ci await assert.rejects( 221cb0ef41Sopenharmony_ci subtle.importKey('not valid', keyData, {}, false, ['wrapKey']), { 231cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE' 241cb0ef41Sopenharmony_ci }); 251cb0ef41Sopenharmony_ci await assert.rejects( 261cb0ef41Sopenharmony_ci subtle.importKey('raw', 1, {}, false, ['deriveBits']), { 271cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE' 281cb0ef41Sopenharmony_ci }); 291cb0ef41Sopenharmony_ci await assert.rejects( 301cb0ef41Sopenharmony_ci subtle.importKey('raw', keyData, { 311cb0ef41Sopenharmony_ci name: 'HMAC' 321cb0ef41Sopenharmony_ci }, false, ['sign', 'verify']), { 331cb0ef41Sopenharmony_ci code: 'ERR_MISSING_OPTION' 341cb0ef41Sopenharmony_ci }); 351cb0ef41Sopenharmony_ci await assert.rejects( 361cb0ef41Sopenharmony_ci subtle.importKey('raw', keyData, { 371cb0ef41Sopenharmony_ci name: 'HMAC', 381cb0ef41Sopenharmony_ci hash: 'SHA-256' 391cb0ef41Sopenharmony_ci }, false, ['deriveBits']), { 401cb0ef41Sopenharmony_ci name: 'SyntaxError', 411cb0ef41Sopenharmony_ci message: 'Unsupported key usage for an HMAC key' 421cb0ef41Sopenharmony_ci }); 431cb0ef41Sopenharmony_ci await assert.rejects( 441cb0ef41Sopenharmony_ci subtle.importKey('raw', keyData, { 451cb0ef41Sopenharmony_ci name: 'HMAC', 461cb0ef41Sopenharmony_ci hash: 'SHA-256', 471cb0ef41Sopenharmony_ci length: 0 481cb0ef41Sopenharmony_ci }, false, ['sign', 'verify']), { 491cb0ef41Sopenharmony_ci name: 'DataError', 501cb0ef41Sopenharmony_ci message: 'Zero-length key is not supported' 511cb0ef41Sopenharmony_ci }); 521cb0ef41Sopenharmony_ci await assert.rejects( 531cb0ef41Sopenharmony_ci subtle.importKey('raw', keyData, { 541cb0ef41Sopenharmony_ci name: 'HMAC', 551cb0ef41Sopenharmony_ci hash: 'SHA-256', 561cb0ef41Sopenharmony_ci length: 1 571cb0ef41Sopenharmony_ci }, false, ['sign', 'verify']), { 581cb0ef41Sopenharmony_ci name: 'DataError', 591cb0ef41Sopenharmony_ci message: 'Invalid key length' 601cb0ef41Sopenharmony_ci }); 611cb0ef41Sopenharmony_ci await assert.rejects( 621cb0ef41Sopenharmony_ci subtle.importKey('jwk', null, { 631cb0ef41Sopenharmony_ci name: 'HMAC', 641cb0ef41Sopenharmony_ci hash: 'SHA-256', 651cb0ef41Sopenharmony_ci }, false, ['sign', 'verify']), { 661cb0ef41Sopenharmony_ci name: 'DataError', 671cb0ef41Sopenharmony_ci message: 'Invalid keyData' 681cb0ef41Sopenharmony_ci }); 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci test().then(common.mustCall()); 721cb0ef41Sopenharmony_ci} 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci// Import/Export HMAC Secret Key 751cb0ef41Sopenharmony_ci{ 761cb0ef41Sopenharmony_ci async function test() { 771cb0ef41Sopenharmony_ci const keyData = webcrypto.getRandomValues(new Uint8Array(32)); 781cb0ef41Sopenharmony_ci const key = await subtle.importKey( 791cb0ef41Sopenharmony_ci 'raw', 801cb0ef41Sopenharmony_ci keyData, { 811cb0ef41Sopenharmony_ci name: 'HMAC', 821cb0ef41Sopenharmony_ci hash: 'SHA-256' 831cb0ef41Sopenharmony_ci }, true, ['sign', 'verify']); 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci const raw = await subtle.exportKey('raw', key); 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci assert.deepStrictEqual( 881cb0ef41Sopenharmony_ci Buffer.from(keyData).toString('hex'), 891cb0ef41Sopenharmony_ci Buffer.from(raw).toString('hex')); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci const jwk = await subtle.exportKey('jwk', key); 921cb0ef41Sopenharmony_ci assert.deepStrictEqual(jwk.key_ops, ['sign', 'verify']); 931cb0ef41Sopenharmony_ci assert(jwk.ext); 941cb0ef41Sopenharmony_ci assert.strictEqual(jwk.kty, 'oct'); 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci assert.deepStrictEqual( 971cb0ef41Sopenharmony_ci Buffer.from(jwk.k, 'base64').toString('hex'), 981cb0ef41Sopenharmony_ci Buffer.from(raw).toString('hex')); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci await assert.rejects( 1011cb0ef41Sopenharmony_ci subtle.importKey( 1021cb0ef41Sopenharmony_ci 'raw', 1031cb0ef41Sopenharmony_ci keyData, 1041cb0ef41Sopenharmony_ci { 1051cb0ef41Sopenharmony_ci name: 'HMAC', 1061cb0ef41Sopenharmony_ci hash: 'SHA-256' 1071cb0ef41Sopenharmony_ci }, 1081cb0ef41Sopenharmony_ci true, 1091cb0ef41Sopenharmony_ci [/* empty usages */]), 1101cb0ef41Sopenharmony_ci { name: 'SyntaxError', message: 'Usages cannot be empty when importing a secret key.' }); 1111cb0ef41Sopenharmony_ci } 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci test().then(common.mustCall()); 1141cb0ef41Sopenharmony_ci} 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci// Import/Export AES Secret Key 1171cb0ef41Sopenharmony_ci{ 1181cb0ef41Sopenharmony_ci async function test() { 1191cb0ef41Sopenharmony_ci const keyData = webcrypto.getRandomValues(new Uint8Array(32)); 1201cb0ef41Sopenharmony_ci const key = await subtle.importKey( 1211cb0ef41Sopenharmony_ci 'raw', 1221cb0ef41Sopenharmony_ci keyData, { 1231cb0ef41Sopenharmony_ci name: 'AES-CTR', 1241cb0ef41Sopenharmony_ci length: 256, 1251cb0ef41Sopenharmony_ci }, true, ['encrypt', 'decrypt']); 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci const raw = await subtle.exportKey('raw', key); 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci assert.deepStrictEqual( 1301cb0ef41Sopenharmony_ci Buffer.from(keyData).toString('hex'), 1311cb0ef41Sopenharmony_ci Buffer.from(raw).toString('hex')); 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci const jwk = await subtle.exportKey('jwk', key); 1341cb0ef41Sopenharmony_ci assert.deepStrictEqual(jwk.key_ops, ['encrypt', 'decrypt']); 1351cb0ef41Sopenharmony_ci assert(jwk.ext); 1361cb0ef41Sopenharmony_ci assert.strictEqual(jwk.kty, 'oct'); 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci assert.deepStrictEqual( 1391cb0ef41Sopenharmony_ci Buffer.from(jwk.k, 'base64').toString('hex'), 1401cb0ef41Sopenharmony_ci Buffer.from(raw).toString('hex')); 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci await assert.rejects( 1431cb0ef41Sopenharmony_ci subtle.importKey( 1441cb0ef41Sopenharmony_ci 'raw', 1451cb0ef41Sopenharmony_ci keyData, 1461cb0ef41Sopenharmony_ci { 1471cb0ef41Sopenharmony_ci name: 'AES-CTR', 1481cb0ef41Sopenharmony_ci length: 256, 1491cb0ef41Sopenharmony_ci }, 1501cb0ef41Sopenharmony_ci true, 1511cb0ef41Sopenharmony_ci [/* empty usages */]), 1521cb0ef41Sopenharmony_ci { name: 'SyntaxError', message: 'Usages cannot be empty when importing a secret key.' }); 1531cb0ef41Sopenharmony_ci } 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ci test().then(common.mustCall()); 1561cb0ef41Sopenharmony_ci} 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci// Import/Export RSA Key Pairs 1591cb0ef41Sopenharmony_ci{ 1601cb0ef41Sopenharmony_ci async function test() { 1611cb0ef41Sopenharmony_ci const { publicKey, privateKey } = await subtle.generateKey({ 1621cb0ef41Sopenharmony_ci name: 'RSA-PSS', 1631cb0ef41Sopenharmony_ci modulusLength: 1024, 1641cb0ef41Sopenharmony_ci publicExponent: new Uint8Array([1, 0, 1]), 1651cb0ef41Sopenharmony_ci hash: 'SHA-384' 1661cb0ef41Sopenharmony_ci }, true, ['sign', 'verify']); 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci const [ 1691cb0ef41Sopenharmony_ci spki, 1701cb0ef41Sopenharmony_ci pkcs8, 1711cb0ef41Sopenharmony_ci publicJwk, 1721cb0ef41Sopenharmony_ci privateJwk, 1731cb0ef41Sopenharmony_ci ] = await Promise.all([ 1741cb0ef41Sopenharmony_ci subtle.exportKey('spki', publicKey), 1751cb0ef41Sopenharmony_ci subtle.exportKey('pkcs8', privateKey), 1761cb0ef41Sopenharmony_ci subtle.exportKey('jwk', publicKey), 1771cb0ef41Sopenharmony_ci subtle.exportKey('jwk', privateKey), 1781cb0ef41Sopenharmony_ci ]); 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci assert(spki); 1811cb0ef41Sopenharmony_ci assert(pkcs8); 1821cb0ef41Sopenharmony_ci assert(publicJwk); 1831cb0ef41Sopenharmony_ci assert(privateJwk); 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci const [ 1861cb0ef41Sopenharmony_ci importedSpkiPublicKey, 1871cb0ef41Sopenharmony_ci importedPkcs8PrivateKey, 1881cb0ef41Sopenharmony_ci importedJwkPublicKey, 1891cb0ef41Sopenharmony_ci importedJwkPrivateKey, 1901cb0ef41Sopenharmony_ci ] = await Promise.all([ 1911cb0ef41Sopenharmony_ci subtle.importKey('spki', spki, { 1921cb0ef41Sopenharmony_ci name: 'RSA-PSS', 1931cb0ef41Sopenharmony_ci hash: 'SHA-384', 1941cb0ef41Sopenharmony_ci }, true, ['verify']), 1951cb0ef41Sopenharmony_ci subtle.importKey('pkcs8', pkcs8, { 1961cb0ef41Sopenharmony_ci name: 'RSA-PSS', 1971cb0ef41Sopenharmony_ci hash: 'SHA-384', 1981cb0ef41Sopenharmony_ci }, true, ['sign']), 1991cb0ef41Sopenharmony_ci subtle.importKey('jwk', publicJwk, { 2001cb0ef41Sopenharmony_ci name: 'RSA-PSS', 2011cb0ef41Sopenharmony_ci hash: 'SHA-384', 2021cb0ef41Sopenharmony_ci }, true, ['verify']), 2031cb0ef41Sopenharmony_ci subtle.importKey('jwk', privateJwk, { 2041cb0ef41Sopenharmony_ci name: 'RSA-PSS', 2051cb0ef41Sopenharmony_ci hash: 'SHA-384', 2061cb0ef41Sopenharmony_ci }, true, ['sign']), 2071cb0ef41Sopenharmony_ci ]); 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci assert(importedSpkiPublicKey); 2101cb0ef41Sopenharmony_ci assert(importedPkcs8PrivateKey); 2111cb0ef41Sopenharmony_ci assert(importedJwkPublicKey); 2121cb0ef41Sopenharmony_ci assert(importedJwkPrivateKey); 2131cb0ef41Sopenharmony_ci } 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci test().then(common.mustCall()); 2161cb0ef41Sopenharmony_ci} 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci// Import/Export EC Key Pairs 2191cb0ef41Sopenharmony_ci{ 2201cb0ef41Sopenharmony_ci async function test() { 2211cb0ef41Sopenharmony_ci const { publicKey, privateKey } = await subtle.generateKey({ 2221cb0ef41Sopenharmony_ci name: 'ECDSA', 2231cb0ef41Sopenharmony_ci namedCurve: 'P-384' 2241cb0ef41Sopenharmony_ci }, true, ['sign', 'verify']); 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ci const [ 2271cb0ef41Sopenharmony_ci spki, 2281cb0ef41Sopenharmony_ci pkcs8, 2291cb0ef41Sopenharmony_ci publicJwk, 2301cb0ef41Sopenharmony_ci privateJwk, 2311cb0ef41Sopenharmony_ci ] = await Promise.all([ 2321cb0ef41Sopenharmony_ci subtle.exportKey('spki', publicKey), 2331cb0ef41Sopenharmony_ci subtle.exportKey('pkcs8', privateKey), 2341cb0ef41Sopenharmony_ci subtle.exportKey('jwk', publicKey), 2351cb0ef41Sopenharmony_ci subtle.exportKey('jwk', privateKey), 2361cb0ef41Sopenharmony_ci ]); 2371cb0ef41Sopenharmony_ci 2381cb0ef41Sopenharmony_ci assert(spki); 2391cb0ef41Sopenharmony_ci assert(pkcs8); 2401cb0ef41Sopenharmony_ci assert(publicJwk); 2411cb0ef41Sopenharmony_ci assert(privateJwk); 2421cb0ef41Sopenharmony_ci 2431cb0ef41Sopenharmony_ci const [ 2441cb0ef41Sopenharmony_ci importedSpkiPublicKey, 2451cb0ef41Sopenharmony_ci importedPkcs8PrivateKey, 2461cb0ef41Sopenharmony_ci importedJwkPublicKey, 2471cb0ef41Sopenharmony_ci importedJwkPrivateKey, 2481cb0ef41Sopenharmony_ci ] = await Promise.all([ 2491cb0ef41Sopenharmony_ci subtle.importKey('spki', spki, { 2501cb0ef41Sopenharmony_ci name: 'ECDSA', 2511cb0ef41Sopenharmony_ci namedCurve: 'P-384' 2521cb0ef41Sopenharmony_ci }, true, ['verify']), 2531cb0ef41Sopenharmony_ci subtle.importKey('pkcs8', pkcs8, { 2541cb0ef41Sopenharmony_ci name: 'ECDSA', 2551cb0ef41Sopenharmony_ci namedCurve: 'P-384' 2561cb0ef41Sopenharmony_ci }, true, ['sign']), 2571cb0ef41Sopenharmony_ci subtle.importKey('jwk', publicJwk, { 2581cb0ef41Sopenharmony_ci name: 'ECDSA', 2591cb0ef41Sopenharmony_ci namedCurve: 'P-384' 2601cb0ef41Sopenharmony_ci }, true, ['verify']), 2611cb0ef41Sopenharmony_ci subtle.importKey('jwk', privateJwk, { 2621cb0ef41Sopenharmony_ci name: 'ECDSA', 2631cb0ef41Sopenharmony_ci namedCurve: 'P-384' 2641cb0ef41Sopenharmony_ci }, true, ['sign']), 2651cb0ef41Sopenharmony_ci ]); 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ci assert(importedSpkiPublicKey); 2681cb0ef41Sopenharmony_ci assert(importedPkcs8PrivateKey); 2691cb0ef41Sopenharmony_ci assert(importedJwkPublicKey); 2701cb0ef41Sopenharmony_ci assert(importedJwkPrivateKey); 2711cb0ef41Sopenharmony_ci } 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci test().then(common.mustCall()); 2741cb0ef41Sopenharmony_ci} 275