pem.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. var msg = {
  100. type: match[1],
  101. procType: null,
  102. contentDomain: null,
  103. dekInfo: null,
  104. headers: [],
  105. body: forge.util.decode64(match[3])
  106. };
  107. rval.push(msg);
  108. // no headers
  109. if(!match[2]) {
  110. continue;
  111. }
  112. // parse headers
  113. var lines = match[2].split(rCRLF);
  114. var li = 0;
  115. while(match && li < lines.length) {
  116. // get line, trim any rhs whitespace
  117. var line = lines[li].replace(/\s+$/, '');
  118. // RFC2822 unfold any following folded lines
  119. for(var nl = li + 1; nl < lines.length; ++nl) {
  120. var next = lines[nl];
  121. if(!/\s/.test(next[0])) {
  122. break;
  123. }
  124. line += next;
  125. li = nl;
  126. }
  127. // parse header
  128. match = line.match(rHeader);
  129. if(match) {
  130. var header = {name: match[1], values: []};
  131. var values = match[2].split(',');
  132. for(var vi = 0; vi < values.length; ++vi) {
  133. header.values.push(ltrim(values[vi]));
  134. }
  135. // Proc-Type must be the first header
  136. if(!msg.procType) {
  137. if(header.name !== 'Proc-Type') {
  138. throw new Error('Invalid PEM formatted message. The first ' +
  139. 'encapsulated header must be "Proc-Type".');
  140. } else if(header.values.length !== 2) {
  141. throw new Error('Invalid PEM formatted message. The "Proc-Type" ' +
  142. 'header must have two subfields.');
  143. }
  144. msg.procType = {version: values[0], type: values[1]};
  145. } else if(!msg.contentDomain && header.name === 'Content-Domain') {
  146. // special-case Content-Domain
  147. msg.contentDomain = values[0] || '';
  148. } else if(!msg.dekInfo && header.name === 'DEK-Info') {
  149. // special-case DEK-Info
  150. if(header.values.length === 0) {
  151. throw new Error('Invalid PEM formatted message. The "DEK-Info" ' +
  152. 'header must have at least one subfield.');
  153. }
  154. msg.dekInfo = {algorithm: values[0], parameters: values[1] || null};
  155. } else {
  156. msg.headers.push(header);
  157. }
  158. }
  159. ++li;
  160. }
  161. if(msg.procType === 'ENCRYPTED' && !msg.dekInfo) {
  162. throw new Error('Invalid PEM formatted message. The "DEK-Info" ' +
  163. 'header must be present if "Proc-Type" is "ENCRYPTED".');
  164. }
  165. }
  166. if(rval.length === 0) {
  167. throw new Error('Invalid PEM formatted message.');
  168. }
  169. return rval;
  170. };
  171. function foldHeader(header) {
  172. var rval = header.name + ': ';
  173. // ensure values with CRLF are folded
  174. var values = [];
  175. var insertSpace = function(match, $1) {
  176. return ' ' + $1;
  177. };
  178. for(var i = 0; i < header.values.length; ++i) {
  179. values.push(header.values[i].replace(/^(\S+\r\n)/, insertSpace));
  180. }
  181. rval += values.join(',') + '\r\n';
  182. // do folding
  183. var length = 0;
  184. var candidate = -1;
  185. for(var i = 0; i < rval.length; ++i, ++length) {
  186. if(length > 65 && candidate !== -1) {
  187. var insert = rval[candidate];
  188. if(insert === ',') {
  189. ++candidate;
  190. rval = rval.substr(0, candidate) + '\r\n ' + rval.substr(candidate);
  191. } else {
  192. rval = rval.substr(0, candidate) +
  193. '\r\n' + insert + rval.substr(candidate + 1);
  194. }
  195. length = (i - candidate - 1);
  196. candidate = -1;
  197. ++i;
  198. } else if(rval[i] === ' ' || rval[i] === '\t' || rval[i] === ',') {
  199. candidate = i;
  200. }
  201. }
  202. return rval;
  203. }
  204. function ltrim(str) {
  205. return str.replace(/^\s+/, '');
  206. }