1 /* tofupolicy.c - Tofu policy helpers.
2 * Copyright (C) 2016 g10 Code GmbH
4 * This file is part of GPGME.
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.
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.
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/>.
33 /* The error code from a FAILURE status line or 0. */
34 gpg_error_t failure_code;
36 /* The error code from an ERROR status line or 0. */
37 gpg_error_t error_code;
43 /* Parse an error status line. Return the error location and the
44 error code. The function may modify ARGS. */
46 parse_error (char *args, gpg_error_t *r_err)
48 char *where = strchr (args, ' ');
56 where = strchr (which, ' ');
64 *r_err = trace_gpg_error (GPG_ERR_INV_ENGINE);
68 *r_err = atoi (which);
75 tofu_policy_status_handler (void *priv, gpgme_status_code_t code, char *args)
77 gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
83 err = _gpgme_op_data_lookup (ctx, OPDATA_TOFU_POLICY, &hook, -1, NULL);
90 case GPGME_STATUS_ERROR:
91 loc = parse_error (args, &err);
95 opd->error_code = err;
98 case GPGME_STATUS_FAILURE:
99 opd->failure_code = _gpgme_parse_failure (args);
102 case GPGME_STATUS_EOF:
104 err = opd->error_code;
105 else if (opd->failure_code)
106 err = opd->failure_code;
117 /* Set the TOFU policy for KEY to POLICY. */
119 tofu_policy_start (gpgme_ctx_t ctx, int synchronous,
120 gpgme_key_t key, gpgme_tofu_policy_t policy)
126 if (ctx->protocol != GPGME_PROTOCOL_OPENPGP)
127 return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
130 return gpg_error (GPG_ERR_INV_VALUE);
132 err = _gpgme_op_reset (ctx, synchronous);
136 err = _gpgme_op_data_lookup (ctx, OPDATA_TOFU_POLICY, &hook,
137 sizeof (*opd), NULL);
142 _gpgme_engine_set_status_handler (ctx->engine, tofu_policy_status_handler,
145 return _gpgme_engine_op_tofu_policy (ctx->engine, key, policy);
150 /* Set the TOFU policy of KEY to POLCIY. This is the asynchronous
153 gpgme_op_tofu_policy_start (gpgme_ctx_t ctx,
154 gpgme_key_t key, gpgme_tofu_policy_t policy)
157 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_tofu_policy_start", ctx,
158 "key=%p, policy=%u", key, (unsigned int)policy);
161 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
163 err = tofu_policy_start (ctx, 0, key, policy);
164 return TRACE_ERR (err);
168 /* This is the synchronous variant of gpgme_op_tofu_policy_start. */
170 gpgme_op_tofu_policy (gpgme_ctx_t ctx,
171 gpgme_key_t key, gpgme_tofu_policy_t policy)
174 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_tofu_policy", ctx,
175 "key=%p, policy=%u", key, (unsigned int)policy);
178 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
180 err = tofu_policy_start (ctx, 1, key, policy);
182 err = _gpgme_wait_one (ctx);
183 return TRACE_ERR (err);