Andreas Krennmair:
[apps/madmutt.git] / cryptglue.c
1 /*
2  * Copyright (C) 2003  Werner Koch <wk@gnupg.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /*
20    This file dispatches the generic crytpo functions to the implemented
21    backend or provides dummy stubs.  Note, that some generic functions are
22    handled in crypt.c.
23 */
24
25 #include "mutt.h"
26 #include "mutt_crypt.h"
27
28 /* Make sure those macros are not defined. */
29 #undef BFNC_PGP_VOID_PASSPHRASE     
30 #undef BFNC_PGP_DECRYPT_MIME           
31 #undef BFNC_PGP_APPLICATION_PGP_HANDLER
32 #undef BFNC_PGP_ENCRYPTED_HANDLER
33 #undef BFNC_PGP_INVOKE_GETKEYS
34 #undef BFNC_PGP_ASK_FOR_KEY
35 #undef BNFC_PGP_CHECK_TRADITIONAL
36 #undef BFNC_PGP_TRADITIONAL_ENCRYPTSIGN  
37 #undef BFNC_PGP_FREE_KEY
38 #undef BFNC_PGP_MAKE_KEY_ATTACHMENT 
39 #undef BFNC_PGP_FINDKEYS
40 #undef BFNC_PGP_SIGN_MESSAGE
41 #undef BFNC_PGP_ENCRYPT_MESSAGE
42 #undef BFNC_PGP_INVOKE_IMPORT
43 #undef BFNC_PGP_VERIFY_ONE
44 #undef BFNC_PGP_KEYID
45 #undef BFNC_PGP_EXTRACT_KEYS_FROM_ATTACHMENT_LIST
46
47 #undef BFNC_SMIME_VOID_PASSPHRASE 
48 #undef BFNC_SMIME_DECRYPT_MIME    
49 #undef BFNC_SMIME_APPLICATION_SMIME_HANDLER 
50 #undef BFNC_SMIME_GETKEYS  
51 #undef BFNC_SMIME_VERIFY_SENDER
52 #undef BFNC_SMIME_ASK_FOR_KEY
53 #undef BFNC_SMIME_FINDKEYS
54 #undef BFNC_SMIME_SIGN_MESSAGE
55 #undef BFNC_SMIME_BUILD_SMIME_ENTITY
56 #undef BFNC_SMIME_INVOKE_IMPORT
57 #undef BFNC_SMIME_VERIFY_ONE
58
59
60 /* The PGP backend */
61 #if defined (CRYPT_BACKEND_CLASSIC_PGP)
62 # include "pgp.h"
63 # define BFNC_PGP_VOID_PASSPHRASE         pgp_void_passphrase
64 # define BFNC_PGP_DECRYPT_MIME            pgp_decrypt_mime
65 # define BFNC_PGP_APPLICATION_PGP_HANDLER pgp_application_pgp_handler
66 # define BFNC_PGP_ENCRYPTED_HANDLER       pgp_encrypted_handler
67 # define BFNC_PGP_INVOKE_GETKEYS          pgp_invoke_getkeys
68 # define BFNC_PGP_ASK_FOR_KEY             pgp_ask_for_key
69 # define BNFC_PGP_CHECK_TRADITIONAL       pgp_check_traditional
70 # define BFNC_PGP_TRADITIONAL_ENCRYPTSIGN pgp_traditional_encryptsign 
71 # define BFNC_PGP_FREE_KEY                pgp_free_key
72 # define BFNC_PGP_MAKE_KEY_ATTACHMENT     pgp_make_key_attachment
73 # define BFNC_PGP_FINDKEYS                pgp_findKeys
74 # define BFNC_PGP_SIGN_MESSAGE            pgp_sign_message
75 # define BFNC_PGP_ENCRYPT_MESSAGE         pgp_encrypt_message
76 # define BFNC_PGP_INVOKE_IMPORT           pgp_invoke_import
77 # define BFNC_PGP_VERIFY_ONE              pgp_verify_one
78 # define BFNC_PGP_KEYID                   pgp_keyid
79 # define BFNC_PGP_EXTRACT_KEYS_FROM_ATTACHMENT_LIST \
80                                        pgp_extract_keys_from_attachment_list
81
82
83 #elif defined (CRYPT_BACKEND_GPGME)
84 # include "crypt-gpgme.h"
85 # define BFNC_PGP_VOID_PASSPHRASE NULL /* not required */
86 # define BFNC_PGP_DECRYPT_MIME     gpg_pgp_decrypt_mime
87
88 #endif /* PGP backend */
89
90
91 /* The SMIME backend */
92 #ifdef CRYPT_BACKEND_CLASSIC_SMIME
93 # include "smime.h"
94 # define BFNC_SMIME_VOID_PASSPHRASE           smime_void_passphrase
95 # define BFNC_SMIME_DECRYPT_MIME              smime_decrypt_mime
96 # define BFNC_SMIME_APPLICATION_SMIME_HANDLER smime_application_smime_handler
97 # define BFNC_SMIME_GETKEYS                   smime_getkeys
98 # define BFNC_SMIME_VERIFY_SENDER             smime_verify_sender
99 # define BFNC_SMIME_ASK_FOR_KEY               smime_ask_for_key
100 # define BFNC_SMIME_FINDKEYS                  smime_findKeys
101 # define BFNC_SMIME_SIGN_MESSAGE              smime_sign_message
102 # define BFNC_SMIME_BUILD_SMIME_ENTITY        smime_build_smime_entity
103 # define BFNC_SMIME_INVOKE_IMPORT             smime_invoke_import
104 # define BFNC_SMIME_VERIFY_ONE            smime_verify_one
105
106 #elif defined (CRYPT_BACKEND_GPGME)
107   /* Already included above (gpgme supports both). */ 
108 # define BFNC_SMIME_VOID_PASSPHRASE NULL /* not required */
109
110 #endif /* SMIME backend */
111
112 \f
113 /*
114     
115     Generic
116
117 */
118
119 /* Show a message that a backend will be invoked. */
120 void crypt_invoke_message (int type)
121 {
122 #if defined (CRYPT_BACKEND_CLASSIC_PGP) || defined(CRYPT_BACKEND_CLASSIC_SMIME)
123   if ((type & APPLICATION_PGP))
124     mutt_message _("Invoking PGP...");
125   if ((type & APPLICATION_SMIME))
126     mutt_message _("Invoking OpenSSL...");
127 #elif defined (CRYPT_BACKEND_GPGME)
128   if ((type & APPLICATION_PGP) || (type & APPLICATION_SMIME) )
129     mutt_message _("Invoking GnuPG...");
130 #endif
131 }
132
133
134 \f
135 /* 
136
137     PGP
138
139 */
140
141
142 /* Reset a PGP passphrase */
143 void crypt_pgp_void_passphrase (void)
144 {
145 #ifdef BFNC_PGP_VOID_PASSPHRASE
146   BFNC_PGP_VOID_PASSPHRASE ();
147 #endif
148 }
149
150 /* Decrypt a PGP/MIME message. */
151 int crypt_pgp_decrypt_mime (FILE *a, FILE **b, BODY *c, BODY **d)
152 {
153 #ifdef BFNC_PGP_DECRYPT_MIME
154   return BFNC_PGP_DECRYPT_MIME (a, b, c, d);
155 #else
156   return -1; /* error */
157 #endif
158 }
159
160 /* MIME handler for the application/pgp content-type. */
161 void crypt_pgp_application_pgp_handler (BODY *m, STATE *s)
162 {
163 #ifdef BFNC_PGP_APPLICATION_PGP_HANDLER
164   BFNC_PGP_APPLICATION_PGP_HANDLER (m, s);
165 #endif
166 }
167
168 /* MIME handler for an PGP/MIME encrypted message. */
169 void crypt_pgp_encrypted_handler (BODY *a, STATE *s)
170 {
171 #ifdef BFNC_PGP_ENCRYPTED_HANDLER
172   BFNC_PGP_ENCRYPTED_HANDLER (a, s);
173 #endif
174 }
175
176 /* fixme: needs documentation. */
177 void crypt_pgp_invoke_getkeys (ADDRESS *addr)
178 {
179 #ifdef BFNC_PGP_INVOKE_GETKEYS
180   BFNC_PGP_INVOKE_GETKEYS (addr);
181 #endif
182 }
183
184 /* Ask for a PGP key. */
185 pgp_key_t crypt_pgp_ask_for_key (char *tag, char *whatfor,
186                                  short abilities, pgp_ring_t keyring)
187 {
188 #ifdef BFNC_PGP_ASK_FOR_KEY
189   return BFNC_PGP_ASK_FOR_KEY (tag, whatfor, abilities, keyring);
190 #else
191   return NULL;
192 #endif
193 }
194
195
196 /* Check for a traditional PGP message in body B. */
197 int crypt_pgp_check_traditional (FILE *fp, BODY *b, int tagged_only)
198 {
199 #ifdef BNFC_PGP_CHECK_TRADITIONAL
200   return BNFC_PGP_CHECK_TRADITIONAL (fp, b, tagged_only);
201 #else
202   return 0; /* no */
203 #endif
204 }
205
206 /* fixme: needs documentation. */
207 BODY *crypt_pgp_traditional_encryptsign (BODY *a, int flags, char *keylist)
208 {
209 #ifdef BFNC_PGP_TRADITIONAL_ENCRYPTSIGN  
210   return BFNC_PGP_TRADITIONAL_ENCRYPTSIGN (a, flags, keylist);
211 #else
212   return NULL;
213 #endif
214 }
215
216 /* Release pgp key KPP. */
217 void crypt_pgp_free_key (pgp_key_t *kpp)
218 {
219 #ifdef BFNC_PGP_FREE_KEY
220   BFNC_PGP_FREE_KEY (kpp);
221 #endif
222 }
223
224
225 /* Generate a PGP public key attachment. */
226 BODY *crypt_pgp_make_key_attachment (char *tempf)
227 {
228 #ifdef BFNC_PGP_MAKE_KEY_ATTACHMENT 
229   return BFNC_PGP_MAKE_KEY_ATTACHMENT (tempf);
230 #else
231   return NULL; /* error */ 
232 #endif
233 }
234
235 /* This routine attempts to find the keyids of the recipients of a
236    message.  It returns NULL if any of the keys can not be found.  */
237 char *crypt_pgp_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
238 {
239 #ifdef BFNC_PGP_FINDKEYS
240   return BFNC_PGP_FINDKEYS (to, cc, bcc);
241 #else
242   return NULL;
243 #endif
244 }
245
246 /* Create a new body with a PGP signed message from A. */
247 BODY *crypt_pgp_sign_message (BODY *a)
248 {
249 #ifdef BFNC_PGP_SIGN_MESSAGE
250   return BFNC_PGP_SIGN_MESSAGE (a);
251 #else
252   return NULL;
253 #endif
254 }
255
256 /* Warning: A is no longer freed in this routine, you need to free it
257    later.  This is necessary for $fcc_attach. */
258 BODY *crypt_pgp_encrypt_message (BODY *a, char *keylist, int sign)
259 {
260 #ifdef BFNC_PGP_ENCRYPT_MESSAGE
261   return BFNC_PGP_ENCRYPT_MESSAGE (a, keylist, sign);
262 #else
263   return NULL;
264 #endif
265 }
266
267 /* Invoke the PGP command to import a key. */
268 void crypt_pgp_invoke_import (const char *fname)
269 {
270 #ifdef BFNC_PGP_INVOKE_IMPORT
271   BFNC_PGP_INVOKE_IMPORT (fname);
272 #endif
273 }
274
275 /* fixme: needs documentation */
276 int crypt_pgp_verify_one (BODY *sigbdy, STATE *s, const char *tempf)
277 {
278 #ifdef BFNC_PGP_VERIFY_ONE
279   return BFNC_PGP_VERIFY_ONE (sigbdy, s, tempf);
280 #else
281   return -1;
282 #endif
283 }
284
285
286 /* Access the keyID in K. */
287 char *crypt_pgp_keyid (pgp_key_t k)
288 {
289 #ifdef BFNC_PGP_KEYID
290   return pgp_keyid (k);
291 #else
292   return "?";
293 #endif
294 }
295
296 /* fixme: needs documentation */
297 void crypt_pgp_extract_keys_from_attachment_list (FILE *fp, int tag, BODY *top)
298 {
299 #ifdef BFNC_PGP_EXTRACT_KEYS_FROM_ATTACHMENT_LIST
300   BFNC_PGP_EXTRACT_KEYS_FROM_ATTACHMENT_LIST (fp, tag, top);
301 #endif
302 }
303
304
305 \f
306 /* 
307
308    S/MIME 
309
310 */
311
312
313 /* Reset an SMIME passphrase */
314 void crypt_smime_void_passphrase (void)
315 {
316 #ifdef BFNC_SMIME_VOID_PASSPHRASE
317   BFNC_SMIME_VOID_PASSPHRASE ();
318 #endif
319 }
320
321
322 /* Decrypt am S/MIME message. */
323 int crypt_smime_decrypt_mime (FILE *a, FILE **b, BODY *c, BODY **d)
324 {
325 #ifdef BFNC_SMIME_DECRYPT_MIME
326   return BFNC_SMIME_DECRYPT_MIME (a, b, c, d);
327 #else
328   return -1; /* error */
329 #endif
330 }
331
332 /* MIME handler for the application/smime content-type. */
333 void crypt_smime_application_smime_handler (BODY *m, STATE *s)
334 {
335 #ifdef BFNC_SMIME_APPLICATION_SMIME_HANDLER
336   BFNC_SMIME_APPLICATION_SMIME_HANDLER (m, s);
337 #endif
338 }
339
340 /* fixme: Needs documentation. */
341 void crypt_smime_getkeys (ENVELOPE *env)
342 {
343 #ifdef BFNC_SMIME_GETKEYS  
344   BFNC_SMIME_GETKEYS (env);
345 #endif
346 }
347
348 /* Check that the sender matches. */
349 int crypt_smime_verify_sender(HEADER *h)
350 {
351 #ifdef BFNC_SMIME_VERIFY_SENDER
352   return BFNC_SMIME_VERIFY_SENDER (h);
353 #else
354   return 1; /* yes */
355 #endif
356 }
357
358 /* Ask for an SMIME key. */
359 char *crypt_smime_ask_for_key (char *prompt, char *mailbox, short public)
360 {
361 #ifdef BFNC_SMIME_ASK_FOR_KEY
362   return BFNC_SMIME_ASK_FOR_KEY (prompt, mailbox, public);
363 #else
364   return NULL; /* error */
365 #endif
366 }
367
368
369 /* This routine attempts to find the keyids of the recipients of a
370    message.  It returns NULL if any of the keys can not be found.  */
371 char *crypt_smime_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
372 {
373 #ifdef BFNC_SMIME_FINDKEYS
374   return BFNC_SMIME_FINDKEYS (to, cc, bcc);
375 #else
376   return NULL;
377 #endif
378 }
379
380 /* fixme: Needs documentation. */
381 BODY *crypt_smime_sign_message (BODY *a)
382 {
383 #ifdef BFNC_SMIME_SIGN_MESSAGE
384   return BFNC_SMIME_SIGN_MESSAGE (a);
385 #else
386   return NULL;
387 #endif
388 }
389
390 /* fixme: needs documentation. */
391 BODY *crypt_smime_build_smime_entity (BODY *a, char *certlist)
392 {
393 #ifdef BFNC_SMIME_BUILD_SMIME_ENTITY
394   return BFNC_SMIME_BUILD_SMIME_ENTITY (a, certlist);
395 #else
396   return NULL;
397 #endif
398 }
399
400 /* Add a certificate and update index file (externally). */
401 void crypt_smime_invoke_import (char *infile, char *mailbox)
402 {
403 #ifdef BFNC_SMIME_INVOKE_IMPORT
404   BFNC_SMIME_INVOKE_IMPORT (infile, mailbox);
405 #endif
406 }
407
408 /* fixme: needs documentation */
409 int crypt_smime_verify_one (BODY *sigbdy, STATE *s, const char *tempf)
410 {
411 #ifdef BFNC_SMIME_VERIFY_ONE
412   return BFNC_SMIME_VERIFY_ONE (sigbdy, s, tempf);
413 #else
414   return -1;
415 #endif
416 }