tests.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var { assert } = require('chai');
  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();
  44. let [ subjectLine,issuerLine, ...cert ] = expected.split(/\r?\n/).filter(c => c);
  45. cert = cert.filter(c => c);
  46. assert.match(subjectLine, /subject=\/?CN\s?=\s?contoso.com/i);
  47. assert.match(issuerLine, /issuer=\/?CN\s?=\s?contoso.com/i);
  48. assert.strictEqual(
  49. pems.cert,
  50. cert.join('\r\n') + '\r\n'
  51. );
  52. done();
  53. });
  54. });
  55. it('should support sha1 algorithm', function (done) {
  56. var pems_sha1 = generate(null, { algorithm: 'sha1' });
  57. assert.ok(forge.pki.certificateFromPem(pems_sha1.cert).siginfo.algorithmOid === forge.pki.oids['sha1WithRSAEncryption'], 'can generate sha1 certs');
  58. done();
  59. });
  60. it('should support sha256 algorithm', function (done) {
  61. var pems_sha256 = generate(null, { algorithm: 'sha256' });
  62. assert.ok(forge.pki.certificateFromPem(pems_sha256.cert).siginfo.algorithmOid === forge.pki.oids['sha256WithRSAEncryption'], 'can generate sha256 certs');
  63. done();
  64. });
  65. describe('with callback', function () {
  66. it('should work without attrs/options', function (done) {
  67. generate(function (err, pems) {
  68. if (err) done(err);
  69. assert.ok(!!pems.private, 'has a private key');
  70. assert.ok(!!pems.public, 'has a public key');
  71. assert.ok(!!pems.cert, 'has a certificate');
  72. assert.ok(!pems.pkcs7, 'should not include a pkcs7 by default');
  73. assert.ok(!pems.clientcert, 'should not include a client cert by default');
  74. assert.ok(!pems.clientprivate, 'should not include a client private key by default');
  75. assert.ok(!pems.clientpublic, 'should not include a client public key by default');
  76. done();
  77. });
  78. });
  79. it('should generate client cert', function (done) {
  80. generate(null, {clientCertificate: true}, function (err, pems) {
  81. if (err) done(err);
  82. assert.ok(!!pems.clientcert, 'should include a client cert when requested');
  83. assert.ok(!!pems.clientprivate, 'should include a client private key when requested');
  84. assert.ok(!!pems.clientpublic, 'should include a client public key when requested');
  85. done();
  86. });
  87. });
  88. it('should include pkcs7', function (done) {
  89. generate([{ name: 'commonName', value: 'contoso.com' }], {pkcs7: true}, function (err, pems) {
  90. if (err) done(err);
  91. assert.ok(!!pems.pkcs7, 'has a pkcs7');
  92. try {
  93. fs.unlinkSync('/tmp/tmp.pkcs7');
  94. } catch (er) {}
  95. fs.writeFileSync('/tmp/tmp.pkcs7', pems.pkcs7);
  96. exec('openssl pkcs7 -print_certs -in /tmp/tmp.pkcs7', function (err, stdout, stderr) {
  97. if (err) {
  98. return done(err);
  99. }
  100. const errorMessage = stderr.toString();
  101. if (errorMessage.length) {
  102. return done(new Error(errorMessage));
  103. }
  104. const expected = stdout.toString();
  105. let [ subjectLine,issuerLine, ...cert ] = expected.split(/\r?\n/).filter(c => c);
  106. assert.match(subjectLine, /subject=\/?CN\s?=\s?contoso.com/i);
  107. assert.match(issuerLine, /issuer=\/?CN\s?=\s?contoso.com/i);
  108. assert.strictEqual(
  109. pems.cert,
  110. cert.join('\r\n') + '\r\n'
  111. );
  112. done();
  113. });
  114. });
  115. });
  116. it('should support sha1 algorithm', function (done) {
  117. generate(null, { algorithm: 'sha1' }, function (err, pems_sha1) {
  118. if (err) done(err);
  119. assert.ok(forge.pki.certificateFromPem(pems_sha1.cert).siginfo.algorithmOid === forge.pki.oids['sha1WithRSAEncryption'], 'can generate sha1 certs');
  120. done();
  121. });
  122. });
  123. it('should support sha256 algorithm', function (done) {
  124. generate(null, { algorithm: 'sha256' }, function (err, pems_sha256) {
  125. if (err) done(err);
  126. assert.ok(forge.pki.certificateFromPem(pems_sha256.cert).siginfo.algorithmOid === forge.pki.oids['sha256WithRSAEncryption'], 'can generate sha256 certs');
  127. done();
  128. });
  129. });
  130. });
  131. });