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