1 /* learncard.c - Handle the LEARN command
2 * Copyright (C) 2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
32 #include "../assuan/assuan.h"
34 struct keypair_info_s
{
35 struct keypair_info_s
*next
;
37 char *id
; /* points into grip */
40 typedef struct keypair_info_s
*KEYPAIR_INFO
;
42 struct kpinfo_cb_parm_s
{
49 release_keypair_info (KEYPAIR_INFO info
)
53 KEYPAIR_INFO tmp
= info
->next
;
61 /* This callback is used by agent_card_leanr and passed the content of
62 all KEYPAIRINFO lines. It merely store this data away */
64 kpinfo_cb (void *opaque
, const char *line
)
66 struct kpinfo_cb_parm_s
*parm
= opaque
;
71 return; /* no need to gather data after an error coccured */
72 item
= xtrycalloc (1, sizeof *item
+ strlen (line
));
75 parm
->error
= GNUPG_Out_Of_Core
;
78 strcpy (item
->hexgrip
, line
);
79 for (p
= item
->hexgrip
; hexdigitp (p
); p
++)
81 if (p
== item
->hexgrip
&& *p
== 'X' && spacep (p
+1))
86 else if ((p
- item
->hexgrip
) != 40 || !spacep (p
))
87 { /* not a 20 byte hex keygrip or not followed by a space */
88 parm
->error
= GNUPG_Invalid_Response
;
96 while (*p
&& !spacep (p
))
99 { /* invalid ID string */
100 parm
->error
= GNUPG_Invalid_Response
;
104 *p
= 0; /* ignore trailing stuff */
107 item
->next
= parm
->info
;
112 /* Create an S-expression with the shadow info. */
113 static unsigned char *
114 make_shadow_info (const char *serialno
, const char *idstring
)
117 unsigned char *info
, *p
;
121 for (s
=serialno
, n
=0; *s
&& s
[1]; s
+= 2)
124 info
= p
= xtrymalloc (1 + 21 + n
125 + 21 + strlen (idstring
) + 1 + 1);
127 sprintf (numbuf
, "%d:", n
);
128 p
= stpcpy (p
, numbuf
);
129 for (s
=serialno
; *s
&& s
[1]; s
+= 2)
131 sprintf (numbuf
, "%d:", strlen (idstring
));
132 p
= stpcpy (p
, numbuf
);
133 p
= stpcpy (p
, idstring
);
140 /* Perform the learn operation. If ASSUAN_CONTEXT is not NULL all new
141 certificates are send via Assuan */
143 agent_handle_learn (void *assuan_context
)
146 struct kpinfo_cb_parm_s parm
;
147 char *serialno
= NULL
;
149 unsigned char grip
[20];
153 memset (&parm
, 0, sizeof parm
);
155 /* Check whether a card is present and get the serial number */
156 rc
= agent_card_serialno (&serialno
);
160 /* now gather all the availabe info */
161 rc
= agent_card_learn (kpinfo_cb
, &parm
);
162 if (!rc
&& parm
.error
)
166 log_debug ("agent_card_learn failed: %s\n", gnupg_strerror (rc
));
170 log_info ("card has S/N: %s\n", serialno
);
171 for (item
= parm
.info
; item
; item
= item
->next
)
173 unsigned char *pubkey
, *shdkey
;
177 log_info (" id: %s (grip=%s)\n", item
->id
, item
->hexgrip
);
180 continue; /* no public key yet available */
182 for (p
=item
->hexgrip
, i
=0; i
< 20; p
+= 2, i
++)
183 grip
[i
] = xtoi_2 (p
);
185 if (!agent_key_available (grip
))
188 /* unknown - store it */
189 rc
= agent_card_readkey (item
->id
, &pubkey
);
192 log_debug ("agent_card_readkey failed: %s\n", gnupg_strerror (rc
));
197 unsigned char *shadow_info
= make_shadow_info (serialno
, item
->id
);
200 rc
= GNUPG_Out_Of_Core
;
204 rc
= agent_shadow_key (pubkey
, shadow_info
, &shdkey
);
210 log_error ("shadowing the key failed: %s\n", gnupg_strerror (rc
));
213 n
= gcry_sexp_canon_len (shdkey
, 0, NULL
, NULL
);
216 rc
= agent_write_private_key (grip
, shdkey
, n
, 0);
220 log_error ("error writing key: %s\n", gnupg_strerror (rc
));
225 log_info ("stored\n");
232 rc
= agent_card_readcert (item
->id
, &derbuf
, &derbuflen
);
235 log_error ("error reading certificate: %s\n",
236 gnupg_strerror (rc
));
240 rc
= assuan_send_data (assuan_context
, derbuf
, derbuflen
);
243 rc
= assuan_send_data (assuan_context
, NULL
, 0);
245 rc
= assuan_write_line (assuan_context
, "END");
248 log_error ("sending certificate failed: %s\n",
249 assuan_strerror (rc
));
250 rc
= map_assuan_err (rc
);
259 release_keypair_info (parm
.info
);