parsePkcs12.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const parsePkcs12 = require('../../../lib/credentials/certificate/parsePkcs12');
  2. const APNKey = require('../../../lib/credentials/certificate/APNKey');
  3. const APNCertificate = require('../../../lib/credentials/certificate/APNCertificate');
  4. const fs = require('fs');
  5. describe('parsePkcs12', function () {
  6. describe('with PKCS#12 data', function () {
  7. let p12, properties;
  8. describe('return value', function () {
  9. let credentials;
  10. before(function () {
  11. p12 = fs.readFileSync('test/credentials/support/certIssuerKey.p12');
  12. credentials = parsePkcs12(p12);
  13. });
  14. it('is an object', function () {
  15. expect(credentials).to.be.an('object');
  16. });
  17. it('contains a private key', function () {
  18. expect(credentials).to.include.keys('key');
  19. });
  20. describe('private key', function () {
  21. it('is an instance of APNKey', function () {
  22. expect(credentials.key).to.be.an.instanceof(APNKey);
  23. });
  24. it('has the correct fingerprint', function () {
  25. expect(credentials.key.fingerprint()).to.equal(
  26. '2d594c9861227dd22ba5ae37cc9354e9117a804d'
  27. );
  28. });
  29. });
  30. it('contains a certificate chain', function () {
  31. expect(credentials).to.include.keys('certificates');
  32. });
  33. describe('certificate chain', function () {
  34. it('is an array', function () {
  35. expect(credentials.certificates).to.be.an('array');
  36. });
  37. it('contains the correct number of certificates', function () {
  38. expect(credentials.certificates.length).to.equal(2);
  39. });
  40. it('contains APNCertificate objects', function () {
  41. const certificates = credentials.certificates;
  42. certificates.forEach(function (certificate) {
  43. expect(certificate).to.be.an.instanceof(APNCertificate);
  44. });
  45. });
  46. it('contains certificates with the correct fingerprints', function () {
  47. const fingerprints = [
  48. '2d594c9861227dd22ba5ae37cc9354e9117a804d',
  49. 'ccff221d67cb3335649f9b4fbb311948af76f4b2',
  50. ];
  51. const certificates = credentials.certificates;
  52. certificates.forEach(function (certificate, index) {
  53. expect(certificate.key().fingerprint()).to.equal(fingerprints[index]);
  54. });
  55. });
  56. });
  57. });
  58. // OpenSSL exports keys having no passphrase as a C string with a \0 byte appended
  59. describe('having empty passphrase (OpenSSL-CLI-generated file)', function () {
  60. describe('return value', function () {
  61. it('has the correct key', function () {
  62. p12 = fs.readFileSync('test/credentials/support/certIssuerKeyOpenSSL.p12');
  63. properties = parsePkcs12(p12);
  64. expect(properties.key.fingerprint()).to.equal('2d594c9861227dd22ba5ae37cc9354e9117a804d');
  65. });
  66. });
  67. });
  68. describe('with correct passphrase', function () {
  69. describe('return value', function () {
  70. it('has the correct key', function () {
  71. p12 = fs.readFileSync('test/credentials/support/certIssuerKeyPassphrase.p12');
  72. properties = parsePkcs12(p12, 'apntest');
  73. expect(properties.key.fingerprint()).to.equal('2d594c9861227dd22ba5ae37cc9354e9117a804d');
  74. });
  75. });
  76. });
  77. describe('with incorrect passphrase', function () {
  78. it('throws', function () {
  79. p12 = fs.readFileSync('test/credentials/support/certIssuerKeyPassphrase.p12');
  80. expect(function () {
  81. parsePkcs12(p12, 'notthepassphrase');
  82. }).to.throw('unable to parse credentials, incorrect passphrase');
  83. });
  84. });
  85. // Unclear whether multiple keys in one PKCS#12 file can be distinguished
  86. // at present if there's more than one just throw a warning. Should also
  87. // do the same thing in apnKeyFromPem
  88. describe('multiple keys', function () {
  89. it('throws', function () {
  90. p12 = fs.readFileSync('test/credentials/support/multipleKeys.p12');
  91. expect(function () {
  92. parsePkcs12(p12);
  93. }).to.throw('multiple keys found in PFX/P12 file');
  94. });
  95. });
  96. });
  97. describe('PEM file', function () {
  98. it('throws', function () {
  99. const pem = fs.readFileSync('test/credentials/support/certKey.pem');
  100. expect(function () {
  101. parsePkcs12(pem);
  102. }).to.throw('unable to parse credentials, not a PFX/P12 file');
  103. });
  104. });
  105. it('returns undefined for undefined', function () {
  106. expect(parsePkcs12()).to.be.undefined;
  107. });
  108. });