1 /* learncard.c - Handle the LEARN command
2 * Copyright (C) 2002, 2003, 2004, 2009 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
32 /* Structures used by the callback mechanism to convey information
33 pertaining to key pairs. */
36 struct keypair_info_s *next;
38 char *id; /* points into grip */
39 char hexgrip[1]; /* The keygrip (i.e. a hash over the public key
40 parameters) formatted as a hex string.
41 Allocated somewhat large to also act as
42 memory for the above ID field. */
44 typedef struct keypair_info_s *KEYPAIR_INFO;
46 struct kpinfo_cb_parm_s
54 /* Structures used by the callback mechanism to convey information
55 pertaining to certificates. */
57 struct certinfo_s *next;
62 typedef struct certinfo_s *CERTINFO;
64 struct certinfo_cb_parm_s
72 /* Structures used by the callback mechanism to convey assuan status
76 char *data; /* Points into keyword. */
79 typedef struct sinfo_s *SINFO;
81 struct sinfo_cb_parm_s {
87 /* Destructor for key information objects. */
89 release_keypair_info (KEYPAIR_INFO info)
93 KEYPAIR_INFO tmp = info->next;
99 /* Destructor for certificate information objects. */
101 release_certinfo (CERTINFO info)
105 CERTINFO tmp = info->next;
111 /* Destructor for status information objects. */
113 release_sinfo (SINFO info)
117 SINFO tmp = info->next;
125 /* This callback is used by agent_card_learn and passed the content of
126 all KEYPAIRINFO lines. It merely stores this data away */
128 kpinfo_cb (void *opaque, const char *line)
130 struct kpinfo_cb_parm_s *parm = opaque;
135 return; /* no need to gather data after an error occurred */
137 if ((parm->error = agent_write_status (parm->ctrl, "PROGRESS",
138 "learncard", "k", "0", "0", NULL)))
141 item = xtrycalloc (1, sizeof *item + strlen (line));
144 parm->error = out_of_core ();
147 strcpy (item->hexgrip, line);
148 for (p = item->hexgrip; hexdigitp (p); p++)
150 if (p == item->hexgrip && *p == 'X' && spacep (p+1))
155 else if ((p - item->hexgrip) != 40 || !spacep (p))
156 { /* not a 20 byte hex keygrip or not followed by a space */
157 parm->error = gpg_error (GPG_ERR_INV_RESPONSE);
165 while (*p && !spacep (p))
168 { /* invalid ID string */
169 parm->error = gpg_error (GPG_ERR_INV_RESPONSE);
173 *p = 0; /* ignore trailing stuff */
176 item->next = parm->info;
181 /* This callback is used by agent_card_learn and passed the content of
182 all CERTINFO lines. It merely stores this data away */
184 certinfo_cb (void *opaque, const char *line)
186 struct certinfo_cb_parm_s *parm = opaque;
192 return; /* no need to gather data after an error occurred */
194 if ((parm->error = agent_write_status (parm->ctrl, "PROGRESS",
195 "learncard", "c", "0", "0", NULL)))
198 type = strtol (line, &p, 10);
201 for (pend = p; *pend && !spacep (pend); pend++)
203 if (p == pend || !*p)
205 parm->error = gpg_error (GPG_ERR_INV_RESPONSE);
208 *pend = 0; /* ignore trailing stuff */
210 item = xtrycalloc (1, sizeof *item + strlen (p));
213 parm->error = out_of_core ();
217 strcpy (item->id, p);
219 item->next = parm->info;
224 /* This callback is used by agent_card_learn and passed the content of
225 all SINFO lines. It merely stores this data away */
227 sinfo_cb (void *opaque, const char *keyword, size_t keywordlen,
230 struct sinfo_cb_parm_s *sparm = opaque;
234 return; /* no need to gather data after an error occurred */
236 item = xtrycalloc (1, sizeof *item + keywordlen + 1 + strlen (data));
239 sparm->error = out_of_core ();
242 memcpy (item->keyword, keyword, keywordlen);
243 item->data = item->keyword + keywordlen;
246 strcpy (item->data, data);
248 item->next = sparm->info;
255 send_cert_back (ctrl_t ctrl, const char *id, void *assuan_context)
261 rc = agent_card_readcert (ctrl, id, &derbuf, &derbuflen);
266 switch (gpg_err_code (rc))
269 case GPG_ERR_NOT_FOUND:
270 action = " - ignored";
276 if (opt.verbose || !*action)
277 log_info ("error reading certificate '%s': %s%s\n",
278 id? id:"?", gpg_strerror (rc), action);
280 return *action? 0 : rc;
283 rc = assuan_send_data (assuan_context, derbuf, derbuflen);
286 rc = assuan_send_data (assuan_context, NULL, 0);
288 rc = assuan_write_line (assuan_context, "END");
291 log_error ("sending certificate failed: %s\n",
298 /* Perform the learn operation. If ASSUAN_CONTEXT is not NULL and
299 SEND is true all new certificates are send back via Assuan. */
301 agent_handle_learn (ctrl_t ctrl, int send, void *assuan_context, int force)
304 struct kpinfo_cb_parm_s parm;
305 struct certinfo_cb_parm_s cparm;
306 struct sinfo_cb_parm_s sparm;
307 const char *serialno = NULL;
310 unsigned char grip[20];
313 static int certtype_list[] = {
318 /* We don't include 110 here because gpgsm can't handle that
319 special root CA format. */
324 memset (&parm, 0, sizeof parm);
325 memset (&cparm, 0, sizeof cparm);
326 memset (&sparm, 0, sizeof sparm);
330 /* Now gather all the available info. */
331 rc = agent_card_learn (ctrl, kpinfo_cb, &parm, certinfo_cb, &cparm,
333 if (!rc && (parm.error || cparm.error || sparm.error))
334 rc = parm.error? parm.error : cparm.error? cparm.error : sparm.error;
337 log_debug ("agent_card_learn failed: %s\n", gpg_strerror (rc));
341 /* Pass on all the collected status information. */
342 for (sitem = sparm.info; sitem; sitem = sitem->next)
344 if (!strcmp (sitem->keyword, "SERIALNO"))
345 serialno = sitem->data;
347 assuan_write_status (assuan_context, sitem->keyword, sitem->data);
352 rc = GPG_ERR_NOT_FOUND;
356 log_info ("card has S/N: %s\n", serialno);
358 /* Write out the certificates in a standard order. */
359 for (i=0; certtype_list[i] != -1; i++)
362 for (citem = cparm.info; citem; citem = citem->next)
364 if (certtype_list[i] != citem->type)
368 log_info (" id: %s (type=%d)\n",
369 citem->id, citem->type);
371 if (assuan_context && send)
373 rc = send_cert_back (ctrl, citem->id, assuan_context);
381 for (item = parm.info; item; item = item->next)
383 unsigned char *pubkey;
386 log_info (" id: %s (grip=%s)\n", item->id, item->hexgrip);
389 continue; /* No public key yet available. */
393 agent_write_status (ctrl, "KEYPAIRINFO",
394 item->hexgrip, item->id, NULL);
397 for (p=item->hexgrip, i=0; i < 20; p += 2, i++)
398 grip[i] = xtoi_2 (p);
400 if (!force && !agent_key_available (grip))
401 continue; /* The key is already available. */
403 /* Unknown key - store it. */
404 rc = agent_card_readkey (ctrl, item->id, &pubkey, NULL);
407 log_debug ("agent_card_readkey failed: %s\n", gpg_strerror (rc));
414 agent_card_getattr (ctrl, "$DISPSERIALNO", &dispserialno,
416 rc = agent_write_shadow_key (grip, serialno, item->id, pubkey, force,
418 xfree (dispserialno);
425 log_info (" id: %s - shadow key created\n", item->id);
427 if (assuan_context && send)
431 /* only send the certificate if we have not done so before */
432 for (citem = cparm.info; citem; citem = citem->next)
434 if (!strcmp (citem->id, item->id))
439 rc = send_cert_back (ctrl, item->id, assuan_context);
448 release_keypair_info (parm.info);
449 release_certinfo (cparm.info);
450 release_sinfo (sparm.info);