First take on changes to allow building with MSC for W32CE.
[gpgme.git] / src / encrypt-sign.c
1 /* encrypt-sign.c -  encrypt and verify functions
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
4  
5    This file is part of GPGME.
6  
7    GPGME is free software; you can redistribute it and/or modify it
8    under the terms of the GNU Lesser General Public License as
9    published by the Free Software Foundation; either version 2.1 of
10    the License, or (at your option) any later version.
11    
12    GPGME is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16    
17    You should have received a copy of the GNU Lesser General Public
18    License along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "gpgme.h"
30 #include "debug.h"
31 #include "context.h"
32 #include "ops.h"
33
34 \f
35 static gpgme_error_t
36 encrypt_sign_status_handler (void *priv, gpgme_status_code_t code, char *args)
37 {
38   gpgme_error_t err;
39
40   err = _gpgme_progress_status_handler (priv, code, args);
41   if (!err)
42     err = _gpgme_encrypt_status_handler (priv, code, args);
43   if (!err)
44     err = _gpgme_sign_status_handler (priv, code, args);
45   return err;
46 }
47
48
49 static gpgme_error_t
50 encrypt_sign_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t recp[],
51                     gpgme_encrypt_flags_t flags,
52                     gpgme_data_t plain, gpgme_data_t cipher)
53 {
54   gpgme_error_t err;
55
56   err = _gpgme_op_reset (ctx, synchronous);
57   if (err)
58     return err;
59
60   if (!plain)
61     return gpg_error (GPG_ERR_NO_DATA);
62   if (!cipher || !recp)
63     return gpg_error (GPG_ERR_INV_VALUE);
64
65   err = _gpgme_op_encrypt_init_result (ctx);
66   if (err)
67     return err;
68
69   err = _gpgme_op_sign_init_result (ctx);
70   if (err)
71     return err;
72
73   if (ctx->passphrase_cb)
74     {
75       err = _gpgme_engine_set_command_handler
76         (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
77       if (err)
78         return err;
79     }
80
81   _gpgme_engine_set_status_handler (ctx->engine,
82                                     encrypt_sign_status_handler, ctx);
83   
84   return _gpgme_engine_op_encrypt_sign (ctx->engine, recp, flags, plain,
85                                         cipher, ctx->use_armor,
86                                         ctx /* FIXME */);
87 }
88
89
90 /* Encrypt plaintext PLAIN within CTX for the recipients RECP and
91    store the resulting ciphertext in CIPHER.  Also sign the ciphertext
92    with the signers in CTX.  */
93 gpgme_error_t
94 gpgme_op_encrypt_sign_start (gpgme_ctx_t ctx, gpgme_key_t recp[],
95                              gpgme_encrypt_flags_t flags,
96                              gpgme_data_t plain, gpgme_data_t cipher)
97 {
98   gpgme_error_t err;
99
100   TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign_start", ctx,
101               "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher);
102   
103   if (_gpgme_debug_trace () && recp)
104     {
105       int i = 0;
106
107       while (recp[i])
108         {
109           TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i],
110                       (recp[i]->subkeys && recp[i]->subkeys->fpr) ? 
111                       recp[i]->subkeys->fpr : "invalid");
112           i++;
113         }
114     }
115
116   err = encrypt_sign_start (ctx, 0, recp, flags, plain, cipher);
117   return err;
118 }
119
120
121 /* Encrypt plaintext PLAIN within CTX for the recipients RECP and
122    store the resulting ciphertext in CIPHER.  Also sign the ciphertext
123    with the signers in CTX.  */
124 gpgme_error_t
125 gpgme_op_encrypt_sign (gpgme_ctx_t ctx, gpgme_key_t recp[],
126                        gpgme_encrypt_flags_t flags,
127                        gpgme_data_t plain, gpgme_data_t cipher)
128 {
129   gpgme_error_t err;
130
131   TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign", ctx,
132               "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher);
133   
134   if (_gpgme_debug_trace () && recp)
135     {
136       int i = 0;
137
138       while (recp[i])
139         {
140           TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i],
141                       (recp[i]->subkeys && recp[i]->subkeys->fpr) ? 
142                       recp[i]->subkeys->fpr : "invalid");
143           i++;
144         }
145     }
146
147   err = encrypt_sign_start (ctx, 1, recp, flags, plain, cipher);
148   if (!err)
149     err = _gpgme_wait_one (ctx);
150   return TRACE_ERR (err);
151 }