| 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> |
| 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" |
| 39 | #include "assuan.h" |
| 40 | #include "util/util.h" |
| 41 | #include "util/membuf.h" |
| 42 | #include "util/support.h" |
| 43 | #include "util/simplelog.h" |
| 44 | |
| 45 | #ifdef _POSIX_OPEN_MAX |
| 46 | #define MAX_OPEN_FDS _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_cardinfo scd_cardinfo_null; |
| 55 | |
| 56 | |
| 57 | |
| 58 | struct scd_context |
| 59 | { |
| 60 | assuan_context_t assuan_ctx; |
| 61 | unsigned int flags; |
| 62 | log_handle_t loghandle; |
| 63 | scd_pincb_t pincb; |
| 64 | void *pincb_cookie; |
| 65 | }; |
| 66 | |
| 67 | /* Callback parameter for learn card */ |
| 68 | struct learn_parm_s |
| 69 | { |
| 70 | void (*kpinfo_cb)(void*, const char *); |
| 71 | void *kpinfo_cb_arg; |
| 72 | void (*certinfo_cb)(void*, const char *); |
| 73 | void *certinfo_cb_arg; |
| 74 | void (*sinfo_cb)(void*, const char *, size_t, const char *); |
| 75 | void *sinfo_cb_arg; |
| 76 | }; |
| 77 | |
| 78 | struct inq_needpin_s |
| 79 | { |
| 80 | scd_context_t ctx; |
| 81 | int (*getpin_cb)(void *, const char *, char*, size_t); |
| 82 | void *getpin_cb_arg; |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | /* Local prototypes. */ |
| 87 | static assuan_error_t membuf_data_cb (void *opaque, |
| 88 | const void *buffer, size_t length); |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | static gpg_error_t scd_serialno_internal (assuan_context_t ctx, |
| 94 | char **r_serialno); |
| 95 | |
| 96 | |
| 97 | |
| 98 | /* Get the socket of GPG-AGENT by gpgconf. */ |
| 99 | static gpg_error_t |
| 100 | get_agent_socket_name (char **gpg_agent_sockname) |
| 101 | { |
| 102 | gpg_error_t err = 0; |
| 103 | FILE *input; |
| 104 | char *result; |
| 105 | size_t len; |
| 106 | |
| 107 | *gpg_agent_sockname = NULL; |
| 108 | |
| 109 | result = xtrymalloc (256); |
| 110 | if (!result) |
| 111 | return gpg_error_from_syserror (); |
| 112 | |
| 113 | /* It is good if we have popen with execv (no SHELL) */ |
| 114 | input = popen (GNUPG_DEFAULT_GPGCONF " --list-dirs agent-socket", "r"); |
| 115 | if (input == NULL) |
| 116 | { |
| 117 | xfree (result); |
| 118 | return gpg_error (GPG_ERR_NOT_FOUND); |
| 119 | } |
| 120 | |
| 121 | len = fread (result, 1, 256, input); |
| 122 | pclose (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 | xfree (result); |
| 132 | err = gpg_error (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_t |
| 145 | agent_scd_getinfo_socket_name (assuan_context_t ctx, char **socket_name) |
| 146 | { |
| 147 | membuf_t data; |
| 148 | gpg_error_t err = 0; |
| 149 | unsigned char *databuf; |
| 150 | size_t datalen; |
| 151 | |
| 152 | init_membuf (&data, 256); |
| 153 | *socket_name = NULL; |
| 154 | |
| 155 | err = assuan_transact (ctx, "SCD GETINFO socket_name", membuf_data_cb, &data, |
| 156 | NULL, NULL, NULL, NULL); |
| 157 | databuf = get_membuf (&data, &datalen); |
| 158 | if (!err) |
| 159 | { |
| 160 | if (databuf && datalen) |
| 161 | { |
| 162 | char *res = xtrymalloc (datalen + 1); |
| 163 | if (!res) |
| 164 | err = gpg_error_from_syserror (); |
| 165 | else |
| 166 | { |
| 167 | memcpy (res, databuf, datalen); |
| 168 | res[datalen] = 0; |
| 169 | *socket_name = res; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | xfree (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_t |
| 183 | get_scd_socket_from_agent (char **socket_name) |
| 184 | { |
| 185 | assuan_context_t ctx = NULL; |
| 186 | gpg_error_t err; |
| 187 | char *gpg_agent_sockname; |
| 188 | |
| 189 | err = get_agent_socket_name (&gpg_agent_sockname); |
| 190 | if (err) |
| 191 | return err; |
| 192 | |
| 193 | err = assuan_socket_connect (&ctx, gpg_agent_sockname, 0); |
| 194 | xfree (gpg_agent_sockname); |
| 195 | if (!err) |
| 196 | err = agent_scd_getinfo_socket_name (ctx, socket_name); |
| 197 | |
| 198 | assuan_disconnect (ctx); |
| 199 | |
| 200 | return err; |
| 201 | } |
| 202 | |
| 203 | /* Send a RESTART to SCDaemon. */ |
| 204 | static void |
| 205 | restart_scd (scd_context_t ctx) |
| 206 | { |
| 207 | assuan_transact (ctx->assuan_ctx, "RESTART", |
| 208 | NULL, NULL, NULL, NULL, NULL, NULL); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | |
| 213 | /* Fork off scdaemon and work by pipes. Returns proper error code or |
| 214 | zero on success. */ |
| 215 | gpg_error_t |
| 216 | scd_connect (scd_context_t *scd_ctx, int use_agent, const char *scd_path, |
| 217 | const char *scd_options, log_handle_t loghandle) |
| 218 | { |
| 219 | assuan_context_t assuan_ctx; |
| 220 | scd_context_t ctx; |
| 221 | gpg_error_t err; |
| 222 | |
| 223 | assuan_ctx = NULL; |
| 224 | |
| 225 | if (fflush (NULL)) |
| 226 | { |
| 227 | err = gpg_error_from_syserror (); |
| 228 | log_msg_error (loghandle, "error flushing pending output: %s", |
| 229 | strerror (errno)); |
| 230 | return err; |
| 231 | } |
| 232 | |
| 233 | ctx = xtrymalloc (sizeof (*ctx)); |
| 234 | if (!ctx) |
| 235 | return gpg_error_from_syserror (); |
| 236 | |
| 237 | ctx->assuan_ctx = NULL; |
| 238 | ctx->flags = 0; |
| 239 | |
| 240 | /* Try using scdaemon under gpg-agent. */ |
| 241 | if (use_agent) |
| 242 | { |
| 243 | char *scd_socket_name = NULL; |
| 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_agent (&scd_socket_name); |
| 250 | if (!err) |
| 251 | err = assuan_socket_connect (&assuan_ctx, scd_socket_name, 0); |
| 252 | |
| 253 | if (!err) |
| 254 | log_msg_debug (loghandle, |
| 255 | "got scdaemon socket name from gpg-agent, " |
| 256 | "connected to socket '%s'", scd_socket_name); |
| 257 | |
| 258 | xfree (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_SCD; |
| 273 | if (!(pgmname = strrchr (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++] = NULL; |
| 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++] = fileno (stderr); |
| 301 | no_close_list[i] = -1; |
| 302 | |
| 303 | /* connect to the scdaemon and perform initial handshaking */ |
| 304 | err = assuan_pipe_connect (&assuan_ctx, scd_path, argv, no_close_list); |
| 305 | if (err) |
| 306 | { |
| 307 | log_msg_error (loghandle, "could not spawn scdaemon: %s", |
| 308 | gpg_strerror (err)); |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | log_msg_debug (loghandle, "spawned a new scdaemon (path: '%s')", |
| 313 | scd_path); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (err) |
| 318 | { |
| 319 | assuan_disconnect (assuan_ctx); |
| 320 | xfree (ctx); |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | /* FIXME: is this the best way? -mo */ |
| 325 | //reset_scd (assuan_ctx); |
| 326 | scd_serialno_internal (assuan_ctx, NULL); |
| 327 | |
| 328 | ctx->assuan_ctx = assuan_ctx; |
| 329 | ctx->flags = 0; |
| 330 | ctx->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_disconnect (scd_context_t scd_ctx) |
| 340 | { |
| 341 | if (scd_ctx) |
| 342 | { |
| 343 | restart_scd (scd_ctx); |
| 344 | assuan_disconnect (scd_ctx->assuan_ctx); |
| 345 | xfree (scd_ctx); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | |
| 350 | void |
| 351 | scd_set_pincb (scd_context_t scd_ctx, |
| 352 | scd_pincb_t pincb, void *cookie) |
| 353 | { |
| 354 | assert (scd_ctx); |
| 355 | |
| 356 | scd_ctx->pincb = pincb; |
| 357 | scd_ctx->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_string (const char *s) |
| 369 | { |
| 370 | char *buffer, *d; |
| 371 | |
| 372 | buffer = d = xtrymalloc (strlen ((const char*)s)+1); |
| 373 | if (!buffer) |
| 374 | return NULL; |
| 375 | while (*s) |
| 376 | { |
| 377 | if (*s == '%' && s[1] && s[2]) |
| 378 | { |
| 379 | s++; |
| 380 | *d = xtoi_2 (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_fpr (const char *hexstr, char *fpr) |
| 407 | { |
| 408 | const char *s; |
| 409 | int n; |
| 410 | |
| 411 | for (s=hexstr, n=0; hexdigitp (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_2 (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_serialno (const char *line) |
| 426 | { |
| 427 | const char *s; |
| 428 | char *p; |
| 429 | |
| 430 | for (s=line; hexdigitp (s); s++) |
| 431 | ; |
| 432 | p = xtrymalloc (s + 1 - line); |
| 433 | if (p) |
| 434 | { |
| 435 | memcpy (p, line, s-line); |
| 436 | p[s-line] = 0; |
| 437 | } |
| 438 | return p; |
| 439 | } |
| 440 | |
| 441 | static int |
| 442 | learn_status_cb (void *opaque, const char *line) |
| 443 | { |
| 444 | struct scd_cardinfo *parm = opaque; |
| 445 | const char *keyword = line; |
| 446 | int keywordlen; |
| 447 | //int i; |
| 448 | |
| 449 | for (keywordlen=0; *line && !spacep (line); line++, keywordlen++) |
| 450 | ; |
| 451 | while (spacep (line)) |
| 452 | line++; |
| 453 | |
| 454 | if (keywordlen == 8 && !memcmp (keyword, "SERIALNO", keywordlen)) |
| 455 | { |
| 456 | xfree (parm->serialno); |
| 457 | parm->serialno = store_serialno (line); |
| 458 | } |
| 459 | else if (keywordlen == 9 && !memcmp (keyword, "DISP-NAME", keywordlen)) |
| 460 | { |
| 461 | xfree (parm->disp_name); |
| 462 | parm->disp_name = unescape_status_string (line); |
| 463 | } |
| 464 | else if (keywordlen == 9 && !memcmp (keyword, "DISP-LANG", keywordlen)) |
| 465 | { |
| 466 | xfree (parm->disp_lang); |
| 467 | parm->disp_lang = unescape_status_string (line); |
| 468 | } |
| 469 | else if (keywordlen == 10 && !memcmp (keyword, "PUBKEY-URL", keywordlen)) |
| 470 | { |
| 471 | xfree (parm->pubkey_url); |
| 472 | parm->pubkey_url = unescape_status_string (line); |
| 473 | } |
| 474 | else if (keywordlen == 10 && !memcmp (keyword, "LOGIN-DATA", keywordlen)) |
| 475 | { |
| 476 | xfree (parm->login_data); |
| 477 | parm->login_data = unescape_status_string (line); |
| 478 | } |
| 479 | else if (keywordlen == 7 && !memcmp (keyword, "KEY-FPR", keywordlen)) |
| 480 | { |
| 481 | int no = atoi (line); |
| 482 | while (*line && !spacep (line)) |
| 483 | line++; |
| 484 | while (spacep (line)) |
| 485 | line++; |
| 486 | if (no == 1) |
| 487 | parm->fpr1valid = unhexify_fpr (line, parm->fpr1); |
| 488 | else if (no == 2) |
| 489 | parm->fpr2valid = unhexify_fpr (line, parm->fpr2); |
| 490 | else if (no == 3) |
| 491 | parm->fpr3valid = unhexify_fpr (line, parm->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_learn (scd_context_t ctx, |
| 501 | struct scd_cardinfo *cardinfo) |
| 502 | { |
| 503 | int rc; |
| 504 | |
| 505 | *cardinfo = scd_cardinfo_null; |
| 506 | rc = assuan_transact (ctx->assuan_ctx, "LEARN --force", |
| 507 | NULL, NULL, NULL, NULL, |
| 508 | learn_status_cb, 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_cardinfo (struct scd_cardinfo info) |
| 517 | { |
| 518 | xfree (info.serialno); |
| 519 | xfree (info.disp_name); |
| 520 | xfree (info.login_data); |
| 521 | xfree (info.pubkey_url); |
| 522 | } |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | /* CMD: SERIALNO. */ |
| 528 | |
| 529 | static int |
| 530 | get_serialno_cb (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 && !spacep (line); line++, keywordlen++) |
| 538 | ; |
| 539 | while (spacep (line)) |
| 540 | line++; |
| 541 | |
| 542 | if (keywordlen == 8 && !memcmp (keyword, "SERIALNO", keywordlen)) |
| 543 | { |
| 544 | if (*serialno) |
| 545 | return gpg_error (GPG_ERR_CONFLICT); /* Unexpected status line. */ |
| 546 | for (n=0,s=line; hexdigitp (s); s++, n++) |
| 547 | ; |
| 548 | if (!n || (n&1)|| !(spacep (s) || !*s) ) |
| 549 | return gpg_error (GPG_ERR_ASS_PARAMETER); |
| 550 | *serialno = xtrymalloc (n+1); |
| 551 | if (!*serialno) |
| 552 | return gpg_error_from_errno (errno); |
| 553 | memcpy (*serialno, line, n); |
| 554 | (*serialno)[n] = 0; |
| 555 | } |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | static gpg_error_t |
| 561 | scd_serialno_internal (assuan_context_t ctx, char **r_serialno) |
| 562 | { |
| 563 | char *serialno; |
| 564 | int rc; |
| 565 | |
| 566 | serialno = NULL; |
| 567 | |
| 568 | rc = assuan_transact (ctx, "SERIALNO", NULL, NULL, NULL, NULL, |
| 569 | get_serialno_cb, &serialno); |
| 570 | if (rc) |
| 571 | goto out; |
| 572 | |
| 573 | if (r_serialno) |
| 574 | *r_serialno = serialno; |
| 575 | else |
| 576 | xfree (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_t |
| 586 | scd_serialno (scd_context_t ctx, char **r_serialno) |
| 587 | { |
| 588 | gpg_error_t err; |
| 589 | |
| 590 | err = scd_serialno_internal (ctx->assuan_ctx, r_serialno); |
| 591 | |
| 592 | return err; |
| 593 | } |
| 594 | |
| 595 | /* CMD: PKSIGN. */ |
| 596 | |
| 597 | |
| 598 | |
| 599 | static int |
| 600 | membuf_data_cb (void *opaque, const void *buffer, size_t length) |
| 601 | { |
| 602 | membuf_t *data = opaque; |
| 603 | |
| 604 | if (buffer) |
| 605 | put_membuf (data, buffer, length); |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /* Handle the NEEDPIN inquiry. */ |
| 610 | static int |
| 611 | inq_needpin (void *opaque, const char *line) |
| 612 | { |
| 613 | struct inq_needpin_s *parm = opaque; |
| 614 | char *pin; |
| 615 | size_t pinlen; |
| 616 | int rc; |
| 617 | |
| 618 | rc = 0; |
| 619 | |
| 620 | if (!strncmp (line, "NEEDPIN", 7) && (line[7] == ' ' || !line[7])) |
| 621 | { |
| 622 | if (!parm->getpin_cb) |
| 623 | { |
| 624 | rc = GPG_ERR_BAD_PIN; |
| 625 | goto out; |
| 626 | } |
| 627 | |
| 628 | line += 7; |
| 629 | while (*line == ' ') |
| 630 | line++; |
| 631 | |
| 632 | pinlen = 90; |
| 633 | pin = xtrymalloc_secure (pinlen); |
| 634 | if (!pin) |
| 635 | { |
| 636 | rc = gpg_error_from_errno (errno); |
| 637 | goto out; |
| 638 | } |
| 639 | |
| 640 | rc = parm->getpin_cb (parm->getpin_cb_arg, line, pin, pinlen); |
| 641 | if (!rc) |
| 642 | rc = assuan_send_data (parm->ctx->assuan_ctx, pin, pinlen); |
| 643 | xfree (pin); |
| 644 | } |
| 645 | else if (!strncmp (line, "POPUPPINPADPROMPT", 17) |
| 646 | && (line[17] == ' ' || !line[17])) |
| 647 | { |
| 648 | if (!parm->getpin_cb) |
| 649 | { |
| 650 | rc = GPG_ERR_BAD_PIN; |
| 651 | goto out; |
| 652 | } |
| 653 | |
| 654 | line += 17; |
| 655 | while (*line == ' ') |
| 656 | line++; |
| 657 | |
| 658 | rc = parm->getpin_cb (parm->getpin_cb_arg, line, NULL, 1); |
| 659 | } |
| 660 | else if (!strncmp (line, "DISMISSPINPADPROMPT", 19) |
| 661 | && (line[19] == ' ' || !line[19])) |
| 662 | { |
| 663 | if (!parm->getpin_cb) |
| 664 | { |
| 665 | rc = GPG_ERR_BAD_PIN; |
| 666 | goto out; |
| 667 | } |
| 668 | |
| 669 | rc = parm->getpin_cb (parm->getpin_cb_arg, "", NULL, 0); |
| 670 | } |
| 671 | else |
| 672 | { |
| 673 | log_msg_error (parm->ctx->loghandle, |
| 674 | "received unsupported inquiry from scdaemon `%s'", |
| 675 | line); |
| 676 | rc = gpg_error (GPG_ERR_ASS_UNKNOWN_INQUIRE); |
| 677 | } |
| 678 | |
| 679 | out: |
| 680 | |
| 681 | return gpg_error (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_t |
| 694 | scd_pksign (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_LINELENGTH]; |
| 701 | membuf_t data; |
| 702 | struct inq_needpin_s inqparm; |
| 703 | size_t len; |
| 704 | unsigned char *sigbuf; |
| 705 | size_t sigbuflen; |
| 706 | |
| 707 | *r_buf = NULL; |
| 708 | *r_buflen = 0; |
| 709 | rc = 0; |
| 710 | |
| 711 | init_membuf (&data, 1024); |
| 712 | |
| 713 | if (indatalen*2 + 50 > DIM(line)) /* FIXME: Are such long inputs |
| 714 | allowed? Should we handle them |
| 715 | differently? */ |
| 716 | { |
| 717 | rc = gpg_error (GPG_ERR_GENERAL); |
| 718 | goto out; |
| 719 | } |
| 720 | |
| 721 | /* Inform scdaemon about the data to be signed. */ |
| 722 | |
| 723 | sprintf (line, "SETDATA "); |
| 724 | p = line + strlen (line); |
| 725 | bin2hex (indata, indatalen, p); |
| 726 | |
| 727 | rc = assuan_transact (ctx->assuan_ctx, line, |
| 728 | NULL, NULL, NULL, NULL, NULL, NULL); |
| 729 | if (rc) |
| 730 | goto out; |
| 731 | |
| 732 | /* Setup NEEDPIN inquiry handler. */ |
| 733 | |
| 734 | inqparm.ctx = ctx; |
| 735 | inqparm.getpin_cb = ctx->pincb; |
| 736 | inqparm.getpin_cb_arg = ctx->pincb_cookie; |
| 737 | |
| 738 | /* Go, sign it. */ |
| 739 | |
| 740 | snprintf (line, DIM(line)-1, "PKSIGN %s", keyid); |
| 741 | line[DIM(line)-1] = 0; |
| 742 | rc = assuan_transact (ctx->assuan_ctx, line, |
| 743 | membuf_data_cb, &data, |
| 744 | inq_needpin, &inqparm, |
| 745 | NULL, NULL); |
| 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_membuf (&data, &sigbuflen); |
| 753 | *r_buflen = sigbuflen; |
| 754 | p = xtrymalloc (*r_buflen); |
| 755 | *r_buf = (unsigned char*)p; |
| 756 | if (!p) |
| 757 | { |
| 758 | rc = gpg_error_from_syserror (); |
| 759 | goto out; |
| 760 | } |
| 761 | |
| 762 | memcpy (p, sigbuf, sigbuflen); |
| 763 | |
| 764 | out: |
| 765 | |
| 766 | xfree (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_readkey (scd_context_t ctx, |
| 779 | const char *id, gcry_sexp_t *key) |
| 780 | { |
| 781 | int rc; |
| 782 | char line[ASSUAN_LINELENGTH]; |
| 783 | membuf_t data; |
| 784 | size_t buflen; |
| 785 | unsigned char *buffer; |
| 786 | |
| 787 | *key = NULL; |
| 788 | buffer = NULL; |
| 789 | init_membuf (&data, 1024); |
| 790 | |
| 791 | /* Execute READKEY command. */ |
| 792 | snprintf (line, DIM(line)-1, "READKEY %s", id); |
| 793 | line[DIM(line)-1] = 0; |
| 794 | rc = assuan_transact (ctx->assuan_ctx, line, |
| 795 | membuf_data_cb, &data, |
| 796 | NULL, NULL, |
| 797 | NULL, NULL); |
| 798 | if (rc) |
| 799 | goto out; |
| 800 | |
| 801 | buffer = get_membuf (&data, &buflen); |
| 802 | if (!buffer) |
| 803 | { |
| 804 | rc = gpg_error (GPG_ERR_ENOMEM); |
| 805 | goto out; |
| 806 | } |
| 807 | |
| 808 | if (!gcry_sexp_canon_len (buffer, buflen, NULL, NULL)) |
| 809 | { |
| 810 | rc = gpg_error (GPG_ERR_INV_VALUE); |
| 811 | *key = NULL; |
| 812 | } |
| 813 | else |
| 814 | rc = gcry_sexp_new (key, buffer, buflen, 1); |
| 815 | |
| 816 | out: |
| 817 | |
| 818 | xfree (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_getinfo (scd_context_t ctx, const char *what, char **result) |
| 831 | { |
| 832 | int rc; |
| 833 | char line[ASSUAN_LINELENGTH]; |
| 834 | membuf_t data; |
| 835 | unsigned char *databuf; |
| 836 | size_t datalen; |
| 837 | char *res; |
| 838 | |
| 839 | *result = NULL; |
| 840 | |
| 841 | sprintf (line, "GETINFO %s", what); |
| 842 | init_membuf (&data, 256); |
| 843 | |
| 844 | rc = assuan_transact (ctx->assuan_ctx, line, membuf_data_cb, &data, |
| 845 | NULL, NULL, NULL, NULL); |
| 846 | if (rc) |
| 847 | goto out; |
| 848 | |
| 849 | databuf = get_membuf (&data, &datalen); |
| 850 | if (databuf && datalen) |
| 851 | { |
| 852 | res = xtrymalloc (datalen + 1); |
| 853 | if (!res) |
| 854 | { |
| 855 | log_msg_error (ctx->loghandle, |
| 856 | "warning: can't store getinfo data: %s", |
| 857 | strerror (errno)); |
| 858 | rc = gpg_error_from_syserror (); |
| 859 | } |
| 860 | else |
| 861 | { |
| 862 | memcpy (res, databuf, datalen); |
| 863 | res[datalen] = 0; |
| 864 | *result = res; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | out: |
| 869 | |
| 870 | xfree (get_membuf (&data, &datalen)); |
| 871 | |
| 872 | return rc; |
| 873 | } |
| 874 | |
| 875 | /* END */ |