You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.5 KiB

4 years ago
  1. var assert = require('assert');
  2. var forge = require('node-forge');
  3. var fs = require('fs');
  4. var exec = require('child_process').exec;
  5. describe('generate', function () {
  6. var generate = require('../index').generate;
  7. it('should work without attrs/options', function (done) {
  8. var pems = generate();
  9. assert.ok(!!pems.private, 'has a private key');
  10. assert.ok(!!pems.fingerprint, 'has fingerprint');
  11. assert.ok(!!pems.public, 'has a public key');
  12. assert.ok(!!pems.cert, 'has a certificate');
  13. assert.ok(!pems.pkcs7, 'should not include a pkcs7 by default');
  14. assert.ok(!pems.clientcert, 'should not include a client cert by default');
  15. assert.ok(!pems.clientprivate, 'should not include a client private key by default');
  16. assert.ok(!pems.clientpublic, 'should not include a client public key by default');
  17. var caStore = forge.pki.createCaStore();
  18. caStore.addCertificate(pems.cert);
  19. done();
  20. });
  21. it('should generate client cert', function (done) {
  22. var pems = generate(null, {clientCertificate: true});
  23. assert.ok(!!pems.clientcert, 'should include a client cert when requested');
  24. assert.ok(!!pems.clientprivate, 'should include a client private key when requested');
  25. assert.ok(!!pems.clientpublic, 'should include a client public key when requested');
  26. done();
  27. });
  28. it('should include pkcs7', function (done) {
  29. var pems = generate([{ name: 'commonName', value: 'contoso.com' }], {pkcs7: true});
  30. assert.ok(!!pems.pkcs7, 'has a pkcs7');
  31. try {
  32. fs.unlinkSync('/tmp/tmp.pkcs7');
  33. } catch (er) {}
  34. fs.writeFileSync('/tmp/tmp.pkcs7', pems.pkcs7);
  35. exec('openssl pkcs7 -print_certs -in /tmp/tmp.pkcs7', function (err, stdout, stderr) {
  36. if (err) {
  37. return done(err);
  38. }
  39. const errorMessage = stderr.toString();
  40. if (errorMessage.length) {
  41. return done(new Error(errorMessage));
  42. }
  43. const expected = stdout.toString().replace(/\n/g, '\r\n'); //node-forge uses \r\n
  44. assert.equal(
  45. `subject=/CN=contoso.com\r\nissuer=/CN=contoso.com\r\n` +
  46. pems.cert +
  47. '\r\n',
  48. expected
  49. );
  50. done();
  51. });
  52. });
  53. it('should support sha1 algorithm', function (done) {
  54. var pems_sha1 = generate(null, { algorithm: 'sha1' });
  55. assert.ok(forge.pki.certificateFromPem(pems_sha1.cert).siginfo.algorithmOid === forge.pki.oids['sha1WithRSAEncryption'], 'can generate sha1 certs');
  56. done();
  57. });
  58. it('should support sha256 algorithm', function (done) {
  59. var pems_sha256 = generate(null, { algorithm: 'sha256' });
  60. assert.ok(forge.pki.certificateFromPem(pems_sha256.cert).siginfo.algorithmOid === forge.pki.oids['sha256WithRSAEncryption'], 'can generate sha256 certs');
  61. done();
  62. });
  63. describe('with callback', function () {
  64. it('should work without attrs/options', function (done) {
  65. generate(function (err, pems) {
  66. if (err) done(err);
  67. assert.ok(!!pems.private, 'has a private key');
  68. assert.ok(!!pems.public, 'has a public key');
  69. assert.ok(!!pems.cert, 'has a certificate');
  70. assert.ok(!pems.pkcs7, 'should not include a pkcs7 by default');
  71. assert.ok(!pems.clientcert, 'should not include a client cert by default');
  72. assert.ok(!pems.clientprivate, 'should not include a client private key by default');
  73. assert.ok(!pems.clientpublic, 'should not include a client public key by default');
  74. done();
  75. });
  76. });
  77. it('should generate client cert', function (done) {
  78. generate(null, {clientCertificate: true}, function (err, pems) {
  79. if (err) done(err);
  80. assert.ok(!!pems.clientcert, 'should include a client cert when requested');
  81. assert.ok(!!pems.clientprivate, 'should include a client private key when requested');
  82. assert.ok(!!pems.clientpublic, 'should include a client public key when requested');
  83. done();
  84. });
  85. });
  86. it('should include pkcs7', function (done) {
  87. generate([{ name: 'commonName', value: 'contoso.com' }], {pkcs7: true}, function (err, pems) {
  88. if (err) done(err);
  89. assert.ok(!!pems.pkcs7, 'has a pkcs7');
  90. try {
  91. fs.unlinkSync('/tmp/tmp.pkcs7');
  92. } catch (er) {}
  93. fs.writeFileSync('/tmp/tmp.pkcs7', pems.pkcs7);
  94. exec('openssl pkcs7 -print_certs -in /tmp/tmp.pkcs7', function (err, stdout, stderr) {
  95. if (err) {
  96. return done(err);
  97. }
  98. const errorMessage = stderr.toString();
  99. if (errorMessage.length) {
  100. return done(new Error(errorMessage));
  101. }
  102. const expected = stdout.toString().replace(/\n/g, '\r\n'); //node-forge uses \r\n
  103. assert.equal(
  104. `subject=/CN=contoso.com\r\nissuer=/CN=contoso.com\r\n` +
  105. pems.cert +
  106. '\r\n',
  107. expected
  108. );
  109. done();
  110. });
  111. });
  112. });
  113. it('should support sha1 algorithm', function (done) {
  114. generate(null, { algorithm: 'sha1' }, function (err, pems_sha1) {
  115. if (err) done(err);
  116. assert.ok(forge.pki.certificateFromPem(pems_sha1.cert).siginfo.algorithmOid === forge.pki.oids['sha1WithRSAEncryption'], 'can generate sha1 certs');
  117. done();
  118. });
  119. });
  120. it('should support sha256 algorithm', function (done) {
  121. generate(null, { algorithm: 'sha256' }, function (err, pems_sha256) {
  122. if (err) done(err);
  123. assert.ok(forge.pki.certificateFromPem(pems_sha256.cert).siginfo.algorithmOid === forge.pki.oids['sha256WithRSAEncryption'], 'can generate sha256 certs');
  124. done();
  125. });
  126. });
  127. });
  128. });