1 /* keysign.c - OpenPGP key signing
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/>.
36 /* The error code from a FAILURE status line or 0. */
37 gpg_error_t failure_code;
39 /* The error code from certain ERROR status lines or 0. */
40 gpg_error_t error_code;
46 release_op_data (void *hook)
48 op_data_t opd = (op_data_t) hook;
54 /* Parse an error status line. Return the error location and the
55 error code. The function may modify ARGS. */
57 parse_error (char *args, gpg_error_t *r_err)
59 char *where = strchr (args, ' ');
67 where = strchr (which, ' ');
75 *r_err = trace_gpg_error (GPG_ERR_INV_ENGINE);
79 *r_err = atoi (which);
86 keysign_status_handler (void *priv, gpgme_status_code_t code, char *args)
88 gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
94 /* Pipe the status code through the progress status handler. */
95 err = _gpgme_progress_status_handler (ctx, code, args);
99 err = _gpgme_op_data_lookup (ctx, OPDATA_KEYSIGN, &hook, -1, NULL);
106 case GPGME_STATUS_ERROR:
107 loc = parse_error (args, &err);
110 if (!opd->error_code)
111 opd->error_code = err;
114 case GPGME_STATUS_FAILURE:
115 opd->failure_code = _gpgme_parse_failure (args);
118 case GPGME_STATUS_EOF:
120 return opd->error_code;
121 else if (opd->failure_code)
122 return opd->failure_code;
125 case GPGME_STATUS_INQUIRE_MAXLEN:
126 if (ctx->status_cb && !ctx->full_status)
128 err = ctx->status_cb (ctx->status_cb_value, "INQUIRE_MAXLEN", args);
141 /* Sign the USERID of KEY using the current set of signers. If USERID
142 * is NULL, sign all user ids. To put several user ids into USERID,
143 * separate them by LF and set the flag GPGME_KEYSIGN_LFSEP. */
145 keysign_start (gpgme_ctx_t ctx, int synchronous,
146 gpgme_key_t key, const char *userid,
147 unsigned long expires, unsigned int flags)
153 if (ctx->protocol != GPGME_PROTOCOL_OPENPGP)
154 return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
156 err = _gpgme_op_reset (ctx, synchronous);
161 return gpg_error (GPG_ERR_INV_ARG);
163 err = _gpgme_op_data_lookup (ctx, OPDATA_KEYSIGN, &hook,
164 sizeof (*opd), release_op_data);
169 _gpgme_engine_set_status_handler (ctx->engine, keysign_status_handler, ctx);
171 if (ctx->passphrase_cb)
173 err = _gpgme_engine_set_command_handler
174 (ctx->engine, _gpgme_passphrase_command_handler, ctx);
179 return _gpgme_engine_op_keysign (ctx->engine,
180 key, userid, expires, flags, ctx);
184 /* Sign the USERID of KEY using the current set of signers. */
186 gpgme_op_keysign_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid,
187 unsigned long expires, unsigned int flags)
191 TRACE_BEG3 (DEBUG_CTX, "gpgme_op_keysign_start", ctx,
192 "key=%p, uid='%s' flags=0x%x", key, userid, flags);
195 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
197 err = keysign_start (ctx, 0, key, userid, expires, flags);
198 return TRACE_ERR (err);
203 gpgme_op_keysign (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid,
204 unsigned long expires, unsigned int flags)
208 TRACE_BEG3 (DEBUG_CTX, "gpgme_op_keysign", ctx,
209 "key=%p, uid='%s' flags=0x%x", key, userid, flags);
212 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
214 err = keysign_start (ctx, 1, key, userid, expires, flags);
216 err = _gpgme_wait_one (ctx);
217 return TRACE_ERR (err);