NeoMutt  2025-12-11-769-g906513
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
cryptglue.c
Go to the documentation of this file.
1
23
36
37#include "config.h"
38#include <stdbool.h>
39#include <stdio.h>
40#include "mutt/lib.h"
41#include "core/lib.h"
42#include "cryptglue.h"
43#include "lib.h"
44#include "crypt_mod.h"
45#ifndef CRYPT_BACKEND_GPGME
46#include "gui/lib.h"
47#endif
48#if defined(CRYPT_BACKEND_GPGME) || defined(USE_AUTOCRYPT)
49#include "config/lib.h"
50#endif
51#ifdef CRYPT_BACKEND_GPGME
52#include "crypt_gpgme.h"
53#endif
54#ifdef CRYPT_BACKEND_CLASSIC_PGP
55#include "pgpkey.h"
56#endif
57#ifdef USE_AUTOCRYPT
58#include "email/lib.h"
59#include "autocrypt/lib.h"
60#include "globals.h"
61#else
62struct Envelope;
63#endif
64
65struct Address;
66struct AddressList;
67
68#ifdef CRYPT_BACKEND_CLASSIC_PGP
69extern const struct CryptModuleSpecs CryptModPgpClassic;
70#endif
71
72#ifdef CRYPT_BACKEND_CLASSIC_SMIME
73extern const struct CryptModuleSpecs CryptModSmimeClassic;
74#endif
75
76#ifdef CRYPT_BACKEND_GPGME
77extern const struct CryptModuleSpecs CryptModPgpGpgme;
78extern const struct CryptModuleSpecs CryptModSmimeGpgme;
79#endif
80
81/* If the crypto module identifier by IDENTIFIER has been registered,
82 * call its function FUNC. Do nothing else. This may be used as an
83 * expression. */
84#define CRYPT_MOD_CALL_CHECK(identifier, func) \
85 (crypto_module_lookup(APPLICATION_##identifier) && \
86 (crypto_module_lookup(APPLICATION_##identifier))->func)
87
88/* Call the function FUNC in the crypto module identified by
89 * IDENTIFIER. This may be used as an expression. */
90#define CRYPT_MOD_CALL(identifier, func) \
91 (*(crypto_module_lookup(APPLICATION_##identifier))->func)
92
98void crypt_init(void)
99{
100#ifdef CRYPT_BACKEND_GPGME
101 const bool c_crypt_use_gpgme = cs_subset_bool(NeoMutt->sub, "crypt_use_gpgme");
102#endif
103#ifdef CRYPT_BACKEND_CLASSIC_PGP
104 if (
105#ifdef CRYPT_BACKEND_GPGME
106 (!c_crypt_use_gpgme)
107#else
108 1
109#endif
110 )
112#endif
113
114#ifdef CRYPT_BACKEND_CLASSIC_SMIME
115 if (
116#ifdef CRYPT_BACKEND_GPGME
117 (!c_crypt_use_gpgme)
118#else
119 1
120#endif
121 )
123#endif
124
125#ifdef CRYPT_BACKEND_GPGME
126 if (c_crypt_use_gpgme)
127 {
130 }
131#endif
132
133#if defined(CRYPT_BACKEND_CLASSIC_PGP) || \
134 defined(CRYPT_BACKEND_CLASSIC_SMIME) || defined(CRYPT_BACKEND_GPGME)
135 if (CRYPT_MOD_CALL_CHECK(PGP, init))
136 CRYPT_MOD_CALL(PGP, init)();
137
138 if (CRYPT_MOD_CALL_CHECK(SMIME, init))
139 CRYPT_MOD_CALL(SMIME, init)();
140#endif
141}
142
147{
149 (CRYPT_MOD_CALL(PGP, cleanup))();
150
151 if (CRYPT_MOD_CALL_CHECK(SMIME, cleanup))
152 (CRYPT_MOD_CALL(SMIME, cleanup))();
153
154#ifdef CRYPT_BACKEND_CLASSIC_PGP
156#endif
157
158#ifdef CRYPT_BACKEND_GPGME
160#endif
161}
162
170{
171 if (((WithCrypto & APPLICATION_PGP) != 0) && (type & APPLICATION_PGP))
172 mutt_message(_("Invoking PGP..."));
173 else if (((WithCrypto & APPLICATION_SMIME) != 0) && (type & APPLICATION_SMIME))
174 mutt_message(_("Invoking S/MIME..."));
175}
176
184{
185 if (((WithCrypto & APPLICATION_PGP) != 0) && (type & APPLICATION_PGP) &&
187 {
188 return true;
189 }
190
191 if (((WithCrypto & APPLICATION_SMIME) != 0) && (type & APPLICATION_SMIME) &&
193 {
194 return true;
195 }
196
197 return false;
198}
199
208
213{
215 return CRYPT_MOD_CALL(PGP, valid_passphrase)();
216
217 return false;
218}
219
223int crypt_pgp_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
224{
225#ifdef USE_AUTOCRYPT
226 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
227 if (c_autocrypt)
228 {
229 OptAutocryptGpgme = true;
230 int result = pgp_gpgme_decrypt_mime(fp_in, fp_out, b, b_dec);
231 OptAutocryptGpgme = false;
232 if (result == 0)
233 {
234 b->is_autocrypt = true;
235 return result;
236 }
237 }
238#endif
239
241 return CRYPT_MOD_CALL(PGP, decrypt_mime)(fp_in, fp_out, b, b_dec);
242
243 return -1;
244}
245
249int crypt_pgp_application_handler(struct Body *b_email, struct State *state)
250{
252 return CRYPT_MOD_CALL(PGP, application_handler)(b_email, state);
253
254 return -1;
255}
256
260int crypt_pgp_encrypted_handler(struct Body *b_email, struct State *state)
261{
262#ifdef USE_AUTOCRYPT
263 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
264 if (c_autocrypt)
265 {
266 OptAutocryptGpgme = true;
267 int result = pgp_gpgme_encrypted_handler(b_email, state);
268 OptAutocryptGpgme = false;
269 if (result == 0)
270 {
271 b_email->is_autocrypt = true;
272 return result;
273 }
274 }
275#endif
276
278 return CRYPT_MOD_CALL(PGP, encrypted_handler)(b_email, state);
279
280 return -1;
281}
282
291
295bool crypt_pgp_check_traditional(FILE *fp, struct Body *b, bool just_one)
296{
298 return CRYPT_MOD_CALL(PGP, pgp_check_traditional)(fp, b, just_one);
299
300 return false;
301}
302
306struct Body *crypt_pgp_traditional_encryptsign(struct Body *b, SecurityFlags flags, char *keylist)
307{
308 if (CRYPT_MOD_CALL_CHECK(PGP, pgp_traditional_encryptsign))
309 return CRYPT_MOD_CALL(PGP, pgp_traditional_encryptsign)(b, flags, keylist);
310
311 return NULL;
312}
313
318{
319 if (CRYPT_MOD_CALL_CHECK(PGP, pgp_make_key_attachment))
320 return CRYPT_MOD_CALL(PGP, pgp_make_key_attachment)();
321
322 return NULL;
323}
324
328char *crypt_pgp_find_keys(struct AddressList *addrlist, bool oppenc_mode)
329{
331 return CRYPT_MOD_CALL(PGP, find_keys)(addrlist, oppenc_mode);
332
333 return NULL;
334}
335
339struct Body *crypt_pgp_sign_message(struct Body *b, const struct AddressList *from)
340{
342 return CRYPT_MOD_CALL(PGP, sign_message)(b, from);
343
344 return NULL;
345}
346
350struct Body *crypt_pgp_encrypt_message(struct Email *e, struct Body *b, char *keylist,
351 int sign, const struct AddressList *from)
352{
353#ifdef USE_AUTOCRYPT
354 if (e->security & SEC_AUTOCRYPT)
355 {
357 return NULL;
358
359 OptAutocryptGpgme = true;
360 struct Body *result = pgp_gpgme_encrypt_message(b, keylist, sign, from);
361 OptAutocryptGpgme = false;
362
363 return result;
364 }
365#endif
366
367 if (CRYPT_MOD_CALL_CHECK(PGP, pgp_encrypt_message))
368 return CRYPT_MOD_CALL(PGP, pgp_encrypt_message)(b, keylist, sign, from);
369
370 return NULL;
371}
372
376void crypt_pgp_invoke_import(const char *fname)
377{
378 if (CRYPT_MOD_CALL_CHECK(PGP, pgp_invoke_import))
379 CRYPT_MOD_CALL(PGP, pgp_invoke_import)(fname);
380}
381
385int crypt_pgp_verify_one(struct Body *b, struct State *state, const char *tempf)
386{
388 return CRYPT_MOD_CALL(PGP, verify_one)(b, state, tempf);
389
390 return -1;
391}
392
397{
398 if (CRYPT_MOD_CALL_CHECK(PGP, send_menu))
399 return CRYPT_MOD_CALL(PGP, send_menu)(e);
400
401 return 0;
402}
403
408{
409 if (CRYPT_MOD_CALL_CHECK(PGP, pgp_extract_key_from_attachment))
410 CRYPT_MOD_CALL(PGP, pgp_extract_key_from_attachment)(fp, b);
411}
412
416void crypt_pgp_set_sender(const char *sender)
417{
418 if (CRYPT_MOD_CALL_CHECK(PGP, set_sender))
419 CRYPT_MOD_CALL(PGP, set_sender)(sender);
420}
421
426{
427 if (CRYPT_MOD_CALL_CHECK(SMIME, void_passphrase))
428 CRYPT_MOD_CALL(SMIME, void_passphrase)();
429}
430
435{
436 if (CRYPT_MOD_CALL_CHECK(SMIME, valid_passphrase))
437 return CRYPT_MOD_CALL(SMIME, valid_passphrase)();
438
439 return false;
440}
441
445int crypt_smime_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
446{
447 if (CRYPT_MOD_CALL_CHECK(SMIME, decrypt_mime))
448 return CRYPT_MOD_CALL(SMIME, decrypt_mime)(fp_in, fp_out, b, b_dec);
449
450 return -1;
451}
452
456int crypt_smime_application_handler(struct Body *b_email, struct State *state)
457{
458 if (CRYPT_MOD_CALL_CHECK(SMIME, application_handler))
459 return CRYPT_MOD_CALL(SMIME, application_handler)(b_email, state);
460
461 return -1;
462}
463
468{
469 if (CRYPT_MOD_CALL_CHECK(SMIME, smime_getkeys))
470 CRYPT_MOD_CALL(SMIME, smime_getkeys)(env);
471}
472
476int crypt_smime_verify_sender(struct Email *e, struct Message *msg)
477{
478 if (CRYPT_MOD_CALL_CHECK(SMIME, smime_verify_sender))
479 return CRYPT_MOD_CALL(SMIME, smime_verify_sender)(e, msg);
480
481 return 1;
482}
483
487char *crypt_smime_find_keys(struct AddressList *addrlist, bool oppenc_mode)
488{
490 return CRYPT_MOD_CALL(SMIME, find_keys)(addrlist, oppenc_mode);
491
492 return NULL;
493}
494
498struct Body *crypt_smime_sign_message(struct Body *b, const struct AddressList *from)
499{
501 return CRYPT_MOD_CALL(SMIME, sign_message)(b, from);
502
503 return NULL;
504}
505
509struct Body *crypt_smime_build_smime_entity(struct Body *b, char *certlist)
510{
511 if (CRYPT_MOD_CALL_CHECK(SMIME, smime_build_smime_entity))
512 return CRYPT_MOD_CALL(SMIME, smime_build_smime_entity)(b, certlist);
513
514 return NULL;
515}
516
520void crypt_smime_invoke_import(const char *infile, const char *mailbox)
521{
522 if (CRYPT_MOD_CALL_CHECK(SMIME, smime_invoke_import))
523 CRYPT_MOD_CALL(SMIME, smime_invoke_import)(infile, mailbox);
524}
525
529int crypt_smime_verify_one(struct Body *b, struct State *state, const char *tempf)
530{
532 return CRYPT_MOD_CALL(SMIME, verify_one)(b, state, tempf);
533
534 return -1;
535}
536
541{
542 if (CRYPT_MOD_CALL_CHECK(SMIME, send_menu))
543 return CRYPT_MOD_CALL(SMIME, send_menu)(e);
544
545 return 0;
546}
547
551void crypt_smime_set_sender(const char *sender)
552{
553 if (CRYPT_MOD_CALL_CHECK(SMIME, set_sender))
554 CRYPT_MOD_CALL(SMIME, set_sender)(sender);
555}
Autocrypt end-to-end encryption.
int mutt_autocrypt_set_sign_as_default_key(struct Email *e)
Set the Autocrypt default key for signing.
Definition autocrypt.c:716
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
static char * find_keys(const struct AddressList *addrlist, unsigned int app, bool oppenc_mode)
Find keys of the recipients of the message.
static int verify_one(struct Body *b, struct State *state, const char *tempfile, bool is_smime)
Do the actual verification step.
void gpgme_id_defaults_cleanup(void)
Free the GPGME IdDefaults cache.
static struct Body * sign_message(struct Body *b, const struct AddressList *from, bool use_smime)
Sign a message.
Wrapper for PGP/SMIME calls to GPGME.
const struct CryptModuleSpecs * crypto_module_lookup(int identifier)
Lookup a crypto module by name.
Definition crypt_mod.c:56
void crypto_module_register(const struct CryptModuleSpecs *specs)
Register a new crypto module.
Definition crypt_mod.c:41
Register crypto modules.
bool crypt_has_module_backend(SecurityFlags type)
Is there a crypto backend for a given type?
Definition cryptglue.c:183
char * crypt_smime_find_keys(struct AddressList *addrlist, bool oppenc_mode)
Wrapper for CryptModuleSpecs::find_keys()
Definition cryptglue.c:487
void crypt_invoke_message(SecurityFlags type)
Display an informative message.
Definition cryptglue.c:169
struct Body * crypt_smime_build_smime_entity(struct Body *b, char *certlist)
Wrapper for CryptModuleSpecs::smime_build_smime_entity()
Definition cryptglue.c:509
struct Body * crypt_smime_sign_message(struct Body *b, const struct AddressList *from)
Wrapper for CryptModuleSpecs::sign_message()
Definition cryptglue.c:498
int crypt_pgp_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
Wrapper for CryptModuleSpecs::decrypt_mime()
Definition cryptglue.c:223
struct Body * crypt_pgp_traditional_encryptsign(struct Body *b, SecurityFlags flags, char *keylist)
Wrapper for CryptModuleSpecs::pgp_traditional_encryptsign()
Definition cryptglue.c:306
char * crypt_pgp_find_keys(struct AddressList *addrlist, bool oppenc_mode)
Wrapper for CryptModuleSpecs::find_keys()
Definition cryptglue.c:328
void crypt_cleanup(void)
Clean up backend.
Definition cryptglue.c:146
void crypt_smime_getkeys(struct Envelope *env)
Wrapper for CryptModuleSpecs::smime_getkeys()
Definition cryptglue.c:467
struct Body * crypt_pgp_sign_message(struct Body *b, const struct AddressList *from)
Wrapper for CryptModuleSpecs::sign_message()
Definition cryptglue.c:339
bool crypt_pgp_check_traditional(FILE *fp, struct Body *b, bool just_one)
Wrapper for CryptModuleSpecs::pgp_check_traditional()
Definition cryptglue.c:295
#define CRYPT_MOD_CALL_CHECK(identifier, func)
Definition cryptglue.c:84
struct Body * crypt_pgp_make_key_attachment(void)
Wrapper for CryptModuleSpecs::pgp_make_key_attachment()
Definition cryptglue.c:317
SecurityFlags crypt_smime_send_menu(struct Email *e)
Wrapper for CryptModuleSpecs::send_menu()
Definition cryptglue.c:540
SecurityFlags crypt_pgp_send_menu(struct Email *e)
Wrapper for CryptModuleSpecs::send_menu()
Definition cryptglue.c:396
void crypt_pgp_invoke_getkeys(struct Address *addr)
Wrapper for CryptModuleSpecs::pgp_invoke_getkeys()
Definition cryptglue.c:286
bool crypt_smime_valid_passphrase(void)
Wrapper for CryptModuleSpecs::valid_passphrase()
Definition cryptglue.c:434
void crypt_pgp_invoke_import(const char *fname)
Wrapper for CryptModuleSpecs::pgp_invoke_import()
Definition cryptglue.c:376
void crypt_smime_void_passphrase(void)
Wrapper for CryptModuleSpecs::void_passphrase()
Definition cryptglue.c:425
void crypt_pgp_extract_key_from_attachment(FILE *fp, struct Body *b)
Wrapper for CryptModuleSpecs::pgp_extract_key_from_attachment()
Definition cryptglue.c:407
void crypt_smime_invoke_import(const char *infile, const char *mailbox)
Wrapper for CryptModuleSpecs::smime_invoke_import()
Definition cryptglue.c:520
int crypt_smime_verify_sender(struct Email *e, struct Message *msg)
Wrapper for CryptModuleSpecs::smime_verify_sender()
Definition cryptglue.c:476
void crypt_pgp_set_sender(const char *sender)
Wrapper for CryptModuleSpecs::set_sender()
Definition cryptglue.c:416
void crypt_smime_set_sender(const char *sender)
Wrapper for CryptModuleSpecs::set_sender()
Definition cryptglue.c:551
int crypt_smime_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
Wrapper for CryptModuleSpecs::decrypt_mime()
Definition cryptglue.c:445
int crypt_smime_verify_one(struct Body *b, struct State *state, const char *tempf)
Wrapper for CryptModuleSpecs::verify_one()
Definition cryptglue.c:529
void crypt_init(void)
Initialise the crypto backends.
Definition cryptglue.c:98
void crypt_pgp_void_passphrase(void)
Wrapper for CryptModuleSpecs::void_passphrase()
Definition cryptglue.c:203
#define CRYPT_MOD_CALL(identifier, func)
Definition cryptglue.c:90
bool crypt_pgp_valid_passphrase(void)
Wrapper for CryptModuleSpecs::valid_passphrase()
Definition cryptglue.c:212
int crypt_pgp_verify_one(struct Body *b, struct State *state, const char *tempf)
Wrapper for CryptModuleSpecs::verify_one()
Definition cryptglue.c:385
struct Body * crypt_pgp_encrypt_message(struct Email *e, struct Body *b, char *keylist, int sign, const struct AddressList *from)
Wrapper for CryptModuleSpecs::pgp_encrypt_message()
Definition cryptglue.c:350
Wrapper around crypto functions.
Structs that make up an email.
bool OptAutocryptGpgme
(pseudo) use Autocrypt context inside ncrypt/crypt_gpgme.c
Definition globals.c:44
Global variables.
const struct CryptModuleSpecs CryptModSmimeGpgme
GPGME SMIME - Implements CryptModuleSpecs -.
const struct CryptModuleSpecs CryptModSmimeClassic
CLI SMIME - Implements CryptModuleSpecs -.
const struct CryptModuleSpecs CryptModPgpGpgme
GPGME PGP - Implements CryptModuleSpecs -.
const struct CryptModuleSpecs CryptModPgpClassic
CLI PGP - Implements CryptModuleSpecs -.
int pgp_gpgme_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
Decrypt an encrypted MIME part - Implements CryptModuleSpecs::decrypt_mime() -.
int pgp_gpgme_encrypted_handler(struct Body *b, struct State *state)
Manage a PGP or S/MIME encrypted MIME part - Implements CryptModuleSpecs::encrypted_handler() -.
struct Body * pgp_gpgme_encrypt_message(struct Body *b, char *keylist, bool sign, const struct AddressList *from)
PGP encrypt an email - Implements CryptModuleSpecs::pgp_encrypt_message() -.
int crypt_pgp_application_handler(struct Body *b_email, struct State *state)
Wrapper for CryptModuleSpecs::application_handler() - Implements handler_t -.
Definition cryptglue.c:249
int crypt_smime_application_handler(struct Body *b_email, struct State *state)
Wrapper for CryptModuleSpecs::application_handler() - Implements handler_t -.
Definition cryptglue.c:456
int crypt_pgp_encrypted_handler(struct Body *b_email, struct State *state)
Wrapper for CryptModuleSpecs::encrypted_handler() - Implements handler_t -.
Definition cryptglue.c:260
#define mutt_message(...)
Definition logging2.h:93
Convenience wrapper for the gui headers.
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
API for encryption/signing of emails.
#define SEC_AUTOCRYPT
(Autocrypt) Message will be, or was Autocrypt encrypt+signed
Definition lib.h:96
uint16_t SecurityFlags
Flags, e.g. SEC_ENCRYPT.
Definition lib.h:85
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition lib.h:99
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition lib.h:100
#define WithCrypto
Definition lib.h:125
void pgp_id_defaults_cleanup(void)
Free the PGP IdDefaults cache.
Definition pgpkey.c:610
PGP key management routines.
An email address.
Definition address.h:35
The body of an email.
Definition body.h:36
bool is_autocrypt
Flag autocrypt-decrypted messages for replying.
Definition body.h:50
int(* encrypted_handler)(struct Body *b, struct State *state)
Definition crypt_mod.h:123
int(* decrypt_mime)(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
Definition crypt_mod.h:99
void(* init)(void)
Definition crypt_mod.h:56
bool(* valid_passphrase)(void)
Definition crypt_mod.h:85
void(* cleanup)(void)
Definition crypt_mod.h:64
int(* application_handler)(struct Body *b, struct State *state)
Definition crypt_mod.h:111
bool(* pgp_check_traditional)(FILE *fp, struct Body *b, bool just_one)
Definition crypt_mod.h:221
void(* void_passphrase)(void)
Definition crypt_mod.h:72
void(* pgp_invoke_getkeys)(struct Address *addr)
Definition crypt_mod.h:243
The envelope/body of an email.
Definition email.h:39
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition email.h:43
The header of an Email.
Definition envelope.h:57
A local copy of an email.
Definition message.h:34
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Keep track when processing files.
Definition state.h:48