aboutsummaryrefslogtreecommitdiffstats
path: root/package/cryptoAPI/pgpjs-encrypt.jsm
blob: b84bbc52d910e7a551685497694243db404a507f (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
/*
 * 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";

var EXPORTED_SYMBOLS = ["pgpjs_encrypt"];


var Services = ChromeUtils.import("resource://gre/modules/Services.jsm").Services;
const EnigmailLog = ChromeUtils.import("chrome://enigmail/content/modules/log.jsm").EnigmailLog;
const getOpenPGPLibrary = ChromeUtils.import("chrome://enigmail/content/modules/stdlib/openpgp-loader.jsm").getOpenPGPLibrary;
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 EnigmailLocale = ChromeUtils.import("chrome://enigmail/content/modules/locale.jsm").EnigmailLocale;

var gLastKeyDecrypted = null;

/**
 * OpenPGP.js implementation of CryptoAPI
 *
 * Encryption-related functions
 */

var pgpjs_encrypt = {
  /**
   * Encrypt messages
   *
   * @param {String} from: keyID of sender/signer
   * @param {String} recipients: keyIDs of recipients, separated by spaces
   * @param {String} hiddenRecipients: keyIDs or email addresses of hidden recipients (bcc), separated by spaces
   * @param {Number} encryptionFlags: Flags for Signed/encrypted/PGP-MIME etc.
   * @param {String} plainText: data to encrypt
   * @param {String} hashAlgorithm: [OPTIONAL] hash algorithm (ignored for this API)
   * @param {nsIWindow} parentWindow: [OPTIONAL] window on top of which to display modal dialogs
   *
   * @return {Promise<Object>}:
   *     - {Number} exitCode:    0 = success / other values: error
   *     - {String} data:        encrypted data
   *     - {String} errorMsg:    error message in case exitCode !== 0
   *     - {Number} statusFlags: Status flags for result
   */

  encryptMessage: async function(from, recipients, hiddenRecipients, encryptionFlags, plainText, hashAlgorithm = null, parentWindow = null) {
    EnigmailLog.DEBUG(`pgpjs-encrypt.jsm: encryptMessage(${from}, ${recipients}, ${encryptionFlags}, ${plainText.length})\n`);

    let retObj = {
      exitCode: 1,
      data: "",
      errorMsg: "",
      statusFlags: 0
    };

    try {
      if (from.substr(0, 2) !== "0x") {
        throw Error("From address is not a key ID");
      }

      let ownKeyId = from;

      if (encryptionFlags & EnigmailConstants.SEND_ENCRYPTED) {
        if (!(encryptionFlags & EnigmailConstants.SEND_SIGNED)) {
          from = null;
        }

        let recipientArr = [];

        if (recipients && recipients.length > 0) {
          recipientArr = recipients.split(/ +/);
          for (let i of recipientArr) {
            if (i.substr(0, 2) !== "0x") {
              throw Error(`Recipient ${i} is not a key ID`);
            }
          }
        }

        let hiddenRcpt = [];
        if (hiddenRecipients && hiddenRecipients.length > 0) {
          hiddenRcpt = hiddenRecipients.split(/ +/);
          for (let i of recipientArr) {
            if (i.substr(0, 2) !== "0x") {
              throw Error(`Hidden recipient ${i} is not a key ID`);
            }
          }
        }

        recipientArr = recipientArr.concat(hiddenRcpt);

        if (encryptionFlags & EnigmailConstants.SEND_ENCRYPT_TO_SELF) recipientArr.push(ownKeyId);
        let result = await encryptData(recipientArr, from, plainText);

        if (typeof(result) === "string") {
          retObj.data = result;
          retObj.exitCode = 0;
        }
      }
      else {
        const detachedSig = ((encryptionFlags & EnigmailConstants.SEND_PGP_MIME) ||
          (encryptionFlags & EnigmailConstants.SEND_ATTACHMENT));

        let result = await signData(from, plainText, detachedSig ? true : false, encryptionFlags);
        if (typeof(result) === "string") {
          retObj.data = result;
          retObj.exitCode = 0;
        }
      }
    }
    catch (ex) {
      EnigmailLog.DEBUG(`pgpjs-encrypt.jsm: encryptMessage: ERROR: ${ex.toString()}\n`);
      EnigmailLog.DEBUG(`${ex.stack}\n`);
      retObj.errorMsg = ex.toString();
    }
    return retObj;
  }

};

/**
 * Encrypt (and possibly sign) some text data
 *
 * @param {Array<String>} recipientKeyIds: Array of key IDs to which to encrypt the message
 * @param {String} signingKeyId:           If provided, the message will be signed using that key.
 *                                         If '' or null, message will not be signed.
 * @param {String} text:                   The message to encrypt.
 * @param {Number} encryptionFlags:        Flags for Signed/encrypted/PGP-MIME etc.
 */
async function encryptData(recipientKeyIds, signingKeyId, text, encryptionFlags) {
  EnigmailLog.DEBUG(`pgpjs-encrypt.jsm: encryptData(${recipientKeyIds.length}, ${signingKeyId})\n`);
  const PgpJS = getOpenPGPLibrary();

  let privateKeys = null;
  let pk = {
    key: null
  };

  if (signingKeyId) {
    privateKeys = await pgpjs_keyStore.getKeysForKeyIds(true, [signingKeyId]);
    pk.key = privateKeys[0];
    if (!await decryptPrivateKey(pk, EnigmailConstants.KEY_DECRYPT_REASON_SIGNCRYPT_MSG, encryptionFlags)) {
      throw Error("No password provided");
    }
  }

  let uniqueKeyIds = [...new Set(recipientKeyIds)]; // make key IDs unique
  let publicKeys = await pgpjs_keyStore.getKeysForKeyIds(false, uniqueKeyIds);

  return await PgpJS.encrypt({
    message: await PgpJS.createMessage({text}),
    encryptionKeys: publicKeys,
    signingKeys: pk.key ? [pk.key] : undefined, // for signing
    format: "armored"
  });
}

/**
 * Sign some text data
 *
 * @param {String} signingKeyId:       Key ID used for signing the message
 * @param {String} text:               Text data to sign
 * @param {Boolean} detachedSignature: If true, create a detached signature.
 *                                     If false, create a clearsigned message.
 * @param {Number} encryptionFlags:    Flags for Signed/encrypted/PGP-MIME etc.
 */
async function signData(signingKeyId, text, detachedSignature, encryptionFlags) {
  EnigmailLog.DEBUG(`pgpjs-encrypt.jsm: signData(${signingKeyId})\n`);
  const PgpJS = getOpenPGPLibrary();

  let privateKeys = null;

  if (!signingKeyId) {
    throw Error("No private key provided");
  }

  privateKeys = await pgpjs_keyStore.getKeysForKeyIds(true, [signingKeyId]);
  let pk = {
    key: privateKeys[0]
  };
  if (!await decryptPrivateKey(pk, EnigmailConstants.KEY_DECRYPT_REASON_SIGN_MSG, encryptionFlags)) {
    throw Error("No password provided");
  }

  if (detachedSignature) {
    return await PgpJS.sign({
      message: await PgpJS.createMessage({text}),
      signingKeys: [pk.key],
      detached: detachedSignature,
      format: "armored"
    });
  }
  else {
    return await PgpJS.sign({
      message: await PgpJS.createCleartextMessage({text}),
      signingKeys: [pk.key],
      detached: detachedSignature,
      format: "armored"
    });
  }
}

/**
 * Decrypt a private key and, if the flag SEND_TEST is provided, keep the decrypted version for another use
 * This is done to allow for testing encryption/signing and then sending the message without needing to enter
 * the password twice.
 *
 * @param {Object} keyData:         <key> contains the private key
 * @param {Text} decryptionMsg:     reason message to display for password dialog
 * @param {Number} encryptionFlags: Flags for Signed/encrypted/PGP-MIME etc.
 *
 * @returns {Boolean} true if key was decrypted, false if not
 */
async function decryptPrivateKey(keyData, decryptionMsg, encryptionFlags) {
  if (encryptionFlags & EnigmailConstants.SEND_TEST) {
    gLastKeyDecrypted = null;
    let decryptedKey = await pgpjs_keys.decryptSecretKey(keyData.key, decryptionMsg);
    if (decryptedKey) {
      keyData.key = decryptedKey;
      gLastKeyDecrypted = keyData.key;
    }

    return decryptedKey !== null;
  }

  // regular message -> use the key once
  if (gLastKeyDecrypted && (gLastKeyDecrypted.getFingerprint() === keyData.key.getFingerprint())) {
    keyData.key = gLastKeyDecrypted;
    gLastKeyDecrypted = null;
    return true;
  }
  else {
    let decryptedKey = await pgpjs_keys.decryptSecretKey(keyData.key, decryptionMsg);
    keyData.key = decryptedKey;
    return  decryptedKey !== null;
  }
}