1 /* learncard.c - Handle the LEARN command
2 * Copyright (C) 2002, 2003, 2004 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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
35 /* Structures used by the callback mechanism to convey information
36 pertaining to key pairs. */
37 struct keypair_info_s
{
38 struct keypair_info_s
*next
;
40 char *id
; /* points into grip */
41 char hexgrip
[1]; /* The keygrip (i.e. a hash over the public key
42 parameters) formatted as a hex string.
43 Allocated somewhat large to also act as
44 memeory for the above ID field. */
46 typedef struct keypair_info_s
*KEYPAIR_INFO
;
48 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
{
71 /* Structures used by the callback mechanism to convey assuan status
75 char *data
; /* Points into keyword. */
78 typedef struct sinfo_s
*SINFO
;
80 struct sinfo_cb_parm_s
{
86 /* Destructor for key information objects. */
88 release_keypair_info (KEYPAIR_INFO info
)
92 KEYPAIR_INFO tmp
= info
->next
;
98 /* Destructor for certificate information objects. */
100 release_certinfo (CERTINFO info
)
104 CERTINFO tmp
= info
->next
;
110 /* Destructor for status information objects. */
112 release_sinfo (SINFO info
)
116 SINFO tmp
= info
->next
;
124 /* This callback is used by agent_card_learn and passed the content of
125 all KEYPAIRINFO lines. It merely stores this data away */
127 kpinfo_cb (void *opaque
, const char *line
)
129 struct kpinfo_cb_parm_s
*parm
= opaque
;
134 return; /* no need to gather data after an error coccured */
135 item
= xtrycalloc (1, sizeof *item
+ strlen (line
));
138 parm
->error
= out_of_core ();
141 strcpy (item
->hexgrip
, line
);
142 for (p
= item
->hexgrip
; hexdigitp (p
); p
++)
144 if (p
== item
->hexgrip
&& *p
== 'X' && spacep (p
+1))
149 else if ((p
- item
->hexgrip
) != 40 || !spacep (p
))
150 { /* not a 20 byte hex keygrip or not followed by a space */
151 parm
->error
= gpg_error (GPG_ERR_INV_RESPONSE
);
159 while (*p
&& !spacep (p
))
162 { /* invalid ID string */
163 parm
->error
= gpg_error (GPG_ERR_INV_RESPONSE
);
167 *p
= 0; /* ignore trailing stuff */
170 item
->next
= parm
->info
;
175 /* This callback is used by agent_card_learn and passed the content of
176 all CERTINFO lines. It merely stores this data away */
178 certinfo_cb (void *opaque
, const char *line
)
180 struct certinfo_cb_parm_s
*parm
= opaque
;
186 return; /* no need to gather data after an error coccured */
188 type
= strtol (line
, &p
, 10);
191 for (pend
= p
; *pend
&& !spacep (pend
); pend
++)
193 if (p
== pend
|| !*p
)
195 parm
->error
= gpg_error (GPG_ERR_INV_RESPONSE
);
198 *pend
= 0; /* ignore trailing stuff */
200 item
= xtrycalloc (1, sizeof *item
+ strlen (p
));
203 parm
->error
= out_of_core ();
207 strcpy (item
->id
, p
);
209 item
->next
= parm
->info
;
214 /* This callback is used by agent_card_learn and passed the content of
215 all SINFO lines. It merely stores this data away */
217 sinfo_cb (void *opaque
, const char *keyword
, size_t keywordlen
,
220 struct sinfo_cb_parm_s
*sparm
= opaque
;
224 return; /* no need to gather data after an error coccured */
226 item
= xtrycalloc (1, sizeof *item
+ keywordlen
+ 1 + strlen (data
));
229 sparm
->error
= out_of_core ();
232 memcpy (item
->keyword
, keyword
, keywordlen
);
233 item
->data
= item
->keyword
+ keywordlen
;
236 strcpy (item
->data
, data
);
238 item
->next
= sparm
->info
;
245 send_cert_back (ctrl_t ctrl
, const char *id
, void *assuan_context
)
251 rc
= agent_card_readcert (ctrl
, id
, &derbuf
, &derbuflen
);
254 log_error ("error reading certificate: %s\n",
259 rc
= assuan_send_data (assuan_context
, derbuf
, derbuflen
);
262 rc
= assuan_send_data (assuan_context
, NULL
, 0);
264 rc
= assuan_write_line (assuan_context
, "END");
267 log_error ("sending certificate failed: %s\n",
274 /* Perform the learn operation. If ASSUAN_CONTEXT is not NULL all new
275 certificates are send back via Assuan. */
277 agent_handle_learn (ctrl_t ctrl
, void *assuan_context
)
280 struct kpinfo_cb_parm_s parm
;
281 struct certinfo_cb_parm_s cparm
;
282 struct sinfo_cb_parm_s sparm
;
283 char *serialno
= NULL
;
286 unsigned char grip
[20];
289 static int certtype_list
[] = {
293 /* We don't include 110 here because gpgsm can't handle it. */
298 memset (&parm
, 0, sizeof parm
);
299 memset (&cparm
, 0, sizeof cparm
);
300 memset (&sparm
, 0, sizeof sparm
);
302 /* Check whether a card is present and get the serial number */
303 rc
= agent_card_serialno (ctrl
, &serialno
);
307 /* Now gather all the available info. */
308 rc
= agent_card_learn (ctrl
, kpinfo_cb
, &parm
, certinfo_cb
, &cparm
,
310 if (!rc
&& (parm
.error
|| cparm
.error
|| sparm
.error
))
311 rc
= parm
.error
? parm
.error
: cparm
.error
? cparm
.error
: sparm
.error
;
314 log_debug ("agent_card_learn failed: %s\n", gpg_strerror (rc
));
318 log_info ("card has S/N: %s\n", serialno
);
320 /* Pass on all the collected status information. */
323 for (sitem
= sparm
.info
; sitem
; sitem
= sitem
->next
)
325 assuan_write_status (assuan_context
, sitem
->keyword
, sitem
->data
);
329 /* Write out the certificates in a standard order. */
330 for (i
=0; certtype_list
[i
] != -1; i
++)
333 for (citem
= cparm
.info
; citem
; citem
= citem
->next
)
335 if (certtype_list
[i
] != citem
->type
)
339 log_info (" id: %s (type=%d)\n",
340 citem
->id
, citem
->type
);
344 rc
= send_cert_back (ctrl
, citem
->id
, assuan_context
);
352 for (item
= parm
.info
; item
; item
= item
->next
)
354 unsigned char *pubkey
, *shdkey
;
358 log_info (" id: %s (grip=%s)\n", item
->id
, item
->hexgrip
);
361 continue; /* No public key yet available. */
365 agent_write_status (ctrl
, "KEYPAIRINFO",
366 item
->hexgrip
, item
->id
, NULL
);
369 for (p
=item
->hexgrip
, i
=0; i
< 20; p
+= 2, i
++)
370 grip
[i
] = xtoi_2 (p
);
372 if (!agent_key_available (grip
))
373 continue; /* The key is already available. */
375 /* Unknown key - store it. */
376 rc
= agent_card_readkey (ctrl
, item
->id
, &pubkey
);
379 log_debug ("agent_card_readkey failed: %s\n", gpg_strerror (rc
));
384 unsigned char *shadow_info
= make_shadow_info (serialno
, item
->id
);
387 rc
= gpg_error (GPG_ERR_ENOMEM
);
391 rc
= agent_shadow_key (pubkey
, shadow_info
, &shdkey
);
397 log_error ("shadowing the key failed: %s\n", gpg_strerror (rc
));
400 n
= gcry_sexp_canon_len (shdkey
, 0, NULL
, NULL
);
403 rc
= agent_write_private_key (grip
, shdkey
, n
, 0);
407 log_error ("error writing key: %s\n", gpg_strerror (rc
));
412 log_info ("stored\n");
418 /* only send the certificate if we have not done so before */
419 for (citem
= cparm
.info
; citem
; citem
= citem
->next
)
421 if (!strcmp (citem
->id
, item
->id
))
426 rc
= send_cert_back (ctrl
, item
->id
, assuan_context
);
436 release_keypair_info (parm
.info
);
437 release_certinfo (cparm
.info
);
438 release_sinfo (sparm
.info
);