NeoMutt  2023-03-22-27-g3cb248
Teaching an old dog new tricks
DOXYGEN
gpgme.c File Reference

Autocrypt GPGME handler. More...

#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"
+ Include dependency graph for gpgme.c:

Go to the source code of this file.

Functions

static int create_gpgme_context (gpgme_ctx_t *ctx)
 Create a GPGME context. More...
 
int mutt_autocrypt_gpgme_init (void)
 Initialise GPGME. More...
 
static int export_keydata (gpgme_ctx_t ctx, gpgme_key_t key, struct Buffer *keydata)
 Export Key data from GPGME into a Buffer. More...
 
int mutt_autocrypt_gpgme_create_key (struct Address *addr, struct Buffer *keyid, struct Buffer *keydata)
 Create a GPGME key. More...
 
int mutt_autocrypt_gpgme_select_key (struct Buffer *keyid, struct Buffer *keydata)
 Select a Autocrypt key. More...
 
int mutt_autocrypt_gpgme_select_or_create_key (struct Address *addr, struct Buffer *keyid, struct Buffer *keydata)
 Ask the user to select or create an Autocrypt key. More...
 
int mutt_autocrypt_gpgme_import_key (const char *keydata, struct Buffer *keyid)
 Read a key from GPGME. More...
 
bool mutt_autocrypt_gpgme_is_valid_key (const char *keyid)
 Is a key id valid? More...
 

Detailed Description

Autocrypt GPGME handler.

Authors
  • Kevin J. McCarthy

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/.

Definition in file gpgme.c.

Function Documentation

◆ create_gpgme_context()

static int create_gpgme_context ( gpgme_ctx_t *  ctx)
static

Create a GPGME context.

Parameters
ctxGPGME context to initialise
Return values
0Success
-1Error

Definition at line 48 of file gpgme.c.

49{
50 const char *const c_autocrypt_dir = cs_subset_path(NeoMutt->sub, "autocrypt_dir");
51 gpgme_error_t err = gpgme_new(ctx);
52 if (!err)
53 err = gpgme_ctx_set_engine_info(*ctx, GPGME_PROTOCOL_OpenPGP, NULL, c_autocrypt_dir);
54 if (err)
55 {
56 mutt_error(_("error creating GPGME context: %s"), gpgme_strerror(err));
57 return -1;
58 }
59
60 return 0;
61}
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
#define mutt_error(...)
Definition: logging.h:87
#define _(a)
Definition: message.h:28
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_init()

int mutt_autocrypt_gpgme_init ( void  )

Initialise GPGME.

Return values
0Always

Definition at line 67 of file gpgme.c.

68{
70 return 0;
71}
void pgp_gpgme_init(void)
Implements CryptModuleSpecs::init() -.
Definition: crypt_gpgme.c:3750
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ export_keydata()

static int export_keydata ( gpgme_ctx_t  ctx,
gpgme_key_t  key,
struct Buffer keydata 
)
static

Export Key data from GPGME into a Buffer.

Parameters
ctxGPGME context
keyGPGME key
keydataBuffer for results
Return values
0Success
-1Error

Definition at line 81 of file gpgme.c.

82{
83 int rc = -1;
84 gpgme_data_t dh = NULL;
85 gpgme_key_t export_keys[2] = { 0 };
86 size_t export_data_len;
87
88 if (gpgme_data_new(&dh))
89 goto cleanup;
90
91 /* This doesn't seem to work */
92#if 0
93 if (gpgme_data_set_encoding (dh, GPGME_DATA_ENCODING_BASE64))
94 goto cleanup;
95#endif
96
97 export_keys[0] = key;
98 export_keys[1] = NULL;
99 if (gpgme_op_export_keys(ctx, export_keys, GPGME_EXPORT_MODE_MINIMAL, dh))
100 goto cleanup;
101
102 char *export_data = gpgme_data_release_and_get_mem(dh, &export_data_len);
103 dh = NULL;
104
105 mutt_b64_buffer_encode(keydata, export_data, export_data_len);
106 gpgme_free(export_data);
107 export_data = NULL;
108
109 rc = 0;
110
111cleanup:
112 gpgme_data_release(dh);
113 return rc;
114}
size_t mutt_b64_buffer_encode(struct Buffer *buf, const char *in, size_t len)
Convert raw bytes to null-terminated base64 string.
Definition: base64.c:199
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_create_key()

int mutt_autocrypt_gpgme_create_key ( struct Address addr,
struct Buffer keyid,
struct Buffer keydata 
)

Create a GPGME key.

Parameters
addrEmail Address
keyidKey id
keydataKey data
Return values
0Success
-1Error

Definition at line 155 of file gpgme.c.

157{
158 int rc = -1;
159 gpgme_ctx_t ctx = NULL;
160 gpgme_genkey_result_t keyresult = NULL;
161 gpgme_key_t primary_key = NULL;
162 struct Buffer *buf = mutt_buffer_pool_get();
163
164 /* GPGME says addresses should not be in idna form */
165 struct Address *copy = mutt_addr_copy(addr);
166 mutt_addr_to_local(copy);
167 mutt_addr_write(buf, copy, false);
168 mutt_addr_free(&copy);
169
170 if (create_gpgme_context(&ctx))
171 goto cleanup;
172
173 /* L10N: Message displayed just before a GPG key is generated for a created
174 autocrypt account. */
175 mutt_message(_("Generating autocrypt key..."));
176
177 /* Primary key */
178 gpgme_error_t err = gpgme_op_createkey(ctx, mutt_buffer_string(buf),
179 "ed25519", 0, 0, NULL,
180 GPGME_CREATE_NOPASSWD | GPGME_CREATE_FORCE |
181 GPGME_CREATE_NOEXPIRE);
182 if (err)
183 {
184 /* L10N: GPGME was unable to generate a key for some reason.
185 %s is the error message returned by GPGME. */
186 mutt_error(_("Error creating autocrypt key: %s"), gpgme_strerror(err));
187 goto cleanup;
188 }
189 keyresult = gpgme_op_genkey_result(ctx);
190 if (!keyresult->fpr)
191 goto cleanup;
192 mutt_buffer_strcpy(keyid, keyresult->fpr);
193 mutt_debug(LL_DEBUG1, "Generated key with id %s\n", mutt_buffer_string(keyid));
194
195 /* Get gpgme_key_t to create the secondary key and export keydata */
196 err = gpgme_get_key(ctx, mutt_buffer_string(keyid), &primary_key, 0);
197 if (err)
198 goto cleanup;
199
200 /* Secondary key */
201 err = gpgme_op_createsubkey(ctx, primary_key, "cv25519", 0, 0,
202 GPGME_CREATE_NOPASSWD | GPGME_CREATE_NOEXPIRE);
203 if (err)
204 {
205 mutt_error(_("Error creating autocrypt key: %s"), gpgme_strerror(err));
206 goto cleanup;
207 }
208
209 /* get keydata */
210 if (export_keydata(ctx, primary_key, keydata))
211 goto cleanup;
212 mutt_debug(LL_DEBUG1, "key has keydata *%s*\n", mutt_buffer_string(keydata));
213
214 rc = 0;
215
216cleanup:
217 gpgme_key_unref(primary_key);
218 gpgme_release(ctx);
220 return rc;
221}
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition: address.c:444
size_t mutt_addr_write(struct Buffer *buf, struct Address *addr, bool display)
Write a single Address to a buffer.
Definition: address.c:1032
struct Address * mutt_addr_copy(const struct Address *addr)
Copy the real address.
Definition: address.c:724
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1324
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:365
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:78
static int create_gpgme_context(gpgme_ctx_t *ctx)
Create a GPGME context.
Definition: gpgme.c:48
static int export_keydata(gpgme_ctx_t ctx, gpgme_key_t key, struct Buffer *keydata)
Export Key data from GPGME into a Buffer.
Definition: gpgme.c:81
#define mutt_message(...)
Definition: logging.h:86
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
An email address.
Definition: address.h:36
String manipulation buffer.
Definition: buffer.h:34
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_select_key()

int mutt_autocrypt_gpgme_select_key ( struct Buffer keyid,
struct Buffer keydata 
)

Select a Autocrypt key.

Parameters
[in]keyidKey id to select
[out]keydataBuffer for resulting Key data
Return values
0Success
-1Error

Definition at line 230 of file gpgme.c.

231{
232 int rc = -1;
233 gpgme_ctx_t ctx = NULL;
234 gpgme_key_t key = NULL;
235
236 OptAutocryptGpgme = true;
238 goto cleanup;
239
240 if (create_gpgme_context(&ctx))
241 goto cleanup;
242
243 if (gpgme_get_key(ctx, mutt_buffer_string(keyid), &key, 0))
244 goto cleanup;
245
246 if (key->revoked || key->expired || key->disabled || key->invalid ||
247 !key->can_encrypt || !key->can_sign)
248 {
249 /* L10N: After selecting a key for an autocrypt account,
250 this is displayed if the key was revoked/expired/disabled/invalid
251 or can't be used for both signing and encryption.
252 %s is the key fingerprint. */
253 mutt_error(_("The key %s is not usable for autocrypt"), mutt_buffer_string(keyid));
254 goto cleanup;
255 }
256
257 if (export_keydata(ctx, key, keydata))
258 goto cleanup;
259
260 rc = 0;
261
262cleanup:
263 OptAutocryptGpgme = false;
264 gpgme_key_unref(key);
265 gpgme_release(ctx);
266 return rc;
267}
int mutt_gpgme_select_secret_key(struct Buffer *keyid)
Select a private Autocrypt key for a new account.
Definition: crypt_gpgme.c:3569
bool OptAutocryptGpgme
(pseudo) use Autocrypt context inside ncrypt/crypt_gpgme.c
Definition: globals.c:67
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_select_or_create_key()

int mutt_autocrypt_gpgme_select_or_create_key ( struct Address addr,
struct Buffer keyid,
struct Buffer keydata 
)

Ask the user to select or create an Autocrypt key.

Parameters
addrEmail Address
keyidKey id
keydataKey data
Return values
0Success
-1Error

Definition at line 277 of file gpgme.c.

279{
280 int rc = -1;
281
282 /* L10N: During autocrypt account creation, this prompt asks the
283 user whether they want to create a new GPG key for the account,
284 or select an existing account from the keyring. */
285 const char *prompt = _("(c)reate new, or (s)elect existing GPG key?");
286 /* L10N: The letters corresponding to the
287 "(c)reate new, or (s)elect existing GPG key?" prompt. */
288 const char *letters = _("cs");
289
290 int choice = mutt_multi_choice(prompt, letters);
291 switch (choice)
292 {
293 case 2: /* select existing */
294 rc = mutt_autocrypt_gpgme_select_key(keyid, keydata);
295 if (rc == 0)
296 break;
297
298 /* L10N: During autocrypt account creation, if selecting an existing key fails
299 for some reason, we prompt to see if they want to create a key instead. */
300 if (mutt_yesorno(_("Create a new GPG key for this account, instead?"), MUTT_YES) != MUTT_YES)
301 break;
302 /* fallthrough */
303
304 case 1: /* create new */
305 rc = mutt_autocrypt_gpgme_create_key(addr, keyid, keydata);
306 }
307
308 return rc;
309}
int mutt_autocrypt_gpgme_create_key(struct Address *addr, struct Buffer *keyid, struct Buffer *keydata)
Create a GPGME key.
Definition: gpgme.c:155
int mutt_autocrypt_gpgme_select_key(struct Buffer *keyid, struct Buffer *keydata)
Select a Autocrypt key.
Definition: gpgme.c:230
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:39
enum QuadOption mutt_yesorno(const char *msg, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:194
int mutt_multi_choice(const char *prompt, const char *letters)
Offer the user a multiple choice question.
Definition: question.c:54
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_import_key()

int mutt_autocrypt_gpgme_import_key ( const char *  keydata,
struct Buffer keyid 
)

Read a key from GPGME.

Parameters
keydataBuffer for key data
keyidBuffer for key id
Return values
0Success
-1Error

Definition at line 318 of file gpgme.c.

319{
320 int rc = -1;
321 gpgme_ctx_t ctx = NULL;
322 gpgme_data_t dh = NULL;
323
324 if (create_gpgme_context(&ctx))
325 goto cleanup;
326
327 struct Buffer *raw_keydata = mutt_buffer_pool_get();
328 if (!mutt_b64_buffer_decode(raw_keydata, keydata))
329 goto cleanup;
330
331 if (gpgme_data_new_from_mem(&dh, mutt_buffer_string(raw_keydata),
332 mutt_buffer_len(raw_keydata), 0))
333 {
334 goto cleanup;
335 }
336
337 if (gpgme_op_import(ctx, dh))
338 goto cleanup;
339
340 gpgme_import_result_t result = gpgme_op_import_result(ctx);
341 if (!result->imports || !result->imports->fpr)
342 goto cleanup;
343 mutt_buffer_strcpy(keyid, result->imports->fpr);
344
345 rc = 0;
346
347cleanup:
348 gpgme_data_release(dh);
349 gpgme_release(ctx);
350 mutt_buffer_pool_release(&raw_keydata);
351 return rc;
352}
int mutt_b64_buffer_decode(struct Buffer *buf, const char *in)
Convert null-terminated base64 string to raw bytes.
Definition: base64.c:217
size_t mutt_buffer_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:409
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_is_valid_key()

bool mutt_autocrypt_gpgme_is_valid_key ( const char *  keyid)

Is a key id valid?

Parameters
keyidKey id to check
Return values
trueKey id is valid

Definition at line 359 of file gpgme.c.

360{
361 bool rc = false;
362 gpgme_ctx_t ctx = NULL;
363 gpgme_key_t key = NULL;
364
365 if (!keyid)
366 return false;
367
368 if (create_gpgme_context(&ctx))
369 goto cleanup;
370
371 if (gpgme_get_key(ctx, keyid, &key, 0))
372 goto cleanup;
373
374 rc = true;
375 if (key->revoked || key->expired || key->disabled || key->invalid || !key->can_encrypt)
376 rc = false;
377
378cleanup:
379 gpgme_key_unref(key);
380 gpgme_release(ctx);
381 return rc;
382}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: