1 /* genkey.c - Key generation.
2 * Copyright (C) 2000 Werner Koch (dd9jn)
3 * Copyright (C) 2001, 2002, 2003, 2004, 2016 g10 Code GmbH
5 * This file is part of GPGME.
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.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
37 struct _gpgme_op_genkey_result result;
39 /* The error code from a FAILURE status line or 0. */
40 gpg_error_t failure_code;
42 /* The error code from certain ERROR status lines or 0. */
43 gpg_error_t error_code;
45 /* Flag to indicate that a UID is to be added. */
48 /* The key parameters passed to the crypto engine. */
49 gpgme_data_t key_parameter;
54 release_op_data (void *hook)
56 op_data_t opd = (op_data_t) hook;
59 free (opd->result.fpr);
60 if (opd->key_parameter)
61 gpgme_data_release (opd->key_parameter);
66 gpgme_op_genkey_result (gpgme_ctx_t ctx)
72 TRACE_BEG (DEBUG_CTX, "gpgme_op_genkey_result", ctx);
74 err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, -1, NULL);
78 TRACE_SUC0 ("result=(null)");
82 TRACE_LOG3 ("fpr = %s, %s, %s", opd->result.fpr,
83 opd->result.primary ? "primary" : "no primary",
84 opd->result.sub ? "sub" : "no sub");
86 TRACE_SUC1 ("result=%p", &opd->result);
92 /* Parse an error status line. Return the error location and the
93 error code. The function may modify ARGS. */
95 parse_error (char *args, gpg_error_t *r_err)
97 char *where = strchr (args, ' ');
105 where = strchr (which, ' ');
113 *r_err = trace_gpg_error (GPG_ERR_INV_ENGINE);
117 *r_err = atoi (which);
124 genkey_status_handler (void *priv, gpgme_status_code_t code, char *args)
126 gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
132 /* Pipe the status code through the progress status handler. */
133 err = _gpgme_progress_status_handler (ctx, code, args);
137 err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, -1, NULL);
144 case GPGME_STATUS_KEY_CREATED:
147 if (*args == 'B' || *args == 'P')
149 opd->result.primary = 1;
152 if (*args == 'B' || *args == 'S')
157 free (opd->result.fpr);
158 opd->result.fpr = strdup (&args[2]);
159 if (!opd->result.fpr)
160 return gpg_error_from_syserror ();
165 case GPGME_STATUS_ERROR:
166 loc = parse_error (args, &err);
169 if (!opd->error_code)
170 opd->error_code = err;
173 case GPGME_STATUS_FAILURE:
174 opd->failure_code = _gpgme_parse_failure (args);
177 case GPGME_STATUS_EOF:
179 return opd->error_code;
180 else if (!opd->uidmode && !opd->result.primary && !opd->result.sub)
181 return gpg_error (GPG_ERR_GENERAL);
182 else if (opd->failure_code)
183 return opd->failure_code;
184 else if (opd->uidmode == 1)
185 opd->result.uid = 1; /* We have no status line, thus this hack. */
188 case GPGME_STATUS_INQUIRE_MAXLEN:
189 if (ctx->status_cb && !ctx->full_status)
191 err = ctx->status_cb (ctx->status_cb_value, "INQUIRE_MAXLEN", args);
205 get_key_parameter (const char *parms, gpgme_data_t *key_parameter)
211 /* Extract the key parameter from the XML structure. */
212 parms = strstr (parms, "<GnupgKeyParms ");
214 return gpg_error (GPG_ERR_INV_VALUE);
216 content = strchr (parms, '>');
218 return gpg_error (GPG_ERR_INV_VALUE);
221 attrib = strstr (parms, "format=\"internal\"");
222 if (!attrib || attrib >= content)
223 return gpg_error (GPG_ERR_INV_VALUE);
225 endtag = strstr (content, "</GnupgKeyParms>");
226 /* FIXME: Check that there are no control statements inside. */
227 while (content[0] == '\n'
228 || (content[0] == '\r' && content[1] == '\n'))
231 return gpgme_data_new_from_mem (key_parameter, content,
232 endtag - content, 1);
237 genkey_start (gpgme_ctx_t ctx, int synchronous, const char *parms,
238 gpgme_data_t pubkey, gpgme_data_t seckey)
243 err = _gpgme_op_reset (ctx, synchronous);
247 err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook,
248 sizeof (*opd), release_op_data);
253 err = get_key_parameter (parms, &opd->key_parameter);
257 _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
259 if (ctx->passphrase_cb)
261 err = _gpgme_engine_set_command_handler
262 (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
267 return _gpgme_engine_op_genkey (ctx->engine,
268 NULL, NULL, 0, 0, NULL, 0,
270 ctx->use_armor? GENKEY_EXTRAFLAG_ARMOR:0,
275 /* Generate a new keypair and add it to the keyring. PUBKEY and
276 SECKEY should be null for now. PARMS specifies what keys should be
279 gpgme_op_genkey_start (gpgme_ctx_t ctx, const char *parms,
280 gpgme_data_t pubkey, gpgme_data_t seckey)
284 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_genkey_start", ctx,
285 "pubkey=%p, seckey=%p", pubkey, seckey);
286 TRACE_LOGBUF (parms, strlen (parms));
289 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
291 err = genkey_start (ctx, 0, parms, pubkey, seckey);
292 return TRACE_ERR (err);
296 /* Generate a new keypair and add it to the keyring. PUBKEY and
297 SECKEY should be null for now. PARMS specifies what keys should be
300 gpgme_op_genkey (gpgme_ctx_t ctx, const char *parms, gpgme_data_t pubkey,
305 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_genkey", ctx,
306 "pubkey=%p, seckey=%p", pubkey, seckey);
307 TRACE_LOGBUF (parms, strlen (parms));
310 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
312 err = genkey_start (ctx, 1, parms, pubkey, seckey);
314 err = _gpgme_wait_one (ctx);
315 return TRACE_ERR (err);
321 createkey_start (gpgme_ctx_t ctx, int synchronous,
322 const char *userid, const char *algo,
323 unsigned long reserved, unsigned long expires,
324 gpgme_key_t anchorkey, unsigned int flags)
330 err = _gpgme_op_reset (ctx, synchronous);
334 if (reserved || anchorkey || !userid)
335 return gpg_error (GPG_ERR_INV_ARG);
337 err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook,
338 sizeof (*opd), release_op_data);
343 _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
345 if (ctx->passphrase_cb)
347 err = _gpgme_engine_set_command_handler
348 (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
353 return _gpgme_engine_op_genkey (ctx->engine,
354 userid, algo, reserved, expires,
357 ctx->use_armor? GENKEY_EXTRAFLAG_ARMOR:0,
364 gpgme_op_createkey_start (gpgme_ctx_t ctx, const char *userid, const char *algo,
365 unsigned long reserved, unsigned long expires,
366 gpgme_key_t anchorkey, unsigned int flags)
370 TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createkey_start", ctx,
371 "userid='%s', algo='%s' flags=0x%x", userid, algo, flags);
374 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
376 err = createkey_start (ctx, 0,
377 userid, algo, reserved, expires, anchorkey, flags);
378 return TRACE_ERR (err);
383 gpgme_op_createkey (gpgme_ctx_t ctx, const char *userid, const char *algo,
384 unsigned long reserved, unsigned long expires,
385 gpgme_key_t anchorkey, unsigned int flags)
389 TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createkey", ctx,
390 "userid='%s', algo='%s' flags=0x%x", userid, algo, flags);
393 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
395 err = createkey_start (ctx, 1,
396 userid, algo, reserved, expires, anchorkey, flags);
398 err = _gpgme_wait_one (ctx);
399 return TRACE_ERR (err);
405 createsubkey_start (gpgme_ctx_t ctx, int synchronous,
408 unsigned long reserved, unsigned long expires,
415 if (ctx->protocol != GPGME_PROTOCOL_OPENPGP)
416 return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
418 err = _gpgme_op_reset (ctx, synchronous);
422 if (reserved || !key)
423 return gpg_error (GPG_ERR_INV_ARG);
425 err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook,
426 sizeof (*opd), release_op_data);
431 _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
433 if (ctx->passphrase_cb)
435 err = _gpgme_engine_set_command_handler
436 (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
441 return _gpgme_engine_op_genkey (ctx->engine,
442 NULL, algo, reserved, expires,
445 ctx->use_armor? GENKEY_EXTRAFLAG_ARMOR:0,
451 /* Add a subkey to an existing KEY. */
453 gpgme_op_createsubkey_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo,
454 unsigned long reserved, unsigned long expires,
459 TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createsubkey_start", ctx,
460 "key=%p, algo='%s' flags=0x%x", key, algo, flags);
463 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
465 err = createsubkey_start (ctx, 0, key, algo, reserved, expires, flags);
466 return TRACE_ERR (err);
471 gpgme_op_createsubkey (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo,
472 unsigned long reserved, unsigned long expires,
477 TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createsubkey", ctx,
478 "key=%p, algo='%s' flags=0x%x", key, algo, flags);
481 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
483 err = createsubkey_start (ctx, 1, key, algo, reserved, expires, flags);
485 err = _gpgme_wait_one (ctx);
486 return TRACE_ERR (err);
492 addrevuid_start (gpgme_ctx_t ctx, int synchronous, int extraflags,
493 gpgme_key_t key, const char *userid, unsigned int flags)
499 if (ctx->protocol != GPGME_PROTOCOL_OPENPGP)
500 return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
503 return gpg_error (GPG_ERR_INV_ARG);
505 err = _gpgme_op_reset (ctx, synchronous);
509 err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook,
510 sizeof (*opd), release_op_data);
515 opd->uidmode = extraflags? 2 : 1;
517 _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
519 if (ctx->passphrase_cb)
521 err = _gpgme_engine_set_command_handler
522 (ctx->engine, _gpgme_passphrase_command_handler, ctx, NULL);
527 return _gpgme_engine_op_genkey (ctx->engine,
537 /* Add USERID to an existing KEY. */
539 gpgme_op_adduid_start (gpgme_ctx_t ctx,
540 gpgme_key_t key, const char *userid, unsigned int flags)
544 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_adduid_start", ctx,
545 "uid='%s' flags=0x%x", userid, flags);
548 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
550 err = addrevuid_start (ctx, 0, 0, key, userid, flags);
551 return TRACE_ERR (err);
556 gpgme_op_adduid (gpgme_ctx_t ctx,
557 gpgme_key_t key, const char *userid, unsigned int flags)
561 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_adduid", ctx,
562 "uid='%s' flags=0x%x", userid, flags);
565 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
567 err = addrevuid_start (ctx, 1, 0, key, userid, flags);
569 err = _gpgme_wait_one (ctx);
570 return TRACE_ERR (err);
574 /* Revoke USERID from KEY. */
576 gpgme_op_revuid_start (gpgme_ctx_t ctx,
577 gpgme_key_t key, const char *userid, unsigned int flags)
581 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_revuid_start", ctx,
582 "uid='%s' flags=0x%x", userid, flags);
585 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
587 err = addrevuid_start (ctx, 0, GENKEY_EXTRAFLAG_REVOKE, key, userid, flags);
588 return TRACE_ERR (err);
593 gpgme_op_revuid (gpgme_ctx_t ctx,
594 gpgme_key_t key, const char *userid, unsigned int flags)
598 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_revuid", ctx,
599 "uid='%s' flags=0x%x", userid, flags);
602 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
604 err = addrevuid_start (ctx, 1, GENKEY_EXTRAFLAG_REVOKE, key, userid, flags);
606 err = _gpgme_wait_one (ctx);
607 return TRACE_ERR (err);
611 /* Set a flag on the USERID of KEY. The only supported flag right now
612 * is "primary" to mark the primary key. */
614 set_uid_flag (gpgme_ctx_t ctx, int synchronous,
615 gpgme_key_t key, const char *userid,
616 const char *name, const char *value)
620 TRACE_BEG4 (DEBUG_CTX, "gpgme_op_set_uid_flag", ctx,
621 "%d uid='%s' '%s'='%s'", synchronous, userid, name, value);
623 if (!ctx || !name || !key || !userid)
624 return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG));
626 if (!strcmp (name, "primary"))
629 err = gpg_error (GPG_ERR_INV_ARG);
631 err = addrevuid_start (ctx, synchronous,
632 GENKEY_EXTRAFLAG_SETPRIMARY, key, userid, 0);
635 return err = gpg_error (GPG_ERR_UNKNOWN_NAME);
637 if (synchronous && !err)
638 err = _gpgme_wait_one (ctx);
639 return TRACE_ERR (err);
643 /* See set_uid_flag. */
645 gpgme_op_set_uid_flag_start (gpgme_ctx_t ctx,
646 gpgme_key_t key, const char *userid,
647 const char *name, const char *value)
649 return set_uid_flag (ctx, 0, key, userid, name, value);
653 /* See set_uid_flag. This is the synchronous variant. */
655 gpgme_op_set_uid_flag (gpgme_ctx_t ctx,
656 gpgme_key_t key, const char *userid,
657 const char *name, const char *value)
659 return set_uid_flag (ctx, 1, key, userid, name, value);