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