Debian Codegraph @

src/scd/scd.c

raw symbols SCIP
1 /* scd.c - Interface to Scdaemon
2 Copyright (C) 2001, 2002, 2005 Free Software Foundation, Inc.
3 Copyright (C) 2007, 2008, 2009 g10code GmbH.
4
5 This file is part of Poldi.
6
7 Poldi is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 Poldi is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see
19 <http://www.gnu.org/licenses/>. */
20
21 #include <poldi.h>cxx . . $ `<file>/src/poldi.h`/
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <assert.h>
29 #include <unistd.h>
30 #include <signal.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34
35 #include <gpg-error.h>
36 #include <gcrypt.h>
37
38 #include "scd.h"cxx . . $ `<file>/src/scd/scd.h`/
39 #include "assuan.h"cxx . . $ `<file>/src/assuan/assuan.h`/
40 #include "util/util.h"cxx . . $ `<file>/src/util/util.h`/
41 #include "util/membuf.h"cxx . . $ `<file>/src/util/membuf.h`/
42 #include "util/support.h"cxx . . $ `<file>/src/util/support.h`/
43 #include "util/simplelog.h"cxx . . $ `<file>/src/util/simplelog.h`/
44
45 #ifdef _POSIX_OPEN_MAXcxx . . $ `<external>/2534844627595019613/posix1_lim.h:85:10`!
46 #define MAX_OPEN_FDScxx . . $ `src/scd/scd.c:46:9`! _POSIX_OPEN_MAX
47 #else
48 #define MAX_OPEN_FDS 20
49 #endif
50
51
52
53 /* Initializer objet for struct scd_cardinfo instances. */
54 struct scd_cardinfocxx . . $ scd_cardinfo# scd_cardinfo_nullcxx . . $ scd_cardinfo_null.;
55
56
57
58 struct scd_contextcxx . . $ scd_context#
59 {
60 assuan_context_tcxx . . $ assuan_context_t# assuan_ctxcxx . . $ scd_context#assuan_ctx.;
61 unsigned int flagscxx . . $ scd_context#flags.;
62 log_handle_tcxx . . $ log_handle_t# loghandlecxx . . $ scd_context#loghandle.;
63 scd_pincb_tcxx . . $ scd_pincb_t# pincbcxx . . $ scd_context#pincb.;
64 void *pincb_cookiecxx . . $ scd_context#pincb_cookie.;
65 };
66
67 /* Callback parameter for learn card */
68 struct learn_parm_scxx . . $ learn_parm_s#
69 {
70 void (*kpinfo_cbcxx . . $ learn_parm_s#kpinfo_cb.)(void*, const char *);
71 void *kpinfo_cb_argcxx . . $ learn_parm_s#kpinfo_cb_arg.;
72 void (*certinfo_cbcxx . . $ learn_parm_s#certinfo_cb.)(void*, const char *);
73 void *certinfo_cb_argcxx . . $ learn_parm_s#certinfo_cb_arg.;
74 void (*sinfo_cbcxx . . $ learn_parm_s#sinfo_cb.)(void*, const char *, size_t, const char *);
75 void *sinfo_cb_argcxx . . $ learn_parm_s#sinfo_cb_arg.;
76 };
77
78 struct inq_needpin_scxx . . $ inq_needpin_s#
79 {
80 scd_context_tcxx . . $ scd_context_t# ctxcxx . . $ inq_needpin_s#ctx.;
81 int (*getpin_cbcxx . . $ inq_needpin_s#getpin_cb.)(void *, const char *, char*, size_t);
82 void *getpin_cb_argcxx . . $ inq_needpin_s#getpin_cb_arg.;
83 };
84
85
86 /* Local prototypes. */
87 static assuan_error_tcxx . . $ assuan_error_t# membuf_data_cbcxx . . $ membuf_data_cb(72e086fd9bc8337a). (void *opaque,
88 const void *buffer, size_t length);
89
90
91
92
93 static gpg_error_tcxx . . $ gpg_error_t# scd_serialno_internalcxx . . $ scd_serialno_internal(5571a7ef705c8c30). (assuan_context_tcxx . . $ assuan_context_t# ctx,
94 char **r_serialno);
95
96
97
98 /* Get the socket of GPG-AGENT by gpgconf. */
99 static gpg_error_tcxx . . $ gpg_error_t#
100 get_agent_socket_namecxx . . $ get_agent_socket_name(658e6f1ffe3b2f0c). (char **gpg_agent_sockname)
101 {
102 gpg_error_tcxx . . $ gpg_error_t# err = 0;
103 FILEcxx . . $ FILE# *input;
104 char *result;
105 size_t len;
106
107 *gpg_agent_sockname = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
108
109 result = xtrymalloccxx . . $ `src/poldi.h:35:9`! (256);
110 if (!result)
111 return gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). ();
112
113 /* It is good if we have popen with execv (no SHELL) */
114 input = popencxx . . $ popen(82ec0787cd37e0a3). (GNUPG_DEFAULT_GPGCONFcxx . . $ `config.h:248:9`! " --list-dirs agent-socket", "r");
115 if (input == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
116 {
117 xfreecxx . . $ `src/poldi.h:39:9`! (result);
118 return gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_NOT_FOUNDcxx . . $ GPG_ERR_NOT_FOUND.);
119 }
120
121 len = freadcxx . . $ fread(738fc8ac1d711e58). (result, 1, 256, input);
122 pclosecxx . . $ pclose(89bc1efcdac0ca22). (input);
123
124 if (len)
125 {
126 *gpg_agent_sockname = result;
127 result[len-1] = 0; /* Chop off the newline. */
128 }
129 else
130 {
131 xfreecxx . . $ `src/poldi.h:39:9`! (result);
132 err = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_NOT_FOUNDcxx . . $ GPG_ERR_NOT_FOUND.);
133 }
134
135 return err;
136 }
137
138 /* Helper function for get_scd_socket_from_agent(), which is used by
139 scd_connect().
140
141 Try to retrieve the SCDaemons socket name from the gpg-agent
142 context CTX. On success, *SOCKET_NAME is filled with a copy ot the
143 socket name. Return proper error code or zero on success. */
144 static gpg_error_tcxx . . $ gpg_error_t#
145 agent_scd_getinfo_socket_namecxx . . $ agent_scd_getinfo_socket_name(5571a7ef705c8c30). (assuan_context_tcxx . . $ assuan_context_t# ctx, char **socket_name)
146 {
147 membuf_tcxx . . $ membuf_t# data;
148 gpg_error_tcxx . . $ gpg_error_t# err = 0;
149 unsigned char *databuf;
150 size_t datalen;
151
152 init_membufcxx . . $ init_membuf(917e2e0aeaea34de). (&data, 256);
153 *socket_name = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
154
155 err = assuan_transactcxx . . $ `src/assuan/assuan.h:120:9`! (ctx, "SCD GETINFO socket_name", membuf_data_cbcxx . . $ membuf_data_cb(72e086fd9bc8337a)., &data,
156 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
157 databuf = get_membufcxx . . $ get_membuf(5dfc76f6663417c1). (&data, &datalen);
158 if (!err)
159 {
160 if (databuf && datalen)
161 {
162 char *res = xtrymalloccxx . . $ `src/poldi.h:35:9`! (datalen + 1);
163 if (!res)
164 err = gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). ();
165 else
166 {
167 memcpycxx . . $ memcpy(cc817cb175f282d). (res, databuf, datalen);
168 res[datalen] = 0;
169 *socket_name = res;
170 }
171 }
172 }
173
174 xfreecxx . . $ `src/poldi.h:39:9`! (databuf);
175
176 return err;
177 }
178
179 /* Retrieve SCDaemons socket name through a running gpg-agent. On
180 success, *SOCKET_NAME contains a copy of the socket name. Returns
181 proper error code or zero on success. */
182 static gpg_error_tcxx . . $ gpg_error_t#
183 get_scd_socket_from_agentcxx . . $ get_scd_socket_from_agent(658e6f1ffe3b2f0c). (char **socket_name)
184 {
185 assuan_context_tcxx . . $ assuan_context_t# ctx = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
186 gpg_error_tcxx . . $ gpg_error_t# err;
187 char *gpg_agent_sockname;
188
189 err = get_agent_socket_namecxx . . $ get_agent_socket_name(658e6f1ffe3b2f0c). (&gpg_agent_sockname);
190 if (err)
191 return err;
192
193 err = assuan_socket_connectcxx . . $ `src/assuan/assuan.h:115:9`! (&ctx, gpg_agent_sockname, 0);
194 xfreecxx . . $ `src/poldi.h:39:9`! (gpg_agent_sockname);
195 if (!err)
196 err = agent_scd_getinfo_socket_namecxx . . $ agent_scd_getinfo_socket_name(5571a7ef705c8c30). (ctx, socket_name);
197
198 assuan_disconnectcxx . . $ `src/assuan/assuan.h:117:9`! (ctx);
199
200 return err;
201 }
202
203 /* Send a RESTART to SCDaemon. */
204 static void
205 restart_scdcxx . . $ restart_scd(96f585546e882556). (scd_context_tcxx . . $ scd_context_t# ctx)
206 {
207 assuan_transactcxx . . $ `src/assuan/assuan.h:120:9`! (ctx->assuan_ctxcxx . . $ scd_context#assuan_ctx., "RESTART",
208 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
209 }
210
211
212
213 /* Fork off scdaemon and work by pipes. Returns proper error code or
214 zero on success. */
215 gpg_error_tcxx . . $ gpg_error_t#
216 scd_connectcxx . . $ scd_connect(b5c77484bce79416). (scd_context_tcxx . . $ scd_context_t# *scd_ctx, int use_agent, const char *scd_path,
217 const char *scd_options, log_handle_tcxx . . $ log_handle_t# loghandle)
218 {
219 assuan_context_tcxx . . $ assuan_context_t# assuan_ctx;
220 scd_context_tcxx . . $ scd_context_t# ctx;
221 gpg_error_tcxx . . $ gpg_error_t# err;
222
223 assuan_ctx = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
224
225 if (fflushcxx . . $ fflush(89bc1efcdac0ca22). (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!))
226 {
227 err = gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). ();
228 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (loghandle, "error flushing pending output: %s",
229 strerrorcxx . . $ strerror(ff224f080444ee45). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
230 return err;
231 }
232
233 ctx = xtrymalloccxx . . $ `src/poldi.h:35:9`! (sizeof (*ctx));
234 if (!ctx)
235 return gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). ();
236
237 ctx->assuan_ctxcxx . . $ scd_context#assuan_ctx. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
238 ctx->flagscxx . . $ scd_context#flags. = 0;
239
240 /* Try using scdaemon under gpg-agent. */
241 if (use_agent)
242 {
243 char *scd_socket_name = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
244
245 /* Note that if gpg-agent is there but no scdaemon yet,
246 * gpg-agent automatically invokes scdaemon by this query
247 * itself.
248 */
249 err = get_scd_socket_from_agentcxx . . $ get_scd_socket_from_agent(658e6f1ffe3b2f0c). (&scd_socket_name);
250 if (!err)
251 err = assuan_socket_connectcxx . . $ `src/assuan/assuan.h:115:9`! (&assuan_ctx, scd_socket_name, 0);
252
253 if (!err)
254 log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (loghandle,
255 "got scdaemon socket name from gpg-agent, "
256 "connected to socket '%s'", scd_socket_name);
257
258 xfreecxx . . $ `src/poldi.h:39:9`! (scd_socket_name);
259 }
260
261 /* If scdaemon under gpg-agent is irrelevant or not available,
262 * let Poldi invoke scdaemon.
263 */
264 if (!use_agent || err)
265 {
266 const char *pgmname;
267 const char *argv[5];
268 int no_close_list[3];
269 int i;
270
271 if (!scd_path || !*scd_path)
272 scd_path = GNUPG_DEFAULT_SCDcxx . . $ `config.h:251:9`!;
273 if (!(pgmname = strrchrcxx . . $ strrchr(2050848944e00d54). (scd_path, '/')))
274 pgmname = scd_path;
275 else
276 pgmname++;
277
278 /* Fill argument vector for scdaemon. */
279
280 i = 0;
281 argv[i++] = pgmname;
282 argv[i++] = "--server";
283 if (scd_options)
284 {
285 argv[i++] = "--options";
286 argv[i++] = scd_options;
287 }
288 argv[i++] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
289
290 i=0;
291
292 #if 0
293 /* FIXME! Am I right in assumung that we do not need this?
294 -mo */
295 if (log_get_fd () != -1)
296 no_close_list[i++] = log_get_fd ();
297 #endif
298
299 /* FIXME: What about stderr? */
300 no_close_list[i++] = filenocxx . . $ fileno(89bc1efcdac0ca22). (stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
301 no_close_list[i] = -1;
302
303 /* connect to the scdaemon and perform initial handshaking */
304 err = assuan_pipe_connectcxx . . $ `src/assuan/assuan.h:113:9`! (&assuan_ctx, scd_path, argv, no_close_list);
305 if (err)
306 {
307 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (loghandle, "could not spawn scdaemon: %s",
308 gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err));
309 }
310 else
311 {
312 log_msg_debugcxx . . $ log_msg_debug(628bea2675049132). (loghandle, "spawned a new scdaemon (path: '%s')",
313 scd_path);
314 }
315 }
316
317 if (err)
318 {
319 assuan_disconnectcxx . . $ `src/assuan/assuan.h:117:9`! (assuan_ctx);
320 xfreecxx . . $ `src/poldi.h:39:9`! (ctx);
321 }
322 else
323 {
324 /* FIXME: is this the best way? -mo */
325 //reset_scd (assuan_ctx);
326 scd_serialno_internalcxx . . $ scd_serialno_internal(5571a7ef705c8c30). (assuan_ctx, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
327
328 ctx->assuan_ctxcxx . . $ scd_context#assuan_ctx. = assuan_ctx;
329 ctx->flagscxx . . $ scd_context#flags. = 0;
330 ctx->loghandlecxx . . $ scd_context#loghandle. = loghandle;
331 *scd_ctx = ctx;
332 }
333
334 return err;
335 }
336
337 /* Disconnect from SCDaemon; destroy the context SCD_CTX. */
338 void
339 scd_disconnectcxx . . $ scd_disconnect(96f585546e882556). (scd_context_tcxx . . $ scd_context_t# scd_ctx)
340 {
341 if (scd_ctx)
342 {
343 restart_scdcxx . . $ restart_scd(96f585546e882556). (scd_ctx);
344 assuan_disconnectcxx . . $ `src/assuan/assuan.h:117:9`! (scd_ctx->assuan_ctxcxx . . $ scd_context#assuan_ctx.);
345 xfreecxx . . $ `src/poldi.h:39:9`! (scd_ctx);
346 }
347 }
348
349
350 void
351 scd_set_pincbcxx . . $ scd_set_pincb(bb86724bcfc9ee60). (scd_context_tcxx . . $ scd_context_t# scd_ctx,
352 scd_pincb_tcxx . . $ scd_pincb_t# pincb, void *cookie)
353 {
354 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (scd_ctx);
355
356 scd_ctx->pincbcxx . . $ scd_context#pincb. = pincb;
357 scd_ctx->pincb_cookiecxx . . $ scd_context#pincb_cookie. = cookie;
358 }
359
360
361
362
363 /* Return a new malloced string by unescaping the string S. Escaping
364 is percent escaping and '+'/space mapping. A binary Nul will
365 silently be replaced by a 0xFF. Function returns NULL to indicate
366 an out of memory status. */
367 static char *
368 unescape_status_stringcxx . . $ unescape_status_string(a8b8e32347182212). (const char *s)
369 {
370 char *buffer, *d;
371
372 buffer = d = xtrymalloccxx . . $ `src/poldi.h:35:9`! (strlen ((const char*)s)+1);
373 if (!buffer)
374 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
375 while (*s)
376 {
377 if (*s == '%' && s[1] && s[2])
378 {
379 s++;
380 *d = xtoi_2cxx . . $ `src/util/util.h:61:9`! (s);
381 if (!*d)
382 *d = '\xff';
383 d++;
384 s += 2;
385 }
386 else if (*s == '+')
387 {
388 *d++ = ' ';
389 s++;
390 }
391 else
392 *d++ = *s++;
393 }
394 *d = 0;
395 return buffer;
396 }
397
398
399
400
401 /* CARD LEARNING. */
402
403 /* Take a 20 byte hexencoded string and put it into the the provided
404 20 byte buffer FPR in binary format. */
405 static int
406 unhexify_fprcxx . . $ unhexify_fpr(2f051e3560ff300f). (const char *hexstr, char *fpr)
407 {
408 const char *s;
409 int n;
410
411 for (s=hexstr, n=0; hexdigitpcxx . . $ `src/util/util.h:48:9`! (s); s++, n++)
412 ;
413 if (*s || (n != 40))
414 return 0; /* no fingerprint (invalid or wrong length). */
415 n /= 2;
416 for (s=hexstr, n=0; *s; s += 2, n++)
417 fpr[n] = xtoi_2cxx . . $ `src/util/util.h:61:9`! (s);
418 return 1; /* okay */
419 }
420
421 /* Take the serial number from LINE and return it verbatim in a newly
422 allocated string. We make sure that only hex characters are
423 returned. */
424 static char *
425 store_serialnocxx . . $ store_serialno(a8b8e32347182212). (const char *line)
426 {
427 const char *s;
428 char *p;
429
430 for (s=line; hexdigitpcxx . . $ `src/util/util.h:48:9`! (s); s++)
431 ;
432 p = xtrymalloccxx . . $ `src/poldi.h:35:9`! (s + 1 - line);
433 if (p)
434 {
435 memcpycxx . . $ memcpy(cc817cb175f282d). (p, line, s-line);
436 p[s-line] = 0;
437 }
438 return p;
439 }
440
441 static int
442 learn_status_cbcxx . . $ learn_status_cb(f53b6be6549e6d95). (void *opaque, const char *line)
443 {
444 struct scd_cardinfocxx . . $ scd_cardinfo# *parm = opaque;
445 const char *keyword = line;
446 int keywordlen;
447 //int i;
448
449 for (keywordlen=0; *line && !spacepcxx . . $ `src/util/util.h:46:9`! (line); line++, keywordlen++)
450 ;
451 while (spacepcxx . . $ `src/util/util.h:46:9`! (line))
452 line++;
453
454 if (keywordlen == 8 && !memcmpcxx . . $ memcmp(36f949851efa3055). (keyword, "SERIALNO", keywordlen))
455 {
456 xfreecxx . . $ `src/poldi.h:39:9`! (parm->serialno);
457 parm->serialnocxx . . $ scd_cardinfo#serialno. = store_serialnocxx . . $ store_serialno(a8b8e32347182212). (line);
458 }
459 else if (keywordlen == 9 && !memcmpcxx . . $ memcmp(36f949851efa3055). (keyword, "DISP-NAME", keywordlen))
460 {
461 xfreecxx . . $ `src/poldi.h:39:9`! (parm->disp_name);
462 parm->disp_namecxx . . $ scd_cardinfo#disp_name. = unescape_status_stringcxx . . $ unescape_status_string(a8b8e32347182212). (line);
463 }
464 else if (keywordlen == 9 && !memcmpcxx . . $ memcmp(36f949851efa3055). (keyword, "DISP-LANG", keywordlen))
465 {
466 xfreecxx . . $ `src/poldi.h:39:9`! (parm->disp_lang);
467 parm->disp_langcxx . . $ scd_cardinfo#disp_lang. = unescape_status_stringcxx . . $ unescape_status_string(a8b8e32347182212). (line);
468 }
469 else if (keywordlen == 10 && !memcmpcxx . . $ memcmp(36f949851efa3055). (keyword, "PUBKEY-URL", keywordlen))
470 {
471 xfreecxx . . $ `src/poldi.h:39:9`! (parm->pubkey_url);
472 parm->pubkey_urlcxx . . $ scd_cardinfo#pubkey_url. = unescape_status_stringcxx . . $ unescape_status_string(a8b8e32347182212). (line);
473 }
474 else if (keywordlen == 10 && !memcmpcxx . . $ memcmp(36f949851efa3055). (keyword, "LOGIN-DATA", keywordlen))
475 {
476 xfreecxx . . $ `src/poldi.h:39:9`! (parm->login_data);
477 parm->login_datacxx . . $ scd_cardinfo#login_data. = unescape_status_stringcxx . . $ unescape_status_string(a8b8e32347182212). (line);
478 }
479 else if (keywordlen == 7 && !memcmpcxx . . $ memcmp(36f949851efa3055). (keyword, "KEY-FPR", keywordlen))
480 {
481 int no = atoicxx . . $ atoi(5c4306d8553b3696). (line);
482 while (*line && !spacepcxx . . $ `src/util/util.h:46:9`! (line))
483 line++;
484 while (spacepcxx . . $ `src/util/util.h:46:9`! (line))
485 line++;
486 if (no == 1)
487 parm->fpr1validcxx . . $ scd_cardinfo#fpr1valid. = unhexify_fprcxx . . $ unhexify_fpr(2f051e3560ff300f). (line, parm->fpr1cxx . . $ scd_cardinfo#fpr1.);
488 else if (no == 2)
489 parm->fpr2validcxx . . $ scd_cardinfo#fpr2valid. = unhexify_fprcxx . . $ unhexify_fpr(2f051e3560ff300f). (line, parm->fpr2cxx . . $ scd_cardinfo#fpr2.);
490 else if (no == 3)
491 parm->fpr3validcxx . . $ scd_cardinfo#fpr3valid. = unhexify_fprcxx . . $ unhexify_fpr(2f051e3560ff300f). (line, parm->fpr3cxx . . $ scd_cardinfo#fpr3.);
492 }
493
494 return 0;
495 }
496
497 /* Read information from card and fill the cardinfo structure
498 CARDINFO. Returns proper error code, zero on success. */
499 int
500 scd_learncxx . . $ scd_learn(f694227e099c0ef7). (scd_context_tcxx . . $ scd_context_t# ctx,
501 struct scd_cardinfocxx . . $ scd_cardinfo# *cardinfo)
502 {
503 int rc;
504
505 *cardinfo = scd_cardinfo_nullcxx . . $ scd_cardinfo_null.;
506 rc = assuan_transactcxx . . $ `src/assuan/assuan.h:120:9`! (ctx->assuan_ctxcxx . . $ scd_context#assuan_ctx., "LEARN --force",
507 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,
508 learn_status_cbcxx . . $ learn_status_cb(f53b6be6549e6d95)., cardinfo);
509
510 return rc;
511 }
512
513 /* Simply release the cardinfo structure INFO. INFO being NULL is
514 okay. */
515 void
516 scd_release_cardinfocxx . . $ scd_release_cardinfo(4bb01e237e9372f6). (struct scd_cardinfocxx . . $ scd_cardinfo# info)
517 {
518 xfreecxx . . $ `src/poldi.h:39:9`! (info.serialno);
519 xfreecxx . . $ `src/poldi.h:39:9`! (info.disp_name);
520 xfreecxx . . $ `src/poldi.h:39:9`! (info.login_data);
521 xfreecxx . . $ `src/poldi.h:39:9`! (info.pubkey_url);
522 }
523
524
525
526
527 /* CMD: SERIALNO. */
528
529 static int
530 get_serialno_cbcxx . . $ get_serialno_cb(f53b6be6549e6d95). (void *opaque, const char *line)
531 {
532 char **serialno = opaque;
533 const char *keyword = line;
534 const char *s;
535 int keywordlen, n;
536
537 for (keywordlen=0; *line && !spacepcxx . . $ `src/util/util.h:46:9`! (line); line++, keywordlen++)
538 ;
539 while (spacepcxx . . $ `src/util/util.h:46:9`! (line))
540 line++;
541
542 if (keywordlen == 8 && !memcmpcxx . . $ memcmp(36f949851efa3055). (keyword, "SERIALNO", keywordlen))
543 {
544 if (*serialno)
545 return gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_CONFLICTcxx . . $ GPG_ERR_CONFLICT.); /* Unexpected status line. */
546 for (n=0,s=line; hexdigitpcxx . . $ `src/util/util.h:48:9`! (s); s++, n++)
547 ;
548 if (!n || (n&1)|| !(spacepcxx . . $ `src/util/util.h:46:9`! (s) || !*s) )
549 return gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_ASS_PARAMETERcxx . . $ GPG_ERR_ASS_PARAMETER.);
550 *serialno = xtrymalloccxx . . $ `src/poldi.h:35:9`! (n+1);
551 if (!*serialno)
552 return gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
553 memcpycxx . . $ memcpy(cc817cb175f282d). (*serialno, line, n);
554 (*serialno)[n] = 0;
555 }
556
557 return 0;
558 }
559
560 static gpg_error_tcxx . . $ gpg_error_t#
561 scd_serialno_internalcxx . . $ scd_serialno_internal(5571a7ef705c8c30). (assuan_context_tcxx . . $ assuan_context_t# ctx, char **r_serialno)
562 {
563 char *serialno;
564 int rc;
565
566 serialno = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
567
568 rc = assuan_transactcxx . . $ `src/assuan/assuan.h:120:9`! (ctx, "SERIALNO", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,
569 get_serialno_cbcxx . . $ get_serialno_cb(f53b6be6549e6d95)., &serialno);
570 if (rc)
571 goto out;
572
573 if (r_serialno)
574 *r_serialno = serialno;
575 else
576 xfreecxx . . $ `src/poldi.h:39:9`! (serialno);
577
578 out:
579
580 return rc;
581 }
582
583 /* Return the serial number of the card or an appropriate error. The
584 serial number is returned as a hexstring. */
585 gpg_error_tcxx . . $ gpg_error_t#
586 scd_serialnocxx . . $ scd_serialno(8fbe1a4b844610be). (scd_context_tcxx . . $ scd_context_t# ctx, char **r_serialno)
587 {
588 gpg_error_tcxx . . $ gpg_error_t# err;
589
590 err = scd_serialno_internalcxx . . $ scd_serialno_internal(5571a7ef705c8c30). (ctx->assuan_ctxcxx . . $ scd_context#assuan_ctx., r_serialno);
591
592 return err;
593 }
594
595 /* CMD: PKSIGN. */
596
597
598
599 static int
600 membuf_data_cbcxx . . $ membuf_data_cb(72e086fd9bc8337a). (void *opaque, const void *buffer, size_t length)
601 {
602 membuf_tcxx . . $ membuf_t# *data = opaque;
603
604 if (buffer)
605 put_membufcxx . . $ put_membuf(1ceb7b077846ab92). (data, buffer, length);
606 return 0;
607 }
608
609 /* Handle the NEEDPIN inquiry. */
610 static int
611 inq_needpincxx . . $ inq_needpin(f53b6be6549e6d95). (void *opaque, const char *line)
612 {
613 struct inq_needpin_scxx . . $ inq_needpin_s# *parm = opaque;
614 char *pin;
615 size_t pinlen;
616 int rc;
617
618 rc = 0;
619
620 if (!strncmpcxx . . $ strncmp(fd278af2f295fa2e). (line, "NEEDPIN", 7) && (line[7] == ' ' || !line[7]))
621 {
622 if (!parm->getpin_cbcxx . . $ inq_needpin_s#getpin_cb.)
623 {
624 rc = GPG_ERR_BAD_PINcxx . . $ GPG_ERR_BAD_PIN.;
625 goto out;
626 }
627
628 line += 7;
629 while (*line == ' ')
630 line++;
631
632 pinlen = 90;
633 pin = xtrymalloc_securecxx . . $ `src/poldi.h:36:9`! (pinlen);
634 if (!pin)
635 {
636 rc = gpg_error_from_errnocxx . . $ gpg_error_from_errno(46cc46841198e427). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
637 goto out;
638 }
639
640 rc = parm->getpin_cbcxx . . $ inq_needpin_s#getpin_cb. (parm->getpin_cb_argcxx . . $ inq_needpin_s#getpin_cb_arg., line, pin, pinlen);
641 if (!rc)
642 rc = assuan_send_datacxx . . $ `src/assuan/assuan.h:126:9`! (parm->ctxcxx . . $ inq_needpin_s#ctx.->assuan_ctxcxx . . $ scd_context#assuan_ctx., pin, pinlen);
643 xfreecxx . . $ `src/poldi.h:39:9`! (pin);
644 }
645 else if (!strncmpcxx . . $ strncmp(fd278af2f295fa2e). (line, "POPUPPINPADPROMPT", 17)
646 && (line[17] == ' ' || !line[17]))
647 {
648 if (!parm->getpin_cbcxx . . $ inq_needpin_s#getpin_cb.)
649 {
650 rc = GPG_ERR_BAD_PINcxx . . $ GPG_ERR_BAD_PIN.;
651 goto out;
652 }
653
654 line += 17;
655 while (*line == ' ')
656 line++;
657
658 rc = parm->getpin_cbcxx . . $ inq_needpin_s#getpin_cb. (parm->getpin_cb_argcxx . . $ inq_needpin_s#getpin_cb_arg., line, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 1);
659 }
660 else if (!strncmpcxx . . $ strncmp(fd278af2f295fa2e). (line, "DISMISSPINPADPROMPT", 19)
661 && (line[19] == ' ' || !line[19]))
662 {
663 if (!parm->getpin_cbcxx . . $ inq_needpin_s#getpin_cb.)
664 {
665 rc = GPG_ERR_BAD_PINcxx . . $ GPG_ERR_BAD_PIN.;
666 goto out;
667 }
668
669 rc = parm->getpin_cbcxx . . $ inq_needpin_s#getpin_cb. (parm->getpin_cb_argcxx . . $ inq_needpin_s#getpin_cb_arg., "", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0);
670 }
671 else
672 {
673 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (parm->ctxcxx . . $ inq_needpin_s#ctx.->loghandlecxx . . $ scd_context#loghandle.,
674 "received unsupported inquiry from scdaemon `%s'",
675 line);
676 rc = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_ASS_UNKNOWN_INQUIREcxx . . $ GPG_ERR_ASS_UNKNOWN_INQUIRE.);
677 }
678
679 out:
680
681 return gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (rc);
682 }
683
684
685 /* Create a signature using the current card. CTX is the handle for
686 the scd subsystem. KEYID identifies the key on the card to use for
687 signing. GETPIN_CB is the callback, which is called for querying of
688 the PIN, GETPIN_CB_ARG is passed as opaque argument to
689 GETPIN_CB. INDATA/INDATALEN is the input for the signature
690 function. The signature created is written into newly allocated
691 memory in *R_BUF, *R_BUFLEN will hold the length of the
692 signature. */
693 gpg_error_tcxx . . $ gpg_error_t#
694 scd_pksigncxx . . $ scd_pksign(def977db0e0fd549). (scd_context_tcxx . . $ scd_context_t# ctx,
695 const char *keyid,
696 const unsigned char *indata, size_t indatalen,
697 unsigned char **r_buf, size_t *r_buflen)
698 {
699 int rc;
700 char *p, line[ASSUAN_LINELENGTHcxx . . $ `src/assuan/assuan.h:376:9`!];
701 membuf_tcxx . . $ membuf_t# data;
702 struct inq_needpin_scxx . . $ inq_needpin_s# inqparm;
703 size_t len;
704 unsigned char *sigbuf;
705 size_t sigbuflen;
706
707 *r_buf = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
708 *r_buflen = 0;
709 rc = 0;
710
711 init_membufcxx . . $ init_membuf(917e2e0aeaea34de). (&data, 1024);
712
713 if (indatalen*2 + 50 > DIMcxx . . $ `src/util/util.h:39:9`!(line)) /* FIXME: Are such long inputs
714 allowed? Should we handle them
715 differently? */
716 {
717 rc = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_GENERALcxx . . $ GPG_ERR_GENERAL.);
718 goto out;
719 }
720
721 /* Inform scdaemon about the data to be signed. */
722
723 sprintfcxx . . $ sprintf(26c8642d26409d6d). (line, "SETDATA ");
724 p = line + strlencxx . . $ strlen(751346f8406fd082). (line);
725 bin2hexcxx . . $ bin2hex(a8ad7dbe182f168a). (indata, indatalen, p);
726
727 rc = assuan_transactcxx . . $ `src/assuan/assuan.h:120:9`! (ctx->assuan_ctxcxx . . $ scd_context#assuan_ctx., line,
728 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
729 if (rc)
730 goto out;
731
732 /* Setup NEEDPIN inquiry handler. */
733
734 inqparm.ctxcxx . . $ inq_needpin_s#ctx. = ctx;
735 inqparm.getpin_cbcxx . . $ inq_needpin_s#getpin_cb. = ctx->pincbcxx . . $ scd_context#pincb.;
736 inqparm.getpin_cb_argcxx . . $ inq_needpin_s#getpin_cb_arg. = ctx->pincb_cookiecxx . . $ scd_context#pincb_cookie.;
737
738 /* Go, sign it. */
739
740 snprintfcxx . . $ snprintf(ea502d73e74c754c). (line, DIMcxx . . $ `src/util/util.h:39:9`!(line)-1, "PKSIGN %s", keyid);
741 line[DIMcxx . . $ `src/util/util.h:39:9`!(line)-1] = 0;
742 rc = assuan_transactcxx . . $ `src/assuan/assuan.h:120:9`! (ctx->assuan_ctxcxx . . $ scd_context#assuan_ctx., line,
743 membuf_data_cbcxx . . $ membuf_data_cb(72e086fd9bc8337a)., &data,
744 inq_needpincxx . . $ inq_needpin(f53b6be6549e6d95)., &inqparm,
745 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
746 if (rc)
747 goto out;
748
749 /* Extract signature. FIXME: can't we do this easier? By reusing
750 membuf, without another alloc/free? */
751
752 sigbuf = get_membufcxx . . $ get_membuf(5dfc76f6663417c1). (&data, &sigbuflen);
753 *r_buflen = sigbuflen;
754 p = xtrymalloccxx . . $ `src/poldi.h:35:9`! (*r_buflen);
755 *r_buf = (unsigned char*)p;
756 if (!p)
757 {
758 rc = gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). ();
759 goto out;
760 }
761
762 memcpycxx . . $ memcpy(cc817cb175f282d). (p, sigbuf, sigbuflen);
763
764 out:
765
766 xfreecxx . . $ `src/poldi.h:39:9`! (get_membuf (&data, &len));
767
768 return rc;
769 }
770
771
772
773 /* CMD: READKEY. */
774
775 /* Read a key with ID and return it in an allocate buffer pointed to
776 by r_BUF as a valid S-expression. */
777 int
778 scd_readkeycxx . . $ scd_readkey(bbef350ea803bf16). (scd_context_tcxx . . $ scd_context_t# ctx,
779 const char *id, gcry_sexp_tcxx . . $ gcry_sexp_t# *key)
780 {
781 int rc;
782 char line[ASSUAN_LINELENGTHcxx . . $ `src/assuan/assuan.h:376:9`!];
783 membuf_tcxx . . $ membuf_t# data;
784 size_t buflen;
785 unsigned char *buffer;
786
787 *key = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
788 buffer = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
789 init_membufcxx . . $ init_membuf(917e2e0aeaea34de). (&data, 1024);
790
791 /* Execute READKEY command. */
792 snprintfcxx . . $ snprintf(ea502d73e74c754c). (line, DIMcxx . . $ `src/util/util.h:39:9`!(line)-1, "READKEY %s", id);
793 line[DIMcxx . . $ `src/util/util.h:39:9`!(line)-1] = 0;
794 rc = assuan_transactcxx . . $ `src/assuan/assuan.h:120:9`! (ctx->assuan_ctxcxx . . $ scd_context#assuan_ctx., line,
795 membuf_data_cbcxx . . $ membuf_data_cb(72e086fd9bc8337a)., &data,
796 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,
797 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
798 if (rc)
799 goto out;
800
801 buffer = get_membufcxx . . $ get_membuf(5dfc76f6663417c1). (&data, &buflen);
802 if (!buffer)
803 {
804 rc = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_ENOMEMcxx . . $ GPG_ERR_ENOMEM.);
805 goto out;
806 }
807
808 if (!gcry_sexp_canon_lencxx . . $ gcry_sexp_canon_len(de1a7274e1c1e7fe). (buffer, buflen, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!))
809 {
810 rc = gpg_errorcxx . . $ gpg_error(6231b2685059dc28). (GPG_ERR_INV_VALUEcxx . . $ GPG_ERR_INV_VALUE.);
811 *key = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
812 }
813 else
814 rc = gcry_sexp_newcxx . . $ gcry_sexp_new(535602b83bfa9847). (key, buffer, buflen, 1);
815
816 out:
817
818 xfreecxx . . $ `src/poldi.h:39:9`! (buffer);
819
820 return rc;
821 }
822
823
824
825
826 /* Sends a GETINFO command for WHAT to the scdaemon through CTX. The
827 newly allocated result is stored in *RESULT. Returns proper error
828 code, zero on success. */
829 int
830 scd_getinfocxx . . $ scd_getinfo(b8459bee76b2d33b). (scd_context_tcxx . . $ scd_context_t# ctx, const char *what, char **result)
831 {
832 int rc;
833 char line[ASSUAN_LINELENGTHcxx . . $ `src/assuan/assuan.h:376:9`!];
834 membuf_tcxx . . $ membuf_t# data;
835 unsigned char *databuf;
836 size_t datalen;
837 char *res;
838
839 *result = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
840
841 sprintfcxx . . $ sprintf(26c8642d26409d6d). (line, "GETINFO %s", what);
842 init_membufcxx . . $ init_membuf(917e2e0aeaea34de). (&data, 256);
843
844 rc = assuan_transactcxx . . $ `src/assuan/assuan.h:120:9`! (ctx->assuan_ctxcxx . . $ scd_context#assuan_ctx., line, membuf_data_cbcxx . . $ membuf_data_cb(72e086fd9bc8337a)., &data,
845 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
846 if (rc)
847 goto out;
848
849 databuf = get_membufcxx . . $ get_membuf(5dfc76f6663417c1). (&data, &datalen);
850 if (databuf && datalen)
851 {
852 res = xtrymalloccxx . . $ `src/poldi.h:35:9`! (datalen + 1);
853 if (!res)
854 {
855 log_msg_errorcxx . . $ log_msg_error(628bea2675049132). (ctx->loghandlecxx . . $ scd_context#loghandle.,
856 "warning: can't store getinfo data: %s",
857 strerrorcxx . . $ strerror(ff224f080444ee45). (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
858 rc = gpg_error_from_syserrorcxx . . $ gpg_error_from_syserror(db52760e79bf9054). ();
859 }
860 else
861 {
862 memcpycxx . . $ memcpy(cc817cb175f282d). (res, databuf, datalen);
863 res[datalen] = 0;
864 *result = res;
865 }
866 }
867
868 out:
869
870 xfreecxx . . $ `src/poldi.h:39:9`! (get_membuf (&data, &datalen));
871
872 return rc;
873 }
874
875 /* END */