generate_key_pair.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { JOSENotSupported } from '../util/errors.js';
  2. function getModulusLengthOption(options) {
  3. const modulusLength = options?.modulusLength ?? 2048;
  4. if (typeof modulusLength !== 'number' || modulusLength < 2048) {
  5. throw new JOSENotSupported('Invalid or unsupported modulusLength option provided, 2048 bits or larger keys must be used');
  6. }
  7. return modulusLength;
  8. }
  9. export async function generateKeyPair(alg, options) {
  10. let algorithm;
  11. let keyUsages;
  12. switch (alg) {
  13. case 'PS256':
  14. case 'PS384':
  15. case 'PS512':
  16. algorithm = {
  17. name: 'RSA-PSS',
  18. hash: `SHA-${alg.slice(-3)}`,
  19. publicExponent: Uint8Array.of(0x01, 0x00, 0x01),
  20. modulusLength: getModulusLengthOption(options),
  21. };
  22. keyUsages = ['sign', 'verify'];
  23. break;
  24. case 'RS256':
  25. case 'RS384':
  26. case 'RS512':
  27. algorithm = {
  28. name: 'RSASSA-PKCS1-v1_5',
  29. hash: `SHA-${alg.slice(-3)}`,
  30. publicExponent: Uint8Array.of(0x01, 0x00, 0x01),
  31. modulusLength: getModulusLengthOption(options),
  32. };
  33. keyUsages = ['sign', 'verify'];
  34. break;
  35. case 'RSA-OAEP':
  36. case 'RSA-OAEP-256':
  37. case 'RSA-OAEP-384':
  38. case 'RSA-OAEP-512':
  39. algorithm = {
  40. name: 'RSA-OAEP',
  41. hash: `SHA-${parseInt(alg.slice(-3), 10) || 1}`,
  42. publicExponent: Uint8Array.of(0x01, 0x00, 0x01),
  43. modulusLength: getModulusLengthOption(options),
  44. };
  45. keyUsages = ['decrypt', 'unwrapKey', 'encrypt', 'wrapKey'];
  46. break;
  47. case 'ES256':
  48. algorithm = { name: 'ECDSA', namedCurve: 'P-256' };
  49. keyUsages = ['sign', 'verify'];
  50. break;
  51. case 'ES384':
  52. algorithm = { name: 'ECDSA', namedCurve: 'P-384' };
  53. keyUsages = ['sign', 'verify'];
  54. break;
  55. case 'ES512':
  56. algorithm = { name: 'ECDSA', namedCurve: 'P-521' };
  57. keyUsages = ['sign', 'verify'];
  58. break;
  59. case 'Ed25519':
  60. case 'EdDSA': {
  61. keyUsages = ['sign', 'verify'];
  62. algorithm = { name: 'Ed25519' };
  63. break;
  64. }
  65. case 'ML-DSA-44':
  66. case 'ML-DSA-65':
  67. case 'ML-DSA-87': {
  68. keyUsages = ['sign', 'verify'];
  69. algorithm = { name: alg };
  70. break;
  71. }
  72. case 'ECDH-ES':
  73. case 'ECDH-ES+A128KW':
  74. case 'ECDH-ES+A192KW':
  75. case 'ECDH-ES+A256KW': {
  76. keyUsages = ['deriveBits'];
  77. const crv = options?.crv ?? 'P-256';
  78. switch (crv) {
  79. case 'P-256':
  80. case 'P-384':
  81. case 'P-521': {
  82. algorithm = { name: 'ECDH', namedCurve: crv };
  83. break;
  84. }
  85. case 'X25519':
  86. algorithm = { name: 'X25519' };
  87. break;
  88. default:
  89. throw new JOSENotSupported('Invalid or unsupported crv option provided, supported values are P-256, P-384, P-521, and X25519');
  90. }
  91. break;
  92. }
  93. default:
  94. throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
  95. }
  96. return crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages);
  97. }