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 <http://www.gnu.org/licenses/>.
33 /* Structures used by the callback mechanism to convey information
34 pertaining to key pairs. */
37 struct keypair_info_s *next;
39 char *id; /* points into grip */
40 char hexgrip[1]; /* The keygrip (i.e. a hash over the public key
41 parameters) formatted as a hex string.
42 Allocated somewhat large to also act as
43 memeory for the above ID field. */
45 typedef struct keypair_info_s *KEYPAIR_INFO;
47 struct kpinfo_cb_parm_s
55 /* Structures used by the callback mechanism to convey information
56 pertaining to certificates. */
58 struct certinfo_s *next;
63 typedef struct certinfo_s *CERTINFO;
65 struct certinfo_cb_parm_s
73 /* Structures used by the callback mechanism to convey assuan status
77 char *data; /* Points into keyword. */
80 typedef struct sinfo_s *SINFO;
82 struct sinfo_cb_parm_s {
88 /* Destructor for key information objects. */
90 release_keypair_info (KEYPAIR_INFO info)
94 KEYPAIR_INFO tmp = info->next;
100 /* Destructor for certificate information objects. */
102 release_certinfo (CERTINFO info)
106 CERTINFO tmp = info->next;
112 /* Destructor for status information objects. */
114 release_sinfo (SINFO info)
118 SINFO tmp = info->next;
126 /* This callback is used by agent_card_learn and passed the content of
127 all KEYPAIRINFO lines. It merely stores this data away */
129 kpinfo_cb (void *opaque, const char *line)
131 struct kpinfo_cb_parm_s *parm = opaque;
136 return; /* no need to gather data after an error coccured */
138 if ((parm->error = agent_write_status (parm->ctrl, "PROGRESS",
139 "learncard", "k", "0", "0", NULL)))
142 item = xtrycalloc (1, sizeof *item + strlen (line));
145 parm->error = out_of_core ();
148 strcpy (item->hexgrip, line);
149 for (p = item->hexgrip; hexdigitp (p); p++)
151 if (p == item->hexgrip && *p == 'X' && spacep (p+1))
156 else if ((p - item->hexgrip) != 40 || !spacep (p))
157 { /* not a 20 byte hex keygrip or not followed by a space */
158 parm->error = gpg_error (GPG_ERR_INV_RESPONSE);
166 while (*p && !spacep (p))
169 { /* invalid ID string */
170 parm->error = gpg_error (GPG_ERR_INV_RESPONSE);
174 *p = 0; /* ignore trailing stuff */
177 item->next = parm->info;
182 /* This callback is used by agent_card_learn and passed the content of
183 all CERTINFO lines. It merely stores this data away */
185 certinfo_cb (void *opaque, const char *line)
187 struct certinfo_cb_parm_s *parm = opaque;
193 return; /* no need to gather data after an error coccured */
195 if ((parm->error = agent_write_status (parm->ctrl, "PROGRESS",
196 "learncard", "c", "0", "0", NULL)))
199 type = strtol (line, &p, 10);
202 for (pend = p; *pend && !spacep (pend); pend++)
204 if (p == pend || !*p)
206 parm->error = gpg_error (GPG_ERR_INV_RESPONSE);
209 *pend = 0; /* ignore trailing stuff */
211 item = xtrycalloc (1, sizeof *item + strlen (p));
214 parm->error = out_of_core ();
218 strcpy (item->id, p);
220 item->next = parm->info;
225 /* This callback is used by agent_card_learn and passed the content of
226 all SINFO lines. It merely stores this data away */
228 sinfo_cb (void *opaque, const char *keyword, size_t keywordlen,
231 struct sinfo_cb_parm_s *sparm = opaque;
235 return; /* no need to gather data after an error coccured */
237 item = xtrycalloc (1, sizeof *item + keywordlen + 1 + strlen (data));
240 sparm->error = out_of_core ();
243 memcpy (item->keyword, keyword, keywordlen);
244 item->data = item->keyword + keywordlen;
247 strcpy (item->data, data);
249 item->next = sparm->info;
256 send_cert_back (ctrl_t ctrl, const char *id, void *assuan_context)
262 rc = agent_card_readcert (ctrl, id, &derbuf, &derbuflen);
267 switch (gpg_err_code (rc))
270 case GPG_ERR_NOT_FOUND:
271 action = " - ignored";
277 if (opt.verbose || !*action)
278 log_info ("error reading certificate '%s': %s%s\n",
279 id? id:"?", gpg_strerror (rc), action);
281 return *action? 0 : rc;
284 rc = assuan_send_data (assuan_context, derbuf, derbuflen);
287 rc = assuan_send_data (assuan_context, NULL, 0);
289 rc = assuan_write_line (assuan_context, "END");
292 log_error ("sending certificate failed: %s\n",
299 /* Perform the learn operation. If ASSUAN_CONTEXT is not NULL and
300 SEND is true all new certificates are send back via Assuan. */
302 agent_handle_learn (ctrl_t ctrl, int send, void *assuan_context, int force)
306 struct kpinfo_cb_parm_s parm;
307 struct certinfo_cb_parm_s cparm;
308 struct sinfo_cb_parm_s sparm;
309 char *serialno = NULL;
312 unsigned char grip[20];
315 static int certtype_list[] = {
320 /* We don't include 110 here because gpgsm can't handle that
321 special root CA format. */
326 memset (&parm, 0, sizeof parm);
327 memset (&cparm, 0, sizeof cparm);
328 memset (&sparm, 0, sizeof sparm);
332 /* Check whether a card is present and get the serial number */
333 rc = agent_card_serialno (ctrl, &serialno);
337 /* Now gather all the available info. */
338 rc = agent_card_learn (ctrl, kpinfo_cb, &parm, certinfo_cb, &cparm,
340 if (!rc && (parm.error || cparm.error || sparm.error))
341 rc = parm.error? parm.error : cparm.error? cparm.error : sparm.error;
344 log_debug ("agent_card_learn failed: %s\n", gpg_strerror (rc));
348 log_info ("card has S/N: %s\n", serialno);
350 /* Pass on all the collected status information. */
353 for (sitem = sparm.info; sitem; sitem = sitem->next)
355 assuan_write_status (assuan_context, sitem->keyword, sitem->data);
359 /* Write out the certificates in a standard order. */
360 for (i=0; certtype_list[i] != -1; i++)
363 for (citem = cparm.info; citem; citem = citem->next)
365 if (certtype_list[i] != citem->type)
369 log_info (" id: %s (type=%d)\n",
370 citem->id, citem->type);
372 if (assuan_context && send)
374 rc = send_cert_back (ctrl, citem->id, assuan_context);
382 for (item = parm.info; item; item = item->next)
384 unsigned char *pubkey, *shdkey;
388 log_info (" id: %s (grip=%s)\n", item->id, item->hexgrip);
391 continue; /* No public key yet available. */
395 agent_write_status (ctrl, "KEYPAIRINFO",
396 item->hexgrip, item->id, NULL);
399 for (p=item->hexgrip, i=0; i < 20; p += 2, i++)
400 grip[i] = xtoi_2 (p);
402 if (!force && !agent_key_available (grip))
403 continue; /* The key is already available. */
405 /* Unknown key - store it. */
406 rc = agent_card_readkey (ctrl, item->id, &pubkey);
409 log_debug ("agent_card_readkey failed: %s\n", gpg_strerror (rc));
414 unsigned char *shadow_info = make_shadow_info (serialno, item->id);
417 rc = gpg_error (GPG_ERR_ENOMEM);
421 rc = agent_shadow_key (pubkey, shadow_info, &shdkey);
427 log_error ("shadowing the key failed: %s\n", gpg_strerror (rc));
430 n = gcry_sexp_canon_len (shdkey, 0, NULL, NULL);
433 rc = agent_write_private_key (grip, shdkey, n, force);
437 log_error ("error writing key: %s\n", gpg_strerror (rc));
442 log_info (" id: %s - shadow key created\n", item->id);
444 if (assuan_context && send)
448 /* only send the certificate if we have not done so before */
449 for (citem = cparm.info; citem; citem = citem->next)
451 if (!strcmp (citem->id, item->id))
456 rc = send_cert_back (ctrl, item->id, assuan_context);
466 release_keypair_info (parm.info);
467 release_certinfo (cparm.info);
468 release_sinfo (sparm.info);