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