1 /* run-keylist.c - Helper to show a key listing.
2 * Copyright (C) 2008, 2009 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://gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
21 /* We need to include config.h so that we know whether we are building
22 with large file system (LFS) support. */
34 #define PGM "run-keylist"
36 #include "run-support.h"
45 fputs ("usage: " PGM " [options] [USERID_or_FILE]\n\n"
47 " --verbose run in verbose mode\n"
48 " --openpgp use the OpenPGP protocol (default)\n"
49 " --cms use the CMS protocol\n"
50 " --secret list only secret keys\n"
51 " --with-secret list pubkeys with secret info filled\n"
52 " --local use GPGME_KEYLIST_MODE_LOCAL\n"
53 " --extern use GPGME_KEYLIST_MODE_EXTERN\n"
54 " --sigs use GPGME_KEYLIST_MODE_SIGS\n"
55 " --tofu use GPGME_KEYLIST_MODE_TOFU\n"
56 " --sig-notations use GPGME_KEYLIST_MODE_SIG_NOTATIONS\n"
57 " --ephemeral use GPGME_KEYLIST_MODE_EPHEMERAL\n"
58 " --v5fpr use GPGME_KEYLIST_MODE_V5FPR\n"
59 " --validate use GPGME_KEYLIST_MODE_VALIDATE\n"
60 " --import import all keys\n"
61 " --offline use offline mode\n"
62 " --no-trust-check disable automatic trust database check\n"
63 " --from-file list all keys in the given file\n"
64 " --from-wkd list key from a web key directory\n"
65 " --require-gnupg required at least the given GnuPG version\n"
66 " --trust-model use the specified trust-model\n"
73 isotimestr (unsigned long value)
76 static char buffer[25+5];
84 snprintf (buffer, sizeof buffer, "%04d-%02d-%02d %02d:%02d:%02d",
85 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
86 tp->tm_hour, tp->tm_min, tp->tm_sec);
93 main (int argc, char **argv)
98 gpgme_keylist_mode_t mode = 0;
100 gpgme_subkey_t subkey;
101 gpgme_keylist_result_t result;
103 gpgme_key_t keyarray[100];
105 gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
108 int no_trust_check = 0;
111 gpgme_data_t data = NULL;
112 char *trust_model = NULL;
118 while (argc && last_argc != argc )
121 if (!strcmp (*argv, "--"))
126 else if (!strcmp (*argv, "--help"))
128 else if (!strcmp (*argv, "--verbose"))
133 else if (!strcmp (*argv, "--openpgp"))
135 protocol = GPGME_PROTOCOL_OpenPGP;
138 else if (!strcmp (*argv, "--cms"))
140 protocol = GPGME_PROTOCOL_CMS;
143 else if (!strcmp (*argv, "--secret"))
148 else if (!strcmp (*argv, "--local"))
150 mode |= GPGME_KEYLIST_MODE_LOCAL;
153 else if (!strcmp (*argv, "--extern"))
155 mode |= GPGME_KEYLIST_MODE_EXTERN;
158 else if (!strcmp (*argv, "--tofu"))
160 mode |= GPGME_KEYLIST_MODE_WITH_TOFU;
163 else if (!strcmp (*argv, "--sigs"))
165 mode |= GPGME_KEYLIST_MODE_SIGS;
168 else if (!strcmp (*argv, "--sig-notations"))
170 mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS;
173 else if (!strcmp (*argv, "--ephemeral"))
175 mode |= GPGME_KEYLIST_MODE_EPHEMERAL;
178 else if (!strcmp (*argv, "--validate"))
180 mode |= GPGME_KEYLIST_MODE_VALIDATE;
183 else if (!strcmp (*argv, "--with-secret"))
185 mode |= GPGME_KEYLIST_MODE_WITH_SECRET;
188 else if (!strcmp (*argv, "--v5fpr"))
190 mode |= GPGME_KEYLIST_MODE_WITH_V5FPR;
193 else if (!strcmp (*argv, "--import"))
198 else if (!strcmp (*argv, "--offline"))
203 else if (!strcmp (*argv, "--no-trust-check"))
208 else if (!strcmp (*argv, "--from-file"))
213 else if (!strcmp (*argv, "--require-gnupg"))
218 gpgme_set_global_flag ("require-gnupg", *argv);
221 else if (!strcmp (*argv, "--from-wkd"))
224 mode |= GPGME_KEYLIST_MODE_LOCATE;
227 else if (!strcmp (*argv, "--trust-model"))
232 trust_model = strdup (*argv);
235 else if (!strncmp (*argv, "--", 2))
241 else if (from_file && !argc)
244 init_gpgme (protocol);
246 err = gpgme_new (&ctx);
248 gpgme_set_protocol (ctx, protocol);
250 gpgme_set_keylist_mode (ctx, mode);
252 gpgme_set_offline (ctx, offline);
256 err = gpgme_set_ctx_flag (ctx, "no-auto-check-trustdb", "1");
262 err = gpgme_set_ctx_flag (ctx, "trust-model", trust_model);
268 err = gpgme_set_ctx_flag (ctx, "auto-key-locate",
269 "clear,nodefault,wkd");
275 err = gpgme_data_new_from_file (&data, *argv, 1);
278 err = gpgme_op_keylist_from_data_start (ctx, data, 0);
281 err = gpgme_op_keylist_start (ctx, argc? argv[0]:NULL, only_secret);
284 while (!(err = gpgme_op_keylist_next (ctx, &key)))
287 gpgme_tofu_info_t ti;
293 printf ("keyid : %s\n", key->subkeys?nonnull (key->subkeys->keyid):"?");
294 printf ("can_cap : %s%s%s%s\n",
295 key->can_encrypt? "e":"",
296 key->can_sign? "s":"",
297 key->can_certify? "c":"",
298 key->can_authenticate? "a":"");
299 printf ("has_cap : %s%s%s%s\n",
300 key->has_encrypt? "e":"",
301 key->has_sign? "s":"",
302 key->has_certify? "c":"",
303 key->has_authenticate? "a":"");
304 printf ("flags :%s%s%s%s%s%s%s%s\n",
305 key->secret? " secret":"",
306 key->revoked? " revoked":"",
307 key->expired? " expired":"",
308 key->disabled? " disabled":"",
309 key->invalid? " invalid":"",
310 key->is_qualified? " qualified":"",
311 key->subkeys && key->subkeys->is_de_vs? " de-vs":"",
312 key->subkeys && key->subkeys->is_cardkey? " cardkey":"");
313 printf ("upd : %lu (%u)\n", key->last_update, key->origin);
315 subkey = key->subkeys;
316 for (nsub=0; subkey; subkey = subkey->next, nsub++)
318 printf ("fpr %2d: %s\n", nsub, nonnull (subkey->fpr));
320 printf ("v5fpr %2d: %s\n", nsub, nonnull (subkey->v5fpr));
322 printf ("grip %2d: %s\n", nsub, subkey->keygrip);
324 printf ("curve %2d: %s\n", nsub, subkey->curve);
325 printf ("caps %2d: %s%s%s%s%s%s\n",
327 subkey->can_encrypt? "e":"",
328 subkey->can_sign? "s":"",
329 subkey->can_certify? "c":"",
330 subkey->can_authenticate? "a":"",
331 subkey->can_renc? "r":"",
332 subkey->can_timestamp? "t":"");
333 printf ("flags %2d:%s%s%s%s%s%s%s%s%s\n",
335 subkey->secret? " secret":"",
336 subkey->revoked? " revoked":"",
337 subkey->expired? " expired":"",
338 subkey->disabled? " disabled":"",
339 subkey->invalid? " invalid":"",
340 subkey->is_group_owned? " group":"",
341 subkey->is_qualified? " qualified":"",
342 subkey->is_de_vs? " de-vs":"",
343 subkey->is_cardkey? " cardkey":"");
345 for (nuids=0, uid=key->uids; uid; uid = uid->next, nuids++)
347 printf ("userid %d: %s\n", nuids, nonnull(uid->uid));
348 printf (" mbox: %s\n", nonnull(uid->address));
349 if (uid->email && uid->email != uid->address)
350 printf (" email: %s\n", uid->email);
352 printf (" name: %s\n", uid->name);
354 printf (" cmmnt: %s\n", uid->comment);
356 printf (" uidhash: %s\n", uid->uidhash);
357 printf (" upd: %lu (%u)\n", uid->last_update, uid->origin);
358 printf (" valid: %s\n",
359 uid->validity == GPGME_VALIDITY_UNKNOWN? "unknown":
360 uid->validity == GPGME_VALIDITY_UNDEFINED? "undefined":
361 uid->validity == GPGME_VALIDITY_NEVER? "never":
362 uid->validity == GPGME_VALIDITY_MARGINAL? "marginal":
363 uid->validity == GPGME_VALIDITY_FULL? "full":
364 uid->validity == GPGME_VALIDITY_ULTIMATE? "ultimate": "[?]");
365 if ((ti = uid->tofu))
367 printf (" tofu: %u (%s)\n", ti->validity,
368 ti->validity == 0? "conflict" :
369 ti->validity == 1? "no history" :
370 ti->validity == 2? "little history" :
371 ti->validity == 3? "enough history" :
372 ti->validity == 4? "lot of history" : "?");
373 printf (" policy: %u (%s)\n", ti->policy,
374 ti->policy == GPGME_TOFU_POLICY_NONE? "none" :
375 ti->policy == GPGME_TOFU_POLICY_AUTO? "auto" :
376 ti->policy == GPGME_TOFU_POLICY_GOOD? "good" :
377 ti->policy == GPGME_TOFU_POLICY_UNKNOWN? "unknown" :
378 ti->policy == GPGME_TOFU_POLICY_BAD? "bad" :
379 ti->policy == GPGME_TOFU_POLICY_ASK? "ask" : "?");
380 printf (" nsigs: %hu\n", ti->signcount);
381 printf (" first: %s\n", isotimestr (ti->signfirst));
382 printf (" last: %s\n", isotimestr (ti->signlast));
383 printf (" nencr: %hu\n", ti->encrcount);
384 printf (" first: %s\n", isotimestr (ti->encrfirst));
385 printf (" last: %s\n", isotimestr (ti->encrlast));
387 for (nsigs=0, ks=uid->signatures; ks; ks = ks->next, nsigs++)
389 printf ("signature %d: %s\n", nsigs, nonnull (ks->uid));
390 printf (" keyid: %s\n", nonnull (ks->keyid));
391 printf (" created: %s\n", isotimestr(ks->timestamp));
392 printf (" expires: %s\n", isotimestr(ks->expires));
393 printf (" class: %x\n", ks->sig_class);
394 printf (" trust depth: %u\n", ks->trust_depth);
395 printf (" trust value: %u\n", ks->trust_value);
396 printf (" trust scope: %s\n", nonnull (ks->trust_scope));
404 if (keyidx < DIM (keyarray)-1)
405 keyarray[keyidx++] = key;
408 fprintf (stderr, PGM": too many keys in import mode"
409 "- skipping this key\n");
410 gpgme_key_unref (key);
414 gpgme_key_unref (key);
416 if (gpgme_err_code (err) != GPG_ERR_EOF)
418 err = gpgme_op_keylist_end (ctx);
420 keyarray[keyidx] = NULL;
421 gpgme_data_release (data);
423 result = gpgme_op_keylist_result (ctx);
424 if (result->truncated)
426 fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
432 gpgme_import_result_t impres;
434 err = gpgme_op_import_keys (ctx, keyarray);
436 impres = gpgme_op_import_result (ctx);
439 fprintf (stderr, PGM ": no import result returned\n");
442 print_import_result (impres);
445 for (keyidx=0; keyarray[keyidx]; keyidx++)
446 gpgme_key_unref (keyarray[keyidx]);