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