parsePemKey.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const parsePemKey = require('../../../lib/credentials/certificate/parsePemKey');
  2. const APNKey = require('../../../lib/credentials/certificate/APNKey');
  3. const fs = require('fs');
  4. describe('parsePemKey', function () {
  5. describe('returns APNKey', function () {
  6. describe('RSA key', function () {
  7. let key;
  8. beforeEach(function () {
  9. const keyData = fs.readFileSync('test/credentials/support/key.pem');
  10. key = parsePemKey(keyData);
  11. });
  12. it('correct type', function () {
  13. expect(key).to.be.an.instanceof(APNKey);
  14. });
  15. it('with correct fingerprint', function () {
  16. expect(key.fingerprint()).to.equal('2d594c9861227dd22ba5ae37cc9354e9117a804d');
  17. });
  18. });
  19. it('openssl-encrypted RSA key, correct password', function () {
  20. const key = fs.readFileSync('test/credentials/support/keyEncrypted.pem');
  21. expect(parsePemKey(key, 'apntest')).to.be.an.instanceof(APNKey);
  22. });
  23. it('PKCS#8 encrypted key, correct password', function () {
  24. const key = fs.readFileSync('test/credentials/support/keyPKCS8Encrypted.pem');
  25. expect(parsePemKey(key, 'apntest')).to.be.an.instanceof(APNKey);
  26. });
  27. it('PEM containing certificates and key', function () {
  28. const certAndKey = fs.readFileSync('test/credentials/support/certKey.pem');
  29. expect(parsePemKey(certAndKey)).to.be.an.instanceof(APNKey);
  30. });
  31. });
  32. describe('throws with', function () {
  33. it('PKCS#8 key (unsupported format)', function () {
  34. const key = fs.readFileSync('test/credentials/support/keyPKCS8.pem');
  35. expect(function () {
  36. parsePemKey(key);
  37. }).to.throw('unable to parse key, unsupported format');
  38. });
  39. it('RSA encrypted key, incorrect passphrase', function () {
  40. const key = fs.readFileSync('test/credentials/support/keyEncrypted.pem');
  41. expect(function () {
  42. parsePemKey(key, 'not-the-passphrase');
  43. }).to.throw('unable to parse key, incorrect passphrase');
  44. });
  45. it('PKCS#8 encrypted key, incorrect passphrase', function () {
  46. const key = fs.readFileSync('test/credentials/support/keyPKCS8Encrypted.pem');
  47. expect(function () {
  48. parsePemKey(key, 'not-the-passphrase');
  49. }).to.throw('unable to parse key, incorrect passphrase');
  50. });
  51. it('PEM certificate', function () {
  52. const cert = fs.readFileSync('test/credentials/support/cert.pem');
  53. expect(function () {
  54. parsePemKey(cert);
  55. }).to.throw('unable to parse key, no private key found');
  56. });
  57. it('PKCS#12 file', function () {
  58. const pkcs12 = fs.readFileSync('test/credentials/support/certIssuerKey.p12');
  59. expect(function () {
  60. parsePemKey(pkcs12);
  61. }).to.throw('unable to parse key, not a valid PEM file');
  62. });
  63. });
  64. describe('multiple keys', function () {
  65. it('throws', function () {
  66. const keys = fs.readFileSync('test/credentials/support/multipleKeys.pem');
  67. expect(function () {
  68. parsePemKey(keys);
  69. }).to.throw('multiple keys found in PEM file');
  70. });
  71. });
  72. describe('returns null', function () {
  73. it('for null', function () {
  74. expect(parsePemKey()).to.be.null;
  75. });
  76. it('for undefined', function () {
  77. expect(parsePemKey()).to.be.null;
  78. });
  79. });
  80. });