Line data Source code
1 : /* op-support.c - Supporting functions.
2 : Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH
3 :
4 : This file is part of GPGME.
5 :
6 : GPGME is free software; you can redistribute it and/or modify it
7 : under the terms of the GNU Lesser General Public License as
8 : published by the Free Software Foundation; either version 2.1 of
9 : the License, or (at your option) any later version.
10 :
11 : GPGME is distributed in the hope that it will be useful, but
12 : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public
17 : License along with this program; if not, see <https://www.gnu.org/licenses/>.
18 : */
19 :
20 : #if HAVE_CONFIG_H
21 : #include <config.h>
22 : #endif
23 : #include <stdlib.h>
24 : #include <errno.h>
25 : #include <string.h>
26 : #ifdef HAVE_LOCALE_H
27 : #include <locale.h>
28 : #endif
29 :
30 : #include "gpgme.h"
31 : #include "context.h"
32 : #include "ops.h"
33 : #include "util.h"
34 : #include "debug.h"
35 :
36 : #if GPG_ERROR_VERSION_NUMBER < 0x011700 /* 1.23 */
37 : # define GPG_ERR_SUBKEYS_EXP_OR_REV 217
38 : #endif
39 :
40 :
41 :
42 : gpgme_error_t
43 0 : _gpgme_op_data_lookup (gpgme_ctx_t ctx, ctx_op_data_id_t type, void **hook,
44 : int size, void (*cleanup) (void *))
45 : {
46 : struct ctx_op_data *data;
47 :
48 0 : if (!ctx)
49 0 : return gpg_error (GPG_ERR_INV_VALUE);
50 :
51 0 : data = ctx->op_data;
52 0 : while (data && data->type != type)
53 0 : data = data->next;
54 0 : if (!data)
55 : {
56 0 : if (size < 0)
57 : {
58 0 : *hook = NULL;
59 0 : return 0;
60 : }
61 :
62 0 : data = calloc (1, sizeof (struct ctx_op_data) + size);
63 0 : if (!data)
64 0 : return gpg_error_from_syserror ();
65 0 : data->magic = CTX_OP_DATA_MAGIC;
66 0 : data->next = ctx->op_data;
67 0 : data->type = type;
68 0 : data->cleanup = cleanup;
69 0 : data->hook = (void *) (((char *) data) + sizeof (struct ctx_op_data));
70 0 : data->references = 1;
71 0 : ctx->op_data = data;
72 : }
73 0 : *hook = data->hook;
74 0 : return 0;
75 : }
76 :
77 :
78 : /* type is: 0: asynchronous operation (use global or user event loop).
79 : 1: synchronous operation (always use private event loop).
80 : 2: asynchronous private operation (use private or user
81 : event loop).
82 : 256: Modification flag to suppress the engine reset.
83 : */
84 : gpgme_error_t
85 0 : _gpgme_op_reset (gpgme_ctx_t ctx, int type)
86 : {
87 0 : gpgme_error_t err = 0;
88 : struct gpgme_io_cbs io_cbs;
89 0 : int no_reset = (type & 256);
90 0 : int reuse_engine = 0;
91 :
92 0 : type &= 255;
93 :
94 0 : _gpgme_release_result (ctx);
95 0 : LOCK (ctx->lock);
96 0 : ctx->canceled = 0;
97 0 : UNLOCK (ctx->lock);
98 :
99 0 : if (ctx->engine && no_reset)
100 0 : reuse_engine = 1;
101 0 : else if (ctx->engine)
102 : {
103 : /* Attempt to reset an existing engine. */
104 :
105 0 : err = _gpgme_engine_reset (ctx->engine);
106 0 : if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
107 : {
108 0 : _gpgme_engine_release (ctx->engine);
109 0 : ctx->engine = NULL;
110 : }
111 : }
112 :
113 0 : if (!ctx->engine)
114 : {
115 : gpgme_engine_info_t info;
116 0 : info = ctx->engine_info;
117 0 : while (info && info->protocol != ctx->protocol)
118 0 : info = info->next;
119 :
120 0 : if (!info)
121 0 : return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
122 :
123 : /* Create an engine object. */
124 0 : err = _gpgme_engine_new (info, &ctx->engine);
125 0 : if (err)
126 0 : return err;
127 : }
128 :
129 0 : if (!reuse_engine)
130 : {
131 0 : err = 0;
132 : #ifdef LC_CTYPE
133 0 : err = _gpgme_engine_set_locale (ctx->engine, LC_CTYPE, ctx->lc_ctype);
134 : #endif
135 : #ifdef LC_MESSAGES
136 0 : if (!err)
137 0 : err = _gpgme_engine_set_locale (ctx->engine,
138 0 : LC_MESSAGES, ctx->lc_messages);
139 : #endif
140 0 : if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
141 0 : err = 0;
142 :
143 0 : if (!err)
144 : {
145 0 : err = _gpgme_engine_set_pinentry_mode (ctx->engine,
146 : ctx->pinentry_mode);
147 0 : if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
148 0 : err = 0;
149 : }
150 :
151 0 : if (!err && ctx->status_cb && ctx->full_status)
152 : {
153 0 : _gpgme_engine_set_status_cb (ctx->engine,
154 : ctx->status_cb, ctx->status_cb_value);
155 : }
156 :
157 0 : if (err)
158 : {
159 0 : _gpgme_engine_release (ctx->engine);
160 0 : ctx->engine = NULL;
161 0 : return err;
162 : }
163 : }
164 :
165 0 : if (ctx->sub_protocol != GPGME_PROTOCOL_DEFAULT)
166 : {
167 0 : err = _gpgme_engine_set_protocol (ctx->engine, ctx->sub_protocol);
168 0 : if (err)
169 0 : return err;
170 : }
171 :
172 0 : if (type == 1 || (type == 2 && !ctx->io_cbs.add))
173 : {
174 : /* Use private event loop. */
175 0 : io_cbs.add = _gpgme_add_io_cb;
176 0 : io_cbs.add_priv = ctx;
177 0 : io_cbs.remove = _gpgme_remove_io_cb;
178 0 : io_cbs.event = _gpgme_wait_private_event_cb;
179 0 : io_cbs.event_priv = ctx;
180 : }
181 0 : else if (! ctx->io_cbs.add)
182 : {
183 : /* Use global event loop. */
184 0 : io_cbs.add = _gpgme_add_io_cb;
185 0 : io_cbs.add_priv = ctx;
186 0 : io_cbs.remove = _gpgme_remove_io_cb;
187 0 : io_cbs.event = _gpgme_wait_global_event_cb;
188 0 : io_cbs.event_priv = ctx;
189 : }
190 : else
191 : {
192 : /* Use user event loop. */
193 0 : io_cbs.add = _gpgme_wait_user_add_io_cb;
194 0 : io_cbs.add_priv = ctx;
195 0 : io_cbs.remove = _gpgme_wait_user_remove_io_cb;
196 0 : io_cbs.event = _gpgme_wait_user_event_cb;
197 0 : io_cbs.event_priv = ctx;
198 : }
199 0 : _gpgme_engine_set_io_cbs (ctx->engine, &io_cbs);
200 0 : return err;
201 : }
202 :
203 :
204 : /* Parse the INV_RECP or INV_SNDR status line in ARGS and return the
205 : result in KEY. If KC_FPR (from the KEY_CONSIDERED status line) is
206 : not NULL take the KC_FLAGS in account. */
207 : gpgme_error_t
208 0 : _gpgme_parse_inv_recp (char *args, int for_signing,
209 : const char *kc_fpr, unsigned int kc_flags,
210 : gpgme_invalid_key_t *key)
211 : {
212 : gpgme_invalid_key_t inv_key;
213 : char *tail;
214 : long int reason;
215 :
216 : (void)for_signing;
217 :
218 0 : inv_key = calloc (1, sizeof (*inv_key));
219 0 : if (!inv_key)
220 0 : return gpg_error_from_syserror ();
221 0 : inv_key->next = NULL;
222 0 : gpg_err_set_errno (0);
223 0 : reason = strtol (args, &tail, 0);
224 0 : if (errno || args == tail || (*tail && *tail != ' '))
225 : {
226 : /* The crypto backend does not behave. */
227 0 : free (inv_key);
228 0 : return trace_gpg_error (GPG_ERR_INV_ENGINE);
229 : }
230 :
231 0 : switch (reason)
232 : {
233 : case 0:
234 0 : if (kc_fpr && (kc_flags & 2))
235 0 : inv_key->reason = gpg_error (GPG_ERR_SUBKEYS_EXP_OR_REV);
236 : else
237 0 : inv_key->reason = gpg_error (GPG_ERR_GENERAL);
238 0 : break;
239 :
240 : case 1:
241 0 : inv_key->reason = gpg_error (GPG_ERR_NO_PUBKEY);
242 0 : break;
243 :
244 : case 2:
245 0 : inv_key->reason = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
246 0 : break;
247 :
248 : case 3:
249 0 : inv_key->reason = gpg_error (GPG_ERR_WRONG_KEY_USAGE);
250 0 : break;
251 :
252 : case 4:
253 0 : inv_key->reason = gpg_error (GPG_ERR_CERT_REVOKED);
254 0 : break;
255 :
256 : case 5:
257 0 : inv_key->reason = gpg_error (GPG_ERR_CERT_EXPIRED);
258 0 : break;
259 :
260 : case 6:
261 0 : inv_key->reason = gpg_error (GPG_ERR_NO_CRL_KNOWN);
262 0 : break;
263 :
264 : case 7:
265 0 : inv_key->reason = gpg_error (GPG_ERR_CRL_TOO_OLD);
266 0 : break;
267 :
268 : case 8:
269 0 : inv_key->reason = gpg_error (GPG_ERR_NO_POLICY_MATCH);
270 0 : break;
271 :
272 : case 9:
273 0 : inv_key->reason = gpg_error (GPG_ERR_NO_SECKEY);
274 0 : break;
275 :
276 : case 10:
277 0 : inv_key->reason = gpg_error (GPG_ERR_PUBKEY_NOT_TRUSTED);
278 0 : break;
279 :
280 : case 11:
281 0 : inv_key->reason = gpg_error (GPG_ERR_MISSING_CERT);
282 0 : break;
283 :
284 : case 12:
285 0 : inv_key->reason = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
286 0 : break;
287 :
288 : case 13:
289 0 : inv_key->reason = gpg_error (252); /*GPG_ERR_KEY_DISABLED*/
290 0 : break;
291 :
292 : case 14:
293 0 : inv_key->reason = gpg_error (GPG_ERR_INV_USER_ID);
294 0 : break;
295 :
296 : default:
297 0 : inv_key->reason = gpg_error (GPG_ERR_GENERAL);
298 0 : break;
299 : }
300 :
301 0 : while (*tail && *tail == ' ')
302 0 : tail++;
303 0 : if (*tail)
304 : {
305 0 : inv_key->fpr = strdup (tail);
306 0 : if (!inv_key->fpr)
307 : {
308 0 : free (inv_key);
309 0 : return gpg_error_from_syserror ();
310 : }
311 : }
312 :
313 0 : *key = inv_key;
314 0 : return 0;
315 : }
316 :
317 :
318 :
319 : /* Parse a KEY_CONSIDERED status line in ARGS and store the
320 : * fingerprint and the flags at R_FPR and R_FLAGS. The caller must
321 : * free the value at R_FPR on success. */
322 : gpgme_error_t
323 0 : _gpgme_parse_key_considered (const char *args,
324 : char **r_fpr, unsigned int *r_flags)
325 : {
326 : char *pend;
327 : size_t n;
328 :
329 0 : *r_fpr = NULL;
330 :
331 0 : pend = strchr (args, ' ');
332 0 : if (!pend || pend == args)
333 0 : return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Bogus status line. */
334 0 : n = pend - args;
335 0 : *r_fpr = malloc (n + 1);
336 0 : if (!*r_fpr)
337 0 : return gpg_error_from_syserror ();
338 0 : memcpy (*r_fpr, args, n);
339 0 : (*r_fpr)[n] = 0;
340 0 : args = pend + 1;
341 :
342 0 : gpg_err_set_errno (0);
343 0 : *r_flags = strtoul (args, &pend, 0);
344 0 : if (errno || args == pend || (*pend && *pend != ' '))
345 : {
346 0 : free (*r_fpr);
347 0 : *r_fpr = NULL;
348 0 : return trace_gpg_error (GPG_ERR_INV_ENGINE);
349 : }
350 :
351 0 : return 0;
352 : }
353 :
354 :
355 : /* Parse the PLAINTEXT status line in ARGS and return the result in
356 : FILENAMEP. */
357 : gpgme_error_t
358 0 : _gpgme_parse_plaintext (char *args, char **filenamep)
359 : {
360 : char *tail;
361 :
362 0 : while (*args == ' ')
363 0 : args++;
364 0 : if (*args == '\0')
365 0 : return 0;
366 :
367 : /* First argument is file type. */
368 0 : while (*args != ' ' && *args != '\0')
369 0 : args++;
370 0 : while (*args == ' ')
371 0 : args++;
372 0 : if (*args == '\0')
373 0 : return 0;
374 :
375 : /* Second argument is the timestamp. */
376 0 : while (*args != ' ' && *args != '\0')
377 0 : args++;
378 0 : while (*args == ' ')
379 0 : args++;
380 0 : if (*args == '\0')
381 0 : return 0;
382 :
383 0 : tail = args;
384 0 : while (*tail != ' ' && *tail != '\0')
385 0 : tail++;
386 0 : *tail = '\0';
387 0 : if (filenamep && *args != '\0')
388 : {
389 0 : char *filename = strdup (args);
390 0 : if (!filename)
391 0 : return gpg_error_from_syserror ();
392 :
393 0 : *filenamep = filename;
394 : }
395 0 : return 0;
396 : }
397 :
398 :
399 : /* Parse a FAILURE status line and return the error code. ARGS is
400 : modified to contain the location part. */
401 : gpgme_error_t
402 0 : _gpgme_parse_failure (char *args)
403 : {
404 : char *where, *which;
405 :
406 0 : where = strchr (args, ' ');
407 0 : if (!where)
408 0 : return trace_gpg_error (GPG_ERR_INV_ENGINE);
409 :
410 0 : *where = '\0';
411 0 : which = where + 1;
412 :
413 0 : where = strchr (which, ' ');
414 0 : if (where)
415 0 : *where = '\0';
416 :
417 0 : where = args;
418 :
419 0 : return atoi (which);
420 : }
|