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