Nico Golde:
[apps/madmutt.git] / cryptglue.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2003  Werner Koch <wk@gnupg.org>
4  * Copyright (C) 2004 g10 Code GmbH
5  *
6  * This file is part of mutt-ng, see http://www.muttng.org/.
7  * It's licensed under the GNU General Public License,
8  * please see the file GPL in the top level source directory.
9  */
10
11 /*
12    This file dispatches the generic crypto functions to the
13    implemented backend or provides dummy stubs.  Note, that some
14    generic functions are handled in crypt.c.
15 */
16
17 /* Note: This file has been changed to make use of the new module
18    system.  Consequently there's a 1:1 mapping between the functions
19    contained in this file and the functions implemented by the crypto
20    modules.  */
21
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include "mutt.h"
27 #include "mutt_crypt.h"
28
29 #include "crypt-mod.h"
30
31 /*
32     
33     Generic
34
35 */
36
37 #ifdef CRYPT_BACKEND_CLASSIC_PGP
38 extern struct crypt_module_specs crypt_mod_pgp_classic;
39 #endif
40
41 #ifdef CRYPT_BACKEND_CLASSIC_SMIME
42 extern struct crypt_module_specs crypt_mod_smime_classic;
43 #endif
44
45 #ifdef CRYPT_BACKEND_GPGME
46 extern struct crypt_module_specs crypt_mod_pgp_gpgme;
47 extern struct crypt_module_specs crypt_mod_smime_gpgme;
48 #endif
49
50 void crypt_init (void)
51 {
52 #ifdef CRYPT_BACKEND_CLASSIC_PGP
53   if (
54 #ifdef CRYPT_BACKEND_GPGME
55        (!option (OPTCRYPTUSEGPGME))
56 #else
57        1
58 #endif
59     )
60     crypto_module_register (&crypt_mod_pgp_classic);
61 #endif
62
63 #ifdef CRYPT_BACKEND_CLASSIC_SMIME
64   if (
65 #ifdef CRYPT_BACKEND_GPGME
66        (!option (OPTCRYPTUSEGPGME))
67 #else
68        1
69 #endif
70     )
71     crypto_module_register (&crypt_mod_smime_classic);
72 #endif
73
74   if (option (OPTCRYPTUSEGPGME)) {
75 #ifdef CRYPT_BACKEND_GPGME
76     crypto_module_register (&crypt_mod_pgp_gpgme);
77     crypto_module_register (&crypt_mod_smime_gpgme);
78 #else
79     mutt_message (_("\"crypt_use_gpgme\" set"
80                     " but not build with GPGME support."));
81     if (mutt_any_key_to_continue (NULL) == -1)
82       mutt_exit (1);
83 #endif
84   }
85
86 #if defined CRYPT_BACKEND_CLASSIG_PGP || defined CRYPT_BACKEND_CLASSIG_SMIME || defined CRYPT_BACKEND_GPGME
87   if (CRYPT_MOD_CALL_CHECK (PGP, init))
88     (CRYPT_MOD_CALL (PGP, init)) ();
89
90   if (CRYPT_MOD_CALL_CHECK (SMIME, init))
91     (CRYPT_MOD_CALL (SMIME, init)) ();
92 #endif
93 }
94
95
96 /* Show a message that a backend will be invoked. */
97 void crypt_invoke_message (int type)
98 {
99   if ((WithCrypto & APPLICATION_PGP) && (type & APPLICATION_PGP)) {
100     mutt_message _("Invoking PGP...");
101   }
102   else if ((WithCrypto & APPLICATION_SMIME) && (type & APPLICATION_SMIME)) {
103     mutt_message _("Invoking SMIME...");
104   }
105 }
106 \f
107
108
109 /* 
110
111     PGP
112
113 */
114
115
116 /* Reset a PGP passphrase */
117 void crypt_pgp_void_passphrase (void)
118 {
119   if (CRYPT_MOD_CALL_CHECK (PGP, void_passphrase))
120     (CRYPT_MOD_CALL (PGP, void_passphrase)) ();
121 }
122
123 int crypt_pgp_valid_passphrase (void)
124 {
125   if (CRYPT_MOD_CALL_CHECK (PGP, valid_passphrase))
126     return (CRYPT_MOD_CALL (PGP, valid_passphrase)) ();
127
128   return 0;
129 }
130
131
132 /* Decrypt a PGP/MIME message. */
133 int crypt_pgp_decrypt_mime (FILE * a, FILE ** b, BODY * c, BODY ** d)
134 {
135   if (CRYPT_MOD_CALL_CHECK (PGP, decrypt_mime))
136     return (CRYPT_MOD_CALL (PGP, decrypt_mime)) (a, b, c, d);
137
138   return -1;
139 }
140
141 /* MIME handler for the application/pgp content-type. */
142 void crypt_pgp_application_pgp_handler (BODY * m, STATE * s)
143 {
144   if (CRYPT_MOD_CALL_CHECK (PGP, application_handler))
145     (CRYPT_MOD_CALL (PGP, application_handler)) (m, s);
146 }
147
148 /* MIME handler for an PGP/MIME encrypted message. */
149 void crypt_pgp_encrypted_handler (BODY * a, STATE * s)
150 {
151   if (CRYPT_MOD_CALL_CHECK (PGP, encrypted_handler))
152     (CRYPT_MOD_CALL (PGP, encrypted_handler)) (a, s);
153 }
154
155 /* fixme: needs documentation. */
156 void crypt_pgp_invoke_getkeys (ADDRESS * addr)
157 {
158   if (CRYPT_MOD_CALL_CHECK (PGP, pgp_invoke_getkeys))
159     (CRYPT_MOD_CALL (PGP, pgp_invoke_getkeys)) (addr);
160 }
161
162 /* Check for a traditional PGP message in body B. */
163 int crypt_pgp_check_traditional (FILE * fp, BODY * b, int tagged_only)
164 {
165   if (CRYPT_MOD_CALL_CHECK (PGP, pgp_check_traditional))
166     return (CRYPT_MOD_CALL (PGP, pgp_check_traditional)) (fp, b, tagged_only);
167
168   return 0;
169 }
170
171 /* fixme: needs documentation. */
172 BODY *crypt_pgp_traditional_encryptsign (BODY * a, int flags, char *keylist)
173 {
174   if (CRYPT_MOD_CALL_CHECK (PGP, pgp_traditional_encryptsign))
175     return (CRYPT_MOD_CALL (PGP, pgp_traditional_encryptsign)) (a, flags,
176                                                                 keylist);
177
178   return NULL;
179 }
180
181 /* Generate a PGP public key attachment. */
182 BODY *crypt_pgp_make_key_attachment (char *tempf)
183 {
184   if (CRYPT_MOD_CALL_CHECK (PGP, pgp_make_key_attachment))
185     return (CRYPT_MOD_CALL (PGP, pgp_make_key_attachment)) (tempf);
186
187   return NULL;
188 }
189
190 /* This routine attempts to find the keyids of the recipients of a
191    message.  It returns NULL if any of the keys can not be found.  */
192 char *crypt_pgp_findkeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
193 {
194   if (CRYPT_MOD_CALL_CHECK (PGP, findkeys))
195     return (CRYPT_MOD_CALL (PGP, findkeys)) (to, cc, bcc);
196
197   return NULL;
198 }
199
200 /* Create a new body with a PGP signed message from A. */
201 BODY *crypt_pgp_sign_message (BODY * a)
202 {
203   if (CRYPT_MOD_CALL_CHECK (PGP, sign_message))
204     return (CRYPT_MOD_CALL (PGP, sign_message)) (a);
205
206   return NULL;
207 }
208
209 /* Warning: A is no longer freed in this routine, you need to free it
210    later.  This is necessary for $fcc_attach. */
211 BODY *crypt_pgp_encrypt_message (BODY * a, char *keylist, int sign)
212 {
213   if (CRYPT_MOD_CALL_CHECK (PGP, pgp_encrypt_message))
214     return (CRYPT_MOD_CALL (PGP, pgp_encrypt_message)) (a, keylist, sign);
215
216   return NULL;
217 }
218
219 /* Invoke the PGP command to import a key. */
220 void crypt_pgp_invoke_import (const char *fname)
221 {
222   if (CRYPT_MOD_CALL_CHECK (PGP, pgp_invoke_import))
223     (CRYPT_MOD_CALL (PGP, pgp_invoke_import)) (fname);
224 }
225
226 /* fixme: needs documentation */
227 int crypt_pgp_verify_one (BODY * sigbdy, STATE * s, const char *tempf)
228 {
229   if (CRYPT_MOD_CALL_CHECK (PGP, verify_one))
230     return (CRYPT_MOD_CALL (PGP, verify_one)) (sigbdy, s, tempf);
231
232   return -1;
233 }
234
235
236 int crypt_pgp_send_menu (HEADER * msg, int *redraw)
237 {
238   if (CRYPT_MOD_CALL_CHECK (PGP, send_menu))
239     return (CRYPT_MOD_CALL (PGP, send_menu)) (msg, redraw);
240
241   return 0;
242 }
243
244
245 /* fixme: needs documentation */
246 void crypt_pgp_extract_keys_from_attachment_list (FILE * fp, int tag,
247                                                   BODY * top)
248 {
249   if (CRYPT_MOD_CALL_CHECK (PGP, pgp_extract_keys_from_attachment_list))
250     (CRYPT_MOD_CALL (PGP, pgp_extract_keys_from_attachment_list)) (fp, tag,
251                                                                    top);
252 }
253 \f
254
255
256 /* 
257
258    S/MIME 
259
260 */
261
262
263 /* Reset an SMIME passphrase */
264 void crypt_smime_void_passphrase (void)
265 {
266   if (CRYPT_MOD_CALL_CHECK (SMIME, void_passphrase))
267     (CRYPT_MOD_CALL (SMIME, void_passphrase)) ();
268 }
269
270 int crypt_smime_valid_passphrase (void)
271 {
272   if (CRYPT_MOD_CALL_CHECK (SMIME, valid_passphrase))
273     return (CRYPT_MOD_CALL (SMIME, valid_passphrase)) ();
274
275   return 0;
276 }
277
278 /* Decrypt am S/MIME message. */
279 int crypt_smime_decrypt_mime (FILE * a, FILE ** b, BODY * c, BODY ** d)
280 {
281   if (CRYPT_MOD_CALL_CHECK (SMIME, decrypt_mime))
282     return (CRYPT_MOD_CALL (SMIME, decrypt_mime)) (a, b, c, d);
283
284   return -1;
285 }
286
287 /* MIME handler for the application/smime content-type. */
288 void crypt_smime_application_smime_handler (BODY * m, STATE * s)
289 {
290   if (CRYPT_MOD_CALL_CHECK (SMIME, application_handler))
291     (CRYPT_MOD_CALL (SMIME, application_handler)) (m, s);
292 }
293
294 /* MIME handler for an PGP/MIME encrypted message. */
295 void crypt_smime_encrypted_handler (BODY * a, STATE * s)
296 {
297   if (CRYPT_MOD_CALL_CHECK (SMIME, encrypted_handler))
298     (CRYPT_MOD_CALL (SMIME, encrypted_handler)) (a, s);
299 }
300
301 /* fixme: Needs documentation. */
302 void crypt_smime_getkeys (ENVELOPE * env)
303 {
304   if (CRYPT_MOD_CALL_CHECK (SMIME, smime_getkeys))
305     (CRYPT_MOD_CALL (SMIME, smime_getkeys)) (env);
306 }
307
308 /* Check that the sender matches. */
309 int crypt_smime_verify_sender (HEADER * h)
310 {
311   if (CRYPT_MOD_CALL_CHECK (SMIME, smime_verify_sender))
312     return (CRYPT_MOD_CALL (SMIME, smime_verify_sender)) (h);
313
314   return 1;
315 }
316
317 /* This routine attempts to find the keyids of the recipients of a
318    message.  It returns NULL if any of the keys can not be found.  */
319 char *crypt_smime_findkeys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc)
320 {
321   if (CRYPT_MOD_CALL_CHECK (SMIME, findkeys))
322     return (CRYPT_MOD_CALL (SMIME, findkeys)) (to, cc, bcc);
323
324   return NULL;
325 }
326
327 /* fixme: Needs documentation. */
328 BODY *crypt_smime_sign_message (BODY * a)
329 {
330   if (CRYPT_MOD_CALL_CHECK (SMIME, sign_message))
331     return (CRYPT_MOD_CALL (SMIME, sign_message)) (a);
332
333   return NULL;
334 }
335
336 /* fixme: needs documentation. */
337 BODY *crypt_smime_build_smime_entity (BODY * a, char *certlist)
338 {
339   if (CRYPT_MOD_CALL_CHECK (SMIME, smime_build_smime_entity))
340     return (CRYPT_MOD_CALL (SMIME, smime_build_smime_entity)) (a, certlist);
341
342   return NULL;
343 }
344
345 /* Add a certificate and update index file (externally). */
346 void crypt_smime_invoke_import (char *infile, char *mailbox)
347 {
348   if (CRYPT_MOD_CALL_CHECK (SMIME, smime_invoke_import))
349     (CRYPT_MOD_CALL (SMIME, smime_invoke_import)) (infile, mailbox);
350 }
351
352 /* fixme: needs documentation */
353 int crypt_smime_verify_one (BODY * sigbdy, STATE * s, const char *tempf)
354 {
355   if (CRYPT_MOD_CALL_CHECK (SMIME, verify_one))
356     return (CRYPT_MOD_CALL (SMIME, verify_one)) (sigbdy, s, tempf);
357
358   return -1;
359 }
360
361 int crypt_smime_send_menu (HEADER * msg, int *redraw)
362 {
363   if (CRYPT_MOD_CALL_CHECK (SMIME, send_menu))
364     return (CRYPT_MOD_CALL (SMIME, send_menu)) (msg, redraw);
365
366   return 0;
367 }