aboutsummaryrefslogtreecommitdiffstats
path: root/lang/js/src/Keyring.js
blob: 9f313577ec6f06ce7137e4d67d9bf56b2e16b15a (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* gpgme.js - Javascript integration for gpgme
 * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
 *
 * This file is part of GPGME.
 *
 * GPGME is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * GPGME is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: LGPL-2.1+
 *
 * Author(s):
 *     Maximilian Krambach <[email protected]>
 */


import { createMessage } from './Message';
import { createKey } from './Key';
import { isFingerprint } from './Helpers';
import { gpgme_error } from './Errors';

/**
 * This class offers access to the gnupg keyring
 */
export class GPGME_Keyring {

    /**
     * Queries Keys (all Keys or a subset) from gnupg.
     *
     * @param {Object} options:
     * @param {String | Array<String>} options.pattern (optional) A pattern to
     * search for in userIds or KeyIds.
     * @param {Boolean} options.prepare_sync (optional) if set to true, most
     * data (with the exception of armored Key blocks) will be cached for the
     * Keys. This enables direct, synchronous use of these properties for
     * all keys. It does not check for changes on the backend. The cached
     * information can be updated with the {@link Key.refresh} method.
     * @param {Boolean} options.search (optional) retrieve Keys from external
     * servers with the method(s) defined in gnupg (e.g. WKD/HKP lookup)
     * @returns {Promise<GPGME_Key[]>}
     * @static
     * @async
     */
    getKeys ({ pattern, prepare_sync = false, search = false } = {}){
        if (typeof arguments[0] !== 'object') {
            return Promise.reject(gpgme_error('PARAM_WRONG'));
        }
        if (arguments.length && typeof arguments[0] !== 'object') {
            return Promise.reject(gpgme_error('PARAM_WRONG'));
        }
        return new Promise(function (resolve, reject) {
            let msg = createMessage('keylist');
            if (pattern) {
                msg.setParameter('keys', pattern);
            }
            msg.setParameter('sigs', true);
            if (search === true){
                msg.setParameter('locate', true);
            }
            msg.post().then(function (result){
                let resultset = [];
                if (result.keys.length === 0){
                    resolve([]);
                } else {
                    let secondrequest;
                    if (prepare_sync === true) {
                        secondrequest = function () {
                            let msg2 = createMessage('keylist');
                            if (pattern){
                                msg2.setParameter('keys', pattern);
                            }
                            msg2.setParameter('secret', true);
                            return msg2.post();
                        };
                    } else {
                        secondrequest = function () {
                            return Promise.resolve(true);
                        };
                    }
                    secondrequest().then(function (answer) {
                        for (let i=0; i < result.keys.length; i++){
                            if (prepare_sync === true){
                                if (answer && answer.keys) {
                                    for (let j=0;
                                        j < answer.keys.length; j++ ){
                                        const a = answer.keys[j];
                                        const b = result.keys[i];
                                        if (
                                            a.fingerprint === b.fingerprint
                                        ) {
                                            if (a.secret === true){
                                                b.hasSecret = true;
                                            } else {
                                                b.hasSecret = false;
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                            let k = createKey(result.keys[i].fingerprint,
                                !prepare_sync, result.keys[i]);
                            resultset.push(k);
                        }
                        resolve(resultset);
                    }, function (error){
                        reject(error);
                    });
                }
            });
        });
    }

    /**
     * @typedef {Object} exportResult The result of a getKeysArmored
     * operation.
     * @property {String} armored The public Key(s) as armored block. Note
     * that the result is one armored block, and not a block per key.
     * @property {Array<String>} secret_fprs (optional) list of
     * fingerprints for those Keys that also have a secret Key available in
     * gnupg. The secret key will not be exported, but the fingerprint can
     * be used in operations needing a secret key.
     */

    /**
     * Fetches the armored public Key blocks for all Keys matching the
     * pattern (if no pattern is given, fetches all keys known to gnupg).
     * @param {Object} options (optional)
     * @param {String|Array<String>} options.pattern The Pattern to
     * search for
     * @param {Boolean} options.with_secret_fpr also return a list of
     * fingerprints for the keys that have a secret key available
     * @returns {Promise<exportResult>} Object containing the
     * armored Key(s) and additional information.
     * @static
     * @async
     */
    getKeysArmored ({ pattern, with_secret_fpr }) {
        return new Promise(function (resolve, reject) {
            let msg = createMessage('export');
            msg.setParameter('armor', true);
            if (with_secret_fpr === true) {
                msg.setParameter('with-sec-fprs', true);
            }
            if (pattern){
                msg.setParameter('keys', pattern);
            }
            msg.post().then(function (answer){
                const result = { armored: answer.data };
                if (with_secret_fpr === true){
                    if (answer.hasOwnProperty('sec-fprs')){
                        result.secret_fprs = answer['sec-fprs'];
                    } else {
                        result.secret_fprs = [];
                    }
                }
                resolve(result);
            }, function (error){
                reject(error);
            });
        });
    }

    /**
     * Returns the Key used by default in gnupg.
     * (a.k.a. 'primary Key or 'main key').
     * It looks up the gpg configuration if set, or the first key that
     * contains a secret key.
     *
     * @returns {Promise<GPGME_Key>}
     * @async
     * @static
     */
    getDefaultKey (prepare_sync = false) {
        let me = this;
        return new Promise(function (resolve, reject){
            let msg = createMessage('config_opt');
            msg.setParameter('component', 'gpg');
            msg.setParameter('option', 'default-key');
            msg.post().then(function (resp){
                if (resp.option !== undefined
                    && resp.option.hasOwnProperty('value')
                    && resp.option.value.length === 1
                    && resp.option.value[0].hasOwnProperty('string')
                    && typeof (resp.option.value[0].string) === 'string'){
                    me.getKeys({ pattern: resp.option.value[0].string,
                        prepare_sync: true }).then(
                        function (keys){
                            if (keys.length === 1){
                                resolve(keys[0]);
                            } else {
                                reject(gpgme_error('KEY_NO_DEFAULT'));
                            }
                        }, function (error){
                            reject(error);
                        });
                } else {
                    let msg = createMessage('keylist');
                    msg.setParameter('secret', true);
                    msg.post().then(function (result){
                        if (result.keys.length === 0){
                            reject(gpgme_error('KEY_NO_DEFAULT'));
                        } else {
                            for (let i=0; i< result.keys.length; i++ ) {
                                if (
                                    result.keys[i].invalid === false &&
                                    result.keys[i].expired === false &&
                                    result.keys[i].revoked === false &&
                                    result.keys[i].can_sign === true
                                ) {
                                    let k = createKey(
                                        result.keys[i].fingerprint,
                                        !prepare_sync,
                                        result.keys[i]);
                                    resolve(k);
                                    break;
                                } else if (i === result.keys.length - 1){
                                    reject(gpgme_error('KEY_NO_DEFAULT'));
                                }
                            }
                        }
                    }, function (error){
                        reject(error);
                    });
                }
            }, function (error){
                reject(error);
            });
        });
    }

    /**
     * @typedef {Object} importResult The result of a Key update
     * @property {Object} summary Numerical summary of the result. See the
     * feedbackValues variable for available Keys values and the gnupg
     * documentation.
     * https://www.gnupg.org/documentation/manuals/gpgme/Importing-Keys.html
     * for details on their meaning.
     * @property {Array<importedKeyResult>} Keys Array of Object containing
     * GPGME_Keys with additional import information
     *
     */

    /**
     * @typedef {Object} importedKeyResult
     * @property {GPGME_Key} key The resulting key
     * @property {String} status:
     *  'nochange' if the Key was not changed,
     *  'newkey' if the Key was imported in gpg, and did not exist
     *    previously,
     *  'change' if the key existed, but details were updated. For details,
     *    Key.changes is available.
     * @property {Boolean} changes.userId Changes in userIds
     * @property {Boolean} changes.signature Changes in signatures
     * @property {Boolean} changes.subkey Changes in subkeys
     */

    /**
     * Import an armored Key block into gnupg. Note that this currently
     * will not succeed on private Key blocks.
     * @param {String} armored Armored Key block of the Key(s) to be
     * imported into gnupg
     * @param {Boolean} prepare_sync prepare the keys for synched use
     * (see {@link getKeys}).
     * @returns {Promise<importResult>} A summary and Keys considered.
     * @async
     * @static
     */
    importKey (armored, prepare_sync) {
        let feedbackValues = ['considered', 'no_user_id', 'imported',
            'imported_rsa', 'unchanged', 'new_user_ids', 'new_sub_keys',
            'new_signatures', 'new_revocations', 'secret_read',
            'secret_imported', 'secret_unchanged', 'skipped_new_keys',
            'not_imported', 'skipped_v3_keys'];
        if (!armored || typeof (armored) !== 'string'){
            return Promise.reject(gpgme_error('PARAM_WRONG'));
        }
        let me = this;
        return new Promise(function (resolve, reject){
            let msg = createMessage('import');
            msg.setParameter('data', armored);
            msg.post().then(function (response){
                let infos = {};
                let fprs = [];
                let summary = {};
                for (let i=0; i < feedbackValues.length; i++ ){
                    summary[feedbackValues[i]] =
                        response.result[feedbackValues[i]];
                }
                if (!response.result.hasOwnProperty('imports') ||
                    response.result.imports.length === 0
                ){
                    resolve({ Keys:[],summary: summary });
                    return;
                }
                for (let res=0; res<response.result.imports.length; res++){
                    let result = response.result.imports[res];
                    let status = '';
                    if (result.status === 0){
                        status = 'nochange';
                    } else if ((result.status & 1) === 1){
                        status = 'newkey';
                    } else {
                        status = 'change';
                    }
                    let changes = {};
                    changes.userId = (result.status & 2) === 2;
                    changes.signature = (result.status & 4) === 4;
                    changes.subkey = (result.status & 8) === 8;
                    // 16 new secret key: not implemented
                    fprs.push(result.fingerprint);
                    infos[result.fingerprint] = {
                        changes: changes,
                        status: status
                    };
                }
                let resultset = [];
                if (prepare_sync === true){
                    me.getKeys({ pattern: fprs, prepare_sync: true })
                        .then(function (result){
                            for (let i=0; i < result.length; i++) {
                                resultset.push({
                                    key: result[i],
                                    changes:
                                        infos[result[i].fingerprint].changes,
                                    status: infos[result[i].fingerprint].status
                                });
                            }
                            resolve({ Keys:resultset,summary: summary });
                        }, function (error){
                            reject(error);
                        });
                } else {
                    for (let i=0; i < fprs.length; i++) {
                        resultset.push({
                            key: createKey(fprs[i]),
                            changes: infos[fprs[i]].changes,
                            status: infos[fprs[i]].status
                        });
                    }
                    resolve({ Keys:resultset,summary:summary });
                }

            }, function (error){
                reject(error);
            });


        });


    }

    /**
     * Convenience function for deleting a Key. See {@link Key#delete} for
     * further information about the return values.
     * @param {String} fingerprint
     * @returns {Promise<Boolean>}
     * @async
     * @static
     */
    deleteKey (fingerprint){
        if (isFingerprint(fingerprint) === true) {
            let key = createKey(fingerprint);
            return key.delete();
        } else {
            return Promise.reject(gpgme_error('KEY_INVALID'));
        }
    }

    /**
     * Generates a new Key pair directly in gpg, and returns a GPGME_Key
     * representing that Key. Please note that due to security concerns,
     * secret Keys can not be deleted or exported from inside gpgme.js.
     * @param {Object} options
     * @param {String} option.userId The user Id, e.g. 'Foo Bar <[email protected]>'
     * @param {String} option.algo (optional) algorithm (and optionally key
     * size) to be used. See {@link supportedKeyAlgos} below for supported
     * values. If omitted, 'default' is used.
     * @param {Number} option.expires (optional) Expiration time in seconds
     * from now. If not set or set to 0, expiration will be 'never'
     *
     * @return {Promise<Key|GPGME_Error>}
     * @async
     */
    generateKey ({ userId, algo = 'default', expires= 0 } = {}){
        if (typeof userId !== 'string'
            // eslint-disable-next-line no-use-before-define
            || (algo && supportedKeyAlgos.indexOf(algo) < 0 )
            || (!Number.isInteger(expires) || expires < 0 )
        ){
            return Promise.reject(gpgme_error('PARAM_WRONG'));
        }
        // eslint-disable-next-line no-use-before-define
        let me = this;
        return new Promise(function (resolve, reject){
            let msg = createMessage('createkey');
            msg.setParameter('userid', userId);
            msg.setParameter('algo', algo);
            msg.setParameter('expires', expires);
            msg.post().then(function (response){
                me.getKeys({
                    pattern: response.fingerprint,
                    prepare_sync: true
                }).then(function (result){
                    resolve(result);
                }, function (error){
                    reject(error);
                });
            }, function (error) {
                reject(error);
            });
        });
    }
}


/**
 * List of algorithms supported for key generation. Please refer to the gnupg
 * documentation for details
 */
const supportedKeyAlgos = [
    'default', 'future-default',
    'rsa', 'rsa2048', 'rsa3072', 'rsa4096',
    'dsa', 'dsa2048', 'dsa3072', 'dsa4096',
    'elg', 'elg2048', 'elg3072', 'elg4096',
    'ed25519',
    'cv25519',
    'brainpoolP256r1', 'brainpoolP384r1', 'brainpoolP512r1',
    'NIST P-256', 'NIST P-384', 'NIST P-521'
];