From c943380b7a2cc9b32f81c22224fc6f92e8ea8469 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Wed, 14 Sep 2016 12:41:16 +0200 Subject: [PATCH] core: New function gpgme_op_revuid. * src/engine.h (GENKEY_EXTRAFLAG_REVOKE): New. * src/genkey.c (adduid_start): Rename to addrevuid_start. Add arg REVOKE and pass it as extraflags. Remove useless ARMOR extraflag. Adjust callers. (gpgme_op_revuid_start, gpgme_op_revuid): New. * src/gpgme.def, src/libgpgme.vers: Add them. * tests/run-genkey.c: Add option --revuid. Signed-off-by: Werner Koch --- NEWS | 2 ++ src/engine-gpg.c | 11 +++++++--- src/engine.h | 3 ++- src/genkey.c | 51 +++++++++++++++++++++++++++++++++++++++------- src/gpgme.def | 2 ++ src/gpgme.h.in | 12 +++++++++-- src/libgpgme.vers | 2 ++ tests/run-genkey.c | 26 +++++++++++++++++++++-- 8 files changed, 94 insertions(+), 15 deletions(-) diff --git a/NEWS b/NEWS index e428bd59..10296ff0 100644 --- a/NEWS +++ b/NEWS @@ -21,6 +21,8 @@ Noteworthy changes in version 1.7.0 (unreleased) [C25/A14/R_] gpgme_op_createsubkey_start NEW. gpgme_op_adduid_start NEW. gpgme_op_adduid NEW. + gpgme_op_revuid_start NEW. + gpgme_op_revuid NEW. gpgme_genkey_result_t EXTENDED: New fields pubkey and seckey. gpgme_signature_t EXTENDED: New field key. gpgme_key_t EXTENDED: New field fpr. diff --git a/src/engine-gpg.c b/src/engine-gpg.c index db6637a2..534d5d10 100644 --- a/src/engine-gpg.c +++ b/src/engine-gpg.c @@ -2115,14 +2115,19 @@ gpg_addkey (engine_gpg_t gpg, static gpgme_error_t gpg_adduid (engine_gpg_t gpg, gpgme_key_t key, - const char *userid) + const char *userid, + unsigned int extraflags) { gpgme_error_t err; if (!key || !key->fpr || !userid) return gpg_error (GPG_ERR_INV_ARG); - err = add_arg (gpg, "--quick-adduid"); + if ((extraflags & GENKEY_EXTRAFLAG_REVOKE)) + err = add_arg (gpg, "--quick-revuid"); + else + err = add_arg (gpg, "--quick-adduid"); + if (!err) err = add_arg (gpg, "--"); if (!err) @@ -2184,7 +2189,7 @@ gpg_genkey (void *engine, else if (!userid && key) err = gpg_addkey (gpg, algo, expires, key, flags, extraflags); else if (userid && key && !algo) - err = gpg_adduid (gpg, key, userid); + err = gpg_adduid (gpg, key, userid, extraflags); else err = gpg_error (GPG_ERR_INV_VALUE); diff --git a/src/engine.h b/src/engine.h index 391caf28..eb37da8f 100644 --- a/src/engine.h +++ b/src/engine.h @@ -25,7 +25,8 @@ #include "gpgme.h" /* Flags used by the EXTRAFLAGS arg of _gpgme_engine_op_genkey. */ -#define GENKEY_EXTRAFLAG_ARMOR 1 +#define GENKEY_EXTRAFLAG_ARMOR 1 +#define GENKEY_EXTRAFLAG_REVOKE 2 struct engine; diff --git a/src/genkey.c b/src/genkey.c index 100e655e..9dcf0be8 100644 --- a/src/genkey.c +++ b/src/genkey.c @@ -181,7 +181,7 @@ genkey_status_handler (void *priv, gpgme_status_code_t code, char *args) return gpg_error (GPG_ERR_GENERAL); else if (opd->failure_code) return opd->failure_code; - else if (opd->uidmode) + else if (opd->uidmode == 1) opd->result.uid = 1; /* We have no status line, thus this hack. */ break; @@ -489,8 +489,8 @@ gpgme_op_createsubkey (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo, static gpgme_error_t -adduid_start (gpgme_ctx_t ctx, int synchronous, - gpgme_key_t key, const char *userid, unsigned int flags) +addrevuid_start (gpgme_ctx_t ctx, int synchronous, int revoke, + gpgme_key_t key, const char *userid, unsigned int flags) { gpgme_error_t err; void *hook; @@ -512,7 +512,7 @@ adduid_start (gpgme_ctx_t ctx, int synchronous, if (err) return err; - opd->uidmode = 1; + opd->uidmode = revoke? 2 : 1; _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx); @@ -528,7 +528,7 @@ adduid_start (gpgme_ctx_t ctx, int synchronous, userid, NULL, 0, 0, key, flags, NULL, - ctx->use_armor? GENKEY_EXTRAFLAG_ARMOR:0, + revoke? GENKEY_EXTRAFLAG_REVOKE : 0, NULL, NULL); } @@ -547,7 +547,7 @@ gpgme_op_adduid_start (gpgme_ctx_t ctx, if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); - err = adduid_start (ctx, 0, key, userid, flags); + err = addrevuid_start (ctx, 0, 0, key, userid, flags); return TRACE_ERR (err); } @@ -564,7 +564,44 @@ gpgme_op_adduid (gpgme_ctx_t ctx, if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); - err = adduid_start (ctx, 1, key, userid, flags); + err = addrevuid_start (ctx, 1, 0, key, userid, flags); + if (!err) + err = _gpgme_wait_one (ctx); + return TRACE_ERR (err); +} + + +/* Revoke USERID from KEY. */ +gpgme_error_t +gpgme_op_revuid_start (gpgme_ctx_t ctx, + gpgme_key_t key, const char *userid, unsigned int flags) +{ + gpgme_error_t err; + + TRACE_BEG2 (DEBUG_CTX, "gpgme_op_revuid_start", ctx, + "uid='%s' flags=0x%x", userid, flags); + + if (!ctx) + return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); + + err = addrevuid_start (ctx, 0, 1, key, userid, flags); + return TRACE_ERR (err); +} + + +gpgme_error_t +gpgme_op_revuid (gpgme_ctx_t ctx, + gpgme_key_t key, const char *userid, unsigned int flags) +{ + gpgme_error_t err; + + TRACE_BEG2 (DEBUG_CTX, "gpgme_op_revuid", ctx, + "uid='%s' flags=0x%x", userid, flags); + + if (!ctx) + return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); + + err = addrevuid_start (ctx, 1, 1, key, userid, flags); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); diff --git a/src/gpgme.def b/src/gpgme.def index 54b04e04..4c7ff414 100644 --- a/src/gpgme.def +++ b/src/gpgme.def @@ -235,6 +235,8 @@ EXPORTS gpgme_op_createsubkey @175 gpgme_op_adduid_start @176 gpgme_op_adduid @177 + gpgme_op_revuid_start @178 + gpgme_op_revuid @179 ; END diff --git a/src/gpgme.h.in b/src/gpgme.h.in index ec436c80..a26b4324 100644 --- a/src/gpgme.h.in +++ b/src/gpgme.h.in @@ -1894,10 +1894,18 @@ gpgme_error_t gpgme_op_createsubkey (gpgme_ctx_t ctx, /* Add USERID to an existing KEY. */ gpgme_error_t gpgme_op_adduid_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, - unsigned int flags); + unsigned int reserved); gpgme_error_t gpgme_op_adduid (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, - unsigned int flags); + unsigned int reserved); + +/* Revoke a USERID from a KEY. */ +gpgme_error_t gpgme_op_revuid_start (gpgme_ctx_t ctx, + gpgme_key_t key, const char *userid, + unsigned int reserved); +gpgme_error_t gpgme_op_revuid (gpgme_ctx_t ctx, + gpgme_key_t key, const char *userid, + unsigned int reserved); diff --git a/src/libgpgme.vers b/src/libgpgme.vers index 0cef9e0b..8193ee4d 100644 --- a/src/libgpgme.vers +++ b/src/libgpgme.vers @@ -109,6 +109,8 @@ GPGME_1.1 { gpgme_op_createsubkey; gpgme_op_adduid_start; gpgme_op_adduid; + gpgme_op_revuid_start; + gpgme_op_revuid; }; diff --git a/tests/run-genkey.c b/tests/run-genkey.c index 959e2ea1..20e7db50 100644 --- a/tests/run-genkey.c +++ b/tests/run-genkey.c @@ -203,9 +203,11 @@ show_usage (int ex) " args: USERID [ALGO [USAGE [EXPIRESECONDS]]]\n" " for addkey: FPR [ALGO [USAGE [EXPIRESECONDS]]]\n" " for adduid: FPR USERID\n" + " for revuid: FPR USERID\n" "Options:\n" " --addkey add a subkey to the key with FPR\n" " --adduid add a user id to the key with FPR\n" + " --revuid Revoke a user id from the key with FPR\n" " --verbose run in verbose mode\n" " --status print status lines from the backend\n" " --progress print progress info\n" @@ -231,6 +233,7 @@ main (int argc, char **argv) int use_loopback = 0; int addkey = 0; int adduid = 0; + int revuid = 0; const char *userid; const char *algo = NULL; const char *newuserid = NULL; @@ -255,12 +258,21 @@ main (int argc, char **argv) { addkey = 1; adduid = 0; + revuid = 0; argc--; argv++; } else if (!strcmp (*argv, "--adduid")) { addkey = 0; adduid = 1; + revuid = 0; + argc--; argv++; + } + else if (!strcmp (*argv, "--revuid")) + { + addkey = 0; + adduid = 0; + revuid = 1; argc--; argv++; } else if (!strcmp (*argv, "--verbose")) @@ -307,7 +319,7 @@ main (int argc, char **argv) show_usage (1); } - if (adduid) + if (adduid || revuid) { if (argc != 2) show_usage (1); @@ -346,7 +358,7 @@ main (int argc, char **argv) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); } - if (addkey || adduid) + if (addkey || adduid || revuid) { gpgme_key_t akey; @@ -378,6 +390,16 @@ main (int argc, char **argv) exit (1); } } + else if (revuid) + { + err = gpgme_op_revuid (ctx, akey, newuserid, flags); + if (err) + { + fprintf (stderr, PGM ": gpgme_op_revuid failed: %s\n", + gpg_strerror (err)); + exit (1); + } + } gpgme_key_unref (akey); } else