Line data Source code
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 :
35 : static gpgme_error_t
36 7 : encrypt_sign_status_handler (void *priv, gpgme_status_code_t code, char *args)
37 : {
38 : gpgme_error_t err;
39 :
40 7 : err = _gpgme_progress_status_handler (priv, code, args);
41 7 : if (!err)
42 7 : err = _gpgme_encrypt_status_handler (priv, code, args);
43 7 : if (!err)
44 7 : err = _gpgme_sign_status_handler (priv, code, args);
45 7 : return err;
46 : }
47 :
48 :
49 : static gpgme_error_t
50 11 : encrypt_sym_status_handler (void *priv, gpgme_status_code_t code, char *args)
51 : {
52 : gpgme_error_t err;
53 :
54 11 : err = _gpgme_progress_status_handler (priv, code, args);
55 11 : if (!err)
56 11 : err = _gpgme_sign_status_handler (priv, code, args);
57 11 : if (!err)
58 11 : err = _gpgme_passphrase_status_handler (priv, code, args);
59 11 : return err;
60 : }
61 :
62 :
63 : static gpgme_error_t
64 2 : encrypt_sign_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t recp[],
65 : gpgme_encrypt_flags_t flags,
66 : gpgme_data_t plain, gpgme_data_t cipher)
67 : {
68 : gpgme_error_t err;
69 : int symmetric;
70 :
71 2 : err = _gpgme_op_reset (ctx, synchronous);
72 2 : if (err)
73 0 : return err;
74 :
75 2 : symmetric = !recp;
76 :
77 2 : if (!plain)
78 0 : return gpg_error (GPG_ERR_NO_DATA);
79 2 : if (!cipher)
80 0 : return gpg_error (GPG_ERR_INV_VALUE);
81 2 : if (recp && !*recp)
82 0 : return gpg_error (GPG_ERR_INV_VALUE);
83 :
84 2 : err = _gpgme_op_encrypt_init_result (ctx);
85 2 : if (err)
86 0 : return err;
87 :
88 2 : err = _gpgme_op_sign_init_result (ctx);
89 2 : if (err)
90 0 : return err;
91 :
92 2 : if (ctx->passphrase_cb)
93 : {
94 2 : err = _gpgme_engine_set_command_handler
95 : (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
96 2 : if (err)
97 0 : return err;
98 : }
99 :
100 2 : _gpgme_engine_set_status_handler (ctx->engine,
101 : symmetric
102 : ? encrypt_sym_status_handler
103 : : encrypt_sign_status_handler,
104 : ctx);
105 :
106 2 : return _gpgme_engine_op_encrypt_sign (ctx->engine, recp, flags, plain,
107 2 : cipher, ctx->use_armor,
108 : ctx /* FIXME */);
109 : }
110 :
111 :
112 : /* Encrypt plaintext PLAIN within CTX for the recipients RECP and
113 : store the resulting ciphertext in CIPHER. Also sign the ciphertext
114 : with the signers in CTX. */
115 : gpgme_error_t
116 0 : gpgme_op_encrypt_sign_start (gpgme_ctx_t ctx, gpgme_key_t recp[],
117 : gpgme_encrypt_flags_t flags,
118 : gpgme_data_t plain, gpgme_data_t cipher)
119 : {
120 : gpgme_error_t err;
121 :
122 0 : TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign_start", ctx,
123 : "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher);
124 :
125 0 : if (!ctx)
126 0 : return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
127 :
128 0 : if (_gpgme_debug_trace () && recp)
129 : {
130 0 : int i = 0;
131 :
132 0 : while (recp[i])
133 : {
134 0 : TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i],
135 : (recp[i]->subkeys && recp[i]->subkeys->fpr) ?
136 : recp[i]->subkeys->fpr : "invalid");
137 0 : i++;
138 : }
139 : }
140 :
141 0 : err = encrypt_sign_start (ctx, 0, recp, flags, plain, cipher);
142 0 : return err;
143 : }
144 :
145 :
146 : /* Encrypt plaintext PLAIN within CTX for the recipients RECP and
147 : store the resulting ciphertext in CIPHER. Also sign the ciphertext
148 : with the signers in CTX. */
149 : gpgme_error_t
150 2 : gpgme_op_encrypt_sign (gpgme_ctx_t ctx, gpgme_key_t recp[],
151 : gpgme_encrypt_flags_t flags,
152 : gpgme_data_t plain, gpgme_data_t cipher)
153 : {
154 : gpgme_error_t err;
155 :
156 2 : TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign", ctx,
157 : "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher);
158 :
159 2 : if (!ctx)
160 0 : return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
161 :
162 2 : if (_gpgme_debug_trace () && recp)
163 : {
164 1 : int i = 0;
165 :
166 4 : while (recp[i])
167 : {
168 2 : TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i],
169 : (recp[i]->subkeys && recp[i]->subkeys->fpr) ?
170 : recp[i]->subkeys->fpr : "invalid");
171 2 : i++;
172 : }
173 : }
174 :
175 2 : err = encrypt_sign_start (ctx, 1, recp, flags, plain, cipher);
176 2 : if (!err)
177 2 : err = _gpgme_wait_one (ctx);
178 2 : return TRACE_ERR (err);
179 : }
|