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