Version 3.0.12.
[pwmd.git] / src / agent.c
blob9880b839dbc75a4081b2a4f4a77420ccf70f78ee
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <pwd.h>
27 #include <sys/types.h>
28 #include <signal.h>
29 #include <sys/wait.h>
30 #include <fcntl.h>
31 #include <dirent.h>
33 #ifdef HAVE_LIMITS_H
34 #include <limits.h>
35 #endif
37 #include "pwmd-error.h"
38 #include "util-misc.h"
39 #include "mem.h"
40 #include "common.h"
41 #include "commands.h"
42 #include "agent.h"
43 #include "util-string.h"
44 #include "mutex.h"
45 #include "rcfile.h"
46 #include "cache.h"
47 #include "cipher.h"
48 #include "crypto.h"
50 static gpg_error_t
51 mem_realloc_cb (void *data, const void *buffer, size_t len)
53 membuf_t *mem = (membuf_t *) data;
54 void *p;
56 if (!buffer)
57 return 0;
59 if ((p = xrealloc (mem->buf, mem->len + len)) == NULL)
60 return 1;
62 mem->buf = p;
63 memcpy ((char *) mem->buf + mem->len, buffer, len);
64 mem->len += len;
65 return 0;
68 /* Forward status messages from gpg-agent to the client. */
69 static gpg_error_t
70 status_cb (void *data, const char *line)
72 struct agent_s *agent = data;
74 agent->inquire_maxlen = 0;
76 if (!strncmp (line, "INQUIRE_MAXLEN ", 15))
78 agent->inquire_maxlen = atoi (line + 15);
79 if (!agent->client_ctx)
80 return 0;
83 return send_status (agent->client_ctx, STATUS_AGENT, "%s", line);
86 static gpg_error_t
87 assuan_command (struct agent_s *a, char **result,
88 size_t * len, const char *cmd)
90 gpg_error_t rc;
92 a->data.len = 0;
93 a->data.buf = NULL;
94 if (result)
95 *result = NULL;
96 if (len)
97 *len = 0;
99 rc = assuan_transact (a->ctx, cmd, mem_realloc_cb, &a->data,
100 a->inquire_cb, a->inquire_data, status_cb, a);
101 if (rc)
102 xfree (a->data.buf);
103 else
105 if (a->data.buf)
107 mem_realloc_cb (&a->data, "", 1);
108 if (result)
109 *result = (char *) a->data.buf;
110 else
111 xfree (a->data.buf);
113 if (len)
114 *len = a->data.len;
118 return rc;
121 /* This commands are sent from launch_agent() after reconnecting to the agent
122 * and also from the initial client connection. */
123 gpg_error_t
124 send_agent_common_options (struct agent_s *agent)
126 gpg_error_t rc;
128 rc = send_to_agent (agent, NULL, NULL, "OPTION cache-ttl-opt-preset=-1");
129 return rc;
132 static gpg_error_t
133 launch_agent (struct agent_s *agent)
135 gpg_error_t rc;
136 assuan_context_t ctx = NULL;
137 static struct assuan_malloc_hooks mhooks = { xmalloc, xrealloc, xfree };
138 char *s;
140 agent->did_restart = 0;
141 rc = assuan_new_ext (&ctx, GPG_ERR_SOURCE_DEFAULT, &mhooks,
142 debug_level ? assuan_log_cb : NULL, "AGENT ");
143 if (rc)
144 return rc;
146 s = config_get_string ("global", "gpg_agent_socket");
147 rc = assuan_socket_connect (ctx, s, ASSUAN_INVALID_PID, 0);
148 xfree (s);
149 if (rc)
151 if (ctx)
152 assuan_release (ctx);
153 return rc;
156 if (agent->ctx)
157 assuan_release (agent->ctx);
159 agent->ctx = ctx;
160 rc = send_agent_common_options (agent);
161 if (!rc && agent->pinentry_opts.display)
162 rc = set_agent_option (agent, "display", agent->pinentry_opts.display);
164 if (!rc && agent->pinentry_opts.ttyname)
165 rc = set_agent_option (agent, "ttyname", agent->pinentry_opts.ttyname);
167 if (!rc && agent->pinentry_opts.ttytype)
168 rc = set_agent_option (agent, "ttytype", agent->pinentry_opts.ttytype);
170 if (!rc && agent->pinentry_opts.lc_messages)
171 rc = set_agent_option (agent, "lc-messages",
172 agent->pinentry_opts.lc_messages);
174 if (!rc && agent->pinentry_opts.lc_ctype)
175 rc = set_agent_option (agent, "lc-ctype", agent->pinentry_opts.lc_ctype);
177 return rc;
180 gpg_error_t
181 send_to_agent (struct agent_s * agent, char **result, size_t * len,
182 const char *fmt, ...)
184 gpg_error_t rc = 0;
185 va_list ap;
186 char *cmd;
188 if (!agent->ctx)
190 rc = launch_agent (agent);
191 if (rc)
192 return rc;
195 va_start (ap, fmt);
196 if (str_vasprintf (&cmd, fmt, ap) > 0)
198 rc = assuan_command (agent, result, len, cmd);
199 if (!agent->restart && gpg_err_source (rc) == GPG_ERR_SOURCE_DEFAULT
200 && (gpg_err_code (rc) == GPG_ERR_ASS_CONNECT_FAILED
201 || gpg_err_code (rc) == GPG_ERR_EPIPE))
203 log_write (_ ("gpg-agent connection died (rc=%i), reconnecting"), rc);
204 agent->restart = 1;
205 rc = launch_agent (agent);
206 if (!rc)
208 agent->did_restart = 1;
209 rc = assuan_command (agent, result, len, cmd);
213 agent->restart = 0;
214 xfree (cmd);
216 else
217 rc = GPG_ERR_ENOMEM;
219 va_end (ap);
220 return rc;
223 static void
224 agent_disconnect (struct agent_s *agent)
226 if (!agent)
227 return;
229 if (agent->ctx)
230 assuan_release (agent->ctx);
232 agent->ctx = NULL;
235 void
236 cleanup_agent (struct agent_s *agent)
238 if (!agent)
239 return;
241 pinentry_free_opts (&agent->pinentry_opts);
242 agent_disconnect (agent);
243 xfree (agent);
246 gpg_error_t
247 agent_init (struct agent_s **agent)
249 struct agent_s *new = xcalloc (1, sizeof (struct agent_s));
251 if (!new)
252 return GPG_ERR_ENOMEM;
254 *agent = new;
255 return 0;
258 static gpg_error_t
259 inquire_cb (void *user, const char *keyword)
261 struct inquire_data_s *idata = user;
263 if (!idata->preset && (!strcmp (keyword, "PASSPHRASE")
264 || !strcmp (keyword, "NEW_PASSPHRASE")))
266 return agent_loopback_cb (idata->crypto, keyword);
268 // SAVE --inquire-keyparam
269 else if (idata->preset && !strcmp (keyword, "KEYPARAM"))
271 idata->preset = 0;
272 return agent_loopback_cb (idata->crypto, keyword);
275 if (idata->crypto->agent->inquire_maxlen
276 && idata->len > idata->crypto->agent->inquire_maxlen)
278 log_write (_("Inquired data too large: have=%u, max=%u"), idata->len,
279 idata->crypto->agent->inquire_maxlen);
280 return GPG_ERR_TOO_LARGE;
283 idata->crypto->agent->inquire_maxlen = 0;
284 return assuan_send_data (idata->crypto->agent->ctx, idata->line,
285 idata->len);
288 /* Retrieve the passphrase needed to decrypt the XML from the secret
289 * key using gpg-agent. */
290 gpg_error_t
291 agent_extract_key (struct crypto_s *crypto, unsigned char **result,
292 size_t * result_len)
294 gpg_error_t rc;
295 gcry_sexp_t enc_sexp = NULL, tmp_sexp;
296 struct inquire_data_s idata = { 0 };
297 char *hexgrip = NULL;
298 char *key;
299 size_t keylen, keysize;
300 char *tmp;
301 int algo = cipher_to_gcrypt (crypto->hdr.flags);
302 int shadowed;
304 rc = gcry_cipher_algo_info (algo, GCRYCTL_GET_KEYLEN, NULL, &keysize);
305 if (rc)
306 return rc;
308 tmp_sexp = gcry_sexp_find_token (crypto->ciphertext_sexp, "enc-val", 0);
309 if (!tmp_sexp)
310 return gpg_error (GPG_ERR_BAD_DATA);
312 hexgrip = bin2hex (crypto->grip, sizeof (crypto->grip));
313 if (!hexgrip)
315 rc = GPG_ERR_ENOMEM;
316 goto fail;
319 rc = cache_is_shadowed (hexgrip);
320 if (rc && rc != GPG_ERR_NO_DATA)
321 goto fail;
323 shadowed = !rc ? 1 : 0;
324 if (!shadowed)
326 /* The key is not on a smartcard. */
327 gcry_sexp_t tmp2_sexp = gcry_sexp_cdr (tmp_sexp);
328 gcry_sexp_release (tmp_sexp);
329 tmp_sexp = gcry_sexp_nth (tmp2_sexp, 0);
330 gcry_sexp_release (tmp2_sexp);
331 rc = gcry_sexp_build (&enc_sexp, NULL, "(enc-val (flags pkcs1) %S)",
332 tmp_sexp);
334 else
335 rc = gcry_sexp_build (&enc_sexp, NULL, "%S", tmp_sexp);
337 gcry_sexp_release (tmp_sexp);
338 if (rc)
339 return rc;
341 crypto->agent->inquire_cb = inquire_cb;
342 idata.crypto = crypto;
343 idata.len = gcry_sexp_sprint (enc_sexp, GCRYSEXP_FMT_CANON, NULL, 0);
344 idata.line = xmalloc (idata.len);
345 if (!idata.line)
347 rc = GPG_ERR_ENOMEM;
348 goto fail;
351 idata.len = gcry_sexp_sprint (enc_sexp, GCRYSEXP_FMT_CANON, idata.line,
352 idata.len);
353 crypto->agent->inquire_data = &idata;
354 gcry_sexp_release (enc_sexp);
355 enc_sexp = NULL;
356 log_write1 (_("Keygrip is %s, bits=%i"), hexgrip,
357 gcry_pk_get_nbits (crypto->pkey_sexp));
358 rc = send_to_agent (crypto->agent, NULL, NULL, "SETKEY %s", hexgrip);
359 if (rc)
360 goto fail;
362 if (!crypto->agent->pinentry_opts.desc)
364 tmp =
365 plus_escape (_
366 ("A %s is required to unlock the secret key for the "
367 "encrypted data file \"%s\". Please enter the %s "
368 "below."), shadowed ? "PIN" : _("passphrase"),
369 crypto->filename, shadowed ? "PIN" : _("passphrase"));
371 else
372 tmp = plus_escape (crypto->agent->pinentry_opts.desc);
374 rc = send_to_agent (crypto->agent, NULL, NULL, "SETKEYDESC %s", tmp);
375 xfree (tmp);
376 if (rc)
377 goto fail;
379 assuan_begin_confidential (crypto->agent->ctx);
380 rc = send_to_agent (crypto->agent, &key, &keylen, "PKDECRYPT");
381 assuan_end_confidential (crypto->agent->ctx);
382 if (rc)
383 goto fail;
385 rc = gcry_sexp_new (&tmp_sexp, key, keylen, 1);
386 xfree (key);
387 if (rc)
388 goto fail;
390 key = (char *) gcry_sexp_nth_data (tmp_sexp, 1, result_len);
391 if (key)
393 *result = gcry_malloc (*result_len);
394 if (!*result)
395 rc = GPG_ERR_ENOMEM;
396 else
397 memcpy (*result, key, *result_len);
399 else
400 rc = GPG_ERR_BAD_DATA;
402 gcry_sexp_release (tmp_sexp);
404 fail:
405 xfree (idata.line);
406 xfree (hexgrip);
408 if (enc_sexp)
409 gcry_sexp_release (enc_sexp);
411 if (rc && gpg_err_source (rc) == GPG_ERR_SOURCE_UNKNOWN)
412 rc = gpg_error (rc);
414 return rc;
417 // FIXME
418 /* Return an error for public key algorithms other than RSA or DSA or ELG.
419 * Others are supported by libgcrypt but I dont know what to do for ECDA. Key
420 * generation works as does signing but decryption fails for unsupported
421 * algorithms so disable them. */
422 static gpg_error_t
423 get_key_info (gcry_sexp_t pubkey, char **hash_str, int *hash_type,
424 size_t *len, int *pk_algo)
426 unsigned nbits = gcry_pk_get_nbits (pubkey);
427 gcry_sexp_t sexp, tmp;
428 char *algo;
429 gpg_error_t rc = 0;
431 tmp = gcry_sexp_find_token (pubkey, "public-key", 0);
432 if(!tmp)
433 return GPG_ERR_NO_PUBKEY;
435 sexp = gcry_sexp_nth (tmp, 1);
436 gcry_sexp_release (tmp);
437 algo = gcry_sexp_nth_string (sexp, 0);
438 gcry_sexp_release (sexp);
440 if (!algo)
442 return GPG_ERR_BAD_PUBKEY;
445 if (!strcmp (algo, "rsa"))
447 *pk_algo = GCRY_PK_RSA;
448 *hash_str = "sha256";
449 *hash_type = GCRY_MD_SHA256;
450 *len = gcry_md_get_algo_dlen (GCRY_MD_SHA256);
452 else if (!strcmp (algo, "elg"))
454 *pk_algo = GCRY_PK_ELG;
455 *hash_str = "sha256";
456 *hash_type = GCRY_MD_SHA256;
457 *len = gcry_md_get_algo_dlen (GCRY_MD_SHA256);
459 else if (!strcmp (algo, "dsa"))
461 *pk_algo = GCRY_PK_DSA;
462 switch (nbits)
464 default:
465 case 512 ... 1024:
466 *hash_str = "sha1";
467 *hash_type = GCRY_MD_SHA1;
468 *len = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
469 break;
470 case 2048:
471 *hash_str = "sha224";
472 *hash_type = GCRY_MD_SHA224;
473 *len = gcry_md_get_algo_dlen (GCRY_MD_SHA224);
474 break;
475 case 3072:
476 *hash_str = "sha256";
477 *hash_type = GCRY_MD_SHA256;
478 *len = gcry_md_get_algo_dlen (GCRY_MD_SHA256);
479 break;
480 case 7680:
481 *hash_str = "sha384";
482 *hash_type = GCRY_MD_SHA384;
483 *len = gcry_md_get_algo_dlen (GCRY_MD_SHA384);
484 break;
485 case 15360:
486 *hash_str = "sha512";
487 *hash_type = GCRY_MD_SHA512;
488 *len = gcry_md_get_algo_dlen (GCRY_MD_SHA512);
489 break;
492 else
493 rc = GPG_ERR_UNSUPPORTED_ALGORITHM;
495 gcry_free (algo);
496 return rc;
499 gpg_error_t
500 agent_verify (gcry_sexp_t pkey, gcry_sexp_t sig_sexp, const void *data,
501 size_t len)
503 size_t hashlen;
504 int pk_algo;
505 unsigned char *hash;
506 gcry_sexp_t data_sexp;
507 char *hash_str = NULL;
508 int hash_type;
509 gpg_error_t rc = get_key_info (pkey, &hash_str, &hash_type, &hashlen,
510 &pk_algo);
512 if (rc)
513 return rc;
515 hash = gcry_malloc (hashlen);
516 if (!hash)
517 return GPG_ERR_ENOMEM;
519 gcry_md_hash_buffer (hash_type, hash, data, len);
520 if (pk_algo == GCRY_PK_RSA || pk_algo == GCRY_PK_ELG)
521 rc = gcry_sexp_build (&data_sexp, NULL,
522 "(data (flags pkcs1) (hash %s %b))", hash_str,
523 hashlen, hash);
524 else
525 rc = gcry_sexp_build (&data_sexp, NULL,
526 "(data (flags raw) (value %b))", hashlen, hash);
528 gcry_free (hash);
529 if (!rc)
531 rc = gcry_pk_verify (sig_sexp, data_sexp, pkey);
532 gcry_sexp_release (data_sexp);
535 return rc;
538 /* Inquire data from the client. The inquire initiated from gpg-agent. */
539 gpg_error_t
540 agent_loopback_cb (void *user, const char *keyword)
542 struct crypto_s *crypto = user;
543 gpg_error_t rc;
544 unsigned char *result;
545 size_t len;
546 int keyparam = 0;
548 if (!strcmp (keyword, "KEYPARAM"))
550 keyparam = 1;
551 rc = assuan_inquire (crypto->client_ctx, keyword, &result, &len, 0);
553 else
554 { // PASSPHRASE or NEW_PASSPHRASE
555 assuan_begin_confidential (crypto->client_ctx);
556 rc = assuan_inquire (crypto->client_ctx, keyword, &result, &len, 0);
557 assuan_end_confidential (crypto->client_ctx);
559 if (!rc && len == 1 && *result == 0)
560 len = 0;
563 if (!rc)
565 if (keyparam && !len)
567 char *tmp = default_key_params (crypto);
569 if (!tmp)
570 return gpg_error (GPG_ERR_ENOMEM);
572 len = strlen (tmp);
573 xfree (result);
574 result = xmalloc (len);
575 memcpy (result, tmp, len);
576 xfree (tmp);
579 pthread_cleanup_push (xfree, result);
581 if (keyparam)
582 rc = assuan_send_data (crypto->agent->ctx, result, len);
583 else
585 assuan_begin_confidential (crypto->agent->ctx);
586 rc = assuan_send_data (crypto->agent->ctx, result, len);
587 assuan_end_confidential (crypto->agent->ctx);
590 pthread_cleanup_pop (1);
592 else if (gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
594 gpg_error_t arc = assuan_write_line (crypto->agent->ctx, "CAN");
596 if (!arc)
598 char *line;
599 size_t len;
601 arc = assuan_read_line (crypto->agent->ctx, &line, &len);
602 if (arc)
603 rc = arc;
605 else
606 rc = arc;
609 return rc;
612 static gpg_error_t
613 sign (gcry_sexp_t * rsexp, gcry_sexp_t sigpkey, struct crypto_s *crypto,
614 const void *data, size_t len)
616 gpg_error_t rc;
617 char *result;
618 unsigned char grip[20];
619 char *tmp;
621 gcry_pk_get_keygrip (sigpkey, grip);
622 tmp = bin2hex (grip, sizeof(grip));
623 pthread_cleanup_push (xfree, tmp);
624 log_write1 (_("Signed with keygrip %s"), tmp);
625 rc = send_to_agent (crypto->agent, NULL, NULL, "SIGKEY %s", tmp);
626 pthread_cleanup_pop (1);
627 if (!rc)
629 size_t hashlen;
630 char *hash_str = NULL;
631 int hash_type;
632 int pk_algo;
634 rc = get_key_info (sigpkey, &hash_str, &hash_type, &hashlen, &pk_algo);
635 if (!rc)
637 unsigned char *hash = gcry_malloc (hashlen);
639 if (!hash)
640 rc = gpg_error (GPG_ERR_ENOMEM);
642 if (!rc)
644 gcry_md_hash_buffer (hash_type, hash, data, len);
645 tmp = bin2hex (hash, hashlen);
646 gcry_free (hash);
647 pthread_cleanup_push (xfree, tmp);
648 rc = send_to_agent (crypto->agent, NULL, NULL,
649 "SETHASH --hash=%s %s", hash_str, tmp);
650 pthread_cleanup_pop (1);
655 if (rc)
656 return rc;
658 if (!rc)
660 struct inquire_data_s idata = { 0 };
662 idata.crypto = crypto;
663 crypto->agent->inquire_data = &idata;
664 crypto->agent->inquire_cb = inquire_cb;
665 rc = send_to_agent (crypto->agent, &result, &len, "PKSIGN");
666 if (!rc)
668 rc = gcry_sexp_sscan (rsexp, NULL, result, len);
669 xfree (result);
673 if (rc && gpg_err_source (rc) == GPG_ERR_SOURCE_UNKNOWN)
674 rc = gpg_error (rc);
676 return rc;
679 /* Encrypt the data file to 'pubkey' and sign with 'sigpkey'. */
680 gpg_error_t
681 encrypt_data_file (assuan_context_t ctx, struct crypto_s * crypto,
682 gcry_sexp_t pubkey, gcry_sexp_t sigpkey,
683 const char *filename, const void *xml, size_t len)
685 gpg_error_t rc;
686 void *data = NULL;
687 size_t data_len = 0;
688 void *enc_xml = NULL;
689 size_t enc_xml_len = 0;
690 unsigned char *iv = NULL;
691 size_t iv_len = 0;
692 int algo = cipher_to_gcrypt (crypto->save.hdr.flags);
693 void *key = NULL;
694 size_t keysize;
695 unsigned char sig_grip[20];
696 unsigned char grip[20];
698 rc = gcry_cipher_algo_info (algo, GCRYCTL_GET_KEYLEN, NULL, &keysize);
699 if (rc)
700 return rc;
702 pthread_cleanup_push (gcry_free, key);
703 key = gcry_random_bytes_secure (keysize, GCRY_STRONG_RANDOM);
704 #ifdef HAVE_PTHREAD_TESTCANCEL
705 pthread_testcancel (); // may have been a long operation
706 #endif
707 pthread_cleanup_pop (0);
708 if (!key)
709 return gpg_error (GPG_ERR_ENOMEM);
711 gcry_pk_get_keygrip (pubkey, grip);
712 gcry_pk_get_keygrip (sigpkey, sig_grip);
713 pthread_cleanup_push (gcry_free, key);
714 rc = encrypt_xml (ctx, key, keysize, algo, xml, len, &enc_xml, &enc_xml_len,
715 &iv, &iv_len, crypto->save.hdr.iterations);
716 if (!rc)
718 gcry_sexp_t sig_sexp = NULL;
719 char *hexgrip = bin2hex (grip, 20);
721 pthread_cleanup_push (xfree, iv);
722 log_write1 (_("Encrypted with keygrip %s"), hexgrip);
723 xfree (hexgrip);
724 hexgrip = bin2hex (sig_grip, 20);
725 pthread_cleanup_push (gcry_free, enc_xml);
726 rc = sign (&sig_sexp, sigpkey, crypto, enc_xml, enc_xml_len);
727 xfree (hexgrip);
729 if (!rc)
731 rc = agent_verify (sigpkey, sig_sexp, enc_xml, enc_xml_len);
732 if (!rc)
734 gcry_sexp_t tmp_sexp;
736 rc = gcry_sexp_build (&tmp_sexp, NULL,
737 "(data (flags pkcs1) (value %b))",
738 keysize, key);
739 if (!rc)
741 gcry_sexp_t key_sexp;
743 pthread_cleanup_push ((void (*)(void *)) gcry_sexp_release,
744 (void *) sig_sexp);
745 rc = gcry_pk_encrypt (&key_sexp, tmp_sexp, pubkey);
746 pthread_cleanup_pop (0);
747 gcry_sexp_release (tmp_sexp);
749 if (!rc)
751 memcpy (crypto->save.hdr.iv, iv, iv_len);
752 crypto->save.hdr.datalen = enc_xml_len;
753 rc = gcry_sexp_build (&tmp_sexp, NULL, "%S%S", key_sexp,
754 sig_sexp);
755 gcry_sexp_release (key_sexp);
757 if (!rc)
759 data_len = gcry_sexp_sprint (tmp_sexp,
760 GCRYSEXP_FMT_CANON,
761 NULL, 0);
762 data = xmalloc (data_len);
763 if (data)
764 gcry_sexp_sprint (tmp_sexp, GCRYSEXP_FMT_CANON,
765 data, data_len);
766 else
767 rc = GPG_ERR_ENOMEM;
769 gcry_sexp_release (tmp_sexp);
776 pthread_cleanup_pop (0); // enc_xml
777 pthread_cleanup_pop (1); // iv
779 if (sig_sexp)
780 gcry_sexp_release (sig_sexp);
783 pthread_cleanup_pop (1); // key
785 if (!rc)
787 pthread_cleanup_push (gcry_free, enc_xml);
788 rc = write_file (crypto, filename, enc_xml, enc_xml_len, data, data_len,
789 pubkey, sigpkey);
790 pthread_cleanup_pop (1); // enc_xml
791 if (!rc)
792 memcpy (&crypto->hdr, &crypto->save.hdr, sizeof (file_header_t));
795 xfree (data);
796 if (rc && gpg_err_source (rc) == GPG_ERR_SOURCE_UNKNOWN)
797 rc = gpg_error (rc);
799 return rc;
802 /* Generated a new keypair from the key parameters in 'sexpstr'. */
803 gpg_error_t
804 generate_key (struct crypto_s * crypto, char *sexpstr, int empty, int preset)
806 gpg_error_t rc;
807 char *pkey;
808 size_t plen;
810 if (crypto->save.s2k_count)
812 rc = send_to_agent (crypto->agent, NULL, NULL,
813 "OPTION s2k-count=%lu", crypto->save.s2k_count);
814 if (rc)
815 return rc;
818 if (!crypto->agent->inquire_cb)
819 crypto->agent->inquire_cb = inquire_cb;
821 rc = send_to_agent (crypto->agent, &pkey, &plen, "GENKEY %s%s",
822 preset ? "--preset " : "",
823 empty ? "--no-protection" : "");
824 if (rc)
825 return rc;
827 if (crypto->save.pkey)
828 gcry_sexp_release (crypto->save.pkey);
830 crypto->save.pkey = NULL;
831 rc = gcry_sexp_new (&crypto->save.pkey, pkey, plen, 1);
832 if (!rc)
834 unsigned char grip[20];
836 gcry_pk_get_keygrip (crypto->save.pkey, grip);
837 char *hexgrip = bin2hex (grip, sizeof (grip));
838 log_write (_("Keygrip is %s"), hexgrip);
839 xfree (hexgrip);
841 if (!crypto->save.sigpkey)
843 gcry_sexp_build ((gcry_sexp_t *) & crypto->save.sigpkey, NULL, "%S",
844 crypto->save.pkey);
848 xfree (pkey);
849 return rc;
852 gpg_error_t
853 set_agent_option (struct agent_s * agent, const char *name, const char *value)
855 return send_to_agent (agent, NULL, NULL, "OPTION %s=%s", name, value);
858 gpg_error_t
859 set_agent_passphrase (struct crypto_s * crypto, const char *key, size_t len)
861 char *hexgrip;
862 struct inquire_data_s idata;
863 gpg_error_t rc;
864 int i;
866 /* This is for use with key files or passphrases obtained from an inquire.
867 * gpg-agent uses strings as passphrases and will truncate the passphrase
868 * at the first encountered null byte. It's only a warning because the
869 * passphrase may belong to a key shared outside of pwmd. */
870 for (i = 0; i < len; i++)
872 if (key[i] == 0)
874 log_write (_("WARNING: keylen=%i, truncated to %i."), len, i);
875 break;
879 hexgrip = bin2hex (crypto->grip, 20);
880 crypto->agent->inquire_cb = inquire_cb;
881 crypto->agent->inquire_data = &idata;
882 idata.crypto = crypto;
883 idata.line = (char *) key, idata.len = len;
884 idata.preset = 1;
885 assuan_begin_confidential (crypto->agent->ctx);
886 rc = send_to_agent (crypto->agent, NULL, NULL,
887 "PRESET_PASSPHRASE --inquire %s -1", hexgrip);
888 assuan_end_confidential (crypto->agent->ctx);
889 idata.preset = 0;
890 xfree (hexgrip);
891 return rc;
894 gpg_error_t
895 set_pinentry_mode (struct agent_s * agent, const char *mode)
897 return set_agent_option (agent, "pinentry-mode", mode);
900 gpg_error_t
901 get_pubkey_bin (struct crypto_s * crypto, const unsigned char *grip,
902 gcry_sexp_t * result)
904 char *hexgrip = bin2hex (grip, 20);
905 gpg_error_t rc;
907 if (!hexgrip)
908 return GPG_ERR_ENOMEM;
910 rc = get_pubkey (crypto, hexgrip, result);
911 xfree (hexgrip);
912 return rc;
915 gpg_error_t
916 get_pubkey (struct crypto_s * crypto, const char *grip, gcry_sexp_t * result)
918 char *pkey = NULL;
919 size_t plen;
920 gpg_error_t rc;
922 rc = send_to_agent (crypto->agent, &pkey, &plen, "READKEY %s", grip);
923 if (!rc)
924 rc = gcry_sexp_new (result, pkey, plen, 1);
926 xfree (pkey);
927 return rc;
930 gpg_error_t
931 agent_set_pinentry_options (struct agent_s * agent)
933 gpg_error_t rc = 0;
934 struct pinentry_option_s *opts = &agent->pinentry_opts;
936 if (!opts->display && getenv ("DISPLAY"))
938 rc = set_agent_option (agent, "display", getenv ("DISPLAY"));
939 if (!rc)
941 opts->display = str_dup (getenv ("DISPLAY"));
944 else if (!opts->ttyname && ttyname (STDOUT_FILENO))
946 rc = set_agent_option (agent, "ttyname", ttyname (STDOUT_FILENO));
947 if (!rc)
949 rc = set_agent_option (agent, "ttytype",
950 opts->ttytype ? opts->ttytype : getenv ("TERM"));
951 if (!rc)
953 xfree (opts->ttyname);
954 xfree (opts->ttytype);
955 opts->ttyname = str_dup (ttyname (STDOUT_FILENO));
956 opts->ttytype = str_dup (getenv ("TERM"));
961 return rc;
964 static gpg_error_t
965 inquire_keyfile (void *user, const char *keyword)
967 struct crypto_s *crypto = user;
968 char *filename = crypto->agent->inquire_data2;
969 char *params = crypto->agent->inquire_data3;
970 int fd;
971 struct stat st;
972 unsigned char *buf;
973 gpg_error_t rc;
975 if (!strcmp (keyword, "KEYPARAM"))
976 return assuan_send_data (crypto->agent->ctx, params, strlen (params));
978 // This function is only used when generating a new keypair.
979 if (strcmp (keyword, "NEW_PASSPHRASE"))
980 return gpg_error (GPG_ERR_ASS_UNKNOWN_INQUIRE);
982 if (stat (filename, &st) == -1)
983 return gpg_error_from_errno (errno);
985 if (crypto->agent->inquire_maxlen
986 && st.st_size > crypto->agent->inquire_maxlen)
988 log_write (_("The passphrase is too large: have=%u, max=%u."),
989 (unsigned) st.st_size, crypto->agent->inquire_maxlen);
990 return GPG_ERR_TOO_LARGE;
993 buf = gcry_malloc_secure (st.st_size);
994 if (!buf)
995 return GPG_ERR_ENOMEM;
997 fd = open (filename, O_RDONLY);
998 if (fd == -1)
999 rc = gpg_error_from_errno (errno);
1000 else
1002 size_t len = read (fd, buf, st.st_size);
1004 if (len == st.st_size)
1006 assuan_begin_confidential (crypto->agent->ctx);
1007 rc = assuan_send_data (crypto->agent->ctx, buf, len);
1008 assuan_end_confidential (crypto->agent->ctx);
1010 else if (len == -1)
1011 rc = gpg_error_from_errno (errno);
1012 else
1013 rc = GPG_ERR_BUFFER_TOO_SHORT;
1016 close (fd);
1017 gcry_free (buf);
1018 return rc;
1021 gpg_error_t
1022 agent_export_common (struct crypto_s * crypto, const char *hexgrip,
1023 const char *sign_hexgrip, int no_passphrase,
1024 const void *data, size_t datalen, const char *outfile,
1025 const char *keyparams, const char *keyfile)
1027 gpg_error_t rc = 0;
1029 if (!sign_hexgrip && hexgrip)
1030 sign_hexgrip = hexgrip;
1032 if (sign_hexgrip)
1034 if (crypto->sigpkey_sexp)
1035 gcry_sexp_release (crypto->sigpkey_sexp);
1037 crypto->sigpkey_sexp = NULL;
1038 rc = get_pubkey (crypto, sign_hexgrip, &crypto->save.sigpkey);
1039 if (rc)
1040 return rc;
1042 gcry_pk_get_keygrip (crypto->save.sigpkey, crypto->sign_grip);
1045 if (hexgrip)
1047 rc = get_pubkey (crypto, hexgrip, &crypto->save.pkey);
1048 if (!rc)
1049 gcry_pk_get_keygrip (crypto->save.pkey, crypto->grip);
1051 else
1053 struct inquire_data_s idata = { 0 };
1054 char *params = keyparams ? str_dup (keyparams)
1055 : default_key_params (crypto);
1057 pthread_cleanup_push (xfree, params);
1058 log_write (_("Generating a new keypair ..."));
1059 if (keyfile)
1061 log_write (_("Using passphrase obtained from file '%s'"), keyfile);
1062 rc = set_pinentry_mode (crypto->agent, "loopback");
1063 crypto->agent->inquire_cb = inquire_keyfile;
1064 crypto->agent->inquire_data = crypto;
1065 crypto->agent->inquire_data2 = (char *) keyfile;
1066 crypto->agent->inquire_data3 = params;
1068 else
1070 idata.line = params;
1071 idata.len = strlen (params);
1072 idata.crypto = crypto;
1073 crypto->agent->inquire_cb = inquire_cb;
1074 crypto->agent->inquire_data = &idata;
1077 if (!rc)
1079 rc = generate_key (crypto, params, no_passphrase, 1);
1080 if (!rc)
1081 gcry_pk_get_keygrip (crypto->save.pkey, crypto->grip);
1084 (void) set_pinentry_mode (crypto->agent, "ask");
1085 pthread_cleanup_pop (1);
1088 if (!rc)
1090 rc = encrypt_data_file (NULL, crypto, crypto->save.pkey,
1091 crypto->save.sigpkey, outfile, data, datalen);
1092 if (!rc)
1094 char *tmp = bin2hex (crypto->grip, sizeof (crypto->grip));
1096 log_write (_("Success! Keygrip is %s."), tmp);
1097 rc = send_to_agent (crypto->agent, NULL, NULL,
1098 "CLEAR_PASSPHRASE --mode=normal %s", tmp);
1099 xfree (tmp);
1101 if (sign_hexgrip)
1103 tmp = bin2hex (crypto->sign_grip, sizeof (crypto->sign_grip));
1104 log_write (_("Signed with keygrip %s."), tmp);
1105 xfree (tmp);
1110 if (rc && gpg_err_source (rc) == GPG_ERR_SOURCE_UNKNOWN)
1111 rc = gpg_error (rc);
1113 return rc;
1116 char *
1117 default_key_params (struct crypto_s *crypto)
1119 return config_get_string (NULL, "keyparam");
1122 gpg_error_t
1123 agent_passwd (struct crypto_s * crypto)
1125 struct inquire_data_s idata = { 0 };
1126 gpg_error_t rc;
1127 char *tmp = bin2hex (crypto->grip, 20);
1129 idata.crypto = crypto;
1130 crypto->agent->inquire_cb = inquire_cb;
1131 crypto->agent->inquire_data = &idata;
1132 rc = send_to_agent (crypto->agent, NULL, NULL, "PASSWD --preset %s", tmp);
1133 xfree (tmp);
1134 return rc;
1137 gpg_error_t
1138 kill_scd (struct agent_s * agent)
1140 gpg_error_t rc = 0;
1142 if (!use_agent)
1143 return 0;
1145 if (config_get_boolean (NULL, "kill_scd"))
1147 rc = send_to_agent (agent, NULL, NULL, "SCD KILLSCD");
1148 if (rc && gpg_err_code (rc) != GPG_ERR_NO_SCDAEMON)
1149 log_write ("%s: %s", __FUNCTION__, pwmd_strerror (rc));
1152 return rc;