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 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. */
35 struct keypair_info_s
{
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 memeory for the above ID field. */
44 typedef struct keypair_info_s
*KEYPAIR_INFO
;
46 struct kpinfo_cb_parm_s
{
53 /* Structures used by the callback mechanism to convey information
54 pertaining to certificates. */
56 struct certinfo_s
*next
;
61 typedef struct certinfo_s
*CERTINFO
;
63 struct certinfo_cb_parm_s
{
69 /* Structures used by the callback mechanism to convey assuan status
73 char *data
; /* Points into keyword. */
76 typedef struct sinfo_s
*SINFO
;
78 struct sinfo_cb_parm_s
{
84 /* Destructor for key information objects. */
86 release_keypair_info (KEYPAIR_INFO info
)
90 KEYPAIR_INFO tmp
= info
->next
;
96 /* Destructor for certificate information objects. */
98 release_certinfo (CERTINFO info
)
102 CERTINFO tmp
= info
->next
;
108 /* Destructor for status information objects. */
110 release_sinfo (SINFO info
)
114 SINFO tmp
= info
->next
;
122 /* This callback is used by agent_card_learn and passed the content of
123 all KEYPAIRINFO lines. It merely stores this data away */
125 kpinfo_cb (void *opaque
, const char *line
)
127 struct kpinfo_cb_parm_s
*parm
= opaque
;
132 return; /* no need to gather data after an error coccured */
133 item
= xtrycalloc (1, sizeof *item
+ strlen (line
));
136 parm
->error
= out_of_core ();
139 strcpy (item
->hexgrip
, line
);
140 for (p
= item
->hexgrip
; hexdigitp (p
); p
++)
142 if (p
== item
->hexgrip
&& *p
== 'X' && spacep (p
+1))
147 else if ((p
- item
->hexgrip
) != 40 || !spacep (p
))
148 { /* not a 20 byte hex keygrip or not followed by a space */
149 parm
->error
= gpg_error (GPG_ERR_INV_RESPONSE
);
157 while (*p
&& !spacep (p
))
160 { /* invalid ID string */
161 parm
->error
= gpg_error (GPG_ERR_INV_RESPONSE
);
165 *p
= 0; /* ignore trailing stuff */
168 item
->next
= parm
->info
;
173 /* This callback is used by agent_card_learn and passed the content of
174 all CERTINFO lines. It merely stores this data away */
176 certinfo_cb (void *opaque
, const char *line
)
178 struct certinfo_cb_parm_s
*parm
= opaque
;
184 return; /* no need to gather data after an error coccured */
186 type
= strtol (line
, &p
, 10);
189 for (pend
= p
; *pend
&& !spacep (pend
); pend
++)
191 if (p
== pend
|| !*p
)
193 parm
->error
= gpg_error (GPG_ERR_INV_RESPONSE
);
196 *pend
= 0; /* ignore trailing stuff */
198 item
= xtrycalloc (1, sizeof *item
+ strlen (p
));
201 parm
->error
= out_of_core ();
205 strcpy (item
->id
, p
);
207 item
->next
= parm
->info
;
212 /* This callback is used by agent_card_learn and passed the content of
213 all SINFO lines. It merely stores this data away */
215 sinfo_cb (void *opaque
, const char *keyword
, size_t keywordlen
,
218 struct sinfo_cb_parm_s
*sparm
= opaque
;
222 return; /* no need to gather data after an error coccured */
224 item
= xtrycalloc (1, sizeof *item
+ keywordlen
+ 1 + strlen (data
));
227 sparm
->error
= out_of_core ();
230 memcpy (item
->keyword
, keyword
, keywordlen
);
231 item
->data
= item
->keyword
+ keywordlen
;
234 strcpy (item
->data
, data
);
236 item
->next
= sparm
->info
;
243 send_cert_back (ctrl_t ctrl
, const char *id
, void *assuan_context
)
249 rc
= agent_card_readcert (ctrl
, id
, &derbuf
, &derbuflen
);
252 log_error ("error reading certificate: %s\n",
257 rc
= assuan_send_data (assuan_context
, derbuf
, derbuflen
);
260 rc
= assuan_send_data (assuan_context
, NULL
, 0);
262 rc
= assuan_write_line (assuan_context
, "END");
265 log_error ("sending certificate failed: %s\n",
272 /* Perform the learn operation. If ASSUAN_CONTEXT is not NULL all new
273 certificates are send back via Assuan. */
275 agent_handle_learn (ctrl_t ctrl
, void *assuan_context
)
278 struct kpinfo_cb_parm_s parm
;
279 struct certinfo_cb_parm_s cparm
;
280 struct sinfo_cb_parm_s sparm
;
281 char *serialno
= NULL
;
284 unsigned char grip
[20];
287 static int certtype_list
[] = {
291 /* We don't include 110 here because gpgsm can't handle it. */
296 memset (&parm
, 0, sizeof parm
);
297 memset (&cparm
, 0, sizeof cparm
);
298 memset (&sparm
, 0, sizeof sparm
);
300 /* Check whether a card is present and get the serial number */
301 rc
= agent_card_serialno (ctrl
, &serialno
);
305 /* Now gather all the available info. */
306 rc
= agent_card_learn (ctrl
, kpinfo_cb
, &parm
, certinfo_cb
, &cparm
,
308 if (!rc
&& (parm
.error
|| cparm
.error
|| sparm
.error
))
309 rc
= parm
.error
? parm
.error
: cparm
.error
? cparm
.error
: sparm
.error
;
312 log_debug ("agent_card_learn failed: %s\n", gpg_strerror (rc
));
316 log_info ("card has S/N: %s\n", serialno
);
318 /* Pass on all the collected status information. */
321 for (sitem
= sparm
.info
; sitem
; sitem
= sitem
->next
)
323 assuan_write_status (assuan_context
, sitem
->keyword
, sitem
->data
);
327 /* Write out the certificates in a standard order. */
328 for (i
=0; certtype_list
[i
] != -1; i
++)
331 for (citem
= cparm
.info
; citem
; citem
= citem
->next
)
333 if (certtype_list
[i
] != citem
->type
)
337 log_info (" id: %s (type=%d)\n",
338 citem
->id
, citem
->type
);
342 rc
= send_cert_back (ctrl
, citem
->id
, assuan_context
);
350 for (item
= parm
.info
; item
; item
= item
->next
)
352 unsigned char *pubkey
, *shdkey
;
356 log_info (" id: %s (grip=%s)\n", item
->id
, item
->hexgrip
);
359 continue; /* No public key yet available. */
363 agent_write_status (ctrl
, "KEYPAIRINFO",
364 item
->hexgrip
, item
->id
, NULL
);
367 for (p
=item
->hexgrip
, i
=0; i
< 20; p
+= 2, i
++)
368 grip
[i
] = xtoi_2 (p
);
370 if (!agent_key_available (grip
))
371 continue; /* The key is already available. */
373 /* Unknown key - store it. */
374 rc
= agent_card_readkey (ctrl
, item
->id
, &pubkey
);
377 log_debug ("agent_card_readkey failed: %s\n", gpg_strerror (rc
));
382 unsigned char *shadow_info
= make_shadow_info (serialno
, item
->id
);
385 rc
= gpg_error (GPG_ERR_ENOMEM
);
389 rc
= agent_shadow_key (pubkey
, shadow_info
, &shdkey
);
395 log_error ("shadowing the key failed: %s\n", gpg_strerror (rc
));
398 n
= gcry_sexp_canon_len (shdkey
, 0, NULL
, NULL
);
401 rc
= agent_write_private_key (grip
, shdkey
, n
, 0);
405 log_error ("error writing key: %s\n", gpg_strerror (rc
));
410 log_info ("stored\n");
416 /* only send the certificate if we have not done so before */
417 for (citem
= cparm
.info
; citem
; citem
= citem
->next
)
419 if (!strcmp (citem
->id
, item
->id
))
424 rc
= send_cert_back (ctrl
, item
->id
, assuan_context
);
434 release_keypair_info (parm
.info
);
435 release_certinfo (cparm
.info
);
436 release_sinfo (sparm
.info
);