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