11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (!common.hasCrypto)
71cb0ef41Sopenharmony_ci  common.skip('missing crypto');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst crypto = require('crypto');
111cb0ef41Sopenharmony_ciconst { subtle } = crypto.webcrypto;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst keyData = {
141cb0ef41Sopenharmony_ci  'Ed25519': {
151cb0ef41Sopenharmony_ci    jwsAlg: 'EdDSA',
161cb0ef41Sopenharmony_ci    spki: Buffer.from(
171cb0ef41Sopenharmony_ci      '302a300506032b6570032100a054b618c12b26c8d43595a5c38dd2b0140b944a' +
181cb0ef41Sopenharmony_ci      '151f75003278c2b6c58ec08f', 'hex'),
191cb0ef41Sopenharmony_ci    pkcs8: Buffer.from(
201cb0ef41Sopenharmony_ci      '302e020100300506032b657004220420d53150bdcd17b4d4b21ae756d4965639' +
211cb0ef41Sopenharmony_ci      'd75b28f56ff9111b1f88326913e445bc', 'hex'),
221cb0ef41Sopenharmony_ci    jwk: {
231cb0ef41Sopenharmony_ci      kty: 'OKP',
241cb0ef41Sopenharmony_ci      crv: 'Ed25519',
251cb0ef41Sopenharmony_ci      x: 'oFS2GMErJsjUNZWlw43SsBQLlEoVH3UAMnjCtsWOwI8',
261cb0ef41Sopenharmony_ci      d: '1TFQvc0XtNSyGudW1JZWOddbKPVv-REbH4gyaRPkRbw'
271cb0ef41Sopenharmony_ci    }
281cb0ef41Sopenharmony_ci  },
291cb0ef41Sopenharmony_ci  'Ed448': {
301cb0ef41Sopenharmony_ci    jwsAlg: 'EdDSA',
311cb0ef41Sopenharmony_ci    spki: Buffer.from(
321cb0ef41Sopenharmony_ci      '3043300506032b6571033a0008cc38160c85bca5656ac4924af7ea97a9161b20' +
331cb0ef41Sopenharmony_ci      '2528273dcb84afd2eeb99ac912a401b34ef15ef4d9486406a6eecc31e5909219' +
341cb0ef41Sopenharmony_ci      'bd54866800', 'hex'),
351cb0ef41Sopenharmony_ci    pkcs8: Buffer.from(
361cb0ef41Sopenharmony_ci      '3047020100300506032b6571043b0439afd05b2fbb153b47c18dfa66baaed0de' +
371cb0ef41Sopenharmony_ci      'fb4e88c651487cdee0fafc40fa3d048fe1cd145a44143243c0468166b5bc161a' +
381cb0ef41Sopenharmony_ci      '82e3b904f3e2fcaaf9', 'hex'),
391cb0ef41Sopenharmony_ci    jwk: {
401cb0ef41Sopenharmony_ci      kty: 'OKP',
411cb0ef41Sopenharmony_ci      crv: 'Ed448',
421cb0ef41Sopenharmony_ci      x: 'CMw4FgyFvKVlasSSSvfql6kWGyAlKCc9y4Sv0u65mskSpAGzTvFe9NlIZAam7' +
431cb0ef41Sopenharmony_ci         'swx5ZCSGb1UhmgA',
441cb0ef41Sopenharmony_ci      d: 'r9BbL7sVO0fBjfpmuq7Q3vtOiMZRSHze4Pr8QPo9BI_hzRRaRBQyQ8BGgWa1v' +
451cb0ef41Sopenharmony_ci         'BYaguO5BPPi_Kr5'
461cb0ef41Sopenharmony_ci    }
471cb0ef41Sopenharmony_ci  },
481cb0ef41Sopenharmony_ci  'X25519': {
491cb0ef41Sopenharmony_ci    jwsAlg: 'ECDH-ES',
501cb0ef41Sopenharmony_ci    spki: Buffer.from(
511cb0ef41Sopenharmony_ci      '302a300506032b656e032100f38d9f4e621a44e0428176a4c8a534b34f07f8db' +
521cb0ef41Sopenharmony_ci      '30152f9ca0167aabf598fe65', 'hex'),
531cb0ef41Sopenharmony_ci    pkcs8: Buffer.from(
541cb0ef41Sopenharmony_ci      '302e020100300506032b656e04220420a8327850317b4b03a5a8b4e923413b1d' +
551cb0ef41Sopenharmony_ci      'a4a642e0d6f7a72cf4d16a549e628a5f', 'hex'),
561cb0ef41Sopenharmony_ci    jwk: {
571cb0ef41Sopenharmony_ci      kty: 'OKP',
581cb0ef41Sopenharmony_ci      crv: 'X25519',
591cb0ef41Sopenharmony_ci      x: '842fTmIaROBCgXakyKU0s08H-NswFS-coBZ6q_WY_mU',
601cb0ef41Sopenharmony_ci      d: 'qDJ4UDF7SwOlqLTpI0E7HaSmQuDW96cs9NFqVJ5iil8'
611cb0ef41Sopenharmony_ci    }
621cb0ef41Sopenharmony_ci  },
631cb0ef41Sopenharmony_ci  'X448': {
641cb0ef41Sopenharmony_ci    jwsAlg: 'ECDH-ES',
651cb0ef41Sopenharmony_ci    spki: Buffer.from(
661cb0ef41Sopenharmony_ci      '3042300506032b656f0339001d451c8c0c369a42eadfc2875cd44953caeb46c4' +
671cb0ef41Sopenharmony_ci      '66dc86568280bfdbbb01f4709a1b0b1e0dd66cf7b11c84119ddc98890db72891' +
681cb0ef41Sopenharmony_ci      '29e30da4', 'hex'),
691cb0ef41Sopenharmony_ci    pkcs8: Buffer.from(
701cb0ef41Sopenharmony_ci      '3046020100300506032b656f043a0438fc818f6546a81f963c27765dc1c05bfd' +
711cb0ef41Sopenharmony_ci      'b169667e5e0cf45318ed1cb93872217ab0d9004e0c7dd0dcb00192f72039cc1a' +
721cb0ef41Sopenharmony_ci      '1dff750ec31c8afb', 'hex'),
731cb0ef41Sopenharmony_ci    jwk: {
741cb0ef41Sopenharmony_ci      kty: 'OKP',
751cb0ef41Sopenharmony_ci      crv: 'X448',
761cb0ef41Sopenharmony_ci      x: 'HUUcjAw2mkLq38KHXNRJU8rrRsRm3IZWgoC_27sB9HCaGwseDdZs97EchBGd3' +
771cb0ef41Sopenharmony_ci         'JiJDbcokSnjDaQ',
781cb0ef41Sopenharmony_ci      d: '_IGPZUaoH5Y8J3ZdwcBb_bFpZn5eDPRTGO0cuThyIXqw2QBODH3Q3LABkvcgO' +
791cb0ef41Sopenharmony_ci         'cwaHf91DsMcivs'
801cb0ef41Sopenharmony_ci    }
811cb0ef41Sopenharmony_ci  }
821cb0ef41Sopenharmony_ci};
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ciconst testVectors = [
851cb0ef41Sopenharmony_ci  {
861cb0ef41Sopenharmony_ci    name: 'Ed25519',
871cb0ef41Sopenharmony_ci    privateUsages: ['sign'],
881cb0ef41Sopenharmony_ci    publicUsages: ['verify']
891cb0ef41Sopenharmony_ci  },
901cb0ef41Sopenharmony_ci  {
911cb0ef41Sopenharmony_ci    name: 'Ed448',
921cb0ef41Sopenharmony_ci    privateUsages: ['sign'],
931cb0ef41Sopenharmony_ci    publicUsages: ['verify']
941cb0ef41Sopenharmony_ci  },
951cb0ef41Sopenharmony_ci  {
961cb0ef41Sopenharmony_ci    name: 'X25519',
971cb0ef41Sopenharmony_ci    privateUsages: ['deriveKey', 'deriveBits'],
981cb0ef41Sopenharmony_ci    publicUsages: []
991cb0ef41Sopenharmony_ci  },
1001cb0ef41Sopenharmony_ci  {
1011cb0ef41Sopenharmony_ci    name: 'X448',
1021cb0ef41Sopenharmony_ci    privateUsages: ['deriveKey', 'deriveBits'],
1031cb0ef41Sopenharmony_ci    publicUsages: []
1041cb0ef41Sopenharmony_ci  },
1051cb0ef41Sopenharmony_ci];
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ciasync function testImportSpki({ name, publicUsages }, extractable) {
1081cb0ef41Sopenharmony_ci  const key = await subtle.importKey(
1091cb0ef41Sopenharmony_ci    'spki',
1101cb0ef41Sopenharmony_ci    keyData[name].spki,
1111cb0ef41Sopenharmony_ci    { name },
1121cb0ef41Sopenharmony_ci    extractable,
1131cb0ef41Sopenharmony_ci    publicUsages);
1141cb0ef41Sopenharmony_ci  assert.strictEqual(key.type, 'public');
1151cb0ef41Sopenharmony_ci  assert.strictEqual(key.extractable, extractable);
1161cb0ef41Sopenharmony_ci  assert.deepStrictEqual(key.usages, publicUsages);
1171cb0ef41Sopenharmony_ci  assert.deepStrictEqual(key.algorithm.name, name);
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  if (extractable) {
1201cb0ef41Sopenharmony_ci    // Test the roundtrip
1211cb0ef41Sopenharmony_ci    const spki = await subtle.exportKey('spki', key);
1221cb0ef41Sopenharmony_ci    assert.strictEqual(
1231cb0ef41Sopenharmony_ci      Buffer.from(spki).toString('hex'),
1241cb0ef41Sopenharmony_ci      keyData[name].spki.toString('hex'));
1251cb0ef41Sopenharmony_ci  } else {
1261cb0ef41Sopenharmony_ci    await assert.rejects(
1271cb0ef41Sopenharmony_ci      subtle.exportKey('spki', key), {
1281cb0ef41Sopenharmony_ci        message: /key is not extractable/
1291cb0ef41Sopenharmony_ci      });
1301cb0ef41Sopenharmony_ci  }
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  // Bad usage
1331cb0ef41Sopenharmony_ci  await assert.rejects(
1341cb0ef41Sopenharmony_ci    subtle.importKey(
1351cb0ef41Sopenharmony_ci      'spki',
1361cb0ef41Sopenharmony_ci      keyData[name].spki,
1371cb0ef41Sopenharmony_ci      { name },
1381cb0ef41Sopenharmony_ci      extractable,
1391cb0ef41Sopenharmony_ci      ['wrapKey']),
1401cb0ef41Sopenharmony_ci    { message: /Unsupported key usage/ });
1411cb0ef41Sopenharmony_ci}
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ciasync function testImportPkcs8({ name, privateUsages }, extractable) {
1441cb0ef41Sopenharmony_ci  const key = await subtle.importKey(
1451cb0ef41Sopenharmony_ci    'pkcs8',
1461cb0ef41Sopenharmony_ci    keyData[name].pkcs8,
1471cb0ef41Sopenharmony_ci    { name },
1481cb0ef41Sopenharmony_ci    extractable,
1491cb0ef41Sopenharmony_ci    privateUsages);
1501cb0ef41Sopenharmony_ci  assert.strictEqual(key.type, 'private');
1511cb0ef41Sopenharmony_ci  assert.strictEqual(key.extractable, extractable);
1521cb0ef41Sopenharmony_ci  assert.deepStrictEqual(key.usages, privateUsages);
1531cb0ef41Sopenharmony_ci  assert.deepStrictEqual(key.algorithm.name, name);
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  if (extractable) {
1561cb0ef41Sopenharmony_ci    // Test the roundtrip
1571cb0ef41Sopenharmony_ci    const pkcs8 = await subtle.exportKey('pkcs8', key);
1581cb0ef41Sopenharmony_ci    assert.strictEqual(
1591cb0ef41Sopenharmony_ci      Buffer.from(pkcs8).toString('hex'),
1601cb0ef41Sopenharmony_ci      keyData[name].pkcs8.toString('hex'));
1611cb0ef41Sopenharmony_ci  } else {
1621cb0ef41Sopenharmony_ci    await assert.rejects(
1631cb0ef41Sopenharmony_ci      subtle.exportKey('pkcs8', key), {
1641cb0ef41Sopenharmony_ci        message: /key is not extractable/
1651cb0ef41Sopenharmony_ci      });
1661cb0ef41Sopenharmony_ci  }
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci  await assert.rejects(
1691cb0ef41Sopenharmony_ci    subtle.importKey(
1701cb0ef41Sopenharmony_ci      'pkcs8',
1711cb0ef41Sopenharmony_ci      keyData[name].pkcs8,
1721cb0ef41Sopenharmony_ci      { name },
1731cb0ef41Sopenharmony_ci      extractable,
1741cb0ef41Sopenharmony_ci      [/* empty usages */]),
1751cb0ef41Sopenharmony_ci    { name: 'SyntaxError', message: 'Usages cannot be empty when importing a private key.' });
1761cb0ef41Sopenharmony_ci}
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ciasync function testImportJwk({ name, publicUsages, privateUsages }, extractable) {
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci  const jwk = keyData[name].jwk;
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci  const [
1831cb0ef41Sopenharmony_ci    publicKey,
1841cb0ef41Sopenharmony_ci    privateKey,
1851cb0ef41Sopenharmony_ci  ] = await Promise.all([
1861cb0ef41Sopenharmony_ci    subtle.importKey(
1871cb0ef41Sopenharmony_ci      'jwk',
1881cb0ef41Sopenharmony_ci      {
1891cb0ef41Sopenharmony_ci        kty: jwk.kty,
1901cb0ef41Sopenharmony_ci        crv: jwk.crv,
1911cb0ef41Sopenharmony_ci        x: jwk.x,
1921cb0ef41Sopenharmony_ci      },
1931cb0ef41Sopenharmony_ci      { name },
1941cb0ef41Sopenharmony_ci      extractable, publicUsages),
1951cb0ef41Sopenharmony_ci    subtle.importKey(
1961cb0ef41Sopenharmony_ci      'jwk',
1971cb0ef41Sopenharmony_ci      jwk,
1981cb0ef41Sopenharmony_ci      { name },
1991cb0ef41Sopenharmony_ci      extractable,
2001cb0ef41Sopenharmony_ci      privateUsages),
2011cb0ef41Sopenharmony_ci    subtle.importKey(
2021cb0ef41Sopenharmony_ci      'jwk',
2031cb0ef41Sopenharmony_ci      {
2041cb0ef41Sopenharmony_ci        alg: keyData[name].jwsAlg,
2051cb0ef41Sopenharmony_ci        kty: jwk.kty,
2061cb0ef41Sopenharmony_ci        crv: jwk.crv,
2071cb0ef41Sopenharmony_ci        x: jwk.x,
2081cb0ef41Sopenharmony_ci      },
2091cb0ef41Sopenharmony_ci      { name },
2101cb0ef41Sopenharmony_ci      extractable, publicUsages),
2111cb0ef41Sopenharmony_ci    subtle.importKey(
2121cb0ef41Sopenharmony_ci      'jwk',
2131cb0ef41Sopenharmony_ci      {
2141cb0ef41Sopenharmony_ci        ...jwk,
2151cb0ef41Sopenharmony_ci        alg: keyData[name].jwsAlg,
2161cb0ef41Sopenharmony_ci      },
2171cb0ef41Sopenharmony_ci      { name },
2181cb0ef41Sopenharmony_ci      extractable,
2191cb0ef41Sopenharmony_ci      privateUsages),
2201cb0ef41Sopenharmony_ci  ]);
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ci  assert.strictEqual(publicKey.type, 'public');
2231cb0ef41Sopenharmony_ci  assert.strictEqual(privateKey.type, 'private');
2241cb0ef41Sopenharmony_ci  assert.strictEqual(publicKey.extractable, extractable);
2251cb0ef41Sopenharmony_ci  assert.strictEqual(privateKey.extractable, extractable);
2261cb0ef41Sopenharmony_ci  assert.deepStrictEqual(publicKey.usages, publicUsages);
2271cb0ef41Sopenharmony_ci  assert.deepStrictEqual(privateKey.usages, privateUsages);
2281cb0ef41Sopenharmony_ci  assert.strictEqual(publicKey.algorithm.name, name);
2291cb0ef41Sopenharmony_ci  assert.strictEqual(privateKey.algorithm.name, name);
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci  if (extractable) {
2321cb0ef41Sopenharmony_ci    // Test the round trip
2331cb0ef41Sopenharmony_ci    const [
2341cb0ef41Sopenharmony_ci      pubJwk,
2351cb0ef41Sopenharmony_ci      pvtJwk,
2361cb0ef41Sopenharmony_ci    ] = await Promise.all([
2371cb0ef41Sopenharmony_ci      subtle.exportKey('jwk', publicKey),
2381cb0ef41Sopenharmony_ci      subtle.exportKey('jwk', privateKey),
2391cb0ef41Sopenharmony_ci    ]);
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci    assert.deepStrictEqual(pubJwk.key_ops, publicUsages);
2421cb0ef41Sopenharmony_ci    assert.strictEqual(pubJwk.ext, true);
2431cb0ef41Sopenharmony_ci    assert.strictEqual(pubJwk.kty, 'OKP');
2441cb0ef41Sopenharmony_ci    assert.strictEqual(pubJwk.x, jwk.x);
2451cb0ef41Sopenharmony_ci    assert.strictEqual(pubJwk.crv, jwk.crv);
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_ci    assert.deepStrictEqual(pvtJwk.key_ops, privateUsages);
2481cb0ef41Sopenharmony_ci    assert.strictEqual(pvtJwk.ext, true);
2491cb0ef41Sopenharmony_ci    assert.strictEqual(pvtJwk.kty, 'OKP');
2501cb0ef41Sopenharmony_ci    assert.strictEqual(pvtJwk.x, jwk.x);
2511cb0ef41Sopenharmony_ci    assert.strictEqual(pvtJwk.crv, jwk.crv);
2521cb0ef41Sopenharmony_ci    assert.strictEqual(pvtJwk.d, jwk.d);
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci    if (jwk.crv.startsWith('Ed')) {
2551cb0ef41Sopenharmony_ci      assert.strictEqual(pubJwk.alg, 'EdDSA');
2561cb0ef41Sopenharmony_ci      assert.strictEqual(pvtJwk.alg, 'EdDSA');
2571cb0ef41Sopenharmony_ci    } else {
2581cb0ef41Sopenharmony_ci      assert.strictEqual(pubJwk.alg, undefined);
2591cb0ef41Sopenharmony_ci      assert.strictEqual(pvtJwk.alg, undefined);
2601cb0ef41Sopenharmony_ci    }
2611cb0ef41Sopenharmony_ci  } else {
2621cb0ef41Sopenharmony_ci    await assert.rejects(
2631cb0ef41Sopenharmony_ci      subtle.exportKey('jwk', publicKey), {
2641cb0ef41Sopenharmony_ci        message: /key is not extractable/
2651cb0ef41Sopenharmony_ci      });
2661cb0ef41Sopenharmony_ci    await assert.rejects(
2671cb0ef41Sopenharmony_ci      subtle.exportKey('jwk', privateKey), {
2681cb0ef41Sopenharmony_ci        message: /key is not extractable/
2691cb0ef41Sopenharmony_ci      });
2701cb0ef41Sopenharmony_ci  }
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ci  {
2731cb0ef41Sopenharmony_ci    const invalidUse = name.startsWith('X') ? 'sig' : 'enc';
2741cb0ef41Sopenharmony_ci    await assert.rejects(
2751cb0ef41Sopenharmony_ci      subtle.importKey(
2761cb0ef41Sopenharmony_ci        'jwk',
2771cb0ef41Sopenharmony_ci        { ...jwk, use: invalidUse },
2781cb0ef41Sopenharmony_ci        { name },
2791cb0ef41Sopenharmony_ci        extractable,
2801cb0ef41Sopenharmony_ci        privateUsages),
2811cb0ef41Sopenharmony_ci      { message: 'Invalid JWK "use" Parameter' });
2821cb0ef41Sopenharmony_ci  }
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ci  if (name.startsWith('Ed')) {
2851cb0ef41Sopenharmony_ci    await assert.rejects(
2861cb0ef41Sopenharmony_ci      subtle.importKey(
2871cb0ef41Sopenharmony_ci        'jwk',
2881cb0ef41Sopenharmony_ci        { kty: jwk.kty, x: jwk.x, crv: jwk.crv, alg: 'foo' },
2891cb0ef41Sopenharmony_ci        { name },
2901cb0ef41Sopenharmony_ci        extractable,
2911cb0ef41Sopenharmony_ci        publicUsages),
2921cb0ef41Sopenharmony_ci      { message: 'JWK "alg" does not match the requested algorithm' });
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci    await assert.rejects(
2951cb0ef41Sopenharmony_ci      subtle.importKey(
2961cb0ef41Sopenharmony_ci        'jwk',
2971cb0ef41Sopenharmony_ci        { ...jwk, alg: 'foo' },
2981cb0ef41Sopenharmony_ci        { name },
2991cb0ef41Sopenharmony_ci        extractable,
3001cb0ef41Sopenharmony_ci        privateUsages),
3011cb0ef41Sopenharmony_ci      { message: 'JWK "alg" does not match the requested algorithm' });
3021cb0ef41Sopenharmony_ci  }
3031cb0ef41Sopenharmony_ci
3041cb0ef41Sopenharmony_ci  for (const crv of [undefined, name === 'Ed25519' ? 'Ed448' : 'Ed25519']) {
3051cb0ef41Sopenharmony_ci    await assert.rejects(
3061cb0ef41Sopenharmony_ci      subtle.importKey(
3071cb0ef41Sopenharmony_ci        'jwk',
3081cb0ef41Sopenharmony_ci        { kty: jwk.kty, x: jwk.x, y: jwk.y, crv },
3091cb0ef41Sopenharmony_ci        { name },
3101cb0ef41Sopenharmony_ci        extractable,
3111cb0ef41Sopenharmony_ci        publicUsages),
3121cb0ef41Sopenharmony_ci      { message: 'JWK "crv" Parameter and algorithm name mismatch' });
3131cb0ef41Sopenharmony_ci
3141cb0ef41Sopenharmony_ci    await assert.rejects(
3151cb0ef41Sopenharmony_ci      subtle.importKey(
3161cb0ef41Sopenharmony_ci        'jwk',
3171cb0ef41Sopenharmony_ci        { ...jwk, crv },
3181cb0ef41Sopenharmony_ci        { name },
3191cb0ef41Sopenharmony_ci        extractable,
3201cb0ef41Sopenharmony_ci        privateUsages),
3211cb0ef41Sopenharmony_ci      { message: 'JWK "crv" Parameter and algorithm name mismatch' });
3221cb0ef41Sopenharmony_ci  }
3231cb0ef41Sopenharmony_ci
3241cb0ef41Sopenharmony_ci  await assert.rejects(
3251cb0ef41Sopenharmony_ci    subtle.importKey(
3261cb0ef41Sopenharmony_ci      'jwk',
3271cb0ef41Sopenharmony_ci      { ...jwk },
3281cb0ef41Sopenharmony_ci      { name },
3291cb0ef41Sopenharmony_ci      extractable,
3301cb0ef41Sopenharmony_ci      [/* empty usages */]),
3311cb0ef41Sopenharmony_ci    { name: 'SyntaxError', message: 'Usages cannot be empty when importing a private key.' });
3321cb0ef41Sopenharmony_ci}
3331cb0ef41Sopenharmony_ci
3341cb0ef41Sopenharmony_ciasync function testImportRaw({ name, publicUsages }) {
3351cb0ef41Sopenharmony_ci  const jwk = keyData[name].jwk;
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_ci  const publicKey = await subtle.importKey(
3381cb0ef41Sopenharmony_ci    'raw',
3391cb0ef41Sopenharmony_ci    Buffer.from(jwk.x, 'base64url'),
3401cb0ef41Sopenharmony_ci    { name },
3411cb0ef41Sopenharmony_ci    true, publicUsages);
3421cb0ef41Sopenharmony_ci
3431cb0ef41Sopenharmony_ci  assert.strictEqual(publicKey.type, 'public');
3441cb0ef41Sopenharmony_ci  assert.deepStrictEqual(publicKey.usages, publicUsages);
3451cb0ef41Sopenharmony_ci  assert.strictEqual(publicKey.algorithm.name, name);
3461cb0ef41Sopenharmony_ci}
3471cb0ef41Sopenharmony_ci
3481cb0ef41Sopenharmony_ci(async function() {
3491cb0ef41Sopenharmony_ci  const tests = [];
3501cb0ef41Sopenharmony_ci  testVectors.forEach((vector) => {
3511cb0ef41Sopenharmony_ci    [true, false].forEach((extractable) => {
3521cb0ef41Sopenharmony_ci      tests.push(testImportSpki(vector, extractable));
3531cb0ef41Sopenharmony_ci      tests.push(testImportPkcs8(vector, extractable));
3541cb0ef41Sopenharmony_ci      tests.push(testImportJwk(vector, extractable));
3551cb0ef41Sopenharmony_ci    });
3561cb0ef41Sopenharmony_ci    tests.push(testImportRaw(vector));
3571cb0ef41Sopenharmony_ci  });
3581cb0ef41Sopenharmony_ci
3591cb0ef41Sopenharmony_ci  await Promise.all(tests);
3601cb0ef41Sopenharmony_ci})().then(common.mustCall());
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci{
3631cb0ef41Sopenharmony_ci  const rsaPublic = crypto.createPublicKey(
3641cb0ef41Sopenharmony_ci    fixtures.readKey('rsa_public_2048.pem'));
3651cb0ef41Sopenharmony_ci  const rsaPrivate = crypto.createPrivateKey(
3661cb0ef41Sopenharmony_ci    fixtures.readKey('rsa_private_2048.pem'));
3671cb0ef41Sopenharmony_ci
3681cb0ef41Sopenharmony_ci  for (const [name, publicUsages, privateUsages] of [
3691cb0ef41Sopenharmony_ci    ['Ed25519', ['verify'], ['sign']],
3701cb0ef41Sopenharmony_ci    ['X448', [], ['deriveBits']],
3711cb0ef41Sopenharmony_ci  ]) {
3721cb0ef41Sopenharmony_ci    assert.rejects(subtle.importKey(
3731cb0ef41Sopenharmony_ci      'spki',
3741cb0ef41Sopenharmony_ci      rsaPublic.export({ format: 'der', type: 'spki' }),
3751cb0ef41Sopenharmony_ci      { name },
3761cb0ef41Sopenharmony_ci      true, publicUsages), { message: /Invalid key type/ });
3771cb0ef41Sopenharmony_ci    assert.rejects(subtle.importKey(
3781cb0ef41Sopenharmony_ci      'pkcs8',
3791cb0ef41Sopenharmony_ci      rsaPrivate.export({ format: 'der', type: 'pkcs8' }),
3801cb0ef41Sopenharmony_ci      { name },
3811cb0ef41Sopenharmony_ci      true, privateUsages), { message: /Invalid key type/ });
3821cb0ef41Sopenharmony_ci  }
3831cb0ef41Sopenharmony_ci}
384