pem.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * Javascript implementation of basic PEM (Privacy Enhanced Mail) algorithms.
  3. *
  4. * See: RFC 1421.
  5. *
  6. * @author Dave Longley
  7. *
  8. * Copyright (c) 2013-2014 Digital Bazaar, Inc.
  9. *
  10. * A Forge PEM object has the following fields:
  11. *
  12. * type: identifies the type of message (eg: "RSA PRIVATE KEY").
  13. *
  14. * procType: identifies the type of processing performed on the message,
  15. * it has two subfields: version and type, eg: 4,ENCRYPTED.
  16. *
  17. * contentDomain: identifies the type of content in the message, typically
  18. * only uses the value: "RFC822".
  19. *
  20. * dekInfo: identifies the message encryption algorithm and mode and includes
  21. * any parameters for the algorithm, it has two subfields: algorithm and
  22. * parameters, eg: DES-CBC,F8143EDE5960C597.
  23. *
  24. * headers: contains all other PEM encapsulated headers -- where order is
  25. * significant (for pairing data like recipient ID + key info).
  26. *
  27. * body: the binary-encoded body.
  28. */
  29. var forge = require('./forge');
  30. require('./util');
  31. // shortcut for pem API
  32. var pem = module.exports = forge.pem = forge.pem || {};
  33. /**
  34. * Encodes (serializes) the given PEM object.
  35. *
  36. * @param msg the PEM message object to encode.
  37. * @param options the options to use:
  38. * maxline the maximum characters per line for the body, (default: 64).
  39. *
  40. * @return the PEM-formatted string.
  41. */
  42. pem.encode = function(msg, options) {
  43. options = options || {};
  44. var rval = '-----BEGIN ' + msg.type + '-----\r\n';
  45. // encode special headers
  46. var header;
  47. if(msg.procType) {
  48. header = {
  49. name: 'Proc-Type',
  50. values: [String(msg.procType.version), msg.procType.type]
  51. };
  52. rval += foldHeader(header);
  53. }
  54. if(msg.contentDomain) {
  55. header = {name: 'Content-Domain', values: [msg.contentDomain]};
  56. rval += foldHeader(header);
  57. }
  58. if(msg.dekInfo) {
  59. header = {name: 'DEK-Info', values: [msg.dekInfo.algorithm]};
  60. if(msg.dekInfo.parameters) {
  61. header.values.push(msg.dekInfo.parameters);
  62. }
  63. rval += foldHeader(header);
  64. }
  65. if(msg.headers) {
  66. // encode all other headers
  67. for(var i = 0; i < msg.headers.length; ++i) {
  68. rval += foldHeader(msg.headers[i]);
  69. }
  70. }
  71. // terminate header
  72. if(msg.procType) {
  73. rval += '\r\n';
  74. }
  75. // add body
  76. rval += forge.util.encode64(msg.body, options.maxline || 64) + '\r\n';
  77. rval += '-----END ' + msg.type + '-----\r\n';
  78. return rval;
  79. };
  80. /**
  81. * Decodes (deserializes) all PEM messages found in the given string.
  82. *
  83. * @param str the PEM-formatted string to decode.
  84. *
  85. * @return the PEM message objects in an array.
  86. */
  87. pem.decode = function(str) {
  88. var rval = [];
  89. // split string into PEM messages (be lenient w/EOF on BEGIN line)
  90. var rMessage = /\s*-----BEGIN ([A-Z0-9- ]+)-----\r?\n?([\x21-\x7e\s]+?(?:\r?\n\r?\n))?([:A-Za-z0-9+\/=\s]+?)-----END \1-----/g;
  91. var rHeader = /([\x21-\x7e]+):\s*([\x21-\x7e\s^:]+)/;
  92. var rCRLF = /\r?\n/;
  93. var match;
  94. while(true) {
  95. match = rMessage.exec(str);
  96. if(!match) {
  97. break;
  98. }
  99. // accept "NEW CERTIFICATE REQUEST" as "CERTIFICATE REQUEST"
  100. // https://datatracker.ietf.org/doc/html/rfc7468#section-7
  101. var type = match[1];
  102. if(type === 'NEW CERTIFICATE REQUEST') {
  103. type = 'CERTIFICATE REQUEST';
  104. }
  105. var msg = {
  106. type: type,
  107. procType: null,
  108. contentDomain: null,
  109. dekInfo: null,
  110. headers: [],
  111. body: forge.util.decode64(match[3])
  112. };
  113. rval.push(msg);
  114. // no headers
  115. if(!match[2]) {
  116. continue;
  117. }
  118. // parse headers
  119. var lines = match[2].split(rCRLF);
  120. var li = 0;
  121. while(match && li < lines.length) {
  122. // get line, trim any rhs whitespace
  123. var line = lines[li].replace(/\s+$/, '');
  124. // RFC2822 unfold any following folded lines
  125. for(var nl = li + 1; nl < lines.length; ++nl) {
  126. var next = lines[nl];
  127. if(!/\s/.test(next[0])) {
  128. break;
  129. }
  130. line += next;
  131. li = nl;
  132. }
  133. // parse header
  134. match = line.match(rHeader);
  135. if(match) {
  136. var header = {name: match[1], values: []};
  137. var values = match[2].split(',');
  138. for(var vi = 0; vi < values.length; ++vi) {
  139. header.values.push(ltrim(values[vi]));
  140. }
  141. // Proc-Type must be the first header
  142. if(!msg.procType) {
  143. if(header.name !== 'Proc-Type') {
  144. throw new Error('Invalid PEM formatted message. The first ' +
  145. 'encapsulated header must be "Proc-Type".');
  146. } else if(header.values.length !== 2) {
  147. throw new Error('Invalid PEM formatted message. The "Proc-Type" ' +
  148. 'header must have two subfields.');
  149. }
  150. msg.procType = {version: values[0], type: values[1]};
  151. } else if(!msg.contentDomain && header.name === 'Content-Domain') {
  152. // special-case Content-Domain
  153. msg.contentDomain = values[0] || '';
  154. } else if(!msg.dekInfo && header.name === 'DEK-Info') {
  155. // special-case DEK-Info
  156. if(header.values.length === 0) {
  157. throw new Error('Invalid PEM formatted message. The "DEK-Info" ' +
  158. 'header must have at least one subfield.');
  159. }
  160. msg.dekInfo = {algorithm: values[0], parameters: values[1] || null};
  161. } else {
  162. msg.headers.push(header);
  163. }
  164. }
  165. ++li;
  166. }
  167. if(msg.procType === 'ENCRYPTED' && !msg.dekInfo) {
  168. throw new Error('Invalid PEM formatted message. The "DEK-Info" ' +
  169. 'header must be present if "Proc-Type" is "ENCRYPTED".');
  170. }
  171. }
  172. if(rval.length === 0) {
  173. throw new Error('Invalid PEM formatted message.');
  174. }
  175. return rval;
  176. };
  177. function foldHeader(header) {
  178. var rval = header.name + ': ';
  179. // ensure values with CRLF are folded
  180. var values = [];
  181. var insertSpace = function(match, $1) {
  182. return ' ' + $1;
  183. };
  184. for(var i = 0; i < header.values.length; ++i) {
  185. values.push(header.values[i].replace(/^(\S+\r\n)/, insertSpace));
  186. }
  187. rval += values.join(',') + '\r\n';
  188. // do folding
  189. var length = 0;
  190. var candidate = -1;
  191. for(var i = 0; i < rval.length; ++i, ++length) {
  192. if(length > 65 && candidate !== -1) {
  193. var insert = rval[candidate];
  194. if(insert === ',') {
  195. ++candidate;
  196. rval = rval.substr(0, candidate) + '\r\n ' + rval.substr(candidate);
  197. } else {
  198. rval = rval.substr(0, candidate) +
  199. '\r\n' + insert + rval.substr(candidate + 1);
  200. }
  201. length = (i - candidate - 1);
  202. candidate = -1;
  203. ++i;
  204. } else if(rval[i] === ' ' || rval[i] === '\t' || rval[i] === ',') {
  205. candidate = i;
  206. }
  207. }
  208. return rval;
  209. }
  210. function ltrim(str) {
  211. return str.replace(/^\s+/, '');
  212. }