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