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