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