diff --git a/g10/pubkey-enc.c b/g10/pubkey-enc.c index 71a48cc41..4e6f893f3 100644 --- a/g10/pubkey-enc.c +++ b/g10/pubkey-enc.c @@ -1,496 +1,500 @@ /* pubkey-enc.c - Process a public key encoded packet. * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2006, 2009, * 2010 Free Software Foundation, Inc. * * This file is part of GnuPG. * * GnuPG is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * GnuPG is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ #include #include #include #include #include "gpg.h" #include "../common/util.h" #include "packet.h" #include "keydb.h" #include "trustdb.h" #include "../common/status.h" #include "options.h" #include "main.h" #include "../common/i18n.h" #include "pkglue.h" #include "call-agent.h" #include "../common/host2net.h" #include "../common/compliance.h" static gpg_error_t get_it (ctrl_t ctrl, PKT_pubkey_enc *k, DEK *dek, PKT_public_key *sk, u32 *keyid); /* Check that the given algo is mentioned in one of the valid user-ids. */ static int is_algo_in_prefs (kbnode_t keyblock, preftype_t type, int algo) { kbnode_t k; for (k = keyblock; k; k = k->next) { if (k->pkt->pkttype == PKT_USER_ID) { PKT_user_id *uid = k->pkt->pkt.user_id; prefitem_t *prefs = uid->prefs; if (uid->created && prefs && !uid->flags.revoked && !uid->flags.expired) { for (; prefs->type; prefs++) if (prefs->type == type && prefs->value == algo) return 1; } } } return 0; } /* * Get the session key from a pubkey enc packet and return it in DEK, * which should have been allocated in secure memory by the caller. */ gpg_error_t get_session_key (ctrl_t ctrl, PKT_pubkey_enc * k, DEK * dek) { PKT_public_key *sk = NULL; int rc; if (DBG_CLOCK) log_clock ("get_session_key enter"); rc = openpgp_pk_test_algo2 (k->pubkey_algo, PUBKEY_USAGE_ENC); if (rc) goto leave; if ((k->keyid[0] || k->keyid[1]) && !opt.try_all_secrets) { sk = xmalloc_clear (sizeof *sk); sk->pubkey_algo = k->pubkey_algo; /* We want a pubkey with this algo. */ if (!(rc = get_seckey (ctrl, sk, k->keyid))) { /* Check compliance. */ if (! gnupg_pk_is_allowed (opt.compliance, PK_USE_DECRYPTION, sk->pubkey_algo, sk->pkey, nbits_from_pk (sk), NULL)) { log_info (_("key %s is not suitable for decryption" " in %s mode\n"), keystr_from_pk (sk), gnupg_compliance_option_string (opt.compliance)); rc = gpg_error (GPG_ERR_PUBKEY_ALGO); } else rc = get_it (ctrl, k, dek, sk, k->keyid); } } else if (opt.skip_hidden_recipients) rc = gpg_error (GPG_ERR_NO_SECKEY); else /* Anonymous receiver: Try all available secret keys. */ { void *enum_context = NULL; u32 keyid[2]; for (;;) { - free_public_key (sk); sk = xmalloc_clear (sizeof *sk); rc = enum_secret_keys (ctrl, &enum_context, sk); if (rc) { + sk = NULL; /* enum_secret_keys turns SK into a shallow copy! */ rc = GPG_ERR_NO_SECKEY; break; } if (sk->pubkey_algo != k->pubkey_algo) continue; if (!(sk->pubkey_usage & PUBKEY_USAGE_ENC)) continue; keyid_from_pk (sk, keyid); if (!opt.quiet) log_info (_("anonymous recipient; trying secret key %s ...\n"), keystr (keyid)); /* Check compliance. */ if (! gnupg_pk_is_allowed (opt.compliance, PK_USE_DECRYPTION, sk->pubkey_algo, sk->pkey, nbits_from_pk (sk), NULL)) { log_info (_("key %s is not suitable for decryption" " in %s mode\n"), keystr_from_pk (sk), gnupg_compliance_option_string (opt.compliance)); continue; } rc = get_it (ctrl, k, dek, sk, keyid); if (!rc) { if (!opt.quiet) log_info (_("okay, we are the anonymous recipient.\n")); + sk = NULL; break; } else if (gpg_err_code (rc) == GPG_ERR_FULLY_CANCELED) - break; /* Don't try any more secret keys. */ + { + sk = NULL; + break; /* Don't try any more secret keys. */ + } } enum_secret_keys (ctrl, &enum_context, NULL); /* free context */ } leave: free_public_key (sk); if (DBG_CLOCK) log_clock ("get_session_key leave"); return rc; } static gpg_error_t get_it (ctrl_t ctrl, PKT_pubkey_enc *enc, DEK *dek, PKT_public_key *sk, u32 *keyid) { gpg_error_t err; byte *frame = NULL; unsigned int n; size_t nframe; u16 csum, csum2; int padding; gcry_sexp_t s_data; char *desc; char *keygrip; byte fp[MAX_FINGERPRINT_LEN]; size_t fpn; if (DBG_CLOCK) log_clock ("decryption start"); /* Get the keygrip. */ err = hexkeygrip_from_pk (sk, &keygrip); if (err) goto leave; /* Convert the data to an S-expression. */ if (sk->pubkey_algo == PUBKEY_ALGO_ELGAMAL || sk->pubkey_algo == PUBKEY_ALGO_ELGAMAL_E) { if (!enc->data[0] || !enc->data[1]) err = gpg_error (GPG_ERR_BAD_MPI); else err = gcry_sexp_build (&s_data, NULL, "(enc-val(elg(a%m)(b%m)))", enc->data[0], enc->data[1]); } else if (sk->pubkey_algo == PUBKEY_ALGO_RSA || sk->pubkey_algo == PUBKEY_ALGO_RSA_E) { if (!enc->data[0]) err = gpg_error (GPG_ERR_BAD_MPI); else err = gcry_sexp_build (&s_data, NULL, "(enc-val(rsa(a%m)))", enc->data[0]); } else if (sk->pubkey_algo == PUBKEY_ALGO_ECDH) { if (!enc->data[0] || !enc->data[1]) err = gpg_error (GPG_ERR_BAD_MPI); else err = gcry_sexp_build (&s_data, NULL, "(enc-val(ecdh(s%m)(e%m)))", enc->data[1], enc->data[0]); } else err = gpg_error (GPG_ERR_BUG); if (err) goto leave; if (sk->pubkey_algo == PUBKEY_ALGO_ECDH) { fingerprint_from_pk (sk, fp, &fpn); log_assert (fpn == 20); } /* Decrypt. */ desc = gpg_format_keydesc (ctrl, sk, FORMAT_KEYDESC_NORMAL, 1); err = agent_pkdecrypt (NULL, keygrip, desc, sk->keyid, sk->main_keyid, sk->pubkey_algo, s_data, &frame, &nframe, &padding); xfree (desc); gcry_sexp_release (s_data); if (err) goto leave; /* Now get the DEK (data encryption key) from the frame * * Old versions encode the DEK in this format (msb is left): * * 0 1 DEK(16 bytes) CSUM(2 bytes) 0 RND(n bytes) 2 * * Later versions encode the DEK like this: * * 0 2 RND(n bytes) 0 A DEK(k bytes) CSUM(2 bytes) * * (mpi_get_buffer already removed the leading zero). * * RND are non-zero randow bytes. * A is the cipher algorithm * DEK is the encryption key (session key) with length k * CSUM */ if (DBG_CRYPTO) log_printhex ("DEK frame:", frame, nframe); n = 0; if (sk->pubkey_algo == PUBKEY_ALGO_ECDH) { gcry_mpi_t shared_mpi; gcry_mpi_t decoded; /* At the beginning the frame are the bytes of shared point MPI. */ err = gcry_mpi_scan (&shared_mpi, GCRYMPI_FMT_USG, frame, nframe, NULL); if (err) { err = gpg_error (GPG_ERR_WRONG_SECKEY); goto leave; } err = pk_ecdh_decrypt (&decoded, fp, enc->data[1]/*encr data as an MPI*/, shared_mpi, sk->pkey); mpi_release (shared_mpi); if(err) goto leave; xfree (frame); err = gcry_mpi_aprint (GCRYMPI_FMT_USG, &frame, &nframe, decoded); mpi_release (decoded); if (err) goto leave; /* Now the frame are the bytes decrypted but padded session key. */ /* Allow double padding for the benefit of DEK size concealment. Higher than this is wasteful. */ if (!nframe || frame[nframe-1] > 8*2 || nframe <= 8 || frame[nframe-1] > nframe) { err = gpg_error (GPG_ERR_WRONG_SECKEY); goto leave; } nframe -= frame[nframe-1]; /* Remove padding. */ log_assert (!n); /* (used just below) */ } else { if (padding) { if (n + 7 > nframe) { err = gpg_error (GPG_ERR_WRONG_SECKEY); goto leave; } /* FIXME: Actually the leading zero is required but due to * the way we encode the output in libgcrypt as an MPI we * are not able to encode that leading zero. However, when * using a Smartcard we are doing it the right way and * therefore we have to skip the zero. This should be fixed * in gpg-agent of course. */ if (!frame[n]) n++; if (frame[n] == 1 && frame[nframe - 1] == 2) { log_info (_("old encoding of the DEK is not supported\n")); err = gpg_error (GPG_ERR_CIPHER_ALGO); goto leave; } if (frame[n] != 2) /* Something went wrong. */ { err = gpg_error (GPG_ERR_WRONG_SECKEY); goto leave; } for (n++; n < nframe && frame[n]; n++) /* Skip the random bytes. */ ; n++; /* Skip the zero byte. */ } } if (n + 4 > nframe) { err = gpg_error (GPG_ERR_WRONG_SECKEY); goto leave; } dek->keylen = nframe - (n + 1) - 2; dek->algo = frame[n++]; err = openpgp_cipher_test_algo (dek->algo); if (err) { if (!opt.quiet && gpg_err_code (err) == GPG_ERR_CIPHER_ALGO) { log_info (_("cipher algorithm %d%s is unknown or disabled\n"), dek->algo, dek->algo == CIPHER_ALGO_IDEA ? " (IDEA)" : ""); } dek->algo = 0; goto leave; } if (dek->keylen != openpgp_cipher_get_algo_keylen (dek->algo)) { err = gpg_error (GPG_ERR_WRONG_SECKEY); goto leave; } /* Copy the key to DEK and compare the checksum. */ csum = buf16_to_u16 (frame+nframe-2); memcpy (dek->key, frame + n, dek->keylen); for (csum2 = 0, n = 0; n < dek->keylen; n++) csum2 += dek->key[n]; if (csum != csum2) { err = gpg_error (GPG_ERR_WRONG_SECKEY); goto leave; } if (DBG_CLOCK) log_clock ("decryption ready"); if (DBG_CRYPTO) log_printhex ("DEK is:", dek->key, dek->keylen); /* Check that the algo is in the preferences and whether it has * expired. Also print a status line with the key's fingerprint. */ { PKT_public_key *pk = NULL; PKT_public_key *mainpk = NULL; KBNODE pkb = get_pubkeyblock (ctrl, keyid); if (!pkb) { err = -1; log_error ("oops: public key not found for preference check\n"); } else if (pkb->pkt->pkt.public_key->selfsigversion > 3 && dek->algo != CIPHER_ALGO_3DES && !opt.quiet && !is_algo_in_prefs (pkb, PREFTYPE_SYM, dek->algo)) log_info (_("WARNING: cipher algorithm %s not found in recipient" " preferences\n"), openpgp_cipher_algo_name (dek->algo)); if (!err) { kbnode_t k; int first = 1; for (k = pkb; k; k = k->next) { if (k->pkt->pkttype == PKT_PUBLIC_KEY || k->pkt->pkttype == PKT_PUBLIC_SUBKEY) { u32 aki[2]; if (first) { first = 0; mainpk = k->pkt->pkt.public_key; } keyid_from_pk (k->pkt->pkt.public_key, aki); if (aki[0] == keyid[0] && aki[1] == keyid[1]) { pk = k->pkt->pkt.public_key; break; } } } if (!pk) BUG (); if (pk->expiredate && pk->expiredate <= make_timestamp ()) { log_info (_("Note: secret key %s expired at %s\n"), keystr (keyid), asctimestamp (pk->expiredate)); } } if (pk && pk->flags.revoked) { log_info (_("Note: key has been revoked")); log_printf ("\n"); show_revocation_reason (ctrl, pk, 1); } if (is_status_enabled () && pk && mainpk) { char pkhex[MAX_FINGERPRINT_LEN*2+1]; char mainpkhex[MAX_FINGERPRINT_LEN*2+1]; hexfingerprint (pk, pkhex, sizeof pkhex); hexfingerprint (mainpk, mainpkhex, sizeof mainpkhex); /* Note that we do not want to create a trustdb just for * getting the ownertrust: If there is no trustdb there can't * be ulitmately trusted key anyway and thus the ownertrust * value is irrelevant. */ write_status_printf (STATUS_DECRYPTION_KEY, "%s %s %c", pkhex, mainpkhex, get_ownertrust_info (ctrl, mainpk, 1)); } release_kbnode (pkb); err = 0; } leave: xfree (frame); xfree (keygrip); return err; } /* * Get the session key from the given string. * String is supposed to be formatted as this: * : */ gpg_error_t get_override_session_key (DEK *dek, const char *string) { const char *s; int i; if (!string) return GPG_ERR_BAD_KEY; dek->algo = atoi (string); if (dek->algo < 1) return GPG_ERR_BAD_KEY; if (!(s = strchr (string, ':'))) return GPG_ERR_BAD_KEY; s++; for (i = 0; i < DIM (dek->key) && *s; i++, s += 2) { int c = hextobyte (s); if (c == -1) return GPG_ERR_BAD_KEY; dek->key[i] = c; } if (*s) return GPG_ERR_BAD_KEY; dek->keylen = i; return 0; } diff --git a/g10/skclist.c b/g10/skclist.c index 8817ee904..5a32b6a17 100644 --- a/g10/skclist.c +++ b/g10/skclist.c @@ -1,595 +1,598 @@ /* skclist.c - Build a list of secret keys * Copyright (C) 1998, 1999, 2000, 2001, 2006, * 2010 Free Software Foundation, Inc. * * This file is part of GnuPG. * * GnuPG is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * GnuPG is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ #include #include #include #include #include #include "gpg.h" #include "options.h" #include "packet.h" #include "../common/status.h" #include "keydb.h" #include "../common/util.h" #include "../common/i18n.h" #include "call-agent.h" /* Return true if Libgcrypt's RNG is in faked mode. */ int random_is_faked (void) { return !!gcry_control (GCRYCTL_FAKED_RANDOM_P, 0); } void release_sk_list (SK_LIST sk_list) { SK_LIST sk_rover; for (; sk_list; sk_list = sk_rover) { sk_rover = sk_list->next; free_public_key (sk_list->pk); xfree (sk_list); } } /* Check that we are only using keys which don't have * the string "(insecure!)" or "not secure" or "do not use" * in one of the user ids. */ static int is_insecure (ctrl_t ctrl, PKT_public_key *pk) { u32 keyid[2]; KBNODE node = NULL, u; int insecure = 0; keyid_from_pk (pk, keyid); node = get_pubkeyblock (ctrl, keyid); for (u = node; u; u = u->next) { if (u->pkt->pkttype == PKT_USER_ID) { PKT_user_id *id = u->pkt->pkt.user_id; if (id->attrib_data) continue; /* skip attribute packets */ if (strstr (id->name, "(insecure!)") || strstr (id->name, "not secure") || strstr (id->name, "do not use") || strstr (id->name, "(INSECURE!)")) { insecure = 1; break; } } } release_kbnode (node); return insecure; } static int key_present_in_sk_list (SK_LIST sk_list, PKT_public_key *pk) { for (; sk_list; sk_list = sk_list->next) { if (!cmp_public_keys (sk_list->pk, pk)) return 0; } return -1; } static int is_duplicated_entry (strlist_t list, strlist_t item) { for (; list && list != item; list = list->next) { if (!strcmp (list->d, item->d)) return 1; } return 0; } gpg_error_t build_sk_list (ctrl_t ctrl, strlist_t locusr, SK_LIST *ret_sk_list, unsigned int use) { gpg_error_t err; SK_LIST sk_list = NULL; /* XXX: Change this function to use get_pubkeys instead of getkey_byname to detect ambiguous key specifications and warn about duplicate keyblocks. For ambiguous key specifications on the command line or provided interactively, prompt the user to select the best key. If a key specification is ambiguous and we are in batch mode, die. */ if (!locusr) /* No user ids given - use the card key or the default key. */ { struct agent_card_info_s info; PKT_public_key *pk; char *serialno; memset (&info, 0, sizeof(info)); pk = xmalloc_clear (sizeof *pk); pk->req_usage = use; /* Check if a card is available. If any, use the key as a hint. */ err = agent_scd_serialno (&serialno, NULL); if (!err) { xfree (serialno); err = agent_scd_getattr ("KEY-FPR", &info); if (err) log_error ("error retrieving key fingerprint from card: %s\n", gpg_strerror (err)); } err = get_seckey_default_or_card (ctrl, pk, info.fpr1valid? info.fpr1 : NULL, 20); if (err) { free_public_key (pk); pk = NULL; log_error ("no default secret key: %s\n", gpg_strerror (err)); write_status_text (STATUS_INV_SGNR, get_inv_recpsgnr_code (err)); } else if ((err = openpgp_pk_test_algo2 (pk->pubkey_algo, use))) { free_public_key (pk); pk = NULL; log_error ("invalid default secret key: %s\n", gpg_strerror (err)); write_status_text (STATUS_INV_SGNR, get_inv_recpsgnr_code (err)); } else { SK_LIST r; if (random_is_faked () && !is_insecure (ctrl, pk)) { log_info (_("key is not flagged as insecure - " "can't use it with the faked RNG!\n")); free_public_key (pk); pk = NULL; write_status_text (STATUS_INV_SGNR, get_inv_recpsgnr_code (GPG_ERR_NOT_TRUSTED)); } else { r = xmalloc (sizeof *r); r->pk = pk; pk = NULL; r->next = sk_list; r->mark = 0; sk_list = r; } } } else /* Check the given user ids. */ { strlist_t locusr_orig = locusr; for (; locusr; locusr = locusr->next) { PKT_public_key *pk; err = 0; /* Do an early check against duplicated entries. However * this won't catch all duplicates because the user IDs may * be specified in different ways. */ if (is_duplicated_entry (locusr_orig, locusr)) { log_info (_("skipped \"%s\": duplicated\n"), locusr->d); continue; } pk = xmalloc_clear (sizeof *pk); pk->req_usage = use; if ((err = getkey_byname (ctrl, NULL, pk, locusr->d, 1, NULL))) { free_public_key (pk); pk = NULL; log_error (_("skipped \"%s\": %s\n"), locusr->d, gpg_strerror (err)); write_status_text_and_buffer (STATUS_INV_SGNR, get_inv_recpsgnr_code (err), locusr->d, strlen (locusr->d), -1); } else if (!key_present_in_sk_list (sk_list, pk)) { free_public_key (pk); pk = NULL; log_info (_("skipped: secret key already present\n")); } else if ((err = openpgp_pk_test_algo2 (pk->pubkey_algo, use))) { free_public_key (pk); pk = NULL; log_error ("skipped \"%s\": %s\n", locusr->d, gpg_strerror (err)); write_status_text_and_buffer (STATUS_INV_SGNR, get_inv_recpsgnr_code (err), locusr->d, strlen (locusr->d), -1); } else { SK_LIST r; if (pk->version == 4 && (use & PUBKEY_USAGE_SIG) && pk->pubkey_algo == PUBKEY_ALGO_ELGAMAL_E) { log_info (_("skipped \"%s\": %s\n"), locusr->d, _("this is a PGP generated Elgamal key which" " is not secure for signatures!")); free_public_key (pk); pk = NULL; write_status_text_and_buffer (STATUS_INV_SGNR, get_inv_recpsgnr_code (GPG_ERR_WRONG_KEY_USAGE), locusr->d, strlen (locusr->d), -1); } else if (random_is_faked () && !is_insecure (ctrl, pk)) { log_info (_("key is not flagged as insecure - " "can't use it with the faked RNG!\n")); free_public_key (pk); pk = NULL; write_status_text_and_buffer (STATUS_INV_SGNR, get_inv_recpsgnr_code (GPG_ERR_NOT_TRUSTED), locusr->d, strlen (locusr->d), -1); } else { r = xmalloc (sizeof *r); r->pk = pk; pk = NULL; r->next = sk_list; r->mark = 0; sk_list = r; } } } } if (!err && !sk_list) { log_error ("no valid signators\n"); write_status_text (STATUS_NO_SGNR, "0"); err = gpg_error (GPG_ERR_NO_USER_ID); } if (err) release_sk_list (sk_list); else *ret_sk_list = sk_list; return err; } /* Enumerate some secret keys (specifically, those specified with * --default-key and --try-secret-key). Use the following procedure: * * 1) Initialize a void pointer to NULL - * 2) Pass a reference to this pointer to this function (content) - * and provide space for the secret key (sk) + * 2) Pass a reference to this pointer to this function (CONTEXT) + * and provide space for the secret key (SK) * 3) Call this function as long as it does not return an error (or * until you are done). The error code GPG_ERR_EOF indicates the * end of the listing. * 4) Call this function a last time with SK set to NULL, * so that can free it's context. * + * TAKE CARE: When the function returns SK belongs to CONTEXT and may + * not be freed by the caller; neither on success nor on error. + * * In pseudo-code: * * void *ctx = NULL; * PKT_public_key *sk = xmalloc_clear (sizeof (*sk)); * * while ((err = enum_secret_keys (&ctx, sk))) * { // Process SK. * if (done) * break; * sk = xmalloc_clear (sizeof (*sk)); * } * * // Release any resources used by CTX. * enum_secret_keys (&ctx, NULL); * * if (gpg_err_code (err) != GPG_ERR_EOF) * ; // An error occurred. */ gpg_error_t enum_secret_keys (ctrl_t ctrl, void **context, PKT_public_key *sk) { gpg_error_t err = 0; const char *name; kbnode_t keyblock; struct { int eof; int state; strlist_t sl; strlist_t card_list; char *serialno; char fpr2[2 * MAX_FINGERPRINT_LEN + 3 ]; struct agent_card_info_s info; kbnode_t keyblock; kbnode_t node; getkey_ctx_t ctx; SK_LIST results; } *c = *context; #if MAX_FINGERPRINT_LEN < KEYGRIP_LEN # error buffer too short for this configuration #endif if (!c) { /* Make a new context. */ c = xtrycalloc (1, sizeof *c); if (!c) { err = gpg_error_from_syserror (); free_public_key (sk); return err; } *context = c; } if (!sk) { /* Free the context. */ xfree (c->serialno); free_strlist (c->card_list); release_sk_list (c->results); release_kbnode (c->keyblock); getkey_end (ctrl, c->ctx); xfree (c); *context = NULL; return 0; } if (c->eof) { free_public_key (sk); return gpg_error (GPG_ERR_EOF); } for (;;) { /* Loop until we have a keyblock. */ while (!c->keyblock) { /* Loop over the list of secret keys. */ do { char *serialno; name = NULL; keyblock = NULL; switch (c->state) { case 0: /* First try to use the --default-key. */ name = parse_def_secret_key (ctrl); c->state = 1; break; case 1: /* Init list of keys to try. */ c->sl = opt.secret_keys_to_try; c->state++; break; case 2: /* Get next item from list. */ if (c->sl) { name = c->sl->d; c->sl = c->sl->next; } else c->state++; break; case 3: /* Init list of card keys to try. */ err = agent_scd_cardlist (&c->card_list); if (!err) agent_scd_serialno (&c->serialno, NULL); c->sl = c->card_list; c->state++; break; case 4: /* Get next item from card list. */ if (c->sl) { err = agent_scd_serialno (&serialno, c->sl->d); if (err) { if (opt.verbose) log_info (_("error getting serial number of card: %s\n"), gpg_strerror (err)); c->sl = c->sl->next; continue; } xfree (serialno); err = agent_scd_getattr ("KEY-FPR", &c->info); if (!err) { if (c->info.fpr2valid) { c->fpr2[0] = '0'; c->fpr2[1] = 'x'; bin2hex (c->info.fpr2, sizeof c->info.fpr2, c->fpr2 + 2); name = c->fpr2; } } else if (gpg_err_code (err) == GPG_ERR_INV_NAME) { /* KEY-FPR not supported by the card - get * the key using the keygrip. */ char *keyref; strlist_t kplist, sl; const char *s; int i; err = agent_scd_getattr_one ("$ENCRKEYID", &keyref); if (!err) { err = agent_scd_keypairinfo (ctrl, &kplist); if (!err) { for (sl = kplist; sl; sl = sl->next) if ((s = strchr (sl->d, ' ')) && !strcmp (s+1, keyref)) break; if (sl) { c->fpr2[0] = '&'; for (i=1, s=sl->d; (*s && *s != ' ' && i < sizeof c->fpr2 - 3); s++, i++) c->fpr2[i] = *s; c->fpr2[i] = 0; name = c->fpr2; } else /* Restore error. */ err = gpg_error (GPG_ERR_INV_NAME); free_strlist (kplist); } } xfree (keyref); } if (err) log_error ("error retrieving key from card: %s\n", gpg_strerror (err)); c->sl = c->sl->next; } else { serialno = c->serialno; if (serialno) { /* Select the original card again. */ agent_scd_serialno (&c->serialno, serialno); xfree (serialno); } c->state++; } break; case 5: /* Init search context to enum all secret keys. */ err = getkey_bynames (ctrl, &c->ctx, NULL, NULL, 1, &keyblock); if (err) { release_kbnode (keyblock); keyblock = NULL; getkey_end (ctrl, c->ctx); c->ctx = NULL; } c->state++; break; case 6: /* Get next item from the context. */ if (c->ctx) { err = getkey_next (ctrl, c->ctx, NULL, &keyblock); if (err) { release_kbnode (keyblock); keyblock = NULL; getkey_end (ctrl, c->ctx); c->ctx = NULL; } } else c->state++; break; default: /* No more names to check - stop. */ c->eof = 1; free_public_key (sk); return gpg_error (GPG_ERR_EOF); } } while ((!name || !*name) && !keyblock); if (keyblock) c->node = c->keyblock = keyblock; else { err = getkey_byname (ctrl, NULL, NULL, name, 1, &c->keyblock); if (err) { /* getkey_byname might return a keyblock even in the error case - I have not checked. Thus better release it. */ release_kbnode (c->keyblock); c->keyblock = NULL; } else c->node = c->keyblock; } } /* Get the next key from the current keyblock. */ for (; c->node; c->node = c->node->next) { if (c->node->pkt->pkttype == PKT_PUBLIC_KEY || c->node->pkt->pkttype == PKT_PUBLIC_SUBKEY) { SK_LIST r; /* Skip this candidate if it's already enumerated. */ for (r = c->results; r; r = r->next) if (!cmp_public_keys (r->pk, c->node->pkt->pkt.public_key)) break; if (r) continue; copy_public_key (sk, c->node->pkt->pkt.public_key); c->node = c->node->next; r = xtrycalloc (1, sizeof (*r)); if (!r) { err = gpg_error_from_syserror (); free_public_key (sk); return err; } r->pk = sk; r->next = c->results; c->results = r; return 0; /* Found. */ } } /* Dispose the keyblock and continue. */ release_kbnode (c->keyblock); c->keyblock = NULL; } }