-
Notifications
You must be signed in to change notification settings - Fork 108
/
get-key-info.js
60 lines (50 loc) · 1.74 KB
/
get-key-info.js
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
/*
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: MPL-2.0
*
* 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 file incorporates work covered by the following copyright and
* permission notice:
*
* WildDuck Mail Agent is licensed under the European Union Public License 1.2 or later.
* https://github.com/nodemailer/wildduck
*/
const Boom = require('@hapi/boom');
const openpgp = require('openpgp/dist/node/openpgp.js');
const tools = require('wildduck/lib/tools');
const i18n = require('#helpers/i18n');
// <https://github.com/nodemailer/wildduck/blob/a15878c7d709473c5b0d4eec2062e9425c9b5e31/lib/api/users.js#L2480>
async function getKeyInfo(pubKeyArmored, locale = i18n.config.defaultLocale) {
if (!pubKeyArmored) {
return false;
}
const pubKey = await openpgp.readKey({
armoredKey: tools.prepareArmoredPubKey(pubKeyArmored),
config: { tolerant: true }
});
if (!pubKey)
throw Boom.badRequest(i18n.translateError('FAILED_TO_PROCESS_PUBLIC_KEY'));
const fingerprint = pubKey.getFingerprint();
const { name, address } = tools.getPGPUserId(pubKey);
const ciphertext = await openpgp.encrypt({
message: await openpgp.createMessage({ text: 'Hello, World!' }),
encryptionKeys: pubKey, // for encryption
format: 'armored',
config: { minRSABits: 1024 }
});
if (ciphertext.startsWith('-----BEGIN PGP MESSAGE')) {
// everything checks out
return {
name,
address,
fingerprint
};
}
throw Boom.badRequest(
i18n.translateError('FAILED_TO_VERIFY_PUBLIC_KEY', locale)
);
}
module.exports = getKeyInfo;