forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpgme.c
380 lines (324 loc) · 9.67 KB
/
gpgme.c
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
/**
* @file
* Autocrypt GPGME handler
*
* @authors
* Copyright (C) 2019 Kevin J. McCarthy <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page autocrypt_gpgme Autocrypt GPGME handler
*
* Autocrypt GPGME handler
*/
#include "config.h"
#include <stddef.h>
#include <gpgme.h>
#include <stdbool.h>
#include "private.h"
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "ncrypt/lib.h"
#include "question/lib.h"
#include "globals.h"
/**
* create_gpgme_context - Create a GPGME context
* @param ctx GPGME context to initialise
* @retval 0 Success
* @retval -1 Error
*/
static int create_gpgme_context(gpgme_ctx_t *ctx)
{
const char *const c_autocrypt_dir = cs_subset_path(NeoMutt->sub, "autocrypt_dir");
gpgme_error_t err = gpgme_new(ctx);
if (!err)
err = gpgme_ctx_set_engine_info(*ctx, GPGME_PROTOCOL_OpenPGP, NULL, c_autocrypt_dir);
if (err)
{
mutt_error(_("error creating GPGME context: %s"), gpgme_strerror(err));
return -1;
}
return 0;
}
/**
* mutt_autocrypt_gpgme_init - Initialise GPGME
* @retval 0 Always
*/
int mutt_autocrypt_gpgme_init(void)
{
pgp_gpgme_init();
return 0;
}
/**
* export_keydata - Export Key data from GPGME into a Buffer
* @param ctx GPGME context
* @param key GPGME key
* @param keydata Buffer for results
* @retval 0 Success
* @retval -1 Error
*/
static int export_keydata(gpgme_ctx_t ctx, gpgme_key_t key, struct Buffer *keydata)
{
int rc = -1;
gpgme_data_t dh = NULL;
gpgme_key_t export_keys[2] = { 0 };
size_t export_data_len;
if (gpgme_data_new(&dh))
goto cleanup;
/* This doesn't seem to work */
#if 0
if (gpgme_data_set_encoding (dh, GPGME_DATA_ENCODING_BASE64))
goto cleanup;
#endif
export_keys[0] = key;
export_keys[1] = NULL;
if (gpgme_op_export_keys(ctx, export_keys, GPGME_EXPORT_MODE_MINIMAL, dh))
goto cleanup;
char *export_data = gpgme_data_release_and_get_mem(dh, &export_data_len);
dh = NULL;
mutt_b64_buffer_encode(keydata, export_data, export_data_len);
gpgme_free(export_data);
export_data = NULL;
rc = 0;
cleanup:
gpgme_data_release(dh);
return rc;
}
#if 0
/**
* mutt_autocrypt_gpgme_export_key - Export a GPGME key
* @param keyid GPGME Key id
* @param keydata Buffer for results
* @retval 0 Success
* @retval -1 Error
*/
int mutt_autocrypt_gpgme_export_key(const char *keyid, struct Buffer *keydata)
{
int rc = -1;
gpgme_ctx_t ctx = NULL;
gpgme_key_t key = NULL;
if (create_gpgme_context(&ctx))
goto cleanup;
if (gpgme_get_key(ctx, keyid, &key, 0))
goto cleanup;
if (export_keydata(ctx, key, keydata))
goto cleanup;
rc = 0;
cleanup:
gpgme_key_unref(key);
gpgme_release(ctx);
return rc;
}
#endif
/**
* mutt_autocrypt_gpgme_create_key - Create a GPGME key
* @param addr Email Address
* @param keyid Key id
* @param keydata Key data
* @retval 0 Success
* @retval -1 Error
*/
int mutt_autocrypt_gpgme_create_key(struct Address *addr, struct Buffer *keyid,
struct Buffer *keydata)
{
int rc = -1;
gpgme_ctx_t ctx = NULL;
gpgme_genkey_result_t keyresult = NULL;
gpgme_key_t primary_key = NULL;
struct Buffer *buf = buf_pool_get();
/* GPGME says addresses should not be in idna form */
struct Address *copy = mutt_addr_copy(addr);
mutt_addr_to_local(copy);
mutt_addr_write(buf, copy, false);
mutt_addr_free(©);
if (create_gpgme_context(&ctx))
goto cleanup;
/* L10N: Message displayed just before a GPG key is generated for a created
autocrypt account. */
mutt_message(_("Generating autocrypt key..."));
/* Primary key */
gpgme_error_t err = gpgme_op_createkey(ctx, buf_string(buf), "ed25519", 0, 0, NULL,
GPGME_CREATE_NOPASSWD | GPGME_CREATE_FORCE |
GPGME_CREATE_NOEXPIRE);
if (err)
{
/* L10N: GPGME was unable to generate a key for some reason.
%s is the error message returned by GPGME. */
mutt_error(_("Error creating autocrypt key: %s"), gpgme_strerror(err));
goto cleanup;
}
keyresult = gpgme_op_genkey_result(ctx);
if (!keyresult->fpr)
goto cleanup;
buf_strcpy(keyid, keyresult->fpr);
mutt_debug(LL_DEBUG1, "Generated key with id %s\n", buf_string(keyid));
/* Get gpgme_key_t to create the secondary key and export keydata */
err = gpgme_get_key(ctx, buf_string(keyid), &primary_key, 0);
if (err)
goto cleanup;
/* Secondary key */
err = gpgme_op_createsubkey(ctx, primary_key, "cv25519", 0, 0,
GPGME_CREATE_NOPASSWD | GPGME_CREATE_NOEXPIRE);
if (err)
{
mutt_error(_("Error creating autocrypt key: %s"), gpgme_strerror(err));
goto cleanup;
}
/* get keydata */
if (export_keydata(ctx, primary_key, keydata))
goto cleanup;
mutt_debug(LL_DEBUG1, "key has keydata *%s*\n", buf_string(keydata));
rc = 0;
cleanup:
gpgme_key_unref(primary_key);
gpgme_release(ctx);
buf_pool_release(&buf);
return rc;
}
/**
* mutt_autocrypt_gpgme_select_key - Select a Autocrypt key
* @param[in] keyid Key id to select
* @param[out] keydata Buffer for resulting Key data
* @retval 0 Success
* @retval -1 Error
*/
int mutt_autocrypt_gpgme_select_key(struct Buffer *keyid, struct Buffer *keydata)
{
int rc = -1;
gpgme_ctx_t ctx = NULL;
gpgme_key_t key = NULL;
OptAutocryptGpgme = true;
if (mutt_gpgme_select_secret_key(keyid))
goto cleanup;
if (create_gpgme_context(&ctx))
goto cleanup;
if (gpgme_get_key(ctx, buf_string(keyid), &key, 0))
goto cleanup;
if (key->revoked || key->expired || key->disabled || key->invalid ||
!key->can_encrypt || !key->can_sign)
{
/* L10N: After selecting a key for an autocrypt account,
this is displayed if the key was revoked/expired/disabled/invalid
or can't be used for both signing and encryption.
%s is the key fingerprint. */
mutt_error(_("The key %s is not usable for autocrypt"), buf_string(keyid));
goto cleanup;
}
if (export_keydata(ctx, key, keydata))
goto cleanup;
rc = 0;
cleanup:
OptAutocryptGpgme = false;
gpgme_key_unref(key);
gpgme_release(ctx);
return rc;
}
/**
* mutt_autocrypt_gpgme_select_or_create_key - Ask the user to select or create an Autocrypt key
* @param addr Email Address
* @param keyid Key id
* @param keydata Key data
* @retval 0 Success
* @retval -1 Error
*/
int mutt_autocrypt_gpgme_select_or_create_key(struct Address *addr, struct Buffer *keyid,
struct Buffer *keydata)
{
int rc = -1;
/* L10N: During autocrypt account creation, this prompt asks the
user whether they want to create a new GPG key for the account,
or select an existing account from the keyring. */
const char *prompt = _("(c)reate new, or (s)elect existing GPG key?");
/* L10N: The letters corresponding to the
"(c)reate new, or (s)elect existing GPG key?" prompt. */
const char *letters = _("cs");
int choice = mutt_multi_choice(prompt, letters);
switch (choice)
{
case 2: /* select existing */
rc = mutt_autocrypt_gpgme_select_key(keyid, keydata);
if (rc == 0)
break;
/* L10N: During autocrypt account creation, if selecting an existing key fails
for some reason, we prompt to see if they want to create a key instead. */
if (mutt_yesorno(_("Create a new GPG key for this account, instead?"), MUTT_YES) != MUTT_YES)
break;
/* fallthrough */
case 1: /* create new */
rc = mutt_autocrypt_gpgme_create_key(addr, keyid, keydata);
}
return rc;
}
/**
* mutt_autocrypt_gpgme_import_key - Read a key from GPGME
* @param keydata Buffer for key data
* @param keyid Buffer for key id
* @retval 0 Success
* @retval -1 Error
*/
int mutt_autocrypt_gpgme_import_key(const char *keydata, struct Buffer *keyid)
{
int rc = -1;
gpgme_ctx_t ctx = NULL;
gpgme_data_t dh = NULL;
if (create_gpgme_context(&ctx))
goto cleanup;
struct Buffer *raw_keydata = buf_pool_get();
if (!mutt_b64_buffer_decode(raw_keydata, keydata))
goto cleanup;
if (gpgme_data_new_from_mem(&dh, buf_string(raw_keydata), buf_len(raw_keydata), 0))
{
goto cleanup;
}
if (gpgme_op_import(ctx, dh))
goto cleanup;
gpgme_import_result_t result = gpgme_op_import_result(ctx);
if (!result->imports || !result->imports->fpr)
goto cleanup;
buf_strcpy(keyid, result->imports->fpr);
rc = 0;
cleanup:
gpgme_data_release(dh);
gpgme_release(ctx);
buf_pool_release(&raw_keydata);
return rc;
}
/**
* mutt_autocrypt_gpgme_is_valid_key - Is a key id valid?
* @param keyid Key id to check
* @retval true Key id is valid
*/
bool mutt_autocrypt_gpgme_is_valid_key(const char *keyid)
{
bool rc = false;
gpgme_ctx_t ctx = NULL;
gpgme_key_t key = NULL;
if (!keyid)
return false;
if (create_gpgme_context(&ctx))
goto cleanup;
if (gpgme_get_key(ctx, keyid, &key, 0))
goto cleanup;
rc = true;
if (key->revoked || key->expired || key->disabled || key->invalid || !key->can_encrypt)
rc = false;
cleanup:
gpgme_key_unref(key);
gpgme_release(ctx);
return rc;
}