agent.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. #ifdef HAVE_W32_SYSTEM
  34. #define PATHSEP_C ';'
  35. #define WINVER 0x0500 /* Required for AllowSetForegroundWindow. */
  36. #include <windows.h>
  37. #else
  38. #define PATHSEP_C ':'
  39. #endif
  40. #include <assuan.h>
  41. #include <gpg-error.h>
  42. #include "debug.h"
  43. #include "support.h"
  44. #include "cert.h"
  45. #include "agent.h"
  46. /* The global agent context. */
  47. static assuan_context_t agent_ctx = NULL;
  48. /* Hack required for Windows. */
  49. void
  50. gnupg_allow_set_foregound_window (pid_t pid)
  51. {
  52. if (!pid || pid == (pid_t)(-1))
  53. return;
  54. #ifdef HAVE_W32_SYSTEM
  55. else if (!AllowSetForegroundWindow (pid))
  56. DEBUG (DBG_CRIT, "AllowSetForegroundWindow(%lu) failed: %i\n",
  57. (unsigned long)pid, GetLastError ());
  58. #endif
  59. }
  60. #ifdef HAVE_W32_SYSTEM
  61. /* Helper function to build_w32_commandline. */
  62. static char *
  63. build_w32_commandline_copy (char *buffer, const char *string)
  64. {
  65. char *p = buffer;
  66. const char *s;
  67. if (!*string) /* Empty string. */
  68. p = stpcpy (p, "\"\"");
  69. else if (strpbrk (string, " \t\n\v\f\""))
  70. {
  71. /* Need top do some kind of quoting. */
  72. p = stpcpy (p, "\"");
  73. for (s=string; *s; s++)
  74. {
  75. *p++ = *s;
  76. if (*s == '\"')
  77. *p++ = *s;
  78. }
  79. *p++ = '\"';
  80. *p = 0;
  81. }
  82. else
  83. p = stpcpy (p, string);
  84. return p;
  85. }
  86. /* Build a command line for use with W32's CreateProcess. On success
  87. CMDLINE gets the address of a newly allocated string. */
  88. static gpg_error_t
  89. build_w32_commandline (const char *pgmname, const char * const *argv,
  90. char **cmdline)
  91. {
  92. int i, n;
  93. const char *s;
  94. char *buf, *p;
  95. *cmdline = NULL;
  96. n = 0;
  97. s = pgmname;
  98. n += strlen (s) + 1 + 2; /* (1 space, 2 quoting */
  99. for (; *s; s++)
  100. if (*s == '\"')
  101. n++; /* Need to double inner quotes. */
  102. for (i=0; (s=argv[i]); i++)
  103. {
  104. n += strlen (s) + 1 + 2; /* (1 space, 2 quoting */
  105. for (; *s; s++)
  106. if (*s == '\"')
  107. n++; /* Need to double inner quotes. */
  108. }
  109. n++;
  110. buf = p = malloc (n);
  111. if (!buf)
  112. return gpg_error_from_syserror ();
  113. p = build_w32_commandline_copy (p, pgmname);
  114. for (i=0; argv[i]; i++)
  115. {
  116. *p++ = ' ';
  117. p = build_w32_commandline_copy (p, argv[i]);
  118. }
  119. *cmdline= buf;
  120. return 0;
  121. }
  122. /* Spawn a new process and immediatley detach from it. The name of
  123. the program to exec is PGMNAME and its arguments are in ARGV (the
  124. programname is automatically passed as first argument). An error
  125. is returned if pgmname is not executable; to make this work it is
  126. necessary to provide an absolute file name. All standard file
  127. descriptors are connected to /dev/null. */
  128. static gpg_error_t
  129. spawn_process_detached (const char *pgmname, const char *argv[])
  130. {
  131. gpg_error_t err;
  132. SECURITY_ATTRIBUTES sec_attr;
  133. PROCESS_INFORMATION pi =
  134. {
  135. NULL, /* Returns process handle. */
  136. 0, /* Returns primary thread handle. */
  137. 0, /* Returns pid. */
  138. 0 /* Returns tid. */
  139. };
  140. STARTUPINFO si;
  141. int cr_flags;
  142. char *cmdline;
  143. if (access (pgmname, X_OK))
  144. return gpg_error_from_syserror ();
  145. /* Prepare security attributes. */
  146. memset (&sec_attr, 0, sizeof sec_attr );
  147. sec_attr.nLength = sizeof sec_attr;
  148. sec_attr.bInheritHandle = FALSE;
  149. /* Build the command line. */
  150. err = build_w32_commandline (pgmname, argv, &cmdline);
  151. if (err)
  152. return err;
  153. /* Start the process. */
  154. memset (&si, 0, sizeof si);
  155. si.cb = sizeof (si);
  156. si.dwFlags = STARTF_USESHOWWINDOW;
  157. si.wShowWindow = SW_MINIMIZE;
  158. cr_flags = (CREATE_DEFAULT_ERROR_MODE
  159. | GetPriorityClass (GetCurrentProcess ())
  160. | CREATE_NEW_PROCESS_GROUP
  161. | DETACHED_PROCESS);
  162. DEBUG (DBG_INFO, "CreateProcess(detached), path=`%s' cmdline=`%s'\n",
  163. pgmname, cmdline);
  164. if (!CreateProcess (pgmname, /* Program to start. */
  165. cmdline, /* Command line arguments. */
  166. &sec_attr, /* Process security attributes. */
  167. &sec_attr, /* Thread security attributes. */
  168. FALSE, /* Inherit handles. */
  169. cr_flags, /* Creation flags. */
  170. NULL, /* Environment. */
  171. NULL, /* Use current drive/directory. */
  172. &si, /* Startup information. */
  173. &pi /* Returns process information. */
  174. ))
  175. {
  176. DEBUG (DBG_CRIT, "CreateProcess(detached) failed: %i\n",
  177. GetLastError ());
  178. free (cmdline);
  179. return gpg_error (GPG_ERR_GENERAL);
  180. }
  181. free (cmdline);
  182. cmdline = NULL;
  183. DEBUG (DBG_INFO, "CreateProcess(detached) ready: hProcess=%p hThread=%p"
  184. " dwProcessID=%d dwThreadId=%d\n", pi.hProcess, pi.hThread,
  185. (int) pi.dwProcessId, (int) pi.dwThreadId);
  186. CloseHandle (pi.hThread);
  187. return 0;
  188. }
  189. #endif
  190. /* Establish a connection to a running GPG agent. */
  191. static gpg_error_t
  192. agent_connect (assuan_context_t *ctx_r)
  193. {
  194. /* If we ever failed to connect via a socket we will force the use
  195. of the pipe based server for the lifetime of the process. */
  196. static int force_pipe_server = 0;
  197. gpg_error_t err = 0;
  198. char *infostr;
  199. char *ptr;
  200. restart:
  201. infostr = force_pipe_server ? NULL : getenv ("GPG_AGENT_INFO");
  202. if (!infostr || !*infostr)
  203. {
  204. char *sockname;
  205. /* First check whether we can connect at the standard
  206. socket. */
  207. sockname = make_filename (default_homedir (), "S.gpg-agent", NULL);
  208. if (! sockname)
  209. return gpg_error_from_errno (errno);
  210. err = assuan_socket_connect (ctx_r, sockname, 0);
  211. if (err)
  212. {
  213. const char *agent_program;
  214. /* With no success start a new server. */
  215. DEBUG (DBG_INFO, "no running GPG agent at %s, starting one\n",
  216. sockname);
  217. agent_program = get_gpg_agent_path ();
  218. #ifdef HAVE_W32_SYSTEM
  219. {
  220. /* Under Windows we start the server in daemon mode. This
  221. is because the default is to use the standard socket
  222. and thus there is no need for the GPG_AGENT_INFO
  223. envvar. This is possible as we don't have a real unix
  224. domain socket but use a plain file and thus there is no
  225. need to care about non-local file systems. */
  226. const char *argv[3];
  227. argv[0] = "--daemon";
  228. argv[1] = "--use-standard-socket";
  229. argv[2] = NULL;
  230. err = spawn_process_detached (agent_program, argv);
  231. if (err)
  232. DEBUG (DBG_CRIT, "failed to start agent `%s': %s\n",
  233. agent_program, gpg_strerror (err));
  234. else
  235. {
  236. /* Give the agent some time to prepare itself. */
  237. Sleep (3 * 1000);
  238. /* Now try again to connect the agent. */
  239. err = assuan_socket_connect (ctx_r, sockname, 0);
  240. }
  241. }
  242. #else /*!HAVE_W32_SYSTEM*/
  243. {
  244. const char *pgmname;
  245. const char *argv[3];
  246. int no_close_list[3];
  247. int i;
  248. if ( !(pgmname = strrchr (agent_program, '/')))
  249. pgmname = agent_program;
  250. else
  251. pgmname++;
  252. argv[0] = pgmname;
  253. argv[1] = "--server";
  254. argv[2] = NULL;
  255. i=0;
  256. no_close_list[i++] = fileno (stderr);
  257. no_close_list[i] = -1;
  258. /* Connect to the agent and perform initial handshaking. */
  259. err = assuan_pipe_connect (ctx_r, agent_program, argv,
  260. no_close_list);
  261. }
  262. #endif /*!HAVE_W32_SYSTEM*/
  263. }
  264. free (sockname);
  265. }
  266. else
  267. {
  268. int pid;
  269. int protocol_version;
  270. infostr = strdup (infostr);
  271. if (!infostr)
  272. return gpg_error_from_errno (errno);
  273. if (!(ptr = strchr (infostr, PATHSEP_C)) || ptr == infostr)
  274. {
  275. DEBUG (DBG_CRIT, "malformed GPG_AGENT_INFO environment variable");
  276. free (infostr);
  277. force_pipe_server = 1;
  278. goto restart;
  279. }
  280. *(ptr++) = 0;
  281. pid = atoi (ptr);
  282. while (*ptr && *ptr != PATHSEP_C)
  283. ptr++;
  284. protocol_version = *ptr ? atoi (ptr + 1) : 0;
  285. if (protocol_version != 1)
  286. {
  287. DEBUG (DBG_CRIT, "GPG agent protocol version '%d' not supported",
  288. protocol_version);
  289. free (infostr);
  290. force_pipe_server = 1;
  291. goto restart;
  292. }
  293. err = assuan_socket_connect (ctx_r, infostr, pid);
  294. free (infostr);
  295. if (err)
  296. {
  297. DEBUG (DBG_CRIT, "cannot connect to GPG agent: %s", gpg_strerror (err));
  298. force_pipe_server = 1;
  299. goto restart;
  300. }
  301. }
  302. if (err)
  303. {
  304. DEBUG (DBG_CRIT, "cannot connect to GPG agent: %s", gpg_strerror (err));
  305. return gpg_error (GPG_ERR_NO_AGENT);
  306. }
  307. if (_scute_debug_flags & DBG_ASSUAN)
  308. assuan_set_log_stream (*ctx_r, _scute_debug_stream);
  309. return 0;
  310. }
  311. /* This is the default inquiry callback. It mainly handles the
  312. Pinentry notifications. */
  313. static gpg_error_t
  314. default_inq_cb (void *opaque, const char *line)
  315. {
  316. (void)opaque;
  317. if (!strncmp (line, "PINENTRY_LAUNCHED", 17) && (line[17]==' '||!line[17]))
  318. {
  319. gnupg_allow_set_foregound_window ((pid_t)strtoul (line+17, NULL, 10));
  320. /* We do not pass errors to avoid breaking other code. */
  321. }
  322. else
  323. DEBUG (DBG_CRIT, "ignoring gpg-agent inquiry `%s'\n", line);
  324. return 0;
  325. }
  326. /* Send a simple command to the agent. */
  327. static gpg_error_t
  328. agent_simple_cmd (assuan_context_t ctx, const char *fmt, ...)
  329. {
  330. gpg_error_t err;
  331. char *optstr;
  332. va_list arg;
  333. int res;
  334. va_start (arg, fmt);
  335. res = vasprintf (&optstr, fmt, arg);
  336. va_end (arg);
  337. if (res < 0)
  338. return gpg_error_from_errno (errno);
  339. err = assuan_transact (ctx, optstr, NULL, NULL, default_inq_cb,
  340. NULL, NULL, NULL);
  341. if (err)
  342. DEBUG (DBG_CRIT, "gpg-agent command '%s' failed: %s", optstr,
  343. gpg_strerror (err));
  344. free (optstr);
  345. return err;
  346. }
  347. /* Configure the GPG agent at connection CTX. */
  348. static gpg_error_t
  349. agent_configure (assuan_context_t ctx)
  350. {
  351. gpg_error_t err = 0;
  352. char *dft_display = NULL;
  353. char *dft_ttyname = NULL;
  354. char *dft_ttytype = NULL;
  355. #if defined(HAVE_SETLOCALE) && (defined(LC_CTYPE) || defined(LC_MESSAGES))
  356. char *old_lc = NULL;
  357. char *dft_lc = NULL;
  358. #endif
  359. char *dft_xauthority = NULL;
  360. char *dft_pinentry_user_data = NULL;
  361. err = agent_simple_cmd (ctx, "RESET");
  362. if (err)
  363. return err;
  364. /* Set up display, terminal and locale options. */
  365. dft_display = getenv ("DISPLAY");
  366. if (dft_display)
  367. err = agent_simple_cmd (ctx, "OPTION display=%s", dft_display);
  368. if (err)
  369. return err;
  370. dft_ttyname = getenv ("GPG_TTY");
  371. if ((!dft_ttyname || !*dft_ttyname) && ttyname (0))
  372. dft_ttyname = ttyname (0);
  373. if (dft_ttyname)
  374. {
  375. err = agent_simple_cmd (ctx, "OPTION ttyname=%s", dft_ttyname);
  376. if (err)
  377. return err;
  378. }
  379. dft_ttytype = getenv ("TERM");
  380. if (dft_ttytype)
  381. err = agent_simple_cmd (ctx, "OPTION ttytype=%s", dft_ttytype);
  382. if (err)
  383. return err;
  384. #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
  385. old_lc = setlocale (LC_CTYPE, NULL);
  386. if (old_lc)
  387. {
  388. old_lc = strdup (old_lc);
  389. if (!old_lc)
  390. return gpg_error_from_errno (errno);
  391. }
  392. dft_lc = setlocale (LC_CTYPE, "");
  393. if (dft_lc)
  394. err = agent_simple_cmd ("OPTION lc-ctype=%s", dft_lc);
  395. if (old_lc)
  396. {
  397. setlocale (LC_CTYPE, old_lc);
  398. free (old_lc);
  399. }
  400. #endif
  401. if (err)
  402. return err;
  403. #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
  404. old_lc = setlocale (LC_MESSAGES, NULL);
  405. if (old_lc)
  406. {
  407. old_lc = strdup (old_lc);
  408. if (!old_lc)
  409. err = gpg_error_from_errno (errno);
  410. }
  411. dft_lc = setlocale (LC_MESSAGES, "");
  412. if (dft_lc)
  413. err = agent_simple_cmd ("OPTION lc-messages=%s", dft_lc);
  414. if (old_lc)
  415. {
  416. setlocale (LC_MESSAGES, old_lc);
  417. free (old_lc);
  418. }
  419. #endif
  420. dft_xauthority = getenv ("XAUTHORITY");
  421. if (dft_xauthority)
  422. err = agent_simple_cmd (ctx, "OPTION xauthority=%s", dft_xauthority);
  423. if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION)
  424. err = 0;
  425. else if (err)
  426. return err;
  427. dft_pinentry_user_data = getenv ("PINENTRY_USER_DATA");
  428. if (dft_pinentry_user_data)
  429. err = agent_simple_cmd (ctx, "OPTION pinentry_user_data=%s",
  430. dft_pinentry_user_data);
  431. if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION)
  432. err = 0;
  433. else if (err)
  434. return err;
  435. err = agent_simple_cmd (ctx, "OPTION allow-pinentry-notify");
  436. if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION)
  437. err = 0;
  438. else if (err)
  439. return err;
  440. return err;
  441. }
  442. /* Try to connect to the agent via socket. Handle the server's
  443. initial greeting. */
  444. gpg_error_t
  445. scute_agent_initialize (void)
  446. {
  447. gpg_error_t err = 0;
  448. if (agent_ctx)
  449. {
  450. DEBUG (DBG_CRIT, "GPG Agent connection already established");
  451. return 0;
  452. }
  453. DEBUG (DBG_INFO, "Establishing connection to gpg-agent");
  454. err = agent_connect (&agent_ctx);
  455. if (err)
  456. return err;
  457. err = agent_configure (agent_ctx);
  458. if (err)
  459. scute_agent_finalize ();
  460. return err;
  461. }
  462. /* Return a new malloced string by unescaping the string S. Escaping
  463. is percent escaping and '+'/space mapping. A binary nul will
  464. silently be replaced by a 0xFF. Function returns NULL to indicate
  465. an out of memory status. */
  466. static char *
  467. unescape_status_string (const unsigned char *src)
  468. {
  469. char *buffer;
  470. char *dst;
  471. buffer = malloc (strlen (src) + 1);
  472. if (!buffer)
  473. return NULL;
  474. dst = buffer;
  475. while (*src)
  476. {
  477. if (*src == '%' && src[1] && src[2])
  478. {
  479. src++;
  480. *dst = xtoi_2 (src);
  481. if (*dst == '\0')
  482. *dst = '\xff';
  483. dst++;
  484. src += 2;
  485. }
  486. else if (*src == '+')
  487. {
  488. *(dst++) = ' ';
  489. src++;
  490. }
  491. else
  492. *(dst++) = *(src++);
  493. }
  494. *dst = 0;
  495. return buffer;
  496. }
  497. /* Take a 20 byte hexencoded string and put it into the the provided
  498. 20 byte buffer FPR in binary format. Returns true if successful,
  499. and false otherwise. */
  500. static int
  501. unhexify_fpr (const char *hexstr, unsigned char *fpr)
  502. {
  503. const char *src;
  504. int cnt;
  505. /* Check for invalid or wrong length. */
  506. for (src = hexstr, cnt = 0; hexdigitp (src); src++, cnt++)
  507. ;
  508. if ((*src && !spacep (src)) || (cnt != 40))
  509. return 0;
  510. cnt /= 2;
  511. for (src = hexstr, cnt = 0; *src && !spacep (src); src += 2, cnt++)
  512. fpr[cnt] = xtoi_2 (src);
  513. return 1;
  514. }
  515. /* Take the serial number from LINE and return it verbatim in a newly
  516. allocated string. We make sure that only hex characters are
  517. returned. */
  518. static char *
  519. store_serialno (const char *line)
  520. {
  521. const char *src;
  522. char *ptr;
  523. for (src = line; hexdigitp (src); src++)
  524. ;
  525. ptr = malloc (src + 1 - line);
  526. if (ptr)
  527. {
  528. memcpy (ptr, line, src - line);
  529. ptr[src - line] = 0;
  530. }
  531. return ptr;
  532. }
  533. /* Release the card info structure INFO. */
  534. void
  535. scute_agent_release_card_info (struct agent_card_info_s *info)
  536. {
  537. if (!info)
  538. return;
  539. free (info->serialno);
  540. free (info->disp_name);
  541. free (info->disp_lang);
  542. free (info->pubkey_url);
  543. free (info->login_data);
  544. memset (info, 0, sizeof (*info));
  545. }
  546. /* FIXME: We are not returning out of memory errors. */
  547. static gpg_error_t
  548. learn_status_cb (void *opaque, const char *line)
  549. {
  550. struct agent_card_info_s *parm = opaque;
  551. const char *keyword = line;
  552. int keywordlen;
  553. int i;
  554. for (keywordlen = 0; *line && !spacep (line); line++, keywordlen++)
  555. ;
  556. while (spacep (line))
  557. line++;
  558. if (keywordlen == 8 && !memcmp (keyword, "SERIALNO", keywordlen))
  559. {
  560. if (parm->serialno)
  561. free (parm->serialno);
  562. parm->serialno = store_serialno (line);
  563. }
  564. else if (keywordlen == 9 && !memcmp (keyword, "DISP-NAME", keywordlen))
  565. {
  566. if (parm->disp_name)
  567. free (parm->disp_name);
  568. parm->disp_name = unescape_status_string (line);
  569. }
  570. else if (keywordlen == 9 && !memcmp (keyword, "DISP-LANG", keywordlen))
  571. {
  572. if (parm->disp_lang)
  573. free (parm->disp_lang);
  574. parm->disp_lang = unescape_status_string (line);
  575. }
  576. else if (keywordlen == 8 && !memcmp (keyword, "DISP-SEX", keywordlen))
  577. {
  578. parm->disp_sex = *line == '1'? 1 : *line == '2' ? 2: 0;
  579. }
  580. else if (keywordlen == 10 && !memcmp (keyword, "PUBKEY-URL", keywordlen))
  581. {
  582. if (parm->pubkey_url)
  583. free (parm->pubkey_url);
  584. parm->pubkey_url = unescape_status_string (line);
  585. }
  586. else if (keywordlen == 10 && !memcmp (keyword, "LOGIN-DATA", keywordlen))
  587. {
  588. if (parm->login_data)
  589. free (parm->login_data);
  590. parm->login_data = unescape_status_string (line);
  591. }
  592. else if (keywordlen == 11 && !memcmp (keyword, "SIG-COUNTER", keywordlen))
  593. {
  594. parm->sig_counter = strtoul (line, NULL, 0);
  595. }
  596. else if (keywordlen == 10 && !memcmp (keyword, "CHV-STATUS", keywordlen))
  597. {
  598. char *p, *buf;
  599. buf = p = unescape_status_string (line);
  600. if (buf)
  601. {
  602. while (spacep (p))
  603. p++;
  604. parm->chv1_cached = atoi (p);
  605. while (*p && !spacep (p))
  606. p++;
  607. while (spacep (p))
  608. p++;
  609. for (i = 0; *p && i < 3; i++)
  610. {
  611. parm->chvmaxlen[i] = atoi (p);
  612. while (*p && !spacep (p))
  613. p++;
  614. while (spacep (p))
  615. p++;
  616. }
  617. for (i=0; *p && i < 3; i++)
  618. {
  619. parm->chvretry[i] = atoi (p);
  620. while (*p && !spacep (p))
  621. p++;
  622. while (spacep (p))
  623. p++;
  624. }
  625. free (buf);
  626. }
  627. }
  628. else if (keywordlen == 7 && !memcmp (keyword, "KEY-FPR", keywordlen))
  629. {
  630. int no = atoi (line);
  631. while (*line && !spacep (line))
  632. line++;
  633. while (spacep (line))
  634. line++;
  635. if (no == 1)
  636. parm->fpr1valid = unhexify_fpr (line, parm->fpr1);
  637. else if (no == 2)
  638. parm->fpr2valid = unhexify_fpr (line, parm->fpr2);
  639. else if (no == 3)
  640. parm->fpr3valid = unhexify_fpr (line, parm->fpr3);
  641. }
  642. else if (keywordlen == 6 && !memcmp (keyword, "CA-FPR", keywordlen))
  643. {
  644. int no = atoi (line);
  645. while (*line && !spacep (line))
  646. line++;
  647. while (spacep (line))
  648. line++;
  649. if (no == 1)
  650. parm->cafpr1valid = unhexify_fpr (line, parm->cafpr1);
  651. else if (no == 2)
  652. parm->cafpr2valid = unhexify_fpr (line, parm->cafpr2);
  653. else if (no == 3)
  654. parm->cafpr3valid = unhexify_fpr (line, parm->cafpr3);
  655. }
  656. else if (keywordlen == 11 && !memcmp (keyword, "KEYPAIRINFO", keywordlen))
  657. {
  658. const char *grip = line;
  659. while (*line && !spacep (line))
  660. line++;
  661. if (line - grip == 40)
  662. {
  663. while (spacep (line))
  664. line++;
  665. if (!memcmp (line, "OPENPGP.", 8))
  666. {
  667. int no;
  668. line += 8;
  669. no = atoi (line);
  670. if (no == 1)
  671. {
  672. memcpy (parm->grip1, grip, 40);
  673. parm->grip1valid = 1;
  674. }
  675. else if (no == 2)
  676. {
  677. memcpy (parm->grip2, grip, 40);
  678. parm->grip2valid = 1;
  679. }
  680. else if (no == 3)
  681. {
  682. memcpy (parm->grip3, grip, 40);
  683. parm->grip3valid = 1;
  684. }
  685. }
  686. }
  687. }
  688. return 0;
  689. }
  690. /* Call the agent to learn about a smartcard. */
  691. gpg_error_t
  692. scute_agent_learn (struct agent_card_info_s *info)
  693. {
  694. gpg_error_t err;
  695. memset (info, 0, sizeof (*info));
  696. err = assuan_transact (agent_ctx, "LEARN --send",
  697. NULL, NULL, default_inq_cb,
  698. NULL, learn_status_cb, info);
  699. return err;
  700. }
  701. static gpg_error_t
  702. read_status_cb (void *opaque, const void *buffer, size_t length)
  703. {
  704. char *flag = opaque;
  705. if (length == 0)
  706. *flag = 'r';
  707. else
  708. *flag = *((char *) buffer);
  709. return 0;
  710. }
  711. /* Check the agent status. This returns 0 if a token is present,
  712. GPG_ERR_CARD_REMOVED if no token is present, and an error code
  713. otherwise. */
  714. gpg_error_t
  715. scute_agent_check_status (void)
  716. {
  717. gpg_error_t err;
  718. char flag = '-';
  719. err = assuan_transact (agent_ctx, "SCD GETINFO status",
  720. read_status_cb, &flag, default_inq_cb,
  721. NULL, NULL, NULL);
  722. if (err)
  723. return err;
  724. if (flag == 'r')
  725. return gpg_error (GPG_ERR_CARD_REMOVED);
  726. return 0;
  727. }
  728. /* Enough space to hold a 2048 bit RSA signature in an S-expression. */
  729. #define MAX_SIGNATURE_LEN 350
  730. struct signature
  731. {
  732. unsigned char data[MAX_SIGNATURE_LEN];
  733. int len;
  734. };
  735. static gpg_error_t
  736. pksign_cb (void *opaque, const void *buffer, size_t length)
  737. {
  738. struct signature *sig = opaque;
  739. if (sig->len + length > MAX_SIGNATURE_LEN)
  740. {
  741. DEBUG (DBG_INFO, "maximum signature length exceeded");
  742. return gpg_error (GPG_ERR_BAD_DATA);
  743. }
  744. memcpy (&sig->data[sig->len], buffer, length);
  745. sig->len += length;
  746. return 0;
  747. }
  748. #define SIG_PREFIX "(7:sig-val(3:rsa(1:s128:"
  749. #define SIG_PREFIX_2 "(7:sig-val(3:rsa(1:s256:"
  750. #define SIG_PREFIX_LEN (sizeof (SIG_PREFIX) - 1)
  751. #define SIG_POSTFIX ")))"
  752. #define SIG_POSTFIX_LEN (sizeof (SIG_POSTFIX) - 1)
  753. #define SIG_LEN 128
  754. #define SIG_LEN_2 128
  755. /* Call the agent to learn about a smartcard. */
  756. gpg_error_t
  757. scute_agent_sign (char *grip, unsigned char *data, int len,
  758. unsigned char *sig_result, unsigned int *sig_len)
  759. {
  760. char cmd[150];
  761. gpg_error_t err;
  762. #define MAX_DATA_LEN 36
  763. unsigned char pretty_data[2 * MAX_DATA_LEN + 1];
  764. int i;
  765. struct signature sig;
  766. sig.len = 0;
  767. if (sig_len == NULL)
  768. return gpg_error (GPG_ERR_INV_ARG);
  769. if (sig_result == NULL)
  770. {
  771. /* FIXME: We return the largest supported size - is that correct? */
  772. *sig_len = SIG_LEN_2;
  773. return 0;
  774. }
  775. if (len > MAX_DATA_LEN)
  776. return gpg_error (GPG_ERR_INV_ARG);
  777. if (grip == NULL || sig_result == NULL || *sig_len < SIG_LEN)
  778. return gpg_error (GPG_ERR_INV_ARG);
  779. snprintf (cmd, sizeof (cmd), "SIGKEY %s", grip);
  780. err = assuan_transact (agent_ctx, cmd, NULL, NULL, default_inq_cb,
  781. NULL, NULL, NULL);
  782. if (err)
  783. return err;
  784. for (i = 0; i < len; i++)
  785. snprintf (&pretty_data[2 * i], 3, "%02X", data[i]);
  786. pretty_data[2 * len] = '\0';
  787. snprintf (cmd, sizeof (cmd), "SETHASH --hash=tls-md5sha1 %s", pretty_data);
  788. err = assuan_transact (agent_ctx, cmd, NULL, NULL, default_inq_cb,
  789. NULL, NULL, NULL);
  790. if (err)
  791. return err;
  792. err = assuan_transact (agent_ctx, "PKSIGN",
  793. pksign_cb, &sig, default_inq_cb, NULL, NULL, NULL);
  794. if (err)
  795. return err;
  796. /* FIXME: we need a real parser to cope with all kind of S-expressions. */
  797. if (sig.len == SIG_PREFIX_LEN + SIG_LEN_2 + SIG_POSTFIX_LEN)
  798. {
  799. if (memcmp (sig.data, SIG_PREFIX_2, SIG_PREFIX_LEN))
  800. return gpg_error (GPG_ERR_BAD_SIGNATURE);
  801. if (memcmp (sig.data + sig.len - SIG_POSTFIX_LEN,
  802. SIG_POSTFIX, SIG_POSTFIX_LEN))
  803. return gpg_error (GPG_ERR_BAD_SIGNATURE);
  804. memcpy (sig_result, sig.data + SIG_PREFIX_LEN, SIG_LEN_2);
  805. *sig_len = SIG_LEN_2;
  806. }
  807. else
  808. {
  809. if (sig.len != SIG_PREFIX_LEN + SIG_LEN + SIG_POSTFIX_LEN)
  810. return gpg_error (GPG_ERR_BAD_SIGNATURE);
  811. if (memcmp (sig.data, SIG_PREFIX, SIG_PREFIX_LEN))
  812. return gpg_error (GPG_ERR_BAD_SIGNATURE);
  813. if (memcmp (sig.data + sig.len - SIG_POSTFIX_LEN,
  814. SIG_POSTFIX, SIG_POSTFIX_LEN))
  815. return gpg_error (GPG_ERR_BAD_SIGNATURE);
  816. memcpy (sig_result, sig.data + SIG_PREFIX_LEN, SIG_LEN);
  817. *sig_len = SIG_LEN;
  818. }
  819. return 0;
  820. }
  821. /* Determine if FPR is trusted. */
  822. gpg_error_t
  823. scute_agent_is_trusted (char *fpr, bool *is_trusted)
  824. {
  825. gpg_error_t err;
  826. bool trusted = false;
  827. char cmd[150];
  828. snprintf (cmd, sizeof (cmd), "ISTRUSTED %s", fpr);
  829. err = assuan_transact (agent_ctx, cmd, NULL, NULL, default_inq_cb,
  830. NULL, NULL, NULL);
  831. if (err && gpg_err_code (err) != GPG_ERR_NOT_TRUSTED)
  832. return err;
  833. else if (!err)
  834. trusted = true;
  835. *is_trusted = trusted;
  836. return 0;
  837. }
  838. #define GET_CERT_INIT_SIZE 2048
  839. struct get_cert_s
  840. {
  841. unsigned char *cert_der;
  842. int cert_der_len;
  843. int cert_der_size;
  844. };
  845. gpg_error_t
  846. get_cert_data_cb (void *opaque, const void *data, size_t data_len)
  847. {
  848. struct get_cert_s *cert_s = opaque;
  849. gpg_error_t err;
  850. int needed_size;
  851. needed_size = cert_s->cert_der_len + data_len;
  852. if (needed_size > cert_s->cert_der_size)
  853. {
  854. unsigned char *new_cert_der;
  855. int new_cert_der_size = cert_s->cert_der_size;
  856. if (new_cert_der_size == 0)
  857. new_cert_der_size = GET_CERT_INIT_SIZE;
  858. while (new_cert_der_size < needed_size)
  859. new_cert_der_size *= 2;
  860. if (cert_s->cert_der == NULL)
  861. new_cert_der = malloc (new_cert_der_size);
  862. else
  863. new_cert_der = realloc (cert_s->cert_der, new_cert_der_size);
  864. if (new_cert_der == NULL)
  865. return gpg_error_from_syserror ();
  866. cert_s->cert_der = new_cert_der;
  867. cert_s->cert_der_size = new_cert_der_size;
  868. }
  869. memcpy (cert_s->cert_der + cert_s->cert_der_len, data, data_len);
  870. cert_s->cert_der_len += data_len;
  871. return 0;
  872. }
  873. /* Try to get certificate for key numer NO. */
  874. gpg_error_t
  875. scute_agent_get_cert (int no, struct cert *cert)
  876. {
  877. gpg_error_t err;
  878. char cmd[150];
  879. struct get_cert_s cert_s;
  880. cert_s.cert_der = NULL;
  881. cert_s.cert_der_len = 0;
  882. cert_s.cert_der_size = 0;
  883. snprintf (cmd, sizeof (cmd), "SCD READCERT OPENPGP.%i", no);
  884. err = assuan_transact (agent_ctx, cmd, get_cert_data_cb, &cert_s,
  885. NULL, NULL, NULL, NULL);
  886. /* Just to be safe... */
  887. if (!err && cert_s.cert_der_len <= 16)
  888. {
  889. DEBUG (DBG_INFO, "bad card certificate rejected");
  890. err = gpg_error (GPG_ERR_BAD_CERT);
  891. }
  892. if (err)
  893. {
  894. if (cert_s.cert_der)
  895. free (cert_s.cert_der);
  896. return err;
  897. }
  898. DEBUG (DBG_INFO, "got certificate from card with length %i",
  899. cert_s.cert_der_len);
  900. cert->cert_der = cert_s.cert_der;
  901. cert->cert_der_len = cert_s.cert_der_len;
  902. return 0;
  903. }
  904. void
  905. scute_agent_finalize (void)
  906. {
  907. if (!agent_ctx)
  908. {
  909. DEBUG (DBG_CRIT, "no GPG Agent connection established");
  910. return;
  911. }
  912. DEBUG (DBG_INFO, "releasing agent context");
  913. assuan_disconnect (agent_ctx);
  914. agent_ctx = NULL;
  915. }