NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
cryptglue.c
Go to the documentation of this file.
1
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 USE_AUTOCRYPT
52#include "email/lib.h"
53#include "autocrypt/lib.h"
54#include "crypt_gpgme.h"
55#include "globals.h"
56#else
57struct Envelope;
58#endif
59
60struct Address;
61struct AddressList;
62
63#ifdef CRYPT_BACKEND_CLASSIC_PGP
64extern const struct CryptModuleSpecs CryptModPgpClassic;
65#endif
66
67#ifdef CRYPT_BACKEND_CLASSIC_SMIME
68extern const struct CryptModuleSpecs CryptModSmimeClassic;
69#endif
70
71#ifdef CRYPT_BACKEND_GPGME
72extern const struct CryptModuleSpecs CryptModPgpGpgme;
73extern const struct CryptModuleSpecs CryptModSmimeGpgme;
74#endif
75
76/* If the crypto module identifier by IDENTIFIER has been registered,
77 * call its function FUNC. Do nothing else. This may be used as an
78 * expression. */
79#define CRYPT_MOD_CALL_CHECK(identifier, func) \
80 (crypto_module_lookup(APPLICATION_##identifier) && \
81 (crypto_module_lookup(APPLICATION_##identifier))->func)
82
83/* Call the function FUNC in the crypto module identified by
84 * IDENTIFIER. This may be used as an expression. */
85#define CRYPT_MOD_CALL(identifier, func) \
86 (*(crypto_module_lookup(APPLICATION_##identifier))->func)
87
93void crypt_init(void)
94{
95#ifdef CRYPT_BACKEND_GPGME
96 const bool c_crypt_use_gpgme = cs_subset_bool(NeoMutt->sub, "crypt_use_gpgme");
97#endif
98#ifdef CRYPT_BACKEND_CLASSIC_PGP
99 if (
100#ifdef CRYPT_BACKEND_GPGME
101 (!c_crypt_use_gpgme)
102#else
103 1
104#endif
105 )
107#endif
108
109#ifdef CRYPT_BACKEND_CLASSIC_SMIME
110 if (
111#ifdef CRYPT_BACKEND_GPGME
112 (!c_crypt_use_gpgme)
113#else
114 1
115#endif
116 )
118#endif
119
120#ifdef CRYPT_BACKEND_GPGME
121 if (c_crypt_use_gpgme)
122 {
125 }
126#endif
127
128#if defined(CRYPT_BACKEND_CLASSIC_PGP) || \
129 defined(CRYPT_BACKEND_CLASSIC_SMIME) || defined(CRYPT_BACKEND_GPGME)
130 if (CRYPT_MOD_CALL_CHECK(PGP, init))
131 CRYPT_MOD_CALL(PGP, init)();
132
133 if (CRYPT_MOD_CALL_CHECK(SMIME, init))
134 CRYPT_MOD_CALL(SMIME, init)();
135#endif
136}
137
142{
144 (CRYPT_MOD_CALL(PGP, cleanup))();
145
146 if (CRYPT_MOD_CALL_CHECK(SMIME, cleanup))
147 (CRYPT_MOD_CALL(SMIME, cleanup))();
148}
149
157{
158 if (((WithCrypto & APPLICATION_PGP) != 0) && (type & APPLICATION_PGP))
159 mutt_message(_("Invoking PGP..."));
160 else if (((WithCrypto & APPLICATION_SMIME) != 0) && (type & APPLICATION_SMIME))
161 mutt_message(_("Invoking S/MIME..."));
162}
163
171{
172 if (((WithCrypto & APPLICATION_PGP) != 0) && (type & APPLICATION_PGP) &&
174 {
175 return true;
176 }
177
178 if (((WithCrypto & APPLICATION_SMIME) != 0) && (type & APPLICATION_SMIME) &&
180 {
181 return true;
182 }
183
184 return false;
185}
186
191{
194}
195
200{
202 return CRYPT_MOD_CALL(PGP, valid_passphrase)();
203
204 return false;
205}
206
210int crypt_pgp_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
211{
212#ifdef USE_AUTOCRYPT
213 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
214 if (c_autocrypt)
215 {
216 OptAutocryptGpgme = true;
217 int result = pgp_gpgme_decrypt_mime(fp_in, fp_out, b, b_dec);
218 OptAutocryptGpgme = false;
219 if (result == 0)
220 {
221 b->is_autocrypt = true;
222 return result;
223 }
224 }
225#endif
226
228 return CRYPT_MOD_CALL(PGP, decrypt_mime)(fp_in, fp_out, b, b_dec);
229
230 return -1;
231}
232
236int crypt_pgp_application_handler(struct Body *b_email, struct State *state)
237{
239 return CRYPT_MOD_CALL(PGP, application_handler)(b_email, state);
240
241 return -1;
242}
243
247int crypt_pgp_encrypted_handler(struct Body *b_email, struct State *state)
248{
249#ifdef USE_AUTOCRYPT
250 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
251 if (c_autocrypt)
252 {
253 OptAutocryptGpgme = true;
254 int result = pgp_gpgme_encrypted_handler(b_email, state);
255 OptAutocryptGpgme = false;
256 if (result == 0)
257 {
258 b_email->is_autocrypt = true;
259 return result;
260 }
261 }
262#endif
263
265 return CRYPT_MOD_CALL(PGP, encrypted_handler)(b_email, state);
266
267 return -1;
268}
269
274{
277}
278
282bool crypt_pgp_check_traditional(FILE *fp, struct Body *b, bool just_one)
283{
285 return CRYPT_MOD_CALL(PGP, pgp_check_traditional)(fp, b, just_one);
286
287 return false;
288}
289
293struct Body *crypt_pgp_traditional_encryptsign(struct Body *b, SecurityFlags flags, char *keylist)
294{
295 if (CRYPT_MOD_CALL_CHECK(PGP, pgp_traditional_encryptsign))
296 return CRYPT_MOD_CALL(PGP, pgp_traditional_encryptsign)(b, flags, keylist);
297
298 return NULL;
299}
300
305{
306 if (CRYPT_MOD_CALL_CHECK(PGP, pgp_make_key_attachment))
307 return CRYPT_MOD_CALL(PGP, pgp_make_key_attachment)();
308
309 return NULL;
310}
311
315char *crypt_pgp_find_keys(struct AddressList *addrlist, bool oppenc_mode)
316{
318 return CRYPT_MOD_CALL(PGP, find_keys)(addrlist, oppenc_mode);
319
320 return NULL;
321}
322
326struct Body *crypt_pgp_sign_message(struct Body *b, const struct AddressList *from)
327{
329 return CRYPT_MOD_CALL(PGP, sign_message)(b, from);
330
331 return NULL;
332}
333
337struct Body *crypt_pgp_encrypt_message(struct Email *e, struct Body *b, char *keylist,
338 int sign, const struct AddressList *from)
339{
340#ifdef USE_AUTOCRYPT
341 if (e->security & SEC_AUTOCRYPT)
342 {
344 return NULL;
345
346 OptAutocryptGpgme = true;
347 struct Body *result = pgp_gpgme_encrypt_message(b, keylist, sign, from);
348 OptAutocryptGpgme = false;
349
350 return result;
351 }
352#endif
353
354 if (CRYPT_MOD_CALL_CHECK(PGP, pgp_encrypt_message))
355 return CRYPT_MOD_CALL(PGP, pgp_encrypt_message)(b, keylist, sign, from);
356
357 return NULL;
358}
359
363void crypt_pgp_invoke_import(const char *fname)
364{
365 if (CRYPT_MOD_CALL_CHECK(PGP, pgp_invoke_import))
366 CRYPT_MOD_CALL(PGP, pgp_invoke_import)(fname);
367}
368
372int crypt_pgp_verify_one(struct Body *b, struct State *state, const char *tempf)
373{
375 return CRYPT_MOD_CALL(PGP, verify_one)(b, state, tempf);
376
377 return -1;
378}
379
384{
385 if (CRYPT_MOD_CALL_CHECK(PGP, send_menu))
386 return CRYPT_MOD_CALL(PGP, send_menu)(e);
387
388 return 0;
389}
390
395{
396 if (CRYPT_MOD_CALL_CHECK(PGP, pgp_extract_key_from_attachment))
397 CRYPT_MOD_CALL(PGP, pgp_extract_key_from_attachment)(fp, b);
398}
399
403void crypt_pgp_set_sender(const char *sender)
404{
405 if (CRYPT_MOD_CALL_CHECK(PGP, set_sender))
406 CRYPT_MOD_CALL(PGP, set_sender)(sender);
407}
408
413{
414 if (CRYPT_MOD_CALL_CHECK(SMIME, void_passphrase))
415 CRYPT_MOD_CALL(SMIME, void_passphrase)();
416}
417
422{
423 if (CRYPT_MOD_CALL_CHECK(SMIME, valid_passphrase))
424 return CRYPT_MOD_CALL(SMIME, valid_passphrase)();
425
426 return false;
427}
428
432int crypt_smime_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
433{
434 if (CRYPT_MOD_CALL_CHECK(SMIME, decrypt_mime))
435 return CRYPT_MOD_CALL(SMIME, decrypt_mime)(fp_in, fp_out, b, b_dec);
436
437 return -1;
438}
439
443int crypt_smime_application_handler(struct Body *b_email, struct State *state)
444{
445 if (CRYPT_MOD_CALL_CHECK(SMIME, application_handler))
446 return CRYPT_MOD_CALL(SMIME, application_handler)(b_email, state);
447
448 return -1;
449}
450
455{
456 if (CRYPT_MOD_CALL_CHECK(SMIME, smime_getkeys))
457 CRYPT_MOD_CALL(SMIME, smime_getkeys)(env);
458}
459
463int crypt_smime_verify_sender(struct Email *e, struct Message *msg)
464{
465 if (CRYPT_MOD_CALL_CHECK(SMIME, smime_verify_sender))
466 return CRYPT_MOD_CALL(SMIME, smime_verify_sender)(e, msg);
467
468 return 1;
469}
470
474char *crypt_smime_find_keys(struct AddressList *addrlist, bool oppenc_mode)
475{
477 return CRYPT_MOD_CALL(SMIME, find_keys)(addrlist, oppenc_mode);
478
479 return NULL;
480}
481
485struct Body *crypt_smime_sign_message(struct Body *b, const struct AddressList *from)
486{
488 return CRYPT_MOD_CALL(SMIME, sign_message)(b, from);
489
490 return NULL;
491}
492
496struct Body *crypt_smime_build_smime_entity(struct Body *b, char *certlist)
497{
498 if (CRYPT_MOD_CALL_CHECK(SMIME, smime_build_smime_entity))
499 return CRYPT_MOD_CALL(SMIME, smime_build_smime_entity)(b, certlist);
500
501 return NULL;
502}
503
507void crypt_smime_invoke_import(const char *infile, const char *mailbox)
508{
509 if (CRYPT_MOD_CALL_CHECK(SMIME, smime_invoke_import))
510 CRYPT_MOD_CALL(SMIME, smime_invoke_import)(infile, mailbox);
511}
512
516int crypt_smime_verify_one(struct Body *b, struct State *state, const char *tempf)
517{
519 return CRYPT_MOD_CALL(SMIME, verify_one)(b, state, tempf);
520
521 return -1;
522}
523
528{
529 if (CRYPT_MOD_CALL_CHECK(SMIME, send_menu))
530 return CRYPT_MOD_CALL(SMIME, send_menu)(e);
531
532 return 0;
533}
534
538void crypt_smime_set_sender(const char *sender)
539{
540 if (CRYPT_MOD_CALL_CHECK(SMIME, set_sender))
541 CRYPT_MOD_CALL(SMIME, set_sender)(sender);
542}
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:697
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.
Definition: crypt_gpgme.c:3534
static int verify_one(struct Body *b, struct State *state, const char *tempfile, bool is_smime)
Do the actual verification step.
Definition: crypt_gpgme.c:1581
static struct Body * sign_message(struct Body *b, const struct AddressList *from, bool use_smime)
Sign a message.
Definition: crypt_gpgme.c:906
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:68
void crypto_module_register(const struct CryptModuleSpecs *specs)
Register a new crypto module.
Definition: crypt_mod.c:54
Register crypto modules.
bool crypt_has_module_backend(SecurityFlags type)
Is there a crypto backend for a given type?
Definition: cryptglue.c:170
char * crypt_smime_find_keys(struct AddressList *addrlist, bool oppenc_mode)
Wrapper for CryptModuleSpecs::find_keys()
Definition: cryptglue.c:474
void crypt_invoke_message(SecurityFlags type)
Display an informative message.
Definition: cryptglue.c:156
struct Body * crypt_smime_build_smime_entity(struct Body *b, char *certlist)
Wrapper for CryptModuleSpecs::smime_build_smime_entity()
Definition: cryptglue.c:496
struct Body * crypt_smime_sign_message(struct Body *b, const struct AddressList *from)
Wrapper for CryptModuleSpecs::sign_message()
Definition: cryptglue.c:485
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:210
struct Body * crypt_pgp_traditional_encryptsign(struct Body *b, SecurityFlags flags, char *keylist)
Wrapper for CryptModuleSpecs::pgp_traditional_encryptsign()
Definition: cryptglue.c:293
char * crypt_pgp_find_keys(struct AddressList *addrlist, bool oppenc_mode)
Wrapper for CryptModuleSpecs::find_keys()
Definition: cryptglue.c:315
void crypt_cleanup(void)
Clean up backend.
Definition: cryptglue.c:141
void crypt_smime_getkeys(struct Envelope *env)
Wrapper for CryptModuleSpecs::smime_getkeys()
Definition: cryptglue.c:454
struct Body * crypt_pgp_sign_message(struct Body *b, const struct AddressList *from)
Wrapper for CryptModuleSpecs::sign_message()
Definition: cryptglue.c:326
bool crypt_pgp_check_traditional(FILE *fp, struct Body *b, bool just_one)
Wrapper for CryptModuleSpecs::pgp_check_traditional()
Definition: cryptglue.c:282
#define CRYPT_MOD_CALL_CHECK(identifier, func)
Definition: cryptglue.c:79
struct Body * crypt_pgp_make_key_attachment(void)
Wrapper for CryptModuleSpecs::pgp_make_key_attachment()
Definition: cryptglue.c:304
SecurityFlags crypt_smime_send_menu(struct Email *e)
Wrapper for CryptModuleSpecs::send_menu()
Definition: cryptglue.c:527
SecurityFlags crypt_pgp_send_menu(struct Email *e)
Wrapper for CryptModuleSpecs::send_menu()
Definition: cryptglue.c:383
void crypt_pgp_invoke_getkeys(struct Address *addr)
Wrapper for CryptModuleSpecs::pgp_invoke_getkeys()
Definition: cryptglue.c:273
bool crypt_smime_valid_passphrase(void)
Wrapper for CryptModuleSpecs::valid_passphrase()
Definition: cryptglue.c:421
void crypt_pgp_invoke_import(const char *fname)
Wrapper for CryptModuleSpecs::pgp_invoke_import()
Definition: cryptglue.c:363
void crypt_smime_void_passphrase(void)
Wrapper for CryptModuleSpecs::void_passphrase()
Definition: cryptglue.c:412
void crypt_pgp_extract_key_from_attachment(FILE *fp, struct Body *b)
Wrapper for CryptModuleSpecs::pgp_extract_key_from_attachment()
Definition: cryptglue.c:394
void crypt_smime_invoke_import(const char *infile, const char *mailbox)
Wrapper for CryptModuleSpecs::smime_invoke_import()
Definition: cryptglue.c:507
int crypt_smime_verify_sender(struct Email *e, struct Message *msg)
Wrapper for CryptModuleSpecs::smime_verify_sender()
Definition: cryptglue.c:463
void crypt_pgp_set_sender(const char *sender)
Wrapper for CryptModuleSpecs::set_sender()
Definition: cryptglue.c:403
void crypt_smime_set_sender(const char *sender)
Wrapper for CryptModuleSpecs::set_sender()
Definition: cryptglue.c:538
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:432
int crypt_smime_verify_one(struct Body *b, struct State *state, const char *tempf)
Wrapper for CryptModuleSpecs::verify_one()
Definition: cryptglue.c:516
void crypt_init(void)
Initialise the crypto backends.
Definition: cryptglue.c:93
void crypt_pgp_void_passphrase(void)
Wrapper for CryptModuleSpecs::void_passphrase()
Definition: cryptglue.c:190
#define CRYPT_MOD_CALL(identifier, func)
Definition: cryptglue.c:85
bool crypt_pgp_valid_passphrase(void)
Wrapper for CryptModuleSpecs::valid_passphrase()
Definition: cryptglue.c:199
int crypt_pgp_verify_one(struct Body *b, struct State *state, const char *tempf)
Wrapper for CryptModuleSpecs::verify_one()
Definition: cryptglue.c:372
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:337
Wrapper around crypto functions.
Structs that make up an email.
bool OptAutocryptGpgme
(pseudo) use Autocrypt context inside ncrypt/crypt_gpgme.c
Definition: globals.c:59
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() -.
Definition: crypt_gpgme.c:1896
int pgp_gpgme_encrypted_handler(struct Body *b, struct State *state)
Manage a PGP or S/MIME encrypted MIME part - Implements CryptModuleSpecs::encrypted_handler() -.
Definition: crypt_gpgme.c:2766
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() -.
Definition: crypt_gpgme.c:1043
int crypt_pgp_application_handler(struct Body *b_email, struct State *state)
Wrapper for CryptModuleSpecs::application_handler() - Implements handler_t -.
Definition: cryptglue.c:236
int crypt_smime_application_handler(struct Body *b_email, struct State *state)
Wrapper for CryptModuleSpecs::application_handler() - Implements handler_t -.
Definition: cryptglue.c:443
int crypt_pgp_encrypted_handler(struct Body *b_email, struct State *state)
Wrapper for CryptModuleSpecs::encrypted_handler() - Implements handler_t -.
Definition: cryptglue.c:247
#define mutt_message(...)
Definition: logging2.h:91
Convenience wrapper for the gui headers.
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
#define SEC_AUTOCRYPT
(Autocrypt) Message will be, or was Autocrypt encrypt+signed
Definition: lib.h:87
uint16_t SecurityFlags
Flags, e.g. SEC_ENCRYPT.
Definition: lib.h:76
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:90
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition: lib.h:91
#define WithCrypto
Definition: lib.h:116
Key value store.
An email address.
Definition: address.h:36
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:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
Keep track when processing files.
Definition: state.h:48