summaryrefslogtreecommitdiffstats
path: root/comm/mail/extensions/openpgp/content/modules/webKey.jsm
blob: 76bd316e63b1e0db62f7c6944e07c9606da97fd6 (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
/*
 * 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 https://mozilla.org/MPL/2.0/.
 */

/**
 * This module serves to integrate WKS (Webkey service) into Enigmail
 */

"use strict";

var EXPORTED_SYMBOLS = ["EnigmailWks"];

const { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);
const { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, {
  EnigmailFuncs: "chrome://openpgp/content/modules/funcs.jsm",
  EnigmailLog: "chrome://openpgp/content/modules/log.jsm",
});

var EnigmailWks = {
  wksClientPath: null,

  /**
   * Get WKS Client path (gpg-wks-client)
   *
   * @param window  : Object - parent window for dialog display
   * @param cb      : Function(retValue) - callback function.
   *                   retValue: nsIFile Object to gpg-wks-client executable or NULL
   * @returns : Object - NULL or a process handle
   */
  getWksClientPathAsync(window, cb) {
    lazy.EnigmailLog.DEBUG("webKey.jsm: getWksClientPathAsync\n");
    throw new Error("Not implemented");
  },

  /**
   * Determine if WKS is supported by email provider
   *
   * @param email : String - user's email address
   * @param window: Object - parent window of dialog display
   * @param cb    : Function(retValue) - callback function.
   *                   retValue: Boolean: true if WKS is supported / false otherwise
   * @returns : Object - process handle
   */
  isWksSupportedAsync(email, window, cb) {
    lazy.EnigmailLog.DEBUG(
      "webKey.jsm: isWksSupportedAsync: email = " + email + "\n"
    );
    throw new Error("Not implemented");
  },

  /**
   * Submit a set of keys to the Web Key Server (WKD)
   *
   * @param keys:     Array of KeyObj
   * @param win:      parent Window for displaying dialogs
   * @param observer: Object (KeySrvListener API)
   *     Object implementing:
   *    - onProgress: function(percentComplete) [only implemented for download()]
   *     - onCancel: function() - the body will be set by the callee
   *
   * @returns Promise<...>
   */
  wksUpload(keys, win, observer = null) {
    lazy.EnigmailLog.DEBUG(`webKey.jsm: wksUpload(): keys = ${keys.length}\n`);
    let ids = getWkdIdentities(keys);

    if (observer === null) {
      observer = {
        onProgress() {},
      };
    }

    observer.isCanceled = false;
    observer.onCancel = function () {
      this.isCanceled = true;
    };

    if (!ids) {
      throw new Error("error");
    }

    if (ids.senderIdentities.length === 0) {
      return new Promise(resolve => {
        resolve([]);
      });
    }

    return performWkdUpload(ids.senderIdentities, win, observer);
  },

  /**
   * Submit a key to the email provider (= send publication request)
   *
   * @param ident : nsIMsgIdentity - user's ID
   * @param key   : Enigmail KeyObject of user's key
   * @param window: Object - parent window of dialog display
   * @param cb    : Function(retValue) - callback function.
   *                   retValue: Boolean: true if submit was successful / false otherwise
   * @returns : Object - process handle
   */

  submitKey(ident, key, window, cb) {
    lazy.EnigmailLog.DEBUG(
      "webKey.jsm: submitKey(): email = " + ident.email + "\n"
    );
    throw new Error("Not implemented");
  },

  /**
   * Submit a key to the email provider (= send publication request)
   *
   * @param ident : nsIMsgIdentity - user's ID
   * @param body  : String -  complete message source of the confirmation-request email obtained
   *                    from the email provider
   * @param window: Object - parent window of dialog display
   * @param cb    : Function(retValue) - callback function.
   *                   retValue: Boolean: true if submit was successful / false otherwise
   * @returns : Object - process handle
   */

  confirmKey(ident, body, window, cb) {
    lazy.EnigmailLog.DEBUG(
      "webKey.jsm: confirmKey: ident=" + ident.email + "\n"
    );
    throw new Error("Not implemented");
  },
};

/**
 * Check if a file exists and is executable
 *
 * @param path:         String - directory name
 * @param execFileName: String - executable name
 *
 * @returns Object - nsIFile if file exists; NULL otherwise
 */

function getWkdIdentities(keys) {
  lazy.EnigmailLog.DEBUG(
    `webKey.jsm: getWkdIdentities(): keys = ${keys.length}\n`
  );
  let senderIdentities = [],
    notFound = [];

  for (let key of keys) {
    try {
      let found = false;
      for (let uid of key.userIds) {
        let email = lazy.EnigmailFuncs.stripEmail(uid.userId).toLowerCase();
        let identity = MailServices.accounts.allIdentities.find(
          id => id.email?.toLowerCase() == email
        );

        if (identity) {
          senderIdentities.push({
            identity,
            fpr: key.fpr,
          });
        }
      }
      if (!found) {
        notFound.push(key);
      }
    } catch (ex) {
      lazy.EnigmailLog.DEBUG(ex + "\n");
      return null;
    }
  }

  return {
    senderIdentities,
    notFound,
  };
}

/**
 * Do the WKD upload and interact with a progress receiver
 *
 * @param keyList:     Object:
 *                       - fprList (String - fingerprint)
 *                       - senderIdentities (nsIMsgIdentity)
 * @param win:         nsIWindow - parent window
 * @param observer:    Object:
 *                       - onProgress: function(percentComplete [0 .. 100])
 *                             called after processing of every key (independent of status)
 *                       - onUpload: function(fpr)
 *                              called after successful uploading of a key
 *                       - onFinished: function(completionStatus, errorMessage, displayError)
 *                       - isCanceled: Boolean - used to determine if process is canceled
 */
function performWkdUpload(keyList, win, observer) {
  lazy.EnigmailLog.DEBUG(
    `webKey.jsm: performWkdUpload: keyList.length=${keyList.length}\n`
  );

  let uploads = [];

  let numKeys = keyList.length;

  // For each key fpr/sender identity pair, check whenever WKS is supported
  // Result is an array of booleans
  for (let i = 0; i < numKeys; i++) {
    let keyFpr = keyList[i].fpr;
    let senderIdent = keyList[i].identity;

    let was_uploaded = new Promise(function (resolve, reject) {
      lazy.EnigmailLog.DEBUG(
        "webKey.jsm: performWkdUpload: _isSupported(): ident=" +
          senderIdent.email +
          ", key=" +
          keyFpr +
          "\n"
      );
      EnigmailWks.isWksSupportedAsync(
        senderIdent.email,
        win,
        function (is_supported) {
          if (observer.isCanceled) {
            lazy.EnigmailLog.DEBUG(
              "webKey.jsm: performWkdUpload: canceled by user\n"
            );
            reject("canceled");
          }

          lazy.EnigmailLog.DEBUG(
            "webKey.jsm: performWkdUpload: ident=" +
              senderIdent.email +
              ", supported=" +
              is_supported +
              "\n"
          );
          resolve(is_supported);
        }
      );
    }).then(function (is_supported) {
      lazy.EnigmailLog.DEBUG(
        `webKey.jsm: performWkdUpload: _submitKey ${is_supported}\n`
      );
      if (is_supported) {
        return new Promise(function (resolve, reject) {
          EnigmailWks.submitKey(
            senderIdent,
            {
              fpr: keyFpr,
            },
            win,
            function (success) {
              observer.onProgress(((i + 1) / numKeys) * 100);
              if (success) {
                resolve(senderIdent);
              } else {
                reject("submitFailed");
              }
            }
          );
        });
      }

      observer.onProgress(((i + 1) / numKeys) * 100);
      return Promise.resolve(null);
    });

    uploads.push(was_uploaded);
  }

  return Promise.all(uploads)
    .catch(function (reason) {
      //let errorMsg = "Could not upload your key to the Web Key Service";
      return [];
    })
    .then(function (senders) {
      let uploaded_uids = [];
      if (senders) {
        senders.forEach(function (val) {
          if (val !== null) {
            uploaded_uids.push(val.email);
          }
        });
      }
      observer.onProgress(100);

      return uploaded_uids;
    });
}