NeoMutt  2019-12-07-60-g0cfa53
Teaching an old dog new tricks
DOXYGEN
cryptglue.c
Go to the documentation of this file.
1 
38 #include "config.h"
39 #include <stdbool.h>
40 #include <stdio.h>
41 #include "mutt/mutt.h"
42 #include "cryptglue.h"
43 #include "crypt_mod.h"
44 #include "ncrypt.h"
45 #ifndef CRYPT_BACKEND_GPGME
46 #include "gui/lib.h"
47 #endif
48 #ifdef USE_AUTOCRYPT
49 #include "email/lib.h"
50 #include "autocrypt/autocrypt.h"
51 #include "globals.h"
52 #include "ncrypt/crypt_gpgme.h"
53 #include "options.h"
54 #else
55 struct Envelope;
56 #endif
57 
58 struct Address;
59 struct AddressList;
60 struct Mailbox;
61 struct State;
62 
63 /* These Config Variables are only used in ncrypt/cryptglue.c */
64 bool C_CryptUseGpgme;
65 
66 #ifdef CRYPT_BACKEND_CLASSIC_PGP
68 #endif
69 
70 #ifdef CRYPT_BACKEND_CLASSIC_SMIME
72 #endif
73 
74 #ifdef CRYPT_BACKEND_GPGME
77 #endif
78 
79 /* If the crypto module identifier by IDENTIFIER has been registered,
80  * call its function FUNC. Do nothing else. This may be used as an
81  * expression. */
82 #define CRYPT_MOD_CALL_CHECK(identifier, func) \
83  (crypto_module_lookup(APPLICATION_##identifier) && \
84  (crypto_module_lookup(APPLICATION_##identifier))->func)
85 
86 /* Call the function FUNC in the crypto module identified by
87  * IDENTIFIER. This may be used as an expression. */
88 #define CRYPT_MOD_CALL(identifier, func) \
89  (*(crypto_module_lookup(APPLICATION_##identifier))->func)
90 
96 void crypt_init(void)
97 {
98 #ifdef CRYPT_BACKEND_CLASSIC_PGP
99  if (
100 #ifdef CRYPT_BACKEND_GPGME
101  (!C_CryptUseGpgme)
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_CryptUseGpgme)
113 #else
114  1
115 #endif
116  )
118 #endif
119 
120  if (C_CryptUseGpgme)
121  {
122 #ifdef CRYPT_BACKEND_GPGME
125 #else
126  mutt_message(_("\"crypt_use_gpgme\" set"
127  " but not built with GPGME support"));
128  if (mutt_any_key_to_continue(NULL) == -1)
129  mutt_exit(1);
130 #endif
131  }
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 
150 {
151  if (((WithCrypto & APPLICATION_PGP) != 0) && (type & APPLICATION_PGP))
152  mutt_message(_("Invoking PGP..."));
153  else if (((WithCrypto & APPLICATION_SMIME) != 0) && (type & APPLICATION_SMIME))
154  mutt_message(_("Invoking S/MIME..."));
155 }
156 
164 {
165  if (((WithCrypto & APPLICATION_PGP) != 0) && (type & APPLICATION_PGP) &&
166  crypto_module_lookup(APPLICATION_PGP))
167  {
168  return true;
169  }
170 
171  if (((WithCrypto & APPLICATION_SMIME) != 0) && (type & APPLICATION_SMIME) &&
172  crypto_module_lookup(APPLICATION_SMIME))
173  {
174  return true;
175  }
176 
177  return false;
178 }
179 
184 {
187 }
188 
193 {
195  return CRYPT_MOD_CALL(PGP, valid_passphrase)();
196 
197  return false;
198 }
199 
203 int crypt_pgp_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **cur)
204 {
205 #ifdef USE_AUTOCRYPT
206  if (C_Autocrypt)
207  {
208  OptAutocryptGpgme = true;
209  int result = pgp_gpgme_decrypt_mime(fp_in, fp_out, b, cur);
210  OptAutocryptGpgme = false;
211  if (result == 0)
212  {
213  b->is_autocrypt = true;
214  return result;
215  }
216  }
217 #endif
218 
220  return CRYPT_MOD_CALL(PGP, decrypt_mime)(fp_in, fp_out, b, cur);
221 
222  return -1;
223 }
224 
230 int crypt_pgp_application_handler(struct Body *m, struct State *s)
231 {
233  return CRYPT_MOD_CALL(PGP, application_handler)(m, s);
234 
235  return -1;
236 }
237 
243 int crypt_pgp_encrypted_handler(struct Body *a, struct State *s)
244 {
245 #ifdef USE_AUTOCRYPT
246  if (C_Autocrypt)
247  {
248  OptAutocryptGpgme = true;
249  int result = pgp_gpgme_encrypted_handler(a, s);
250  OptAutocryptGpgme = false;
251  if (result == 0)
252  {
253  a->is_autocrypt = true;
254  return result;
255  }
256  }
257 #endif
258 
260  return CRYPT_MOD_CALL(PGP, encrypted_handler)(a, s);
261 
262  return -1;
263 }
264 
269 {
272 }
273 
277 int crypt_pgp_check_traditional(FILE *fp, struct Body *b, bool just_one)
278 {
280  return CRYPT_MOD_CALL(PGP, pgp_check_traditional)(fp, b, just_one);
281 
282  return 0;
283 }
284 
288 struct Body *crypt_pgp_traditional_encryptsign(struct Body *a, int flags, char *keylist)
289 {
290  if (CRYPT_MOD_CALL_CHECK(PGP, pgp_traditional_encryptsign))
291  return CRYPT_MOD_CALL(PGP, pgp_traditional_encryptsign)(a, flags, keylist);
292 
293  return NULL;
294 }
295 
300 {
301  if (CRYPT_MOD_CALL_CHECK(PGP, pgp_make_key_attachment))
302  return CRYPT_MOD_CALL(PGP, pgp_make_key_attachment)();
303 
304  return NULL;
305 }
306 
310 char *crypt_pgp_find_keys(struct AddressList *addrlist, bool oppenc_mode)
311 {
313  return CRYPT_MOD_CALL(PGP, find_keys)(addrlist, oppenc_mode);
314 
315  return NULL;
316 }
317 
321 struct Body *crypt_pgp_sign_message(struct Body *a, const struct AddressList *from)
322 {
324  return CRYPT_MOD_CALL(PGP, sign_message)(a, from);
325 
326  return NULL;
327 }
328 
332 struct Body *crypt_pgp_encrypt_message(struct Email *e, struct Body *a, char *keylist,
333  int sign, const struct AddressList *from)
334 {
335 #ifdef USE_AUTOCRYPT
336  if (e->security & SEC_AUTOCRYPT)
337  {
339  return NULL;
340 
341  OptAutocryptGpgme = true;
342  struct Body *result = pgp_gpgme_encrypt_message(a, keylist, sign, from);
343  OptAutocryptGpgme = false;
344 
345  return result;
346  }
347 #endif
348 
349  if (CRYPT_MOD_CALL_CHECK(PGP, pgp_encrypt_message))
350  return CRYPT_MOD_CALL(PGP, pgp_encrypt_message)(a, keylist, sign, from);
351 
352  return NULL;
353 }
354 
358 void crypt_pgp_invoke_import(const char *fname)
359 {
360  if (CRYPT_MOD_CALL_CHECK(PGP, pgp_invoke_import))
361  CRYPT_MOD_CALL(PGP, pgp_invoke_import)(fname);
362 }
363 
367 int crypt_pgp_verify_one(struct Body *sigbdy, struct State *s, const char *tempf)
368 {
370  return CRYPT_MOD_CALL(PGP, verify_one)(sigbdy, s, tempf);
371 
372  return -1;
373 }
374 
379 {
380  if (CRYPT_MOD_CALL_CHECK(PGP, send_menu))
381  return CRYPT_MOD_CALL(PGP, send_menu)(e);
382 
383  return 0;
384 }
385 
389 void crypt_pgp_extract_key_from_attachment(FILE *fp, struct Body *top)
390 {
391  if (CRYPT_MOD_CALL_CHECK(PGP, pgp_extract_key_from_attachment))
392  CRYPT_MOD_CALL(PGP, pgp_extract_key_from_attachment)(fp, top);
393 }
394 
398 void crypt_pgp_set_sender(const char *sender)
399 {
400  if (CRYPT_MOD_CALL_CHECK(PGP, set_sender))
401  CRYPT_MOD_CALL(PGP, set_sender)(sender);
402 }
403 
408 {
409  if (CRYPT_MOD_CALL_CHECK(SMIME, void_passphrase))
410  CRYPT_MOD_CALL(SMIME, void_passphrase)();
411 }
412 
417 {
418  if (CRYPT_MOD_CALL_CHECK(SMIME, valid_passphrase))
419  return CRYPT_MOD_CALL(SMIME, valid_passphrase)();
420 
421  return false;
422 }
423 
427 int crypt_smime_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **cur)
428 {
429  if (CRYPT_MOD_CALL_CHECK(SMIME, decrypt_mime))
430  return CRYPT_MOD_CALL(SMIME, decrypt_mime)(fp_in, fp_out, b, cur);
431 
432  return -1;
433 }
434 
440 int crypt_smime_application_handler(struct Body *m, struct State *s)
441 {
442  if (CRYPT_MOD_CALL_CHECK(SMIME, application_handler))
443  return CRYPT_MOD_CALL(SMIME, application_handler)(m, s);
444 
445  return -1;
446 }
447 
451 void crypt_smime_getkeys(struct Envelope *env)
452 {
453  if (CRYPT_MOD_CALL_CHECK(SMIME, smime_getkeys))
454  CRYPT_MOD_CALL(SMIME, smime_getkeys)(env);
455 }
456 
460 int crypt_smime_verify_sender(struct Mailbox *m, struct Email *e)
461 {
462  if (CRYPT_MOD_CALL_CHECK(SMIME, smime_verify_sender))
463  return CRYPT_MOD_CALL(SMIME, smime_verify_sender)(m, e);
464 
465  return 1;
466 }
467 
471 char *crypt_smime_find_keys(struct AddressList *addrlist, bool oppenc_mode)
472 {
473  if (CRYPT_MOD_CALL_CHECK(SMIME, find_keys))
474  return CRYPT_MOD_CALL(SMIME, find_keys)(addrlist, oppenc_mode);
475 
476  return NULL;
477 }
478 
482 struct Body *crypt_smime_sign_message(struct Body *a, const struct AddressList *from)
483 {
485  return CRYPT_MOD_CALL(SMIME, sign_message)(a, from);
486 
487  return NULL;
488 }
489 
493 struct Body *crypt_smime_build_smime_entity(struct Body *a, char *certlist)
494 {
495  if (CRYPT_MOD_CALL_CHECK(SMIME, smime_build_smime_entity))
496  return CRYPT_MOD_CALL(SMIME, smime_build_smime_entity)(a, certlist);
497 
498  return NULL;
499 }
500 
504 void crypt_smime_invoke_import(const char *infile, const char *mailbox)
505 {
506  if (CRYPT_MOD_CALL_CHECK(SMIME, smime_invoke_import))
507  CRYPT_MOD_CALL(SMIME, smime_invoke_import)(infile, mailbox);
508 }
509 
513 int crypt_smime_verify_one(struct Body *sigbdy, struct State *s, const char *tempf)
514 {
515  if (CRYPT_MOD_CALL_CHECK(SMIME, verify_one))
516  return CRYPT_MOD_CALL(SMIME, verify_one)(sigbdy, s, tempf);
517 
518  return -1;
519 }
520 
525 {
526  if (CRYPT_MOD_CALL_CHECK(SMIME, send_menu))
527  return CRYPT_MOD_CALL(SMIME, send_menu)(e);
528 
529  return 0;
530 }
531 
535 void crypt_smime_set_sender(const char *sender)
536 {
537  if (CRYPT_MOD_CALL_CHECK(SMIME, set_sender))
538  CRYPT_MOD_CALL(SMIME, set_sender)(sender);
539 }
Convenience wrapper for the gui headers.
#define SEC_AUTOCRYPT
Message will be, or was Autocrypt encrypt+signed.
Definition: ncrypt.h:131
WHERE bool C_Autocrypt
Config: Enables the Autocrypt feature.
Definition: globals.h:198
The envelope/body of an email.
Definition: email.h:37
int(* encrypted_handler)(struct Body *m, struct State *s)
Manage a PGP or S/MIME encrypted MIME part.
Definition: crypt_mod.h:90
char * crypt_smime_find_keys(struct AddressList *addrlist, bool oppenc_mode)
Wrapper for CryptModuleSpecs::find_keys()
Definition: cryptglue.c:471
struct CryptModuleSpecs CryptModPgpGpgme
GPGME PGP - Implements CryptModuleSpecs.
Wrapper for PGP/SMIME calls to GPGME.
Structs that make up an email.
int crypt_pgp_application_handler(struct Body *m, struct State *s)
Wrapper for CryptModuleSpecs::application_handler()
Definition: cryptglue.c:230
#define mutt_message(...)
Definition: logging.h:83
Wrapper around crypto functions.
void crypt_pgp_void_passphrase(void)
Wrapper for CryptModuleSpecs::void_passphrase()
Definition: cryptglue.c:183
bool crypt_smime_valid_passphrase(void)
Wrapper for CryptModuleSpecs::valid_passphrase()
Definition: cryptglue.c:416
bool C_CryptUseGpgme
Config: Use GPGME crypto backend.
Definition: cryptglue.c:61
int(* application_handler)(struct Body *m, struct State *s)
Manage the MIME type "application/pgp" or "application/smime".
Definition: crypt_mod.h:82
void(* void_passphrase)(void)
Forget the cached passphrase.
Definition: crypt_mod.h:55
int(* pgp_check_traditional)(FILE *fp, struct Body *b, bool just_one)
Look for inline (non-MIME) PGP content.
Definition: crypt_mod.h:158
void crypt_pgp_set_sender(const char *sender)
Wrapper for CryptModuleSpecs::set_sender()
Definition: cryptglue.c:398
#define _(a)
Definition: message.h:28
bool is_autocrypt
Flag autocrypt-decrypted messages for replying.
Definition: body.h:79
An email address.
Definition: address.h:34
WHERE bool OptAutocryptGpgme
(pseudo) use Autocrypt context inside ncrypt/crypt_gpgme.c
Definition: options.h:32
#define CRYPT_MOD_CALL(identifier, func)
Definition: cryptglue.c:88
static char * find_keys(struct AddressList *addrlist, unsigned int app, bool oppenc_mode)
Find keys of the recipients of the message.
Definition: crypt_gpgme.c:5232
int crypt_smime_verify_sender(struct Mailbox *m, struct Email *e)
Wrapper for CryptModuleSpecs::smime_verify_sender()
Definition: cryptglue.c:460
void crypto_module_register(struct CryptModuleSpecs *specs)
Register a new crypto module.
Definition: crypt_mod.c:51
int crypt_smime_application_handler(struct Body *m, struct State *s)
Wrapper for CryptModuleSpecs::application_handler()
Definition: cryptglue.c:440
struct Body * pgp_gpgme_encrypt_message(struct Body *a, char *keylist, bool sign, const struct AddressList *from)
Implements CryptModuleSpecs::pgp_encrypt_message()
Definition: crypt_gpgme.c:1572
The body of an email.
Definition: body.h:34
Hundreds of global variables to back the user variables.
struct CryptModuleSpecs CryptModSmimeClassic
CLI SMIME - Implements CryptModuleSpecs.
void crypt_smime_set_sender(const char *sender)
Wrapper for CryptModuleSpecs::set_sender()
Definition: cryptglue.c:535
struct Body * crypt_smime_sign_message(struct Body *a, const struct AddressList *from)
Wrapper for CryptModuleSpecs::sign_message()
Definition: cryptglue.c:482
int pgp_gpgme_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **cur)
Implements CryptModuleSpecs::decrypt_mime()
Definition: crypt_gpgme.c:2379
struct CryptModuleSpecs CryptModPgpClassic
CLI PGP - Implements CryptModuleSpecs.
int crypt_smime_send_menu(struct Email *e)
Wrapper for CryptModuleSpecs::send_menu()
Definition: cryptglue.c:524
int pgp_gpgme_encrypted_handler(struct Body *a, struct State *s)
Implements CryptModuleSpecs::encrypted_handler()
Definition: crypt_gpgme.c:3227
int crypt_pgp_encrypted_handler(struct Body *a, struct State *s)
Wrapper for CryptModuleSpecs::encrypted_handler()
Definition: cryptglue.c:243
int(* decrypt_mime)(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **cur)
Decrypt an encrypted MIME part.
Definition: crypt_mod.h:74
void crypt_smime_void_passphrase(void)
Wrapper for CryptModuleSpecs::void_passphrase()
Definition: cryptglue.c:407
void crypt_pgp_extract_key_from_attachment(FILE *fp, struct Body *top)
Wrapper for CryptModuleSpecs::pgp_extract_key_from_attachment()
Definition: cryptglue.c:389
int mutt_autocrypt_set_sign_as_default_key(struct Email *e)
Set the Autocrypt default key for signing.
Definition: autocrypt.c:674
struct Body * crypt_pgp_encrypt_message(struct Email *e, struct Body *a, char *keylist, int sign, const struct AddressList *from)
Wrapper for CryptModuleSpecs::pgp_encrypt_message()
Definition: cryptglue.c:332
void crypt_invoke_message(SecurityFlags type)
Display an informative message.
Definition: cryptglue.c:149
#define CRYPT_MOD_CALL_CHECK(identifier, func)
Definition: cryptglue.c:82
void crypt_pgp_invoke_import(const char *fname)
Wrapper for CryptModuleSpecs::pgp_invoke_import()
Definition: cryptglue.c:358
A mailbox.
Definition: mailbox.h:80
Crypto API.
Definition: crypt_mod.h:44
void crypt_smime_getkeys(struct Envelope *env)
Wrapper for CryptModuleSpecs::smime_getkeys()
Definition: cryptglue.c:451
void crypt_pgp_invoke_getkeys(struct Address *addr)
Wrapper for CryptModuleSpecs::pgp_invoke_getkeys()
Definition: cryptglue.c:268
int crypt_pgp_send_menu(struct Email *e)
Wrapper for CryptModuleSpecs::send_menu()
Definition: cryptglue.c:378
struct Body * crypt_pgp_traditional_encryptsign(struct Body *a, int flags, char *keylist)
Wrapper for CryptModuleSpecs::pgp_traditional_encryptsign()
Definition: cryptglue.c:288
int crypt_smime_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **cur)
Wrapper for CryptModuleSpecs::decrypt_mime()
Definition: cryptglue.c:427
API for encryption/signing of emails.
void crypt_init(void)
Initialise the crypto backends.
Definition: cryptglue.c:96
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/ncrypt.h pgplib.h, smime.h
Definition: email.h:39
bool(* valid_passphrase)(void)
Ensure we have a valid passphrase.
Definition: crypt_mod.h:64
struct CryptModuleSpecs * crypto_module_lookup(int identifier)
Lookup a crypto module by name.
Definition: crypt_mod.c:65
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: ncrypt.h:134
static int verify_one(struct Body *sigbdy, struct State *s, const char *tempfile, bool is_smime)
Do the actual verification step.
Definition: crypt_gpgme.c:2076
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:207
bool crypt_has_module_backend(SecurityFlags type)
Is there a crypto backend for a given type?
Definition: cryptglue.c:163
int crypt_pgp_check_traditional(FILE *fp, struct Body *b, bool just_one)
Wrapper for CryptModuleSpecs::pgp_check_traditional()
Definition: cryptglue.c:277
int mutt_any_key_to_continue(const char *s)
Prompt the user to &#39;press any key&#39; and wait.
Definition: curs_lib.c:578
struct Body * crypt_smime_build_smime_entity(struct Body *a, char *certlist)
Wrapper for CryptModuleSpecs::smime_build_smime_entity()
Definition: cryptglue.c:493
struct Body * crypt_pgp_make_key_attachment(void)
Wrapper for CryptModuleSpecs::pgp_make_key_attachment()
Definition: cryptglue.c:299
char * crypt_pgp_find_keys(struct AddressList *addrlist, bool oppenc_mode)
Wrapper for CryptModuleSpecs::find_keys()
Definition: cryptglue.c:310
void crypt_smime_invoke_import(const char *infile, const char *mailbox)
Wrapper for CryptModuleSpecs::smime_invoke_import()
Definition: cryptglue.c:504
int crypt_pgp_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **cur)
Wrapper for CryptModuleSpecs::decrypt_mime()
Definition: cryptglue.c:203
static struct Body * sign_message(struct Body *a, const struct AddressList *from, bool use_smime)
Sign a message.
Definition: crypt_gpgme.c:1435
Autocrypt end-to-end encryption.
struct Body * crypt_pgp_sign_message(struct Body *a, const struct AddressList *from)
Wrapper for CryptModuleSpecs::sign_message()
Definition: cryptglue.c:321
int crypt_smime_verify_one(struct Body *sigbdy, struct State *s, const char *tempf)
Wrapper for CryptModuleSpecs::verify_one()
Definition: cryptglue.c:513
Keep track when processing files.
Definition: state.h:44
Handling of global boolean variables.
uint16_t SecurityFlags
Flags, e.g. SEC_ENCRYPT.
Definition: ncrypt.h:120
#define WithCrypto
Definition: ncrypt.h:160
void(* pgp_invoke_getkeys)(struct Address *addr)
Run a command to download a PGP key.
Definition: crypt_mod.h:172
void(* init)(void)
Initialise the crypto module.
Definition: crypt_mod.h:51
bool crypt_pgp_valid_passphrase(void)
Wrapper for CryptModuleSpecs::valid_passphrase()
Definition: cryptglue.c:192
struct CryptModuleSpecs CryptModSmimeGpgme
GPGME SMIME - Implements CryptModuleSpecs.
Register crypto modules.
The header of an Email.
Definition: envelope.h:54
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition: ncrypt.h:135
int crypt_pgp_verify_one(struct Body *sigbdy, struct State *s, const char *tempf)
Wrapper for CryptModuleSpecs::verify_one()
Definition: cryptglue.c:367