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-04-25 17:45:39 +00:00
|
|
|
import {createMessage} from './Message'
|
2018-04-23 15:18:46 +00:00
|
|
|
import {GPGME_Key} from './Key'
|
2018-05-03 16:03:22 +00:00
|
|
|
import { isFingerprint } from './Helpers';
|
2018-04-25 13:59:36 +00:00
|
|
|
import { gpgme_error } from './Errors';
|
2018-04-26 15:13:34 +00:00
|
|
|
import { Connection } from './Connection';
|
2018-04-23 15:18:46 +00:00
|
|
|
|
|
|
|
export class GPGME_Keyring {
|
2018-04-24 16:44:30 +00:00
|
|
|
constructor(connection){
|
|
|
|
this.connection = connection;
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 16:44:30 +00:00
|
|
|
set connection(connection){
|
|
|
|
if (!this._connection && connection instanceof Connection){
|
|
|
|
this._connection = connection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
get connection(){
|
|
|
|
if (this._connection instanceof Connection){
|
|
|
|
if (this._connection.isConnected){
|
|
|
|
return this._connection;
|
|
|
|
}
|
2018-04-25 13:59:36 +00:00
|
|
|
return gpgme_error('CONN_DISCONNECTED');
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
2018-04-25 13:59:36 +00:00
|
|
|
return gpgme_error('CONN_NO_CONNECT');
|
2018-04-23 15:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String} (optional) pattern A pattern to search for, in userIds or KeyIds
|
|
|
|
* @param {Boolean} (optional) Include listing of secret keys
|
|
|
|
* @returns {Promise.<Array<GPGME_Key>>}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getKeys(pattern, include_secret){
|
2018-04-25 17:45:39 +00:00
|
|
|
let msg = createMessage('listkeys');
|
|
|
|
if (msg instanceof Error){
|
|
|
|
return Promise.reject(msg);
|
|
|
|
}
|
2018-04-23 15:18:46 +00:00
|
|
|
if (pattern && typeof(pattern) === 'string'){
|
|
|
|
msg.setParameter('pattern', pattern);
|
|
|
|
}
|
|
|
|
if (include_secret){
|
|
|
|
msg.setParameter('with-secret', true);
|
|
|
|
}
|
2018-05-03 12:12:10 +00:00
|
|
|
let me = this;
|
2018-04-23 15:18:46 +00:00
|
|
|
|
2018-04-24 16:44:30 +00:00
|
|
|
this.connection.post(msg).then(function(result){
|
2018-04-23 15:18:46 +00:00
|
|
|
let fpr_list = [];
|
|
|
|
let resultset = [];
|
|
|
|
if (!Array.isArray(result.keys)){
|
|
|
|
//TODO check assumption keys = Array<String fingerprints>
|
|
|
|
fpr_list = [result.keys];
|
|
|
|
} else {
|
|
|
|
fpr_list = result.keys;
|
|
|
|
}
|
|
|
|
for (let i=0; i < fpr_list.length; i++){
|
2018-05-03 12:12:10 +00:00
|
|
|
let newKey = new GPGME_Key(fpr_list[i], me._connection);
|
2018-04-23 15:18:46 +00:00
|
|
|
if (newKey instanceof GPGME_Key){
|
|
|
|
resultset.push(newKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.resolve(resultset);
|
2018-04-27 18:03:09 +00:00
|
|
|
}, function(error){
|
|
|
|
//TODO error handling
|
2018-04-23 15:18:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} flags subset filter expecting at least one of the
|
|
|
|
* filters described below. True will filter on the condition, False will
|
|
|
|
* reverse the filter, if not present or undefined, the filter will not be
|
|
|
|
* considered. Please note that some combination may not make sense
|
|
|
|
* @param {Boolean} flags.secret Only Keys containing a secret part.
|
|
|
|
* @param {Boolean} flags.revoked revoked Keys only
|
|
|
|
* @param {Boolean} flags.expired Expired Keys only
|
|
|
|
* @param {String} (optional) pattern A pattern to search for, in userIds or KeyIds
|
|
|
|
* @returns {Promise Array<GPGME_Key>}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getSubset(flags, pattern){
|
|
|
|
if (flags === undefined) {
|
|
|
|
throw('ERR_WRONG_PARAM');
|
|
|
|
};
|
|
|
|
let secretflag = false;
|
|
|
|
if (flags.hasOwnProperty(secret) && flags.secret){
|
|
|
|
secretflag = true;
|
|
|
|
}
|
|
|
|
this.getKeys(pattern, secretflag).then(function(queryset){
|
|
|
|
let resultset = [];
|
|
|
|
for (let i=0; i < queryset.length; i++ ){
|
|
|
|
let conditions = [];
|
|
|
|
let anticonditions = [];
|
|
|
|
if (secretflag === true){
|
|
|
|
conditions.push('hasSecret');
|
|
|
|
} else if (secretflag === false){
|
|
|
|
anticonditions.push('hasSecret');
|
|
|
|
}
|
2018-04-25 13:59:36 +00:00
|
|
|
/**
|
2018-04-23 15:18:46 +00:00
|
|
|
if (flags.defaultKey === true){
|
|
|
|
conditions.push('isDefault');
|
|
|
|
} else if (flags.defaultKey === false){
|
|
|
|
anticonditions.push('isDefault');
|
|
|
|
}
|
2018-04-25 13:59:36 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* if (flags.valid === true){
|
2018-04-23 15:18:46 +00:00
|
|
|
anticonditions.push('isInvalid');
|
|
|
|
} else if (flags.valid === false){
|
|
|
|
conditions.push('isInvalid');
|
|
|
|
}
|
2018-04-25 13:59:36 +00:00
|
|
|
*/
|
2018-04-23 15:18:46 +00:00
|
|
|
if (flags.revoked === true){
|
|
|
|
conditions.push('isRevoked');
|
|
|
|
} else if (flags.revoked === false){
|
|
|
|
anticonditions.push('isRevoked');
|
|
|
|
}
|
|
|
|
if (flags.expired === true){
|
|
|
|
conditions.push('isExpired');
|
|
|
|
} else if (flags.expired === false){
|
|
|
|
anticonditions.push('isExpired');
|
|
|
|
}
|
|
|
|
let decision = undefined;
|
|
|
|
for (let con = 0; con < conditions.length; con ++){
|
|
|
|
if (queryset[i][conditions[con]] !== true){
|
|
|
|
decision = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let acon = 0; acon < anticonditions.length; acon ++){
|
|
|
|
if (queryset[i][anticonditions[acon]] === true){
|
|
|
|
decision = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (decision !== false){
|
|
|
|
resultset.push(queryset[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.resolve(resultset);
|
2018-04-27 18:03:09 +00:00
|
|
|
}, function(error){
|
|
|
|
//TODO error handling
|
2018-04-23 15:18:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|