ec-keys.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var { assert } = require('chai');
  2. var crypto = require('crypto');
  3. describe('EC keys', function () {
  4. var generate = require('../index').generate;
  5. it('should generate EC certificate with P-256 curve (default)', async function () {
  6. var pems = await generate(null, { keyType: 'ec' });
  7. assert.ok(!!pems.private, 'has a private key');
  8. assert.ok(!!pems.public, 'has a public key');
  9. assert.ok(!!pems.cert, 'has a certificate');
  10. assert.ok(!!pems.fingerprint, 'has fingerprint');
  11. const privateKey = crypto.createPrivateKey(pems.private);
  12. assert.strictEqual(privateKey.asymmetricKeyType, 'ec', 'should be EC key');
  13. assert.strictEqual(privateKey.asymmetricKeyDetails.namedCurve, 'prime256v1', 'should use P-256 curve');
  14. const cert = new crypto.X509Certificate(pems.cert);
  15. assert.ok(cert.subject, 'cert has a subject');
  16. });
  17. it('should generate EC certificate with P-384 curve', async function () {
  18. var pems = await generate(null, { keyType: 'ec', curve: 'P-384' });
  19. const privateKey = crypto.createPrivateKey(pems.private);
  20. assert.strictEqual(privateKey.asymmetricKeyType, 'ec', 'should be EC key');
  21. assert.strictEqual(privateKey.asymmetricKeyDetails.namedCurve, 'secp384r1', 'should use P-384 curve');
  22. const cert = new crypto.X509Certificate(pems.cert);
  23. assert.ok(cert.publicKey, 'can generate P-384 EC certs');
  24. });
  25. it('should generate EC certificate with P-521 curve', async function () {
  26. var pems = await generate(null, { keyType: 'ec', curve: 'P-521' });
  27. const privateKey = crypto.createPrivateKey(pems.private);
  28. assert.strictEqual(privateKey.asymmetricKeyType, 'ec', 'should be EC key');
  29. assert.strictEqual(privateKey.asymmetricKeyDetails.namedCurve, 'secp521r1', 'should use P-521 curve');
  30. const cert = new crypto.X509Certificate(pems.cert);
  31. assert.ok(cert.publicKey, 'can generate P-521 EC certs');
  32. });
  33. it('should generate valid EC key pair that work together', async function () {
  34. var pems = await generate(null, { keyType: 'ec', curve: 'P-256' });
  35. const testData = 'Hello, World!';
  36. const privateKey = crypto.createPrivateKey(pems.private);
  37. const publicKey = crypto.createPublicKey(pems.public);
  38. // Sign with private key
  39. const sign = crypto.createSign('SHA256');
  40. sign.update(testData);
  41. sign.end();
  42. const signature = sign.sign(privateKey);
  43. // Verify with public key
  44. const verify = crypto.createVerify('SHA256');
  45. verify.update(testData);
  46. verify.end();
  47. const isValid = verify.verify(publicKey, signature);
  48. assert.isTrue(isValid, 'EC public key should verify signature from EC private key');
  49. });
  50. it('should support EC with sha256 algorithm', async function () {
  51. var pems = await generate(null, { keyType: 'ec', algorithm: 'sha256' });
  52. const cert = new crypto.X509Certificate(pems.cert);
  53. assert.ok(cert.publicKey, 'can generate EC cert with sha256');
  54. });
  55. it('should support EC with sha384 algorithm', async function () {
  56. var pems = await generate(null, { keyType: 'ec', curve: 'P-384', algorithm: 'sha384' });
  57. const cert = new crypto.X509Certificate(pems.cert);
  58. assert.ok(cert.publicKey, 'can generate EC cert with sha384');
  59. });
  60. it('should support EC with sha512 algorithm', async function () {
  61. var pems = await generate(null, { keyType: 'ec', curve: 'P-521', algorithm: 'sha512' });
  62. const cert = new crypto.X509Certificate(pems.cert);
  63. assert.ok(cert.publicKey, 'can generate EC cert with sha512');
  64. });
  65. it('should generate EC client certificate', async function () {
  66. var pems = await generate(null, { keyType: 'ec', clientCertificate: true });
  67. assert.ok(!!pems.clientcert, 'should include a client cert');
  68. assert.ok(!!pems.clientprivate, 'should include a client private key');
  69. assert.ok(!!pems.clientpublic, 'should include a client public key');
  70. const clientPrivateKey = crypto.createPrivateKey(pems.clientprivate);
  71. assert.strictEqual(clientPrivateKey.asymmetricKeyType, 'ec', 'client key should be EC');
  72. });
  73. it('should support passphrase with EC keys', async function () {
  74. const passphrase = 'ec-secret-passphrase';
  75. var pems = await generate(null, { keyType: 'ec', passphrase: passphrase });
  76. assert.include(pems.private, 'ENCRYPTED', 'EC private key should be encrypted');
  77. const privateKey = crypto.createPrivateKey({
  78. key: pems.private,
  79. passphrase: passphrase
  80. });
  81. assert.strictEqual(privateKey.asymmetricKeyType, 'ec', 'decrypted key should be EC');
  82. });
  83. it('should support using existing EC keyPair', async function () {
  84. const firstPems = await generate(null, { keyType: 'ec', curve: 'P-256' });
  85. const secondPems = await generate(null, {
  86. keyType: 'ec',
  87. curve: 'P-256',
  88. keyPair: {
  89. privateKey: firstPems.private,
  90. publicKey: firstPems.public
  91. }
  92. });
  93. assert.strictEqual(firstPems.private, secondPems.private, 'should use provided EC private key');
  94. assert.strictEqual(firstPems.public, secondPems.public, 'should use provided EC public key');
  95. });
  96. it('should support custom attributes with EC', async function () {
  97. const attrs = [
  98. { name: 'commonName', value: 'ec-test.example.com' },
  99. { name: 'countryName', value: 'US' },
  100. { name: 'organizationName', value: 'EC Test Corp' }
  101. ];
  102. var pems = await generate(attrs, { keyType: 'ec' });
  103. const cert = new crypto.X509Certificate(pems.cert);
  104. assert.include(cert.subject, 'CN=ec-test.example.com', 'should include custom CN');
  105. assert.include(cert.subject, 'O=EC Test Corp', 'should include custom organization');
  106. });
  107. });