generate.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { createSecretKey, generateKeyPair as generateKeyPairCb } from 'crypto';
  2. import { promisify } from 'util';
  3. import random from './random.js';
  4. import { setModulusLength } from './check_modulus_length.js';
  5. import { JOSENotSupported } from '../util/errors.js';
  6. const generate = promisify(generateKeyPairCb);
  7. export async function generateSecret(alg, options) {
  8. let length;
  9. switch (alg) {
  10. case 'HS256':
  11. case 'HS384':
  12. case 'HS512':
  13. case 'A128CBC-HS256':
  14. case 'A192CBC-HS384':
  15. case 'A256CBC-HS512':
  16. length = parseInt(alg.slice(-3), 10);
  17. break;
  18. case 'A128KW':
  19. case 'A192KW':
  20. case 'A256KW':
  21. case 'A128GCMKW':
  22. case 'A192GCMKW':
  23. case 'A256GCMKW':
  24. case 'A128GCM':
  25. case 'A192GCM':
  26. case 'A256GCM':
  27. length = parseInt(alg.slice(1, 4), 10);
  28. break;
  29. default:
  30. throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
  31. }
  32. return createSecretKey(random(new Uint8Array(length >> 3)));
  33. }
  34. export async function generateKeyPair(alg, options) {
  35. var _a, _b;
  36. switch (alg) {
  37. case 'RS256':
  38. case 'RS384':
  39. case 'RS512':
  40. case 'PS256':
  41. case 'PS384':
  42. case 'PS512':
  43. case 'RSA-OAEP':
  44. case 'RSA-OAEP-256':
  45. case 'RSA-OAEP-384':
  46. case 'RSA-OAEP-512':
  47. case 'RSA1_5': {
  48. const modulusLength = (_a = options === null || options === void 0 ? void 0 : options.modulusLength) !== null && _a !== void 0 ? _a : 2048;
  49. if (typeof modulusLength !== 'number' || modulusLength < 2048) {
  50. throw new JOSENotSupported('Invalid or unsupported modulusLength option provided, 2048 bits or larger keys must be used');
  51. }
  52. const keypair = await generate('rsa', {
  53. modulusLength,
  54. publicExponent: 0x10001,
  55. });
  56. setModulusLength(keypair.privateKey, modulusLength);
  57. setModulusLength(keypair.publicKey, modulusLength);
  58. return keypair;
  59. }
  60. case 'ES256':
  61. return generate('ec', { namedCurve: 'P-256' });
  62. case 'ES256K':
  63. return generate('ec', { namedCurve: 'secp256k1' });
  64. case 'ES384':
  65. return generate('ec', { namedCurve: 'P-384' });
  66. case 'ES512':
  67. return generate('ec', { namedCurve: 'P-521' });
  68. case 'EdDSA': {
  69. switch (options === null || options === void 0 ? void 0 : options.crv) {
  70. case undefined:
  71. case 'Ed25519':
  72. return generate('ed25519');
  73. case 'Ed448':
  74. return generate('ed448');
  75. default:
  76. throw new JOSENotSupported('Invalid or unsupported crv option provided, supported values are Ed25519 and Ed448');
  77. }
  78. }
  79. case 'ECDH-ES':
  80. case 'ECDH-ES+A128KW':
  81. case 'ECDH-ES+A192KW':
  82. case 'ECDH-ES+A256KW':
  83. const crv = (_b = options === null || options === void 0 ? void 0 : options.crv) !== null && _b !== void 0 ? _b : 'P-256';
  84. switch (crv) {
  85. case undefined:
  86. case 'P-256':
  87. case 'P-384':
  88. case 'P-521':
  89. return generate('ec', { namedCurve: crv });
  90. case 'X25519':
  91. return generate('x25519');
  92. case 'X448':
  93. return generate('x448');
  94. default:
  95. throw new JOSENotSupported('Invalid or unsupported crv option provided, supported values are P-256, P-384, P-521, X25519, and X448');
  96. }
  97. default:
  98. throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
  99. }
  100. }