[6a3a178] | 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 |
|
---|
| 32 | // shortcut for pem API
|
---|
| 33 | var pem = module.exports = forge.pem = forge.pem || {};
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * Encodes (serializes) the given PEM object.
|
---|
| 37 | *
|
---|
| 38 | * @param msg the PEM message object to encode.
|
---|
| 39 | * @param options the options to use:
|
---|
| 40 | * maxline the maximum characters per line for the body, (default: 64).
|
---|
| 41 | *
|
---|
| 42 | * @return the PEM-formatted string.
|
---|
| 43 | */
|
---|
| 44 | pem.encode = function(msg, options) {
|
---|
| 45 | options = options || {};
|
---|
| 46 | var rval = '-----BEGIN ' + msg.type + '-----\r\n';
|
---|
| 47 |
|
---|
| 48 | // encode special headers
|
---|
| 49 | var header;
|
---|
| 50 | if(msg.procType) {
|
---|
| 51 | header = {
|
---|
| 52 | name: 'Proc-Type',
|
---|
| 53 | values: [String(msg.procType.version), msg.procType.type]
|
---|
| 54 | };
|
---|
| 55 | rval += foldHeader(header);
|
---|
| 56 | }
|
---|
| 57 | if(msg.contentDomain) {
|
---|
| 58 | header = {name: 'Content-Domain', values: [msg.contentDomain]};
|
---|
| 59 | rval += foldHeader(header);
|
---|
| 60 | }
|
---|
| 61 | if(msg.dekInfo) {
|
---|
| 62 | header = {name: 'DEK-Info', values: [msg.dekInfo.algorithm]};
|
---|
| 63 | if(msg.dekInfo.parameters) {
|
---|
| 64 | header.values.push(msg.dekInfo.parameters);
|
---|
| 65 | }
|
---|
| 66 | rval += foldHeader(header);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | if(msg.headers) {
|
---|
| 70 | // encode all other headers
|
---|
| 71 | for(var i = 0; i < msg.headers.length; ++i) {
|
---|
| 72 | rval += foldHeader(msg.headers[i]);
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | // terminate header
|
---|
| 77 | if(msg.procType) {
|
---|
| 78 | rval += '\r\n';
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | // add body
|
---|
| 82 | rval += forge.util.encode64(msg.body, options.maxline || 64) + '\r\n';
|
---|
| 83 |
|
---|
| 84 | rval += '-----END ' + msg.type + '-----\r\n';
|
---|
| 85 | return rval;
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Decodes (deserializes) all PEM messages found in the given string.
|
---|
| 90 | *
|
---|
| 91 | * @param str the PEM-formatted string to decode.
|
---|
| 92 | *
|
---|
| 93 | * @return the PEM message objects in an array.
|
---|
| 94 | */
|
---|
| 95 | pem.decode = function(str) {
|
---|
| 96 | var rval = [];
|
---|
| 97 |
|
---|
| 98 | // split string into PEM messages (be lenient w/EOF on BEGIN line)
|
---|
| 99 | var rMessage = /\s*-----BEGIN ([A-Z0-9- ]+)-----\r?\n?([\x21-\x7e\s]+?(?:\r?\n\r?\n))?([:A-Za-z0-9+\/=\s]+?)-----END \1-----/g;
|
---|
| 100 | var rHeader = /([\x21-\x7e]+):\s*([\x21-\x7e\s^:]+)/;
|
---|
| 101 | var rCRLF = /\r?\n/;
|
---|
| 102 | var match;
|
---|
| 103 | while(true) {
|
---|
| 104 | match = rMessage.exec(str);
|
---|
| 105 | if(!match) {
|
---|
| 106 | break;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | var msg = {
|
---|
| 110 | type: match[1],
|
---|
| 111 | procType: null,
|
---|
| 112 | contentDomain: null,
|
---|
| 113 | dekInfo: null,
|
---|
| 114 | headers: [],
|
---|
| 115 | body: forge.util.decode64(match[3])
|
---|
| 116 | };
|
---|
| 117 | rval.push(msg);
|
---|
| 118 |
|
---|
| 119 | // no headers
|
---|
| 120 | if(!match[2]) {
|
---|
| 121 | continue;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | // parse headers
|
---|
| 125 | var lines = match[2].split(rCRLF);
|
---|
| 126 | var li = 0;
|
---|
| 127 | while(match && li < lines.length) {
|
---|
| 128 | // get line, trim any rhs whitespace
|
---|
| 129 | var line = lines[li].replace(/\s+$/, '');
|
---|
| 130 |
|
---|
| 131 | // RFC2822 unfold any following folded lines
|
---|
| 132 | for(var nl = li + 1; nl < lines.length; ++nl) {
|
---|
| 133 | var next = lines[nl];
|
---|
| 134 | if(!/\s/.test(next[0])) {
|
---|
| 135 | break;
|
---|
| 136 | }
|
---|
| 137 | line += next;
|
---|
| 138 | li = nl;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | // parse header
|
---|
| 142 | match = line.match(rHeader);
|
---|
| 143 | if(match) {
|
---|
| 144 | var header = {name: match[1], values: []};
|
---|
| 145 | var values = match[2].split(',');
|
---|
| 146 | for(var vi = 0; vi < values.length; ++vi) {
|
---|
| 147 | header.values.push(ltrim(values[vi]));
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | // Proc-Type must be the first header
|
---|
| 151 | if(!msg.procType) {
|
---|
| 152 | if(header.name !== 'Proc-Type') {
|
---|
| 153 | throw new Error('Invalid PEM formatted message. The first ' +
|
---|
| 154 | 'encapsulated header must be "Proc-Type".');
|
---|
| 155 | } else if(header.values.length !== 2) {
|
---|
| 156 | throw new Error('Invalid PEM formatted message. The "Proc-Type" ' +
|
---|
| 157 | 'header must have two subfields.');
|
---|
| 158 | }
|
---|
| 159 | msg.procType = {version: values[0], type: values[1]};
|
---|
| 160 | } else if(!msg.contentDomain && header.name === 'Content-Domain') {
|
---|
| 161 | // special-case Content-Domain
|
---|
| 162 | msg.contentDomain = values[0] || '';
|
---|
| 163 | } else if(!msg.dekInfo && header.name === 'DEK-Info') {
|
---|
| 164 | // special-case DEK-Info
|
---|
| 165 | if(header.values.length === 0) {
|
---|
| 166 | throw new Error('Invalid PEM formatted message. The "DEK-Info" ' +
|
---|
| 167 | 'header must have at least one subfield.');
|
---|
| 168 | }
|
---|
| 169 | msg.dekInfo = {algorithm: values[0], parameters: values[1] || null};
|
---|
| 170 | } else {
|
---|
| 171 | msg.headers.push(header);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | ++li;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | if(msg.procType === 'ENCRYPTED' && !msg.dekInfo) {
|
---|
| 179 | throw new Error('Invalid PEM formatted message. The "DEK-Info" ' +
|
---|
| 180 | 'header must be present if "Proc-Type" is "ENCRYPTED".');
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | if(rval.length === 0) {
|
---|
| 185 | throw new Error('Invalid PEM formatted message.');
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | return rval;
|
---|
| 189 | };
|
---|
| 190 |
|
---|
| 191 | function foldHeader(header) {
|
---|
| 192 | var rval = header.name + ': ';
|
---|
| 193 |
|
---|
| 194 | // ensure values with CRLF are folded
|
---|
| 195 | var values = [];
|
---|
| 196 | var insertSpace = function(match, $1) {
|
---|
| 197 | return ' ' + $1;
|
---|
| 198 | };
|
---|
| 199 | for(var i = 0; i < header.values.length; ++i) {
|
---|
| 200 | values.push(header.values[i].replace(/^(\S+\r\n)/, insertSpace));
|
---|
| 201 | }
|
---|
| 202 | rval += values.join(',') + '\r\n';
|
---|
| 203 |
|
---|
| 204 | // do folding
|
---|
| 205 | var length = 0;
|
---|
| 206 | var candidate = -1;
|
---|
| 207 | for(var i = 0; i < rval.length; ++i, ++length) {
|
---|
| 208 | if(length > 65 && candidate !== -1) {
|
---|
| 209 | var insert = rval[candidate];
|
---|
| 210 | if(insert === ',') {
|
---|
| 211 | ++candidate;
|
---|
| 212 | rval = rval.substr(0, candidate) + '\r\n ' + rval.substr(candidate);
|
---|
| 213 | } else {
|
---|
| 214 | rval = rval.substr(0, candidate) +
|
---|
| 215 | '\r\n' + insert + rval.substr(candidate + 1);
|
---|
| 216 | }
|
---|
| 217 | length = (i - candidate - 1);
|
---|
| 218 | candidate = -1;
|
---|
| 219 | ++i;
|
---|
| 220 | } else if(rval[i] === ' ' || rval[i] === '\t' || rval[i] === ',') {
|
---|
| 221 | candidate = i;
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | return rval;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | function ltrim(str) {
|
---|
| 229 | return str.replace(/^\s+/, '');
|
---|
| 230 | }
|
---|