aboutsummaryrefslogtreecommitdiffstats
path: root/package/cryptoAPI/pgpjs-crypto-main.jsm
blob: a391da2519420e0beca8e74e5db8cf0f41282d32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

"use strict";

const Services = ChromeUtils.import("resource://gre/modules/Services.jsm").Services;
const EnigmailLog = ChromeUtils.import("chrome://enigmail/content/modules/log.jsm").EnigmailLog;
const EnigmailPrefs = ChromeUtils.import("chrome://enigmail/content/modules/prefs.jsm").EnigmailPrefs;
const EnigmailFiles = ChromeUtils.import("chrome://enigmail/content/modules/files.jsm").EnigmailFiles;
const EnigmailArmor = ChromeUtils.import("chrome://enigmail/content/modules/armor.jsm").EnigmailArmor;
const EnigmailLocale = ChromeUtils.import("chrome://enigmail/content/modules/locale.jsm").EnigmailLocale;
const EnigmailTime = ChromeUtils.import("chrome://enigmail/content/modules/time.jsm").EnigmailTime;
const EnigmailConstants = ChromeUtils.import("chrome://enigmail/content/modules/constants.jsm").EnigmailConstants;
const pgpjs_keys = ChromeUtils.import("chrome://enigmail/content/modules/cryptoAPI/pgpjs-keys.jsm").pgpjs_keys;
const pgpjs_keyStore = ChromeUtils.import("chrome://enigmail/content/modules/cryptoAPI/pgpjs-keystore.jsm").pgpjs_keyStore;
const getOpenPGPLibrary = ChromeUtils.import("chrome://enigmail/content/modules/stdlib/openpgp-loader.jsm").getOpenPGPLibrary;

var EXPORTED_SYMBOLS = ["pgpjs_crypto"];

var pgpjs_crypto = {
  /**
   * Process an OpenPGP message
   *
   * @param {String} encrypted     The encrypted data
   * @param {Object} options       Decryption options
   *
   * @return {Promise<Object>} - Return object with decryptedData and status information:
   *     - {String} decryptedData
   *     - {Number} exitCode
   *     - {Number} statusFlags
   *     - {String} errorMsg
   *     - {String} blockSeparation
   *
   * Use Promise.catch to handle failed decryption.
   * retObj.errorMsg will be an error message in this case.
   */

  processPgpMessage: async function(encrypted, options) {
    EnigmailLog.DEBUG(`pgpjs-crypro-main.jsm: processPgpMessage(${encrypted.length})\n`);

    let result;
    try {
      result = await PgpJsWorkerParent.sendMessage("processPgpMessage", {
        encrypted,
        options
      });
    }
    catch (ex) {
      EnigmailLog.DEBUG(`pgpjs-decrypt.jsm: processPgpMessage: ERROR: ${ex.toString()}\n`);
      result.errorMsg = ex.toString();
      result.exitCode = 1;
    }

    return result;
  },

  verify: async function(data, options) {
    EnigmailLog.DEBUG(`pgpjs-decrypt.jsm: verify(${data.length})\n`);

    let result = {};

    let blocks = EnigmailArmor.locateArmoredBlocks(data);

    result.statusFlags = 0;
    result.exitCode = 1;

    try {
      if (blocks && blocks.length > 0) {
        if (blocks[0].blocktype === "SIGNED MESSAGE") {
          result = await PgpJsWorkerParent.sendMessage("verify", {
            data: data.substring(blocks[0].begin, blocks[0].end),
            options
          });
        }
      }
    }
    catch (ex) {
      EnigmailLog.DEBUG(`pgpjs-decrypt.jsm: verify: ERROR: ${ex.toString()}\n`);
      result.errorMsg = ex.toString();
      result.statusFlags = EnigmailConstants.UNVERIFIED_SIGNATURE;
    }
    return result;
  },

  verifyDetached: async function(data, signature, returnData = false) {
    EnigmailLog.DEBUG(`pgpjs-decrypt.jsm: verifyDetached(${data.length})\n`);

    let result = await PgpJsWorkerParent.sendMessage("verifyDetached", {
      data,
      signature,
      returnData
    });
    return result;
  },


  verifyFile: async function(dataFilePath, signatureFilePath) {
    const PgpJS = getOpenPGPLibrary();

    let dataFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
    let sigFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
    EnigmailFiles.initPath(dataFile, dataFilePath);
    EnigmailFiles.initPath(sigFile, signatureFilePath);

    if (!dataFile.exists()) {
      throw new Error(`Data file ${dataFilePath} does not exist`);
    }
    if (!sigFile.exists()) {
      throw new Error(`Signature file ${signatureFilePath} does not exist`);
    }

    let data = EnigmailFiles.readBinaryFile(dataFile);
    let sig = EnigmailFiles.readBinaryFile(sigFile);

    if (sig.search(/^-----BEGIN PGP/m) < 0) {
      let msg = await PgpJS.signature.read(ensureUint8Array(sig));
      sig = msg.armor();
    }

    let ret = await this.verifyDetached(data, sig, false);

    if (ret.statusFlags & (EnigmailConstants.BAD_SIGNATURE | EnigmailConstants.UNVERIFIED_SIGNATURE)) {
      throw ret.errorMsg ? ret.errorMsg : EnigmailLocale.getString("unverifiedSig") + " - " + EnigmailLocale.getString("msgSignedUnkownKey");
    }

    const detailArr = ret.sigDetails.split(/ /);
    const dateTime = EnigmailTime.getDateTime(detailArr[2], true, true);
    return ret.errorMsg + "\n" + EnigmailLocale.getString("keyAndSigDate", [ret.keyId, dateTime]);
  },

  /**
   * Encrypt (and possibly sign) some text data
   *
   * @param {String} text:             The data to encrypt.
   * @param {Array<Key>} publicKeys:   Array of keys to which to encrypt the message
   * @param {Key} signingKey:          If provided, the message will be signed using that key.
   *                                   If null, message will not be signed.
   */
  encryptData: async function(text, publicKeys, signingKey) {
    EnigmailLog.DEBUG(`pgpjs-encrypt.jsm: encryptData(${text.length})\n`);
    const PgpJS = getOpenPGPLibrary();

    let publicKeyPackets = new PgpJS.PacketList();
    publicKeyPackets = publicKeyPackets.concat(await publicKeys.toPacketList());
    let armoredPk = PgpJS.armor(PgpJS.enums.armor.publicKey, publicKeyPackets.write());

    let armoredSk = null;

    if (signingKey) {
      let signingKeyPackets = new PgpJS.PacketList();
      signingKeyPackets = signingKeyPackets.concat(await signingKey.toPacketList());
      armoredSk = PgpJS.armor(PgpJS.enums.armor.privateKey, signingKeyPackets.write());
    }

    let result = await PgpJsWorkerParent.sendMessage("encryptData", {
      text,
      encryptionKeys: armoredPk,
      signingKeys: armoredSk
    });

    return result;
  },

  /**
   * Sign some text data
   *
   * @param {String} text:                The data to sign.
   * @param {Key} signingKey:             The key used to sign the text.
   * @param {Boolean} detachedSignature:  Create a detached signature (true) or clearsigned message (false).
   */
  signData: async function(text, signingKey, detachedSignature) {
    EnigmailLog.DEBUG(`pgpjs-encrypt.jsm: signData(${text.length})\n`);
    const PgpJS = getOpenPGPLibrary();


    let signingKeyPackets = new PgpJS.PacketList();
    signingKeyPackets = signingKeyPackets.concat(await signingKey.toPacketList());
    let armoredSk = PgpJS.armor(PgpJS.enums.armor.privateKey, signingKeyPackets.write());

    let result = await PgpJsWorkerParent.sendMessage("signData", {
      text,
      signingKeys: armoredSk,
      detachedSignature
    });

    return result;
  }
};

function ensureUint8Array(stringOrUint8Array) {
  if (typeof stringOrUint8Array === "string") {

    const result = new Uint8Array(stringOrUint8Array.length);
    for (let i = 0; i < stringOrUint8Array.length; i++) {
      result[i] = stringOrUint8Array.charCodeAt(i);
    }
    return result;

  }

  return stringOrUint8Array;
}


/**
 * Callback functions called from worker for tasks requiring chrome
 */

var WorkerRequestHandler = {
  /**
   * Get secret keys as armored string
   *
   * @param {Array<String>} keyIds
   * @returns {String}
   */
  getSecretKeysForIds: async function(keyIds) {
    EnigmailLog.DEBUG(`pgpjs-crypto-main.jsm: getSecretKeysForIds()\n`);
    let secretKeys = await pgpjs_keyStore.readSecretKeys(keyIds, false);

    return secretKeys;
  },

  getPublicKeysForIds: async function(keyIds) {
    EnigmailLog.DEBUG(`pgpjs-crypto-main.jsm: getPublicKeysForIds()\n`);
    let publicKeys = await pgpjs_keyStore.readPublicKeys(keyIds);

    return publicKeys;
  },

  getDecryptedSecretKey: async function(options) {
    EnigmailLog.DEBUG(`pgpjs-crypto-main.jsm: getDecryptedSecretKey()\n`);
    const PgpJS = getOpenPGPLibrary();

    let secretKey = await pgpjs_keyStore.getKeysForKeyIds(true, [options.secretKeyFpr], false);
    let dedryptedSecKey = await pgpjs_keys.decryptSecretKey(secretKey[0], options.decryptionReason);
    let packets = new PgpJS.PacketList();

    // remove all 3rd-party signatures that just grow the key
    for (let u in dedryptedSecKey.users) {
      dedryptedSecKey.users[u].otherCertifications = [];
    }

    if (dedryptedSecKey) {
      packets = packets.concat(await dedryptedSecKey.toPacketList());
      return PgpJS.armor(PgpJS.enums.armor.privateKey, packets.write());
    }

    return null;
  },

  downloadMissingKeys: async function(keyIds) {
    EnigmailLog.DEBUG(`pgpjs-crypto-main.jsm: downloadMissingKeys()\n`);

    const EnigmailKeyServer = ChromeUtils.import("chrome://enigmail/content/modules/keyserver.jsm").EnigmailKeyServer;
    const PgpJS = getOpenPGPLibrary();
    let packets = new PgpJS.PacketList();

    try {
      const keyserver = EnigmailPrefs.getAutoKeyRetrieveServer();

      if (keyserver && keyserver.length > 0) {
        const keyList = "0x" + keyIds.join(" 0x");
        const ret = await EnigmailKeyServer.download(keyList, keyserver);

        if (ret.result === 0 && ret.keyList.length > 0) {
          let foundKeys = await pgpjs_keyStore.getKeysForKeyIds(false, keyIds);
          packets = packets.concat(await foundKeys.toPacketList());
        }
      }
    }
    catch (x) {}

    return PgpJS.armor(PgpJS.enums.armor.publicKey, packets.write());
  }

};

var pendingPromises = [];
var gTransactionId = 0;

const cryptoWorker = new Worker('chrome://enigmail/content/modules/cryptoAPI/pgpjs-crypto-worker.js');

cryptoWorker.onmessage = async function(e) {
  if ("logMessage" in e.data) {
    if (e.data.logLevel >= 5) {
      EnigmailLog.DEBUG(`${e.data.logMessage}\n`);
    }
    else if (e.data.logLevel === 0) {
      EnigmailLog.ERROR(`${e.data.logMessage}\n`);
    }
    return;
  }

  if (!("trxId" in e.data)) {
    EnigmailLog.ERROR(`pgpjs-crypto-worker.jsm. onmessage: cannot deliver message received from worker ${e.data}\n`);
  }

  if ("func" in e.data) {
    let method = e.data.func;
    let args = e.data.param || null;

    if (method in WorkerRequestHandler) {
      try {
        let workerResult = await WorkerRequestHandler[method](args);
        cryptoWorker.postMessage({
          trxId: e.data.trxId,
          result: workerResult
        });
      }
      catch (ex) {
        cryptoWorker.postMessage({
          trxId: e.data.trxId,
          error: `${ex.toString()}\n${ex.stack}`
        });
      }
    }
    else {
      EnigmailLog.ERROR(`pgpjs-crypto-worker.jsm. onmessage: Unknown function call ${e.data.func} received from worker\n`);
    }
    return;
  }

  if (pendingPromises[e.data.trxId]) {
    if ("result" in e.data) {
      pendingPromises[e.data.trxId].resolve(e.data.result);
    }
    else {
      EnigmailLog.ERROR(`${e.data.error}\n`);
      pendingPromises[e.data.trxId].reject(e.data.error);
    }
    delete pendingPromises[e.data.trxId];
  }
};

cryptoWorker.onerror = function(e) {
  EnigmailLog.ERROR(`pgpjs-crypto-worker.jsm. onerror: Error received from worker: ${e.message}\n`);
};


const PgpJsWorkerParent = {

  sendMessage(functionName, param, transferables) {
    let trxId = ++gTransactionId;
    return new Promise((resolve, reject) => {
      cryptoWorker.postMessage({
        func: functionName,
        trxId: trxId,
        param: param
      }, transferables);

      pendingPromises[trxId] = {
        resolve,
        reject
      };
    });
  }
};