node_key.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const crypto_1 = require("crypto");
  4. const get_named_curve_js_1 = require("./get_named_curve.js");
  5. const errors_js_1 = require("../util/errors.js");
  6. const check_modulus_length_js_1 = require("./check_modulus_length.js");
  7. const flags_js_1 = require("./flags.js");
  8. const PSS = {
  9. padding: crypto_1.constants.RSA_PKCS1_PSS_PADDING,
  10. saltLength: crypto_1.constants.RSA_PSS_SALTLEN_DIGEST,
  11. };
  12. const ecCurveAlgMap = new Map([
  13. ['ES256', 'P-256'],
  14. ['ES256K', 'secp256k1'],
  15. ['ES384', 'P-384'],
  16. ['ES512', 'P-521'],
  17. ]);
  18. function keyForCrypto(alg, key) {
  19. switch (alg) {
  20. case 'EdDSA':
  21. if (!['ed25519', 'ed448'].includes(key.asymmetricKeyType)) {
  22. throw new TypeError('Invalid key for this operation, its asymmetricKeyType must be ed25519 or ed448');
  23. }
  24. return key;
  25. case 'RS256':
  26. case 'RS384':
  27. case 'RS512':
  28. if (key.asymmetricKeyType !== 'rsa') {
  29. throw new TypeError('Invalid key for this operation, its asymmetricKeyType must be rsa');
  30. }
  31. (0, check_modulus_length_js_1.default)(key, alg);
  32. return key;
  33. case flags_js_1.rsaPssParams && 'PS256':
  34. case flags_js_1.rsaPssParams && 'PS384':
  35. case flags_js_1.rsaPssParams && 'PS512':
  36. if (key.asymmetricKeyType === 'rsa-pss') {
  37. const { hashAlgorithm, mgf1HashAlgorithm, saltLength } = key.asymmetricKeyDetails;
  38. const length = parseInt(alg.slice(-3), 10);
  39. if (hashAlgorithm !== undefined &&
  40. (hashAlgorithm !== `sha${length}` || mgf1HashAlgorithm !== hashAlgorithm)) {
  41. throw new TypeError(`Invalid key for this operation, its RSA-PSS parameters do not meet the requirements of "alg" ${alg}`);
  42. }
  43. if (saltLength !== undefined && saltLength > length >> 3) {
  44. throw new TypeError(`Invalid key for this operation, its RSA-PSS parameter saltLength does not meet the requirements of "alg" ${alg}`);
  45. }
  46. }
  47. else if (key.asymmetricKeyType !== 'rsa') {
  48. throw new TypeError('Invalid key for this operation, its asymmetricKeyType must be rsa or rsa-pss');
  49. }
  50. (0, check_modulus_length_js_1.default)(key, alg);
  51. return { key, ...PSS };
  52. case !flags_js_1.rsaPssParams && 'PS256':
  53. case !flags_js_1.rsaPssParams && 'PS384':
  54. case !flags_js_1.rsaPssParams && 'PS512':
  55. if (key.asymmetricKeyType !== 'rsa') {
  56. throw new TypeError('Invalid key for this operation, its asymmetricKeyType must be rsa');
  57. }
  58. (0, check_modulus_length_js_1.default)(key, alg);
  59. return { key, ...PSS };
  60. case 'ES256':
  61. case 'ES256K':
  62. case 'ES384':
  63. case 'ES512': {
  64. if (key.asymmetricKeyType !== 'ec') {
  65. throw new TypeError('Invalid key for this operation, its asymmetricKeyType must be ec');
  66. }
  67. const actual = (0, get_named_curve_js_1.default)(key);
  68. const expected = ecCurveAlgMap.get(alg);
  69. if (actual !== expected) {
  70. throw new TypeError(`Invalid key curve for the algorithm, its curve must be ${expected}, got ${actual}`);
  71. }
  72. return { dsaEncoding: 'ieee-p1363', key };
  73. }
  74. default:
  75. throw new errors_js_1.JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
  76. }
  77. }
  78. exports.default = keyForCrypto;