NeoMutt  2022-04-29-178-g3b62e6
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 "options.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:4148
+ 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 char buf[1024] = { 0 };
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, sizeof(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, buf, "ed25519", 0, 0, NULL,
179 GPGME_CREATE_NOPASSWD | GPGME_CREATE_FORCE |
180 GPGME_CREATE_NOEXPIRE);
181 if (err)
182 {
183 /* L10N: GPGME was unable to generate a key for some reason.
184 %s is the error message returned by GPGME. */
185 mutt_error(_("Error creating autocrypt key: %s"), gpgme_strerror(err));
186 goto cleanup;
187 }
188 keyresult = gpgme_op_genkey_result(ctx);
189 if (!keyresult->fpr)
190 goto cleanup;
191 mutt_buffer_strcpy(keyid, keyresult->fpr);
192 mutt_debug(LL_DEBUG1, "Generated key with id %s\n", mutt_buffer_string(keyid));
193
194 /* Get gpgme_key_t to create the secondary key and export keydata */
195 err = gpgme_get_key(ctx, mutt_buffer_string(keyid), &primary_key, 0);
196 if (err)
197 goto cleanup;
198
199 /* Secondary key */
200 err = gpgme_op_createsubkey(ctx, primary_key, "cv25519", 0, 0,
201 GPGME_CREATE_NOPASSWD | GPGME_CREATE_NOEXPIRE);
202 if (err)
203 {
204 mutt_error(_("Error creating autocrypt key: %s"), gpgme_strerror(err));
205 goto cleanup;
206 }
207
208 /* get keydata */
209 if (export_keydata(ctx, primary_key, keydata))
210 goto cleanup;
211 mutt_debug(LL_DEBUG1, "key has keydata *%s*\n", mutt_buffer_string(keydata));
212
213 rc = 0;
214
215cleanup:
216 gpgme_key_unref(primary_key);
217 gpgme_release(ctx);
218 return rc;
219}
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition: address.c:440
struct Address * mutt_addr_copy(const struct Address *addr)
Copy the real address.
Definition: address.c:716
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1351
size_t mutt_addr_write(char *buf, size_t buflen, struct Address *addr, bool display)
Write a single Address to a buffer.
Definition: address.c:1025
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:310
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
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
An email address.
Definition: address.h:36
+ 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 228 of file gpgme.c.

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

277{
278 int rc = -1;
279
280 /* L10N: During autocrypt account creation, this prompt asks the
281 user whether they want to create a new GPG key for the account,
282 or select an existing account from the keyring. */
283 const char *prompt = _("(c)reate new, or (s)elect existing GPG key?");
284 /* L10N: The letters corresponding to the
285 "(c)reate new, or (s)elect existing GPG key?" prompt. */
286 const char *letters = _("cs");
287
288 int choice = mutt_multi_choice(prompt, letters);
289 switch (choice)
290 {
291 case 2: /* select existing */
292 rc = mutt_autocrypt_gpgme_select_key(keyid, keydata);
293 if (rc == 0)
294 break;
295
296 /* L10N: During autocrypt account creation, if selecting an existing key fails
297 for some reason, we prompt to see if they want to create a key instead. */
298 if (mutt_yesorno(_("Create a new GPG key for this account, instead?"), MUTT_YES) != MUTT_YES)
299 break;
300 /* fallthrough */
301
302 case 1: /* create new */
303 rc = mutt_autocrypt_gpgme_create_key(addr, keyid, keydata);
304 }
305
306 return rc;
307}
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:228
@ 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 316 of file gpgme.c.

317{
318 int rc = -1;
319 gpgme_ctx_t ctx = NULL;
320 gpgme_data_t dh = NULL;
321
322 if (create_gpgme_context(&ctx))
323 goto cleanup;
324
325 struct Buffer *raw_keydata = mutt_buffer_pool_get();
326 if (!mutt_b64_buffer_decode(raw_keydata, keydata))
327 goto cleanup;
328
329 if (gpgme_data_new_from_mem(&dh, mutt_buffer_string(raw_keydata),
330 mutt_buffer_len(raw_keydata), 0))
331 {
332 goto cleanup;
333 }
334
335 if (gpgme_op_import(ctx, dh))
336 goto cleanup;
337
338 gpgme_import_result_t result = gpgme_op_import_result(ctx);
339 if (!result->imports || !result->imports->fpr)
340 goto cleanup;
341 mutt_buffer_strcpy(keyid, result->imports->fpr);
342
343 rc = 0;
344
345cleanup:
346 gpgme_data_release(dh);
347 gpgme_release(ctx);
348 mutt_buffer_pool_release(&raw_keydata);
349 return rc;
350}
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:354
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
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_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 357 of file gpgme.c.

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