2016-05-26 07:58:50 +00:00
|
|
|
/**
|
|
|
|
* Mailvelope - secure email with OpenPGP encryption for Webmail
|
|
|
|
* Copyright (C) 2016 Mailvelope GmbH
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License version 3
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
const log = require('npmlog');
|
|
|
|
const util = require('./util');
|
2016-06-01 06:59:25 +00:00
|
|
|
const tpl = require('../email/templates.json');
|
2016-05-27 17:57:48 +00:00
|
|
|
|
2016-05-26 11:45:32 +00:00
|
|
|
/**
|
|
|
|
* Database documents have the format:
|
|
|
|
* {
|
2016-05-27 17:57:48 +00:00
|
|
|
* _id: "02C134D079701934", // the 16 byte key id in uppercase hex
|
2016-05-26 11:45:32 +00:00
|
|
|
* publicKeyArmored: "-----BEGIN PGP PUBLIC KEY BLOCK----- ... -----END PGP PUBLIC KEY BLOCK-----"
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
const DB_TYPE = 'publickey';
|
|
|
|
|
2016-05-26 07:58:50 +00:00
|
|
|
/**
|
2016-05-28 13:17:46 +00:00
|
|
|
* A service that handlers PGP public keys queries to the database
|
2016-05-26 07:58:50 +00:00
|
|
|
*/
|
|
|
|
class PublicKey {
|
|
|
|
|
|
|
|
/**
|
2016-05-28 13:17:46 +00:00
|
|
|
* Create an instance of the service
|
2016-05-27 17:57:48 +00:00
|
|
|
* @param {Object} openpgp An instance of OpenPGP.js
|
|
|
|
* @param {Object} mongo An instance of the MongoDB client
|
|
|
|
* @param {Object} email An instance of the Email Sender
|
2016-06-01 10:28:37 +00:00
|
|
|
* @param {Object} userId An instance of the UserId service
|
2016-05-26 07:58:50 +00:00
|
|
|
*/
|
2016-06-01 10:28:37 +00:00
|
|
|
constructor(openpgp, mongo, email, userId) {
|
2016-05-27 17:57:48 +00:00
|
|
|
this._openpgp = openpgp;
|
2016-05-26 07:58:50 +00:00
|
|
|
this._mongo = mongo;
|
2016-05-27 17:57:48 +00:00
|
|
|
this._email = email;
|
2016-06-01 10:28:37 +00:00
|
|
|
this._userId = userId;
|
2016-05-26 07:58:50 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
/**
|
|
|
|
* Persist a new public key
|
2016-05-29 14:47:45 +00:00
|
|
|
* @param {String} publicKeyArmored The ascii armored pgp key block
|
|
|
|
* @param {String} primaryEmail (optional) The key's primary email address
|
|
|
|
* @param {Object} origin Required for links to the keyserver e.g. { protocol:'https', host:'openpgpkeys@example.com' }
|
2016-05-27 17:57:48 +00:00
|
|
|
* @yield {undefined}
|
|
|
|
*/
|
|
|
|
*put(options) {
|
|
|
|
// parse key block
|
2016-05-29 14:47:45 +00:00
|
|
|
let publicKeyArmored = options.publicKeyArmored, primaryEmail = options.primaryEmail, origin = options.origin;
|
2016-06-01 10:28:37 +00:00
|
|
|
publicKeyArmored = publicKeyArmored.trim(); // remove whitespace
|
|
|
|
let params = this._parseKey(publicKeyArmored);
|
2016-05-27 17:57:48 +00:00
|
|
|
// check for existing verfied key by id or email addresses
|
2016-06-01 10:28:37 +00:00
|
|
|
let verified = yield this._userId.getVerfied(params);
|
2016-05-27 17:57:48 +00:00
|
|
|
if (verified) {
|
2016-06-01 10:28:37 +00:00
|
|
|
util.throw(304, 'Key for this user already exists');
|
2016-05-27 17:57:48 +00:00
|
|
|
}
|
2016-06-01 10:28:37 +00:00
|
|
|
// store key in database
|
|
|
|
let userIds = yield this._persisKey(publicKeyArmored, params);
|
2016-05-28 08:49:26 +00:00
|
|
|
// send mails to verify user ids (send only one if primary email is provided)
|
2016-06-01 10:28:37 +00:00
|
|
|
yield this._sendVerifyEmail(userIds, primaryEmail, origin);
|
2016-05-27 17:57:48 +00:00
|
|
|
}
|
2016-05-26 11:45:32 +00:00
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
/**
|
|
|
|
* Parse an ascii armored pgp key block and get its parameters.
|
2016-06-01 10:28:37 +00:00
|
|
|
* @param {String} publicKeyArmored ascii armored pgp key block
|
|
|
|
* @return {Object} key's id and user ids
|
2016-05-27 17:57:48 +00:00
|
|
|
*/
|
2016-06-01 10:28:37 +00:00
|
|
|
_parseKey(publicKeyArmored) {
|
2016-05-27 17:57:48 +00:00
|
|
|
let keys, userIds = [];
|
|
|
|
try {
|
|
|
|
keys = this._openpgp.key.readArmored(publicKeyArmored).keys;
|
|
|
|
} catch(e) {
|
|
|
|
log.error('public-key', 'Failed to parse PGP key:\n%s', publicKeyArmored, e);
|
2016-05-28 21:31:25 +00:00
|
|
|
util.throw(500, 'Failed to parse PGP key');
|
2016-05-27 17:57:48 +00:00
|
|
|
}
|
|
|
|
// get key user ids
|
|
|
|
keys.forEach(key => userIds = userIds.concat(key.getUserIds()));
|
|
|
|
userIds = util.deDup(userIds);
|
|
|
|
// get key id
|
|
|
|
return {
|
|
|
|
keyid: keys[0].primaryKey.getKeyId().toHex().toUpperCase(),
|
|
|
|
userIds: util.parseUserIds(userIds)
|
|
|
|
};
|
2016-05-26 11:45:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 10:28:37 +00:00
|
|
|
/**
|
|
|
|
* Persist the public key and its user ids in the database.
|
|
|
|
* @param {String} publicKeyArmored ascii armored pgp key block
|
|
|
|
* @param {Object} params public key parameters
|
|
|
|
* @yield {Array} The persisted user id documents
|
|
|
|
*/
|
|
|
|
*_persisKey(publicKeyArmored, params) {
|
|
|
|
// delete old/unverified key and user ids with the same key id
|
|
|
|
yield this.remove({ keyid:params.keyid });
|
|
|
|
// persist new user ids
|
|
|
|
let userIds = yield this._userId.batch(params);
|
|
|
|
// persist new key
|
|
|
|
let r = yield this._mongo.create({ _id:params.keyid, publicKeyArmored }, DB_TYPE);
|
|
|
|
if (r.insertedCount !== 1) {
|
|
|
|
// rollback user ids
|
|
|
|
yield this.remove({ keyid:params.keyid });
|
|
|
|
util.throw(500, 'Failed to persist key');
|
|
|
|
}
|
|
|
|
return userIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send verification emails to the public keys user ids for verification.
|
|
|
|
* If a primary email address is provided only one email will be sent.
|
|
|
|
* @param {Array} userIds user id documents containg the verification nonces
|
|
|
|
* @param {string} primaryEmail the public key's primary email address
|
|
|
|
* @param {Object} origin the server's origin (required for email links)
|
|
|
|
* @yield {undefined}
|
|
|
|
*/
|
|
|
|
*_sendVerifyEmail(userIds, primaryEmail, origin) {
|
|
|
|
let primaryUserId = userIds.find(uid => uid.email === primaryEmail);
|
|
|
|
if (primaryUserId) {
|
|
|
|
userIds = [primaryUserId];
|
|
|
|
}
|
|
|
|
for (let userId of userIds) {
|
|
|
|
yield this._email.send({ template:tpl.verifyKey, userId, origin });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
/**
|
|
|
|
* Fetch a verified public key from the database. Either the key id or the
|
|
|
|
* email address muss be provided.
|
2016-05-31 09:02:27 +00:00
|
|
|
* @param {String} keyid (optional) The public key id
|
|
|
|
* @param {String} email (optional) The user's email address
|
|
|
|
* @yield {Object} The public key document
|
2016-05-27 17:57:48 +00:00
|
|
|
*/
|
|
|
|
*get(options) {
|
|
|
|
let keyid = options.keyid, email = options.email;
|
2016-06-01 10:28:37 +00:00
|
|
|
let verified = yield this._userId.getVerfied({
|
2016-05-27 17:57:48 +00:00
|
|
|
keyid: keyid ? keyid.toUpperCase() : undefined,
|
|
|
|
userIds: email ? [{ email:email.toLowerCase() }] : undefined
|
|
|
|
});
|
2016-05-28 08:49:26 +00:00
|
|
|
if (!verified) {
|
2016-05-28 21:31:25 +00:00
|
|
|
util.throw(404, 'Key not found');
|
2016-05-27 17:57:48 +00:00
|
|
|
}
|
2016-05-28 08:49:26 +00:00
|
|
|
return yield this._mongo.get({ _id:verified.keyid }, DB_TYPE);
|
2016-05-26 11:45:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 06:59:25 +00:00
|
|
|
/**
|
|
|
|
* Request removal of the public key by flagging all user ids and sending
|
|
|
|
* a verification email to the primary email address. Only one email
|
|
|
|
* needs to sent to a single user id to authenticate removal of all user ids
|
|
|
|
* that belong the a certain key id.
|
|
|
|
* @param {String} keyid (optional) The public key id
|
|
|
|
* @param {String} email (optional) The user's email address
|
|
|
|
* @param {Object} origin Required for links to the keyserver e.g. { protocol:'https', host:'openpgpkeys@example.com' }
|
|
|
|
* @yield {undefined}
|
|
|
|
*/
|
|
|
|
*requestRemove(options) {
|
|
|
|
let keyid = options.keyid, email = options.email, origin = options.origin;
|
2016-06-01 10:28:37 +00:00
|
|
|
let userIds = yield this._userId.flagForRemove({ keyid, email }, DB_TYPE);
|
|
|
|
if (!userIds.length) {
|
|
|
|
util.throw(404, 'User id not found');
|
|
|
|
}
|
2016-06-01 06:59:25 +00:00
|
|
|
for (let userId of userIds) {
|
|
|
|
yield this._email.send({ template:tpl.verifyRemove, userId, origin });
|
|
|
|
}
|
2016-05-26 11:45:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 06:59:25 +00:00
|
|
|
/**
|
|
|
|
* Verify the removal of the user's key id by proving knowledge of the nonce.
|
|
|
|
* Also deletes all user id documents of that key id.
|
|
|
|
* @param {string} keyid public key id
|
|
|
|
* @param {string} nonce The verification nonce proving email address ownership
|
|
|
|
* @yield {undefined}
|
|
|
|
*/
|
|
|
|
*verifyRemove(options) {
|
|
|
|
let keyid = options.keyid, nonce = options.nonce;
|
|
|
|
let flagged = yield this._userId.getFlaggedForRemove({ keyid, nonce });
|
|
|
|
if (!flagged) {
|
|
|
|
util.throw(404, 'User id not found');
|
|
|
|
}
|
|
|
|
yield this.remove({ keyid });
|
2016-05-27 17:57:48 +00:00
|
|
|
}
|
2016-05-26 11:45:32 +00:00
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
/**
|
|
|
|
* Delete a public key document and its corresponding user id documents.
|
2016-05-31 09:02:27 +00:00
|
|
|
* @param {String} keyid The key id
|
2016-05-27 17:57:48 +00:00
|
|
|
* @yield {undefined}
|
|
|
|
*/
|
|
|
|
*remove(options) {
|
2016-06-01 06:59:25 +00:00
|
|
|
let keyid = options.keyid;
|
2016-05-27 17:57:48 +00:00
|
|
|
// remove key document
|
2016-06-01 06:59:25 +00:00
|
|
|
yield this._mongo.remove({ _id:keyid }, DB_TYPE);
|
2016-05-27 17:57:48 +00:00
|
|
|
// remove matching user id documents
|
2016-06-01 10:28:37 +00:00
|
|
|
yield this._userId.remove({ keyid });
|
2016-05-26 11:45:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PublicKey;
|