2018-04-23 15:18:46 +00:00
|
|
|
/* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1+
|
2018-06-06 11:05:53 +00:00
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Maximilian Krambach <mkrambach@intevation.de>
|
2018-04-23 15:18:46 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-06 11:05:53 +00:00
|
|
|
|
2018-08-20 13:12:01 +00:00
|
|
|
import { createMessage } from './Message';
|
|
|
|
import { createKey } from './Key';
|
2018-06-06 09:57:41 +00:00
|
|
|
import { isFingerprint } from './Helpers';
|
2018-04-25 13:59:36 +00:00
|
|
|
import { gpgme_error } from './Errors';
|
2018-04-23 15:18:46 +00:00
|
|
|
|
2018-07-10 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* This class offers access to the gnupg keyring
|
|
|
|
*/
|
2018-04-23 15:18:46 +00:00
|
|
|
export class GPGME_Keyring {
|
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
/**
|
|
|
|
* Queries Keys (all Keys or a subset) from gnupg.
|
|
|
|
*
|
2018-08-23 10:15:59 +00:00
|
|
|
* @param {Object} options:
|
2018-08-22 16:37:46 +00:00
|
|
|
* @param {String | Array<String>} options.pattern (optional) A pattern to
|
2018-08-20 10:12:43 +00:00
|
|
|
* search for in userIds or KeyIds.
|
2018-08-22 16:37:46 +00:00
|
|
|
* @param {Boolean} options.prepare_sync (optional) if set to true, most
|
|
|
|
* data (with the exception of armored Key blocks) will be cached for the
|
2018-08-20 10:12:43 +00:00
|
|
|
* 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.
|
2018-08-22 16:37:46 +00:00
|
|
|
* @param {Boolean} options.search (optional) retrieve Keys from external
|
2018-08-20 10:12:43 +00:00
|
|
|
* servers with the method(s) defined in gnupg (e.g. WKD/HKP lookup)
|
|
|
|
* @returns {Promise<Array<GPGME_Key>>}
|
|
|
|
* @static
|
|
|
|
* @async
|
|
|
|
*/
|
2018-08-27 10:32:28 +00:00
|
|
|
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'));
|
|
|
|
}
|
2018-08-20 13:12:01 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
2018-08-20 10:12:43 +00:00
|
|
|
let msg = createMessage('keylist');
|
2018-08-23 10:15:59 +00:00
|
|
|
if (pattern) {
|
|
|
|
msg.setParameter('keys', pattern);
|
2018-08-20 10:12:43 +00:00
|
|
|
}
|
|
|
|
msg.setParameter('sigs', true);
|
2018-08-23 10:15:59 +00:00
|
|
|
if (search === true){
|
2018-08-20 10:12:43 +00:00
|
|
|
msg.setParameter('locate', true);
|
|
|
|
}
|
2018-08-20 13:12:01 +00:00
|
|
|
msg.post().then(function (result){
|
2018-08-20 10:12:43 +00:00
|
|
|
let resultset = [];
|
|
|
|
if (result.keys.length === 0){
|
|
|
|
resolve([]);
|
|
|
|
} else {
|
|
|
|
let secondrequest;
|
2018-08-23 10:15:59 +00:00
|
|
|
if (prepare_sync === true) {
|
2018-08-20 13:12:01 +00:00
|
|
|
secondrequest = function () {
|
2018-08-20 10:12:43 +00:00
|
|
|
let msg2 = createMessage('keylist');
|
2018-08-23 10:15:59 +00:00
|
|
|
if (pattern){
|
|
|
|
msg2.setParameter('keys', pattern);
|
2018-08-22 10:18:55 +00:00
|
|
|
}
|
2018-08-20 10:12:43 +00:00
|
|
|
msg2.setParameter('secret', true);
|
|
|
|
return msg2.post();
|
|
|
|
};
|
2018-06-06 09:57:41 +00:00
|
|
|
} else {
|
2018-08-20 13:12:01 +00:00
|
|
|
secondrequest = function () {
|
2018-08-20 10:12:43 +00:00
|
|
|
return Promise.resolve(true);
|
|
|
|
};
|
|
|
|
}
|
2018-08-20 13:12:01 +00:00
|
|
|
secondrequest().then(function (answer) {
|
2018-08-20 10:12:43 +00:00
|
|
|
for (let i=0; i < result.keys.length; i++){
|
2018-08-23 10:15:59 +00:00
|
|
|
if (prepare_sync === true){
|
2018-08-20 10:12:43 +00:00
|
|
|
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;
|
2018-07-09 09:24:46 +00:00
|
|
|
}
|
2018-08-20 10:12:43 +00:00
|
|
|
break;
|
2018-07-09 09:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-20 10:12:43 +00:00
|
|
|
let k = createKey(result.keys[i].fingerprint,
|
2018-08-23 10:15:59 +00:00
|
|
|
!prepare_sync, result.keys[i]);
|
2018-08-20 10:12:43 +00:00
|
|
|
resultset.push(k);
|
|
|
|
}
|
|
|
|
resolve(resultset);
|
2018-08-20 13:12:01 +00:00
|
|
|
}, function (error){
|
2018-08-20 10:12:43 +00:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}
|
2018-05-25 09:53:24 +00:00
|
|
|
});
|
2018-08-20 10:12:43 +00:00
|
|
|
});
|
|
|
|
}
|
2018-05-30 15:05:54 +00:00
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2018-07-12 09:36:55 +00:00
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
/**
|
|
|
|
* Fetches the armored public Key blocks for all Keys matching the
|
|
|
|
* pattern (if no pattern is given, fetches all keys known to gnupg).
|
2018-08-22 16:37:46 +00:00
|
|
|
* @param {Object} options (optional)
|
|
|
|
* @param {String|Array<String>} options.pattern The Pattern to
|
2018-08-20 10:12:43 +00:00
|
|
|
* search for
|
2018-08-22 16:37:46 +00:00
|
|
|
* @param {Boolean} options.with_secret_fpr also return a list of
|
2018-08-20 10:12:43 +00:00
|
|
|
* fingerprints for the keys that have a secret key available
|
|
|
|
* @returns {Promise<exportResult|GPGME_Error>} Object containing the
|
|
|
|
* armored Key(s) and additional information.
|
|
|
|
* @static
|
|
|
|
* @async
|
|
|
|
*/
|
2018-08-23 10:15:59 +00:00
|
|
|
getKeysArmored ({ pattern, with_secret_fpr }) {
|
2018-08-20 13:12:01 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
2018-08-20 10:12:43 +00:00
|
|
|
let msg = createMessage('export');
|
|
|
|
msg.setParameter('armor', true);
|
2018-08-23 10:15:59 +00:00
|
|
|
if (with_secret_fpr === true) {
|
2018-08-20 10:12:43 +00:00
|
|
|
msg.setParameter('with-sec-fprs', true);
|
|
|
|
}
|
2018-08-23 10:15:59 +00:00
|
|
|
if (pattern){
|
|
|
|
msg.setParameter('keys', pattern);
|
2018-08-20 10:12:43 +00:00
|
|
|
}
|
2018-08-20 13:12:01 +00:00
|
|
|
msg.post().then(function (answer){
|
|
|
|
const result = { armored: answer.data };
|
2018-08-23 10:15:59 +00:00
|
|
|
if (with_secret_fpr === true){
|
2018-08-22 16:37:46 +00:00
|
|
|
if (answer.hasOwnProperty('sec-fprs')){
|
|
|
|
result.secret_fprs = answer['sec-fprs'];
|
|
|
|
} else {
|
|
|
|
result.secret_fprs = [];
|
|
|
|
}
|
2018-07-27 18:56:11 +00:00
|
|
|
}
|
2018-08-20 10:12:43 +00:00
|
|
|
resolve(result);
|
2018-08-20 13:12:01 +00:00
|
|
|
}, function (error){
|
2018-08-20 10:12:43 +00:00
|
|
|
reject(error);
|
2018-05-30 15:05:54 +00:00
|
|
|
});
|
2018-08-20 10:12:43 +00:00
|
|
|
});
|
|
|
|
}
|
2018-05-30 15:05:54 +00:00
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
/**
|
|
|
|
* 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|GPGME_Error>}
|
|
|
|
* @async
|
|
|
|
* @static
|
|
|
|
*/
|
2018-08-20 13:12:01 +00:00
|
|
|
getDefaultKey (prepare_sync = false) {
|
2018-08-20 10:12:43 +00:00
|
|
|
let me = this;
|
2018-08-20 13:12:01 +00:00
|
|
|
return new Promise(function (resolve, reject){
|
2018-08-20 10:12:43 +00:00
|
|
|
let msg = createMessage('config_opt');
|
|
|
|
msg.setParameter('component', 'gpg');
|
|
|
|
msg.setParameter('option', 'default-key');
|
2018-08-20 13:12:01 +00:00
|
|
|
msg.post().then(function (resp){
|
2018-08-20 10:12:43 +00:00
|
|
|
if (resp.option !== undefined
|
|
|
|
&& resp.option.hasOwnProperty('value')
|
|
|
|
&& resp.option.value.length === 1
|
|
|
|
&& resp.option.value[0].hasOwnProperty('string')
|
2018-08-20 13:12:01 +00:00
|
|
|
&& typeof (resp.option.value[0].string) === 'string'){
|
2018-08-23 15:55:35 +00:00
|
|
|
me.getKeys({ pattern: resp.option.value[0].string,
|
|
|
|
prepare_sync: true }).then(
|
2018-08-20 13:12:01 +00:00
|
|
|
function (keys){
|
|
|
|
if (keys.length === 1){
|
2018-08-20 10:12:43 +00:00
|
|
|
resolve(keys[0]);
|
2018-08-16 09:29:10 +00:00
|
|
|
} else {
|
2018-08-20 10:12:43 +00:00
|
|
|
reject(gpgme_error('KEY_NO_DEFAULT'));
|
2018-06-13 13:22:03 +00:00
|
|
|
}
|
2018-08-20 13:12:01 +00:00
|
|
|
}, function (error){
|
2018-07-27 18:56:11 +00:00
|
|
|
reject(error);
|
|
|
|
});
|
2018-08-20 10:12:43 +00:00
|
|
|
} else {
|
|
|
|
let msg = createMessage('keylist');
|
|
|
|
msg.setParameter('secret', true);
|
2018-08-20 13:12:01 +00:00
|
|
|
msg.post().then(function (result){
|
2018-08-20 10:12:43 +00:00
|
|
|
if (result.keys.length === 0){
|
|
|
|
reject(gpgme_error('KEY_NO_DEFAULT'));
|
|
|
|
} else {
|
|
|
|
for (let i=0; i< result.keys.length; i++ ) {
|
2018-08-23 09:28:18 +00:00
|
|
|
if (
|
|
|
|
result.keys[i].invalid === false &&
|
|
|
|
result.keys[i].expired === false &&
|
|
|
|
result.keys[i].revoked === false &&
|
|
|
|
result.keys[i].can_sign === true
|
|
|
|
) {
|
2018-08-20 10:12:43 +00:00
|
|
|
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'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-20 13:12:01 +00:00
|
|
|
}, function (error){
|
2018-08-20 10:12:43 +00:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}
|
2018-08-20 13:12:01 +00:00
|
|
|
}, function (error){
|
2018-08-20 10:12:43 +00:00
|
|
|
reject(error);
|
2018-06-13 13:22:03 +00:00
|
|
|
});
|
2018-08-20 10:12:43 +00:00
|
|
|
});
|
|
|
|
}
|
2018-04-23 15:18:46 +00:00
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
*/
|
2018-07-10 12:32:26 +00:00
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2018-07-10 12:32:26 +00:00
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
/**
|
|
|
|
* 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'];
|
2018-08-20 13:12:01 +00:00
|
|
|
if (!armored || typeof (armored) !== 'string'){
|
2018-08-20 10:12:43 +00:00
|
|
|
return Promise.reject(gpgme_error('PARAM_WRONG'));
|
|
|
|
}
|
|
|
|
let me = this;
|
2018-08-20 13:12:01 +00:00
|
|
|
return new Promise(function (resolve, reject){
|
2018-08-20 10:12:43 +00:00
|
|
|
let msg = createMessage('import');
|
|
|
|
msg.setParameter('data', armored);
|
2018-08-20 13:12:01 +00:00
|
|
|
msg.post().then(function (response){
|
2018-08-20 10:12:43 +00:00
|
|
|
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
|
|
|
|
){
|
2018-08-20 13:12:01 +00:00
|
|
|
resolve({ Keys:[],summary: summary });
|
2018-08-20 10:12:43 +00:00
|
|
|
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';
|
2018-08-16 15:58:11 +00:00
|
|
|
}
|
2018-08-20 10:12:43 +00:00
|
|
|
let changes = {};
|
|
|
|
changes.userId = (result.status & 2) === 2;
|
|
|
|
changes.signature = (result.status & 4) === 4;
|
|
|
|
changes.subkey = (result.status & 8) === 8;
|
2018-08-20 13:12:01 +00:00
|
|
|
// 16 new secret key: not implemented
|
2018-08-20 10:12:43 +00:00
|
|
|
fprs.push(result.fingerprint);
|
|
|
|
infos[result.fingerprint] = {
|
|
|
|
changes: changes,
|
|
|
|
status: status
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let resultset = [];
|
|
|
|
if (prepare_sync === true){
|
2018-08-22 16:37:46 +00:00
|
|
|
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);
|
|
|
|
});
|
2018-08-20 10:12:43 +00:00
|
|
|
} 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
|
|
|
|
});
|
2018-06-06 09:57:41 +00:00
|
|
|
}
|
2018-08-20 13:12:01 +00:00
|
|
|
resolve({ Keys:resultset,summary:summary });
|
2018-08-20 10:12:43 +00:00
|
|
|
}
|
2018-06-06 09:57:41 +00:00
|
|
|
|
2018-08-20 13:12:01 +00:00
|
|
|
}, function (error){
|
2018-08-20 10:12:43 +00:00
|
|
|
reject(error);
|
|
|
|
});
|
2018-06-06 09:57:41 +00:00
|
|
|
|
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
});
|
2018-06-06 09:57:41 +00:00
|
|
|
|
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
}
|
2018-06-06 09:57:41 +00:00
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
/**
|
|
|
|
* Convenience function for deleting a Key. See {@link Key.delete} for
|
|
|
|
* further information about the return values.
|
|
|
|
* @param {String} fingerprint
|
|
|
|
* @returns {Promise<Boolean|GPGME_Error>}
|
|
|
|
* @async
|
|
|
|
* @static
|
|
|
|
*/
|
2018-08-20 13:12:01 +00:00
|
|
|
deleteKey (fingerprint){
|
2018-08-20 10:12:43 +00:00
|
|
|
if (isFingerprint(fingerprint) === true) {
|
|
|
|
let key = createKey(fingerprint);
|
|
|
|
return key.delete();
|
|
|
|
} else {
|
|
|
|
return Promise.reject(gpgme_error('KEY_INVALID'));
|
|
|
|
}
|
|
|
|
}
|
2018-06-06 09:57:41 +00:00
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2018-08-22 16:37:46 +00:00
|
|
|
* @param {Object} options
|
|
|
|
* @param {String} option.userId The user Id, e.g. 'Foo Bar <foo@bar.baz>'
|
|
|
|
* @param {String} option.algo (optional) algorithm (and optionally key
|
|
|
|
* size) to be used. See {@link supportedKeyAlgos} below for supported
|
2018-08-20 16:05:34 +00:00
|
|
|
* values. If ommitted, 'default' is used.
|
2018-08-22 16:37:46 +00:00
|
|
|
* @param {Number} option.expires (optional) Expiration time in seconds
|
|
|
|
* from now. If not set or set to 0, expiration will be 'never'
|
|
|
|
* @param {String} options.subkey_algo (optional) algorithm of the
|
|
|
|
* encryption subkey. If ommited the same as algo is used.
|
2018-08-20 10:12:43 +00:00
|
|
|
*
|
|
|
|
* @return {Promise<Key|GPGME_Error>}
|
|
|
|
* @async
|
|
|
|
*/
|
2018-08-27 10:32:28 +00:00
|
|
|
generateKey ({ userId, algo = 'default', expires= 0, subkey_algo } = {}){
|
2018-08-23 10:15:59 +00:00
|
|
|
if (typeof userId !== 'string'
|
2018-08-20 13:12:01 +00:00
|
|
|
// eslint-disable-next-line no-use-before-define
|
2018-08-23 10:15:59 +00:00
|
|
|
|| (algo && supportedKeyAlgos.indexOf(algo) < 0 )
|
|
|
|
|| (!Number.isInteger(expires) || expires < 0 )
|
2018-08-20 10:12:43 +00:00
|
|
|
){
|
|
|
|
return Promise.reject(gpgme_error('PARAM_WRONG'));
|
|
|
|
}
|
2018-08-20 16:05:34 +00:00
|
|
|
// eslint-disable-next-line no-use-before-define
|
2018-08-23 10:15:59 +00:00
|
|
|
if (subkey_algo && supportedKeyAlgos.indexOf(subkey_algo) < 0){
|
2018-08-20 16:05:34 +00:00
|
|
|
return Promise.reject(gpgme_error('PARAM_WRONG'));
|
|
|
|
}
|
2018-08-20 10:12:43 +00:00
|
|
|
let me = this;
|
2018-08-20 13:12:01 +00:00
|
|
|
return new Promise(function (resolve, reject){
|
2018-08-20 10:12:43 +00:00
|
|
|
let msg = createMessage('createkey');
|
2018-08-23 10:15:59 +00:00
|
|
|
msg.setParameter('userid', userId);
|
|
|
|
msg.setParameter('algo', algo);
|
|
|
|
if (subkey_algo) {
|
|
|
|
msg.setParameter('subkey-algo',subkey_algo );
|
2018-06-11 10:08:50 +00:00
|
|
|
}
|
2018-08-23 10:15:59 +00:00
|
|
|
msg.setParameter('expires', expires);
|
2018-08-20 13:12:01 +00:00
|
|
|
msg.post().then(function (response){
|
2018-08-27 10:32:28 +00:00
|
|
|
me.getKeys({
|
|
|
|
pattern: response.fingerprint,
|
|
|
|
prepare_sync: true
|
|
|
|
}).then(function (result){
|
|
|
|
resolve(result);
|
|
|
|
}, function (error){
|
|
|
|
reject(error);
|
|
|
|
});
|
2018-08-20 13:12:01 +00:00
|
|
|
}, function (error) {
|
2018-08-20 10:12:43 +00:00
|
|
|
reject(error);
|
2018-06-11 10:08:50 +00:00
|
|
|
});
|
2018-08-20 10:12:43 +00:00
|
|
|
});
|
2018-07-27 18:56:11 +00:00
|
|
|
}
|
2018-06-06 09:57:41 +00:00
|
|
|
}
|
2018-06-11 10:08:50 +00:00
|
|
|
|
2018-08-20 10:12:43 +00:00
|
|
|
|
2018-06-11 10:08:50 +00:00
|
|
|
/**
|
2018-07-10 12:32:26 +00:00
|
|
|
* List of algorithms supported for key generation. Please refer to the gnupg
|
|
|
|
* documentation for details
|
2018-06-11 10:08:50 +00:00
|
|
|
*/
|
|
|
|
const supportedKeyAlgos = [
|
|
|
|
'default',
|
2018-06-19 07:26:01 +00:00
|
|
|
'rsa', 'rsa2048', 'rsa3072', 'rsa4096',
|
|
|
|
'dsa', 'dsa2048', 'dsa3072', 'dsa4096',
|
|
|
|
'elg', 'elg2048', 'elg3072', 'elg4096',
|
2018-06-11 10:08:50 +00:00
|
|
|
'ed25519',
|
2018-06-19 07:26:01 +00:00
|
|
|
'cv25519',
|
|
|
|
'brainpoolP256r1', 'brainpoolP384r1', 'brainpoolP512r1',
|
|
|
|
'NIST P-256', 'NIST P-384', 'NIST P-521'
|
2018-06-11 10:08:50 +00:00
|
|
|
];
|