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