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.

102 lines
2.6 KiB

4 years ago
  1. /**
  2. * Javascript implementation of a basic Public Key Infrastructure, including
  3. * support for RSA public and private keys.
  4. *
  5. * @author Dave Longley
  6. *
  7. * Copyright (c) 2010-2013 Digital Bazaar, Inc.
  8. */
  9. var forge = require('./forge');
  10. require('./asn1');
  11. require('./oids');
  12. require('./pbe');
  13. require('./pem');
  14. require('./pbkdf2');
  15. require('./pkcs12');
  16. require('./pss');
  17. require('./rsa');
  18. require('./util');
  19. require('./x509');
  20. // shortcut for asn.1 API
  21. var asn1 = forge.asn1;
  22. /* Public Key Infrastructure (PKI) implementation. */
  23. var pki = module.exports = forge.pki = forge.pki || {};
  24. /**
  25. * NOTE: THIS METHOD IS DEPRECATED. Use pem.decode() instead.
  26. *
  27. * Converts PEM-formatted data to DER.
  28. *
  29. * @param pem the PEM-formatted data.
  30. *
  31. * @return the DER-formatted data.
  32. */
  33. pki.pemToDer = function(pem) {
  34. var msg = forge.pem.decode(pem)[0];
  35. if(msg.procType && msg.procType.type === 'ENCRYPTED') {
  36. throw new Error('Could not convert PEM to DER; PEM is encrypted.');
  37. }
  38. return forge.util.createBuffer(msg.body);
  39. };
  40. /**
  41. * Converts an RSA private key from PEM format.
  42. *
  43. * @param pem the PEM-formatted private key.
  44. *
  45. * @return the private key.
  46. */
  47. pki.privateKeyFromPem = function(pem) {
  48. var msg = forge.pem.decode(pem)[0];
  49. if(msg.type !== 'PRIVATE KEY' && msg.type !== 'RSA PRIVATE KEY') {
  50. var error = new Error('Could not convert private key from PEM; PEM ' +
  51. 'header type is not "PRIVATE KEY" or "RSA PRIVATE KEY".');
  52. error.headerType = msg.type;
  53. throw error;
  54. }
  55. if(msg.procType && msg.procType.type === 'ENCRYPTED') {
  56. throw new Error('Could not convert private key from PEM; PEM is encrypted.');
  57. }
  58. // convert DER to ASN.1 object
  59. var obj = asn1.fromDer(msg.body);
  60. return pki.privateKeyFromAsn1(obj);
  61. };
  62. /**
  63. * Converts an RSA private key to PEM format.
  64. *
  65. * @param key the private key.
  66. * @param maxline the maximum characters per line, defaults to 64.
  67. *
  68. * @return the PEM-formatted private key.
  69. */
  70. pki.privateKeyToPem = function(key, maxline) {
  71. // convert to ASN.1, then DER, then PEM-encode
  72. var msg = {
  73. type: 'RSA PRIVATE KEY',
  74. body: asn1.toDer(pki.privateKeyToAsn1(key)).getBytes()
  75. };
  76. return forge.pem.encode(msg, {maxline: maxline});
  77. };
  78. /**
  79. * Converts a PrivateKeyInfo to PEM format.
  80. *
  81. * @param pki the PrivateKeyInfo.
  82. * @param maxline the maximum characters per line, defaults to 64.
  83. *
  84. * @return the PEM-formatted private key.
  85. */
  86. pki.privateKeyInfoToPem = function(pki, maxline) {
  87. // convert to DER, then PEM-encode
  88. var msg = {
  89. type: 'PRIVATE KEY',
  90. body: asn1.toDer(pki).getBytes()
  91. };
  92. return forge.pem.encode(msg, {maxline: maxline});
  93. };