import.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.importJWK = exports.importPKCS8 = exports.importX509 = exports.importSPKI = void 0;
  4. const base64url_js_1 = require("../runtime/base64url.js");
  5. const asn1_js_1 = require("../runtime/asn1.js");
  6. const jwk_to_key_js_1 = require("../runtime/jwk_to_key.js");
  7. const errors_js_1 = require("../util/errors.js");
  8. const is_object_js_1 = require("../lib/is_object.js");
  9. async function importSPKI(spki, alg, options) {
  10. if (typeof spki !== 'string' || spki.indexOf('-----BEGIN PUBLIC KEY-----') !== 0) {
  11. throw new TypeError('"spki" must be SPKI formatted string');
  12. }
  13. return (0, asn1_js_1.fromSPKI)(spki, alg, options);
  14. }
  15. exports.importSPKI = importSPKI;
  16. async function importX509(x509, alg, options) {
  17. if (typeof x509 !== 'string' || x509.indexOf('-----BEGIN CERTIFICATE-----') !== 0) {
  18. throw new TypeError('"x509" must be X.509 formatted string');
  19. }
  20. return (0, asn1_js_1.fromX509)(x509, alg, options);
  21. }
  22. exports.importX509 = importX509;
  23. async function importPKCS8(pkcs8, alg, options) {
  24. if (typeof pkcs8 !== 'string' || pkcs8.indexOf('-----BEGIN PRIVATE KEY-----') !== 0) {
  25. throw new TypeError('"pkcs8" must be PKCS#8 formatted string');
  26. }
  27. return (0, asn1_js_1.fromPKCS8)(pkcs8, alg, options);
  28. }
  29. exports.importPKCS8 = importPKCS8;
  30. async function importJWK(jwk, alg, octAsKeyObject) {
  31. var _a;
  32. if (!(0, is_object_js_1.default)(jwk)) {
  33. throw new TypeError('JWK must be an object');
  34. }
  35. alg || (alg = jwk.alg);
  36. switch (jwk.kty) {
  37. case 'oct':
  38. if (typeof jwk.k !== 'string' || !jwk.k) {
  39. throw new TypeError('missing "k" (Key Value) Parameter value');
  40. }
  41. octAsKeyObject !== null && octAsKeyObject !== void 0 ? octAsKeyObject : (octAsKeyObject = jwk.ext !== true);
  42. if (octAsKeyObject) {
  43. return (0, jwk_to_key_js_1.default)({ ...jwk, alg, ext: (_a = jwk.ext) !== null && _a !== void 0 ? _a : false });
  44. }
  45. return (0, base64url_js_1.decode)(jwk.k);
  46. case 'RSA':
  47. if (jwk.oth !== undefined) {
  48. throw new errors_js_1.JOSENotSupported('RSA JWK "oth" (Other Primes Info) Parameter value is not supported');
  49. }
  50. case 'EC':
  51. case 'OKP':
  52. return (0, jwk_to_key_js_1.default)({ ...jwk, alg });
  53. default:
  54. throw new errors_js_1.JOSENotSupported('Unsupported "kty" (Key Type) Parameter value');
  55. }
  56. }
  57. exports.importJWK = importJWK;