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