agent.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /* agent.c - Talking to gpg-agent.
  2. Copyright (C) 2006, 2007, 2008 g10 Code GmbH
  3. This file is part of Scute.
  4. Scute is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. Scute is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Scute; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. In addition, as a special exception, g10 Code GmbH gives permission
  16. to link this library: with the Mozilla Foundation's code for
  17. Mozilla (or with modified versions of it that use the same license
  18. as the "Mozilla" code), and distribute the linked executables. You
  19. must obey the GNU General Public License in all respects for all of
  20. the code used other than "Mozilla". If you modify this file, you
  21. may extend this exception to your version of the file, but you are
  22. not obligated to do so. If you do not wish to do so, delete this
  23. exception statement from your version. */
  24. #if HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <locale.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. #include <stdarg.h>
  33. #include <assuan.h>
  34. #include <gpg-error.h>
  35. #include "debug.h"
  36. #include "support.h"
  37. #include "agent.h"
  38. #ifdef HAVE_W32_SYSTEM
  39. #define PATHSEP_C ';'
  40. #else
  41. #define PATHSEP_C ':'
  42. #endif
  43. /* The global agent context. */
  44. static assuan_context_t agent_ctx = NULL;
  45. #ifdef HAVE_W32_SYSTEM
  46. /* Helper function to build_w32_commandline. */
  47. static char *
  48. build_w32_commandline_copy (char *buffer, const char *string)
  49. {
  50. char *p = buffer;
  51. const char *s;
  52. if (!*string) /* Empty string. */
  53. p = stpcpy (p, "\"\"");
  54. else if (strpbrk (string, " \t\n\v\f\""))
  55. {
  56. /* Need top do some kind of quoting. */
  57. p = stpcpy (p, "\"");
  58. for (s=string; *s; s++)
  59. {
  60. *p++ = *s;
  61. if (*s == '\"')
  62. *p++ = *s;
  63. }
  64. *p++ = '\"';
  65. *p = 0;
  66. }
  67. else
  68. p = stpcpy (p, string);
  69. return p;
  70. }
  71. /* Build a command line for use with W32's CreateProcess. On success
  72. CMDLINE gets the address of a newly allocated string. */
  73. static gpg_error_t
  74. build_w32_commandline (const char *pgmname, const char * const *argv,
  75. char **cmdline)
  76. {
  77. int i, n;
  78. const char *s;
  79. char *buf, *p;
  80. *cmdline = NULL;
  81. n = 0;
  82. s = pgmname;
  83. n += strlen (s) + 1 + 2; /* (1 space, 2 quoting */
  84. for (; *s; s++)
  85. if (*s == '\"')
  86. n++; /* Need to double inner quotes. */
  87. for (i=0; (s=argv[i]); i++)
  88. {
  89. n += strlen (s) + 1 + 2; /* (1 space, 2 quoting */
  90. for (; *s; s++)
  91. if (*s == '\"')
  92. n++; /* Need to double inner quotes. */
  93. }
  94. n++;
  95. buf = p = malloc (n);
  96. if (!buf)
  97. return gpg_error_from_syserror ();
  98. p = build_w32_commandline_copy (p, pgmname);
  99. for (i=0; argv[i]; i++)
  100. {
  101. *p++ = ' ';
  102. p = build_w32_commandline_copy (p, argv[i]);
  103. }
  104. *cmdline= buf;
  105. return 0;
  106. }
  107. #endif /*HAVE_W32_SYSTEM*/
  108. /* Spawn a new process and immediatley detach from it. The name of
  109. the program to exec is PGMNAME and its arguments are in ARGV (the
  110. programname is automatically passed as first argument). An error
  111. is returned if pgmname is not executable; to make this work it is
  112. necessary to provide an absolute file name. All standard file
  113. descriptors are connected to /dev/null. */
  114. static gpg_error_t
  115. spawn_process_detached (const char *pgmname, const char *argv[])
  116. {
  117. #ifdef HAVE_W32_SYSTEM
  118. gpg_error_t err;
  119. SECURITY_ATTRIBUTES sec_attr;
  120. PROCESS_INFORMATION pi =
  121. {
  122. NULL, /* Returns process handle. */
  123. 0, /* Returns primary thread handle. */
  124. 0, /* Returns pid. */
  125. 0 /* Returns tid. */
  126. };
  127. STARTUPINFO si;
  128. int cr_flags;
  129. char *cmdline;
  130. if (access (pgmname, X_OK))
  131. return gpg_error_from_syserror ();
  132. /* Prepare security attributes. */
  133. memset (&sec_attr, 0, sizeof sec_attr );
  134. sec_attr.nLength = sizeof sec_attr;
  135. sec_attr.bInheritHandle = FALSE;
  136. /* Build the command line. */
  137. err = build_w32_commandline (pgmname, argv, &cmdline);
  138. if (err)
  139. return err;
  140. /* Start the process. */
  141. memset (&si, 0, sizeof si);
  142. si.cb = sizeof (si);
  143. si.dwFlags = STARTF_USESHOWWINDOW;
  144. si.wShowWindow = SW_MINIMIZE;
  145. cr_flags = (CREATE_DEFAULT_ERROR_MODE
  146. | GetPriorityClass (GetCurrentProcess ())
  147. | CREATE_NEW_PROCESS_GROUP
  148. | DETACHED_PROCESS);
  149. DEBUG ("CreateProcess(detached), path=`%s' cmdline=`%s'\n",
  150. pgmname, cmdline);
  151. if (!CreateProcess (pgmname, /* Program to start. */
  152. cmdline, /* Command line arguments. */
  153. &sec_attr, /* Process security attributes. */
  154. &sec_attr, /* Thread security attributes. */
  155. FALSE, /* Inherit handles. */
  156. cr_flags, /* Creation flags. */
  157. NULL, /* Environment. */
  158. NULL, /* Use current drive/directory. */
  159. &si, /* Startup information. */
  160. &pi /* Returns process information. */
  161. ))
  162. {
  163. DEBUG ("CreateProcess(detached) failed: %i\n", GetLastError ());
  164. free (cmdline);
  165. return gpg_error (GPG_ERR_GENERAL);
  166. }
  167. free (cmdline);
  168. cmdline = NULL;
  169. DEBUG ("CreateProcess(detached) ready: hProcess=%p hThread=%p"
  170. " dwProcessID=%d dwThreadId=%d\n",
  171. pi.hProcess, pi.hThread,
  172. (int) pi.dwProcessId, (int) pi.dwThreadId);
  173. CloseHandle (pi.hThread);
  174. return 0;
  175. #else
  176. pid_t pid;
  177. int i;
  178. if (getuid() != geteuid())
  179. return gpg_error (GPG_ERR_BUG);
  180. if (access (pgmname, X_OK))
  181. return gpg_error_from_syserror ();
  182. pid = fork ();
  183. if (pid == (pid_t)(-1))
  184. {
  185. DEBUG (_("error forking process: %s\n"), strerror (errno));
  186. return gpg_error_from_syserror ();
  187. }
  188. if (!pid)
  189. {
  190. gcry_control (GCRYCTL_TERM_SECMEM);
  191. if (setsid() == -1 || chdir ("/"))
  192. _exit (1);
  193. pid = fork (); /* Double fork to let init take over the new child. */
  194. if (pid == (pid_t)(-1))
  195. _exit (1);
  196. if (pid)
  197. _exit (0); /* Let the parent exit immediately. */
  198. do_exec (pgmname, argv, -1, -1, -1, NULL);
  199. /*NOTREACHED*/
  200. }
  201. if (waitpid (pid, NULL, 0) == -1)
  202. DEBUG ("waitpid failed in spawn_process_detached: %s", strerror (errno));
  203. return 0;
  204. #endif /* !HAVE_W32_SYSTEM*/
  205. }
  206. /* Establish a connection to a running GPG agent. */
  207. static gpg_error_t
  208. agent_connect (assuan_context_t *ctx_r)
  209. {
  210. /* If we ever failed to connect via a socket we will force the use
  211. of the pipe based server for the lifetime of the process. */
  212. static int force_pipe_server = 0;
  213. gpg_error_t err = 0;
  214. char *infostr;
  215. char *ptr;
  216. restart:
  217. infostr = force_pipe_server ? NULL : getenv ("GPG_AGENT_INFO");
  218. if (!infostr || !*infostr)
  219. {
  220. char *sockname;
  221. /* First check whether we can connect at the standard
  222. socket. */
  223. sockname = make_filename (default_homedir (), "S.gpg-agent", NULL);
  224. if (! sockname)
  225. return gpg_error_from_errno (errno);
  226. err = assuan_socket_connect (ctx_r, sockname, 0);
  227. if (err)
  228. {
  229. const char *agent_program;
  230. /* With no success start a new server. */
  231. DEBUG ("no running GPG agent, starting one");
  232. agent_program = get_gpg_agent_path ();
  233. #ifdef HAVE_W32_SYSTEM
  234. {
  235. /* Under Windows we start the server in daemon mode. This
  236. is because the default is to use the standard socket
  237. and thus there is no need for the GPG_AGENT_INFO
  238. envvar. This is possible as we don't have a real unix
  239. domain socket but use a plain file and thus there is no
  240. need to care about non-local file systems. */
  241. const char *argv[3];
  242. argv[0] = "--daemon";
  243. argv[1] = "--use-standard-socket";
  244. argv[2] = NULL;
  245. err = spawn_process_detached (agent_program, argv);
  246. if (err)
  247. DEBUG ("failed to start agent `%s': %s\n",
  248. agent_program, gpg_strerror (err));
  249. else
  250. {
  251. /* Give the agent some time to prepare itself. */
  252. _sleep (3);
  253. /* Now try again to connect the agent. */
  254. err = assuan_socket_connect (ctx_r, sockname, 0);
  255. }
  256. }
  257. #else /*!HAVE_W32_SYSTEM*/
  258. {
  259. const char *pgmname;
  260. const char *argv[3];
  261. int no_close_list[3];
  262. int i;
  263. if ( !(pgmname = strrchr (agent_program, '/')))
  264. pgmname = agent_program;
  265. else
  266. pgmname++;
  267. argv[0] = pgmname;
  268. argv[1] = "--server";
  269. argv[2] = NULL;
  270. i=0;
  271. no_close_list[i++] = fileno (stderr);
  272. no_close_list[i] = -1;
  273. /* Connect to the agent and perform initial handshaking. */
  274. err = assuan_pipe_connect (ctx_r, agent_program, argv,
  275. no_close_list);
  276. }
  277. #endif /*!HAVE_W32_SYSTEM*/
  278. }
  279. free (sockname);
  280. }
  281. else
  282. {
  283. int pid;
  284. int protocol_version;
  285. infostr = strdup (infostr);
  286. if (!infostr)
  287. return gpg_error_from_errno (errno);
  288. if (!(ptr = strchr (infostr, PATHSEP_C)) || ptr == infostr)
  289. {
  290. DEBUG ("malformed GPG_AGENT_INFO environment variable");
  291. free (infostr);
  292. force_pipe_server = 1;
  293. goto restart;
  294. }
  295. *(ptr++) = 0;
  296. pid = atoi (ptr);
  297. while (*ptr && *ptr != PATHSEP_C)
  298. ptr++;
  299. protocol_version = *ptr ? atoi (ptr + 1) : 0;
  300. if (protocol_version != 1)
  301. {
  302. DEBUG ("GPG agent protocol version '%d' not supported",
  303. protocol_version);
  304. free (infostr);
  305. force_pipe_server = 1;
  306. goto restart;
  307. }
  308. err = assuan_socket_connect (ctx_r, infostr, pid);
  309. free (infostr);
  310. if (err)
  311. {
  312. DEBUG ("cannot connect to GPG agent: %s", gpg_strerror (err));
  313. force_pipe_server = 1;
  314. goto restart;
  315. }
  316. }
  317. if (err)
  318. {
  319. DEBUG ("cannot connect to GPG agent: %s", gpg_strerror (err));
  320. return gpg_error (GPG_ERR_NO_AGENT);
  321. }
  322. return 0;
  323. }
  324. /* Send a simple command to the agent. */
  325. static gpg_error_t
  326. agent_simple_cmd (assuan_context_t ctx, const char *fmt, ...)
  327. {
  328. gpg_error_t err;
  329. char *optstr;
  330. va_list arg;
  331. int res;
  332. va_start (arg, fmt);
  333. res = vasprintf (&optstr, fmt, arg);
  334. va_end (arg);
  335. if (res < 0)
  336. return gpg_error_from_errno (errno);
  337. err = assuan_transact (ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL);
  338. if (err)
  339. DEBUG ("gpg-agent command '%s' failed: %s", optstr, gpg_strerror (err));
  340. free (optstr);
  341. return err;
  342. }
  343. /* Configure the GPG agent at connection CTX. */
  344. static gpg_error_t
  345. agent_configure (assuan_context_t ctx)
  346. {
  347. gpg_error_t err = 0;
  348. char *dft_display = NULL;
  349. char *dft_ttyname = NULL;
  350. char *dft_ttytype = NULL;
  351. #if defined(HAVE_SETLOCALE) && (defined(LC_CTYPE) || defined(LC_MESSAGES))
  352. char *old_lc = NULL;
  353. char *dft_lc = NULL;
  354. #endif
  355. char *dft_xauthority = NULL;
  356. char *dft_pinentry_user_data = NULL;
  357. err = agent_simple_cmd (ctx, "RESET");
  358. if (err)
  359. return err;
  360. /* Set up display, terminal and locale options. */
  361. dft_display = getenv ("DISPLAY");
  362. if (dft_display)
  363. err = agent_simple_cmd (ctx, "OPTION display=%s", dft_display);
  364. if (err)
  365. return err;
  366. dft_ttyname = getenv ("GPG_TTY");
  367. if ((!dft_ttyname || !*dft_ttyname) && ttyname (0))
  368. dft_ttyname = ttyname (0);
  369. if (!dft_ttyname)
  370. return 0;
  371. err = agent_simple_cmd (ctx, "OPTION ttyname=%s", dft_ttyname);
  372. if (err)
  373. return err;
  374. dft_ttytype = getenv ("TERM");
  375. if (dft_ttytype)
  376. err = agent_simple_cmd (ctx, "OPTION ttytype=%s", dft_ttytype);
  377. if (err)
  378. return err;
  379. #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
  380. old_lc = setlocale (LC_CTYPE, NULL);
  381. if (old_lc)
  382. {
  383. old_lc = strdup (old_lc);
  384. if (!old_lc)
  385. return gpg_error_from_errno (errno);
  386. }
  387. dft_lc = setlocale (LC_CTYPE, "");
  388. if (dft_lc)
  389. err = agent_simple_cmd ("OPTION lc-ctype=%s", dft_lc);
  390. if (old_lc)
  391. {
  392. setlocale (LC_CTYPE, old_lc);
  393. free (old_lc);
  394. }
  395. #endif
  396. if (err)
  397. return err;
  398. #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
  399. old_lc = setlocale (LC_MESSAGES, NULL);
  400. if (old_lc)
  401. {
  402. old_lc = strdup (old_lc);
  403. if (!old_lc)
  404. err = gpg_error_from_errno (errno);
  405. }
  406. dft_lc = setlocale (LC_MESSAGES, "");
  407. if (dft_lc)
  408. err = agent_simple_cmd ("OPTION lc-messages=%s", dft_lc);
  409. if (old_lc)
  410. {
  411. setlocale (LC_MESSAGES, old_lc);
  412. free (old_lc);
  413. }
  414. #endif
  415. dft_xauthority = getenv ("XAUTHORITY");
  416. if (dft_xauthority)
  417. err = agent_simple_cmd (ctx, "OPTION xauthority=%s", dft_display);
  418. if (err)
  419. return err;
  420. dft_pinentry_user_data = getenv ("PINENTRY_USER_DATA");
  421. if (dft_pinentry_user_data)
  422. err = agent_simple_cmd (ctx, "OPTION pinentry_user_data=%s", dft_display);
  423. if (err)
  424. return err;
  425. return err;
  426. }
  427. /* Try to connect to the agent via socket. Handle the server's
  428. initial greeting. */
  429. gpg_error_t
  430. scute_agent_initialize (void)
  431. {
  432. gpg_error_t err = 0;
  433. if (agent_ctx)
  434. {
  435. DEBUG ("GPG Agent connection already established");
  436. return 0;
  437. }
  438. err = agent_connect (&agent_ctx);
  439. if (err)
  440. return err;
  441. err = agent_configure (agent_ctx);
  442. if (err)
  443. scute_agent_finalize ();
  444. return err;
  445. }
  446. /* Return a new malloced string by unescaping the string S. Escaping
  447. is percent escaping and '+'/space mapping. A binary nul will
  448. silently be replaced by a 0xFF. Function returns NULL to indicate
  449. an out of memory status. */
  450. static char *
  451. unescape_status_string (const unsigned char *src)
  452. {
  453. char *buffer;
  454. char *dst;
  455. buffer = malloc (strlen (src) + 1);
  456. if (!buffer)
  457. return NULL;
  458. dst = buffer;
  459. while (*src)
  460. {
  461. if (*src == '%' && src[1] && src[2])
  462. {
  463. src++;
  464. *dst = xtoi_2 (src);
  465. if (*dst == '\0')
  466. *dst = '\xff';
  467. dst++;
  468. src += 2;
  469. }
  470. else if (*src == '+')
  471. {
  472. *(dst++) = ' ';
  473. src++;
  474. }
  475. else
  476. *(dst++) = *(src++);
  477. }
  478. *dst = 0;
  479. return buffer;
  480. }
  481. /* Take a 20 byte hexencoded string and put it into the the provided
  482. 20 byte buffer FPR in binary format. Returns true if successful,
  483. and false otherwise. */
  484. static int
  485. unhexify_fpr (const char *hexstr, unsigned char *fpr)
  486. {
  487. const char *src;
  488. int cnt;
  489. /* Check for invalid or wrong length. */
  490. for (src = hexstr, cnt = 0; hexdigitp (src); src++, cnt++)
  491. ;
  492. if ((*src && !spacep (src)) || (cnt != 40))
  493. return 0;
  494. cnt /= 2;
  495. for (src = hexstr, cnt = 0; *src && !spacep (src); src += 2, cnt++)
  496. fpr[cnt] = xtoi_2 (src);
  497. return 1;
  498. }
  499. /* Take the serial number from LINE and return it verbatim in a newly
  500. allocated string. We make sure that only hex characters are
  501. returned. */
  502. static char *
  503. store_serialno (const char *line)
  504. {
  505. const char *src;
  506. char *ptr;
  507. for (src = line; hexdigitp (src); src++)
  508. ;
  509. ptr = malloc (src + 1 - line);
  510. if (ptr)
  511. {
  512. memcpy (ptr, line, src - line);
  513. ptr[src - line] = 0;
  514. }
  515. return ptr;
  516. }
  517. /* Release the card info structure INFO. */
  518. void
  519. scute_agent_release_card_info (struct agent_card_info_s *info)
  520. {
  521. if (!info)
  522. return;
  523. free (info->serialno);
  524. free (info->disp_name);
  525. free (info->disp_lang);
  526. free (info->pubkey_url);
  527. free (info->login_data);
  528. memset (info, 0, sizeof (*info));
  529. }
  530. /* FIXME: We are not returning out of memory errors. */
  531. static gpg_error_t
  532. learn_status_cb (void *opaque, const char *line)
  533. {
  534. struct agent_card_info_s *parm = opaque;
  535. const char *keyword = line;
  536. int keywordlen;
  537. int i;
  538. for (keywordlen = 0; *line && !spacep (line); line++, keywordlen++)
  539. ;
  540. while (spacep (line))
  541. line++;
  542. if (keywordlen == 8 && !memcmp (keyword, "SERIALNO", keywordlen))
  543. {
  544. if (parm->serialno)
  545. free (parm->serialno);
  546. parm->serialno = store_serialno (line);
  547. }
  548. else if (keywordlen == 9 && !memcmp (keyword, "DISP-NAME", keywordlen))
  549. {
  550. if (parm->disp_name)
  551. free (parm->disp_name);
  552. parm->disp_name = unescape_status_string (line);
  553. }
  554. else if (keywordlen == 9 && !memcmp (keyword, "DISP-LANG", keywordlen))
  555. {
  556. if (parm->disp_lang)
  557. free (parm->disp_lang);
  558. parm->disp_lang = unescape_status_string (line);
  559. }
  560. else if (keywordlen == 8 && !memcmp (keyword, "DISP-SEX", keywordlen))
  561. {
  562. parm->disp_sex = *line == '1'? 1 : *line == '2' ? 2: 0;
  563. }
  564. else if (keywordlen == 10 && !memcmp (keyword, "PUBKEY-URL", keywordlen))
  565. {
  566. if (parm->pubkey_url)
  567. free (parm->pubkey_url);
  568. parm->pubkey_url = unescape_status_string (line);
  569. }
  570. else if (keywordlen == 10 && !memcmp (keyword, "LOGIN-DATA", keywordlen))
  571. {
  572. if (parm->login_data)
  573. free (parm->login_data);
  574. parm->login_data = unescape_status_string (line);
  575. }
  576. else if (keywordlen == 11 && !memcmp (keyword, "SIG-COUNTER", keywordlen))
  577. {
  578. parm->sig_counter = strtoul (line, NULL, 0);
  579. }
  580. else if (keywordlen == 10 && !memcmp (keyword, "CHV-STATUS", keywordlen))
  581. {
  582. char *p, *buf;
  583. buf = p = unescape_status_string (line);
  584. if (buf)
  585. {
  586. while (spacep (p))
  587. p++;
  588. parm->chv1_cached = atoi (p);
  589. while (*p && !spacep (p))
  590. p++;
  591. while (spacep (p))
  592. p++;
  593. for (i = 0; *p && i < 3; i++)
  594. {
  595. parm->chvmaxlen[i] = atoi (p);
  596. while (*p && !spacep (p))
  597. p++;
  598. while (spacep (p))
  599. p++;
  600. }
  601. for (i=0; *p && i < 3; i++)
  602. {
  603. parm->chvretry[i] = atoi (p);
  604. while (*p && !spacep (p))
  605. p++;
  606. while (spacep (p))
  607. p++;
  608. }
  609. free (buf);
  610. }
  611. }
  612. else if (keywordlen == 7 && !memcmp (keyword, "KEY-FPR", keywordlen))
  613. {
  614. int no = atoi (line);
  615. while (*line && !spacep (line))
  616. line++;
  617. while (spacep (line))
  618. line++;
  619. if (no == 1)
  620. parm->fpr1valid = unhexify_fpr (line, parm->fpr1);
  621. else if (no == 2)
  622. parm->fpr2valid = unhexify_fpr (line, parm->fpr2);
  623. else if (no == 3)
  624. parm->fpr3valid = unhexify_fpr (line, parm->fpr3);
  625. }
  626. else if (keywordlen == 6 && !memcmp (keyword, "CA-FPR", keywordlen))
  627. {
  628. int no = atoi (line);
  629. while (*line && !spacep (line))
  630. line++;
  631. while (spacep (line))
  632. line++;
  633. if (no == 1)
  634. parm->cafpr1valid = unhexify_fpr (line, parm->cafpr1);
  635. else if (no == 2)
  636. parm->cafpr2valid = unhexify_fpr (line, parm->cafpr2);
  637. else if (no == 3)
  638. parm->cafpr3valid = unhexify_fpr (line, parm->cafpr3);
  639. }
  640. else if (keywordlen == 11 && !memcmp (keyword, "KEYPAIRINFO", keywordlen))
  641. {
  642. const char *grip = line;
  643. while (*line && !spacep (line))
  644. line++;
  645. if (line - grip == 40)
  646. {
  647. while (spacep (line))
  648. line++;
  649. if (!memcmp (line, "OPENPGP.", 8))
  650. {
  651. int no;
  652. line += 8;
  653. no = atoi (line);
  654. if (no == 1)
  655. {
  656. memcpy (parm->grip1, grip, 40);
  657. parm->grip1valid = 1;
  658. }
  659. else if (no == 2)
  660. {
  661. memcpy (parm->grip2, grip, 40);
  662. parm->grip2valid = 1;
  663. }
  664. else if (no == 3)
  665. {
  666. memcpy (parm->grip3, grip, 40);
  667. parm->grip3valid = 1;
  668. }
  669. }
  670. }
  671. }
  672. return 0;
  673. }
  674. /* Call the agent to learn about a smartcard. */
  675. gpg_error_t
  676. scute_agent_learn (struct agent_card_info_s *info)
  677. {
  678. gpg_error_t err;
  679. memset (info, 0, sizeof (*info));
  680. err = assuan_transact (agent_ctx, "LEARN --send",
  681. NULL, NULL, NULL, NULL, learn_status_cb, info);
  682. return err;
  683. }
  684. static gpg_error_t
  685. read_status_cb (void *opaque, const void *buffer, size_t length)
  686. {
  687. char *flag = opaque;
  688. if (length == 0)
  689. *flag = 'r';
  690. else
  691. *flag = *((char *) buffer);
  692. return 0;
  693. }
  694. /* Check the agent status. This returns 0 if a token is present,
  695. GPG_ERR_CARD_REMOVED if no token is present, and an error code
  696. otherwise. */
  697. gpg_error_t
  698. scute_agent_check_status (void)
  699. {
  700. gpg_error_t err;
  701. char flag = '-';
  702. err = assuan_transact (agent_ctx, "SCD GETINFO status",
  703. read_status_cb, &flag, NULL, NULL, NULL, NULL);
  704. if (err)
  705. return err;
  706. if (flag == 'r')
  707. return gpg_error (GPG_ERR_CARD_REMOVED);
  708. return 0;
  709. }
  710. #define MAX_SIGNATURE_LEN 256
  711. struct signature
  712. {
  713. unsigned char data[MAX_SIGNATURE_LEN];
  714. int len;
  715. };
  716. static gpg_error_t
  717. pksign_cb (void *opaque, const void *buffer, size_t length)
  718. {
  719. struct signature *sig = opaque;
  720. if (sig->len + length > MAX_SIGNATURE_LEN)
  721. return gpg_error (GPG_ERR_BAD_DATA);
  722. memcpy (&sig->data[sig->len], buffer, length);
  723. sig->len += length;
  724. return 0;
  725. }
  726. #define SIG_PREFIX "(7:sig-val(3:rsa(1:s128:"
  727. #define SIG_PREFIX_LEN (sizeof (SIG_PREFIX) - 1)
  728. #define SIG_POSTFIX ")))"
  729. #define SIG_POSTFIX_LEN (sizeof (SIG_POSTFIX) - 1)
  730. #define SIG_LEN 128
  731. /* Call the agent to learn about a smartcard. */
  732. gpg_error_t
  733. scute_agent_sign (char *grip, unsigned char *data, int len,
  734. unsigned char *sig_result, unsigned int *sig_len)
  735. {
  736. char cmd[150];
  737. gpg_error_t err;
  738. #define MAX_DATA_LEN 36
  739. unsigned char pretty_data[2 * MAX_DATA_LEN + 1];
  740. int i;
  741. struct signature sig;
  742. sig.len = 0;
  743. if (sig_len == NULL)
  744. return gpg_error (GPG_ERR_INV_ARG);
  745. if (sig_result == NULL)
  746. {
  747. *sig_len = SIG_LEN;
  748. return 0;
  749. }
  750. if (len > MAX_DATA_LEN)
  751. return gpg_error (GPG_ERR_INV_ARG);
  752. if (grip == NULL || sig_result == NULL || *sig_len < SIG_LEN)
  753. return gpg_error (GPG_ERR_INV_ARG);
  754. snprintf (cmd, sizeof (cmd), "SIGKEY %s", grip);
  755. err = assuan_transact (agent_ctx, cmd, NULL, NULL, NULL, NULL, NULL, NULL);
  756. if (err)
  757. return err;
  758. for (i = 0; i < len; i++)
  759. sprintf (&pretty_data[2 * i], "%02X", data[i]);
  760. pretty_data[2 * len] = '\0';
  761. snprintf (cmd, sizeof (cmd), "sethash --hash=tls-md5sha1 %s", pretty_data);
  762. err = assuan_transact (agent_ctx, cmd, NULL, NULL, NULL, NULL, NULL, NULL);
  763. if (err)
  764. return err;
  765. err = assuan_transact (agent_ctx, "PKSIGN",
  766. pksign_cb, &sig, NULL, NULL, NULL, NULL);
  767. if (err)
  768. return err;
  769. if (sig.len != SIG_PREFIX_LEN + SIG_LEN + SIG_POSTFIX_LEN)
  770. return gpg_error (GPG_ERR_BAD_SIGNATURE);
  771. if (memcmp (sig.data, SIG_PREFIX, SIG_PREFIX_LEN))
  772. return gpg_error (GPG_ERR_BAD_SIGNATURE);
  773. if (memcmp (sig.data + sig.len - SIG_POSTFIX_LEN,
  774. SIG_POSTFIX, SIG_POSTFIX_LEN))
  775. return gpg_error (GPG_ERR_BAD_SIGNATURE);
  776. memcpy (sig_result, sig.data + SIG_PREFIX_LEN, SIG_LEN);
  777. *sig_len = SIG_LEN;
  778. return 0;
  779. }
  780. /* Determine if FPR is trusted. */
  781. gpg_error_t scute_agent_is_trusted (char *fpr, bool *is_trusted)
  782. {
  783. gpg_error_t err;
  784. bool trusted = false;
  785. char cmd[150];
  786. snprintf (cmd, sizeof (cmd), "ISTRUSTED %s", fpr);
  787. err = assuan_transact (agent_ctx, cmd, NULL, NULL, NULL, NULL, NULL, NULL);
  788. if (err && gpg_err_code (err) != GPG_ERR_NOT_TRUSTED)
  789. return err;
  790. else if (!err)
  791. trusted = true;
  792. *is_trusted = trusted;
  793. return 0;
  794. }
  795. void
  796. scute_agent_finalize (void)
  797. {
  798. if (!agent_ctx)
  799. {
  800. DEBUG ("no GPG Agent connection established");
  801. return;
  802. }
  803. assuan_disconnect (agent_ctx);
  804. agent_ctx = NULL;
  805. }