2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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/>.
27 #include <sys/types.h>
37 #include "pwmd-error.h"
38 #include "util-misc.h"
43 #include "util-string.h"
51 mem_realloc_cb (void *data
, const void *buffer
, size_t len
)
53 membuf_t
*mem
= (membuf_t
*) data
;
59 if ((p
= xrealloc (mem
->buf
, mem
->len
+ len
)) == NULL
)
63 memcpy ((char *) mem
->buf
+ mem
->len
, buffer
, len
);
69 status_cb (void *data
, const char *line
)
71 struct agent_s
*agent
= data
;
73 agent
->inquire_maxlen
= 0;
75 if (!strncmp (line
, "INQUIRE_MAXLEN ", 15))
77 agent
->inquire_maxlen
= atoi (line
+ 15);
78 if (!agent
->client_ctx
)
82 return send_status (agent
->client_ctx
, STATUS_AGENT
, "%s", line
);
86 assuan_command (struct agent_s
*a
, char **result
,
87 size_t * len
, const char *cmd
)
98 rc
= assuan_transact (a
->ctx
, cmd
, mem_realloc_cb
, &a
->data
,
99 a
->inquire_cb
, a
->inquire_data
, status_cb
, a
);
106 mem_realloc_cb (&a
->data
, "", 1);
108 *result
= (char *) a
->data
.buf
;
120 /* This commands are sent from launch_agent() after reconnecting to the agent
121 * and also from the initial client connection. */
123 send_agent_common_options (struct agent_s
*agent
)
127 rc
= send_to_agent (agent
, NULL
, NULL
, "OPTION cache-ttl-opt-preset=-1");
132 launch_agent (struct agent_s
*agent
)
135 assuan_context_t ctx
= NULL
;
136 const char *t
= NULL
;
137 static struct assuan_malloc_hooks mhooks
= { xmalloc
, xrealloc
, xfree
};
138 char *s
, buf
[LINE_MAX
];
141 agent
->did_restart
= 0;
142 s
= config_get_string ("global", "agent_env_file");
145 char *ss
= expand_homedir (s
);
148 fp
= fopen (ss
, "r");
153 while ((p
= fgets (buf
, sizeof (buf
), fp
)))
156 if (!strncmp (p
, "GPG_AGENT_INFO=", strlen ("GPG_AGENT_INFO=")))
158 setenv ("GPG_AGENT_INFO", p
+ strlen ("GPG_AGENT_INFO="),
167 log_write ("%s: %s", s
, pwmd_strerror (gpg_error_from_syserror ()));
172 t
= getenv ("GPG_AGENT_INFO");
174 return gpg_error (GPG_ERR_NO_AGENT
);
176 rc
= assuan_new_ext (&ctx
, GPG_ERR_SOURCE_DEFAULT
, &mhooks
,
177 debug_level
? assuan_log_cb
: NULL
, "AGENT ");
181 char **fields
= str_split (t
, ":", 0);
182 rc
= assuan_socket_connect (ctx
, fields
[0], ASSUAN_INVALID_PID
, 0);
187 assuan_release (ctx
);
192 assuan_release (agent
->ctx
);
195 rc
= send_agent_common_options (agent
);
196 if (!rc
&& agent
->pinentry_opts
.display
)
197 rc
= set_agent_option (agent
, "display", agent
->pinentry_opts
.display
);
199 if (!rc
&& agent
->pinentry_opts
.ttyname
)
200 rc
= set_agent_option (agent
, "ttyname", agent
->pinentry_opts
.ttyname
);
202 if (!rc
&& agent
->pinentry_opts
.ttytype
)
203 rc
= set_agent_option (agent
, "ttytype", agent
->pinentry_opts
.ttytype
);
205 if (!rc
&& agent
->pinentry_opts
.lc_messages
)
206 rc
= set_agent_option (agent
, "lc-messages",
207 agent
->pinentry_opts
.lc_messages
);
209 if (!rc
&& agent
->pinentry_opts
.lc_ctype
)
210 rc
= set_agent_option (agent
, "lc-ctype", agent
->pinentry_opts
.lc_ctype
);
216 send_to_agent (struct agent_s
* agent
, char **result
, size_t * len
,
217 const char *fmt
, ...)
225 rc
= launch_agent (agent
);
231 if (str_vasprintf (&cmd
, fmt
, ap
) > 0)
233 rc
= assuan_command (agent
, result
, len
, cmd
);
234 if (!agent
->restart
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_DEFAULT
235 && (gpg_err_code (rc
) == GPG_ERR_ASS_CONNECT_FAILED
236 || gpg_err_code (rc
) == GPG_ERR_EPIPE
))
238 log_write (_ ("gpg-agent connection died (rc=%i), reconnecting"), rc
);
240 rc
= launch_agent (agent
);
243 agent
->did_restart
= 1;
244 rc
= assuan_command (agent
, result
, len
, cmd
);
259 agent_disconnect (struct agent_s
*agent
)
265 assuan_release (agent
->ctx
);
271 cleanup_agent (struct agent_s
*agent
)
276 pinentry_free_opts (&agent
->pinentry_opts
);
277 agent_disconnect (agent
);
282 agent_init (struct agent_s
**agent
)
284 struct agent_s
*new = xcalloc (1, sizeof (struct agent_s
));
287 return GPG_ERR_ENOMEM
;
294 inquire_cb (void *user
, const char *keyword
)
296 struct inquire_data_s
*idata
= user
;
298 if (!idata
->preset
&& (!strcmp (keyword
, "PASSPHRASE")
299 || !strcmp (keyword
, "NEW_PASSPHRASE")))
301 return agent_loopback_cb (idata
->crypto
, keyword
);
303 // SAVE --inquire-keyparam
304 else if (idata
->preset
&& !strcmp (keyword
, "KEYPARAM"))
307 return agent_loopback_cb (idata
->crypto
, keyword
);
310 if (idata
->crypto
->agent
->inquire_maxlen
311 && idata
->len
> idata
->crypto
->agent
->inquire_maxlen
)
313 log_write (_("Inquired data too large: have=%u, max=%u"), idata
->len
,
314 idata
->crypto
->agent
->inquire_maxlen
);
315 return GPG_ERR_TOO_LARGE
;
318 idata
->crypto
->agent
->inquire_maxlen
= 0;
319 return assuan_send_data (idata
->crypto
->agent
->ctx
, idata
->line
,
324 agent_extract_key (struct crypto_s
*crypto
, unsigned char **result
,
328 gcry_sexp_t enc_sexp
= NULL
, tmp_sexp
;
329 struct inquire_data_s idata
= { 0 };
330 char *hexgrip
= NULL
;
332 size_t keylen
, keysize
;
334 int algo
= cipher_to_gcrypt (crypto
->hdr
.flags
);
337 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, &keysize
);
341 tmp_sexp
= gcry_sexp_find_token (crypto
->ciphertext_sexp
, "enc-val", 0);
343 return gpg_error (GPG_ERR_BAD_DATA
);
345 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
352 rc
= cache_is_shadowed (hexgrip
);
353 if (rc
&& rc
!= GPG_ERR_NO_DATA
)
356 shadowed
= !rc
? 1 : 0;
359 gcry_sexp_t tmp2_sexp
= gcry_sexp_cdr (tmp_sexp
);
360 gcry_sexp_release (tmp_sexp
);
361 tmp_sexp
= gcry_sexp_nth (tmp2_sexp
, 0);
362 gcry_sexp_release (tmp2_sexp
);
363 rc
= gcry_sexp_build (&enc_sexp
, NULL
, "(enc-val (flags pkcs1) %S)",
367 rc
= gcry_sexp_build (&enc_sexp
, NULL
, "%S", tmp_sexp
);
369 gcry_sexp_release (tmp_sexp
);
373 crypto
->agent
->inquire_cb
= inquire_cb
;
374 idata
.crypto
= crypto
;
375 idata
.len
= gcry_sexp_sprint (enc_sexp
, GCRYSEXP_FMT_CANON
, NULL
, 0);
376 idata
.line
= xmalloc (idata
.len
);
383 idata
.len
= gcry_sexp_sprint (enc_sexp
, GCRYSEXP_FMT_CANON
, idata
.line
,
385 crypto
->agent
->inquire_data
= &idata
;
386 gcry_sexp_release (enc_sexp
);
388 log_write1 (_("Keygrip is %s, bits=%i"), hexgrip
,
389 gcry_pk_get_nbits (crypto
->pkey_sexp
));
390 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
, "SETKEY %s", hexgrip
);
394 if (!crypto
->agent
->pinentry_opts
.desc
)
398 ("A %s is required to unlock the secret key for the "
399 "encrypted data file \"%s\". Please enter the %s "
400 "below."), shadowed
? "PIN" : _("passphrase"),
401 crypto
->filename
, shadowed
? "PIN" : _("passphrase"));
404 tmp
= plus_escape (crypto
->agent
->pinentry_opts
.desc
);
406 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
, "SETKEYDESC %s", tmp
);
411 assuan_begin_confidential (crypto
->agent
->ctx
);
412 rc
= send_to_agent (crypto
->agent
, &key
, &keylen
, "PKDECRYPT");
413 assuan_end_confidential (crypto
->agent
->ctx
);
417 rc
= gcry_sexp_new (&tmp_sexp
, key
, keylen
, 1);
422 key
= (char *) gcry_sexp_nth_data (tmp_sexp
, 1, result_len
);
425 *result
= gcry_malloc (*result_len
);
429 memcpy (*result
, key
, *result_len
);
432 rc
= GPG_ERR_BAD_DATA
;
434 gcry_sexp_release (tmp_sexp
);
441 gcry_sexp_release (enc_sexp
);
443 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
450 get_key_info (gcry_sexp_t pubkey
, char **hash_str
, int *hash_type
,
451 size_t *len
, int *pk_algo
)
453 unsigned nbits
= gcry_pk_get_nbits (pubkey
);
454 gcry_sexp_t sexp
, tmp
;
458 tmp
= gcry_sexp_find_token (pubkey
, "public-key", 0);
460 return GPG_ERR_NO_PUBKEY
;
462 sexp
= gcry_sexp_nth (tmp
, 1);
463 gcry_sexp_release (tmp
);
464 algo
= gcry_sexp_nth_string (sexp
, 0);
465 gcry_sexp_release (sexp
);
469 return GPG_ERR_BAD_PUBKEY
;
472 if (!strcmp (algo
, "rsa"))
474 *pk_algo
= GCRY_PK_RSA
;
475 *hash_str
= "sha256";
476 *hash_type
= GCRY_MD_SHA256
;
477 *len
= gcry_md_get_algo_dlen (GCRY_MD_SHA256
);
479 else if (!strcmp (algo
, "dsa"))
481 *pk_algo
= GCRY_PK_DSA
;
487 *hash_type
= GCRY_MD_SHA1
;
488 *len
= gcry_md_get_algo_dlen (GCRY_MD_SHA1
);
491 *hash_str
= "sha224";
492 *hash_type
= GCRY_MD_SHA224
;
493 *len
= gcry_md_get_algo_dlen (GCRY_MD_SHA224
);
496 *hash_str
= "sha256";
497 *hash_type
= GCRY_MD_SHA256
;
498 *len
= gcry_md_get_algo_dlen (GCRY_MD_SHA256
);
501 *hash_str
= "sha384";
502 *hash_type
= GCRY_MD_SHA384
;
503 *len
= gcry_md_get_algo_dlen (GCRY_MD_SHA384
);
506 *hash_str
= "sha512";
507 *hash_type
= GCRY_MD_SHA512
;
508 *len
= gcry_md_get_algo_dlen (GCRY_MD_SHA512
);
513 rc
= GPG_ERR_UNSUPPORTED_ALGORITHM
;
520 agent_verify (gcry_sexp_t pkey
, gcry_sexp_t sig_sexp
, const void *data
,
526 gcry_sexp_t data_sexp
;
527 char *hash_str
= NULL
;
529 gpg_error_t rc
= get_key_info (pkey
, &hash_str
, &hash_type
, &hashlen
,
535 hash
= gcry_malloc (hashlen
);
537 return GPG_ERR_ENOMEM
;
539 gcry_md_hash_buffer (hash_type
, hash
, data
, len
);
540 if (pk_algo
== GCRY_PK_RSA
)
541 rc
= gcry_sexp_build (&data_sexp
, NULL
,
542 "(data (flags pkcs1) (hash %s %b))", hash_str
,
545 rc
= gcry_sexp_build (&data_sexp
, NULL
,
546 "(data (flags raw) (value %b))", hashlen
, hash
);
551 rc
= gcry_pk_verify (sig_sexp
, data_sexp
, pkey
);
552 gcry_sexp_release (data_sexp
);
559 agent_loopback_cb (void *user
, const char *keyword
)
561 struct crypto_s
*crypto
= user
;
563 unsigned char *result
;
567 if (!strcmp (keyword
, "KEYPARAM"))
570 rc
= assuan_inquire (crypto
->client_ctx
, keyword
, &result
, &len
, 0);
573 { // PASSPHRASE or NEW_PASSPHRASE
574 assuan_begin_confidential (crypto
->client_ctx
);
575 rc
= assuan_inquire (crypto
->client_ctx
, keyword
, &result
, &len
, 0);
576 assuan_end_confidential (crypto
->client_ctx
);
578 if (!rc
&& len
== 1 && *result
== 0)
584 if (keyparam
&& !len
)
586 char *tmp
= default_key_params (crypto
);
589 return gpg_error (GPG_ERR_ENOMEM
);
592 result
= xmalloc (len
);
593 memcpy (result
, tmp
, len
);
597 pthread_cleanup_push (xfree
, result
);
600 rc
= assuan_send_data (crypto
->agent
->ctx
, result
, len
);
603 assuan_begin_confidential (crypto
->agent
->ctx
);
604 rc
= assuan_send_data (crypto
->agent
->ctx
, result
, len
);
605 assuan_end_confidential (crypto
->agent
->ctx
);
608 pthread_cleanup_pop (1);
610 else if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
612 gpg_error_t arc
= assuan_write_line (crypto
->agent
->ctx
, "CAN");
619 arc
= assuan_read_line (crypto
->agent
->ctx
, &line
, &len
);
631 sign (gcry_sexp_t
* rsexp
, gcry_sexp_t sigpkey
, struct crypto_s
*crypto
,
632 const void *data
, size_t len
)
636 unsigned char grip
[20];
639 gcry_pk_get_keygrip (sigpkey
, grip
);
640 tmp
= bin2hex (grip
, sizeof(grip
));
641 pthread_cleanup_push (xfree
, tmp
);
642 log_write1 (_("Signed with keygrip %s"), tmp
);
643 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
, "SIGKEY %s", tmp
);
644 pthread_cleanup_pop (1);
649 char *hash_str
= NULL
;
653 rc
= get_key_info (sigpkey
, &hash_str
, &hash_type
, &hashlen
, &pk_algo
);
656 hash
= gcry_malloc (hashlen
);
658 rc
= gpg_error (GPG_ERR_ENOMEM
);
662 gcry_md_hash_buffer (hash_type
, hash
, data
, len
);
663 tmp
= bin2hex (hash
, hashlen
);
665 pthread_cleanup_push (xfree
, tmp
);
666 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
,
667 "SETHASH --hash=%s %s", hash_str
, tmp
);
668 pthread_cleanup_pop (1);
678 struct inquire_data_s idata
= { 0 };
680 idata
.crypto
= crypto
;
681 crypto
->agent
->inquire_data
= &idata
;
682 crypto
->agent
->inquire_cb
= inquire_cb
;
683 rc
= send_to_agent (crypto
->agent
, &result
, &len
, "PKSIGN");
686 rc
= gcry_sexp_sscan (rsexp
, NULL
, result
, len
);
691 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
698 encrypt_data_file (assuan_context_t ctx
, struct crypto_s
* crypto
,
699 gcry_sexp_t pubkey
, gcry_sexp_t sigpkey
,
700 const char *filename
, const void *xml
, size_t len
)
705 void *enc_xml
= NULL
;
706 size_t enc_xml_len
= 0;
707 unsigned char *iv
= NULL
;
709 int algo
= cipher_to_gcrypt (crypto
->save
.hdr
.flags
);
712 unsigned char sig_grip
[20];
713 unsigned char grip
[20];
715 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, &keysize
);
719 pthread_cleanup_push (gcry_free
, key
);
720 key
= gcry_random_bytes_secure (keysize
, GCRY_STRONG_RANDOM
);
721 #ifdef HAVE_PTHREAD_TESTCANCEL
722 pthread_testcancel (); // may have been a long operation
724 pthread_cleanup_pop (0);
726 return gpg_error (GPG_ERR_ENOMEM
);
728 gcry_pk_get_keygrip (pubkey
, grip
);
729 gcry_pk_get_keygrip (sigpkey
, sig_grip
);
730 pthread_cleanup_push (gcry_free
, key
);
731 rc
= encrypt_xml (ctx
, key
, keysize
, algo
, xml
, len
, &enc_xml
, &enc_xml_len
,
732 &iv
, &iv_len
, crypto
->save
.hdr
.iterations
);
735 gcry_sexp_t sig_sexp
= NULL
;
736 char *hexgrip
= bin2hex (grip
, 20);
738 pthread_cleanup_push (xfree
, iv
);
739 log_write1 (_("Encrypted with keygrip %s"), hexgrip
);
741 hexgrip
= bin2hex (sig_grip
, 20);
742 pthread_cleanup_push (gcry_free
, enc_xml
);
743 rc
= sign (&sig_sexp
, sigpkey
, crypto
, enc_xml
, enc_xml_len
);
748 rc
= agent_verify (sigpkey
, sig_sexp
, enc_xml
, enc_xml_len
);
751 gcry_sexp_t tmp_sexp
;
753 rc
= gcry_sexp_build (&tmp_sexp
, NULL
,
754 "(data (flags pkcs1) (value %b))",
758 gcry_sexp_t key_sexp
;
760 pthread_cleanup_push ((void (*)(void *)) gcry_sexp_release
,
762 rc
= gcry_pk_encrypt (&key_sexp
, tmp_sexp
, pubkey
);
763 pthread_cleanup_pop (0);
764 gcry_sexp_release (tmp_sexp
);
768 memcpy (crypto
->save
.hdr
.iv
, iv
, iv_len
);
769 crypto
->save
.hdr
.datalen
= enc_xml_len
;
770 rc
= gcry_sexp_build (&tmp_sexp
, NULL
, "%S%S", key_sexp
,
772 gcry_sexp_release (key_sexp
);
776 data_len
= gcry_sexp_sprint (tmp_sexp
,
779 data
= xmalloc (data_len
);
781 gcry_sexp_sprint (tmp_sexp
, GCRYSEXP_FMT_CANON
,
786 gcry_sexp_release (tmp_sexp
);
793 pthread_cleanup_pop (0); // enc_xml
794 pthread_cleanup_pop (1); // iv
797 gcry_sexp_release (sig_sexp
);
800 pthread_cleanup_pop (1); // key
804 pthread_cleanup_push (gcry_free
, enc_xml
);
805 rc
= write_file (crypto
, filename
, enc_xml
, enc_xml_len
, data
, data_len
,
807 pthread_cleanup_pop (1); // enc_xml
809 memcpy (&crypto
->hdr
, &crypto
->save
.hdr
, sizeof (file_header_t
));
813 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
820 generate_key (struct crypto_s
* crypto
, char *sexpstr
, int empty
, int preset
)
826 if (crypto
->save
.s2k_count
)
828 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
,
829 "OPTION s2k-count=%lu", crypto
->save
.s2k_count
);
834 if (!crypto
->agent
->inquire_cb
)
835 crypto
->agent
->inquire_cb
= inquire_cb
;
837 rc
= send_to_agent (crypto
->agent
, &pkey
, &plen
, "GENKEY %s%s",
838 preset
? "--preset " : "",
839 empty
? "--no-protection" : "");
843 if (crypto
->save
.pkey
)
844 gcry_sexp_release (crypto
->save
.pkey
);
846 crypto
->save
.pkey
= NULL
;
847 rc
= gcry_sexp_new (&crypto
->save
.pkey
, pkey
, plen
, 1);
850 unsigned char grip
[20];
852 gcry_pk_get_keygrip (crypto
->save
.pkey
, grip
);
853 char *hexgrip
= bin2hex (grip
, sizeof (grip
));
854 log_write (_("Keygrip is %s"), hexgrip
);
857 if (!crypto
->save
.sigpkey
)
859 gcry_sexp_build ((gcry_sexp_t
*) & crypto
->save
.sigpkey
, NULL
, "%S",
869 set_agent_option (struct agent_s
* agent
, const char *name
, const char *value
)
871 return send_to_agent (agent
, NULL
, NULL
, "OPTION %s=%s", name
, value
);
875 set_agent_passphrase (struct crypto_s
* crypto
, const char *key
, size_t len
)
878 struct inquire_data_s idata
;
882 /* This is for use with key files or passphrases obtained from an inquire.
883 * gpg-agent uses strings as passphrases and will truncate the passphrase
884 * at the first encountered null byte. It's only a warning because the
885 * passphrase may belong to a key shared outside of pwmd. */
886 for (i
= 0; i
< len
; i
++)
890 log_write (_("WARNING: keylen=%i, truncated to %i."), len
, i
);
895 hexgrip
= bin2hex (crypto
->grip
, 20);
896 crypto
->agent
->inquire_cb
= inquire_cb
;
897 crypto
->agent
->inquire_data
= &idata
;
898 idata
.crypto
= crypto
;
899 idata
.line
= (char *) key
, idata
.len
= len
;
901 assuan_begin_confidential (crypto
->agent
->ctx
);
902 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
,
903 "PRESET_PASSPHRASE --inquire %s -1", hexgrip
);
904 assuan_end_confidential (crypto
->agent
->ctx
);
911 set_pinentry_mode (struct agent_s
* agent
, const char *mode
)
913 return set_agent_option (agent
, "pinentry-mode", mode
);
917 get_pubkey_bin (struct crypto_s
* crypto
, const unsigned char *grip
,
918 gcry_sexp_t
* result
)
920 char *hexgrip
= bin2hex (grip
, 20);
924 return GPG_ERR_ENOMEM
;
926 rc
= get_pubkey (crypto
, hexgrip
, result
);
932 get_pubkey (struct crypto_s
* crypto
, const char *grip
, gcry_sexp_t
* result
)
938 rc
= send_to_agent (crypto
->agent
, &pkey
, &plen
, "READKEY %s", grip
);
940 rc
= gcry_sexp_new (result
, pkey
, plen
, 1);
947 agent_set_pinentry_options (struct agent_s
* agent
)
950 struct pinentry_option_s
*opts
= &agent
->pinentry_opts
;
952 if (!opts
->display
&& getenv ("DISPLAY"))
954 rc
= set_agent_option (agent
, "display", getenv ("DISPLAY"));
957 opts
->display
= str_dup (getenv ("DISPLAY"));
960 else if (!opts
->ttyname
&& ttyname (STDOUT_FILENO
))
962 rc
= set_agent_option (agent
, "ttyname", ttyname (STDOUT_FILENO
));
965 rc
= set_agent_option (agent
, "ttytype",
966 opts
->ttytype
? opts
->ttytype
: getenv ("TERM"));
969 xfree (opts
->ttyname
);
970 xfree (opts
->ttytype
);
971 opts
->ttyname
= str_dup (ttyname (STDOUT_FILENO
));
972 opts
->ttytype
= str_dup (getenv ("TERM"));
981 inquire_keyfile (void *user
, const char *keyword
)
983 struct crypto_s
*crypto
= user
;
984 char *filename
= crypto
->agent
->inquire_data2
;
985 char *params
= crypto
->agent
->inquire_data3
;
992 if (!strcmp (keyword
, "KEYPARAM"))
993 return assuan_send_data (crypto
->agent
->ctx
, params
, strlen (params
));
995 // This function is only used when generating a new keypair.
996 if (strcmp (keyword
, "NEW_PASSPHRASE"))
997 return gpg_error (GPG_ERR_ASS_UNKNOWN_INQUIRE
);
999 if (stat (filename
, &st
) == -1)
1000 return gpg_error_from_syserror ();
1002 if (crypto
->agent
->inquire_maxlen
1003 && st
.st_size
> crypto
->agent
->inquire_maxlen
)
1005 log_write (_("The passphrase is too large: have=%u, max=%u."),
1006 (unsigned) st
.st_size
, crypto
->agent
->inquire_maxlen
);
1007 return GPG_ERR_TOO_LARGE
;
1010 buf
= gcry_malloc_secure (st
.st_size
);
1012 return GPG_ERR_ENOMEM
;
1014 fd
= open (filename
, O_RDONLY
);
1016 rc
= gpg_error_from_syserror ();
1019 len
= read (fd
, buf
, st
.st_size
);
1020 if (len
== st
.st_size
)
1022 assuan_begin_confidential (crypto
->agent
->ctx
);
1023 rc
= assuan_send_data (crypto
->agent
->ctx
, buf
, len
);
1024 assuan_end_confidential (crypto
->agent
->ctx
);
1027 rc
= gpg_error_from_syserror ();
1029 rc
= GPG_ERR_BUFFER_TOO_SHORT
;
1038 agent_export_common (struct crypto_s
* crypto
, const char *hexgrip
,
1039 const char *sign_hexgrip
, int no_passphrase
,
1040 const void *data
, size_t datalen
, const char *outfile
,
1041 const char *keyparams
, const char *keyfile
)
1045 if (!sign_hexgrip
&& hexgrip
)
1046 sign_hexgrip
= hexgrip
;
1050 if (crypto
->sigpkey_sexp
)
1051 gcry_sexp_release (crypto
->sigpkey_sexp
);
1053 crypto
->sigpkey_sexp
= NULL
;
1054 rc
= get_pubkey (crypto
, sign_hexgrip
, &crypto
->save
.sigpkey
);
1058 gcry_pk_get_keygrip (crypto
->save
.sigpkey
, crypto
->sign_grip
);
1063 rc
= get_pubkey (crypto
, hexgrip
, &crypto
->save
.pkey
);
1065 gcry_pk_get_keygrip (crypto
->save
.pkey
, crypto
->grip
);
1069 struct inquire_data_s idata
= { 0 };
1070 char *params
= keyparams
? str_dup (keyparams
)
1071 : default_key_params (crypto
);
1073 pthread_cleanup_push (xfree
, params
);
1074 log_write (_("Generating a new keypair ..."));
1077 log_write (_("Using passphrase obtained from file '%s'"), keyfile
);
1078 rc
= set_pinentry_mode (crypto
->agent
, "loopback");
1079 crypto
->agent
->inquire_cb
= inquire_keyfile
;
1080 crypto
->agent
->inquire_data
= crypto
;
1081 crypto
->agent
->inquire_data2
= (char *) keyfile
;
1082 crypto
->agent
->inquire_data3
= params
;
1086 idata
.line
= params
;
1087 idata
.len
= strlen (params
);
1088 idata
.crypto
= crypto
;
1089 crypto
->agent
->inquire_cb
= inquire_cb
;
1090 crypto
->agent
->inquire_data
= &idata
;
1095 rc
= generate_key (crypto
, params
, no_passphrase
, 1);
1097 gcry_pk_get_keygrip (crypto
->save
.pkey
, crypto
->grip
);
1100 (void) set_pinentry_mode (crypto
->agent
, "ask");
1101 pthread_cleanup_pop (1);
1106 rc
= encrypt_data_file (NULL
, crypto
, crypto
->save
.pkey
,
1107 crypto
->save
.sigpkey
, outfile
, data
, datalen
);
1110 char *tmp
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
1112 log_write (_("Success! Keygrip is %s."), tmp
);
1113 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
,
1114 "CLEAR_PASSPHRASE --mode=normal %s", tmp
);
1119 tmp
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
1120 log_write (_("Signed with keygrip %s."), tmp
);
1126 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
1127 rc
= gpg_error (rc
);
1133 default_key_params (struct crypto_s
*crypto
)
1135 return config_get_string (NULL
, "keyparam");
1139 agent_passwd (struct crypto_s
* crypto
)
1141 struct inquire_data_s idata
= { 0 };
1143 char *tmp
= bin2hex (crypto
->grip
, 20);
1145 idata
.crypto
= crypto
;
1146 crypto
->agent
->inquire_cb
= inquire_cb
;
1147 crypto
->agent
->inquire_data
= &idata
;
1148 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
, "PASSWD --preset %s", tmp
);
1154 kill_scd (struct agent_s
* agent
)
1161 if (config_get_boolean (NULL
, "kill_scd"))
1163 rc
= send_to_agent (agent
, NULL
, NULL
, "SCD KILLSCD");
1164 if (rc
&& gpg_err_code (rc
) != GPG_ERR_NO_SCDAEMON
)
1165 log_write ("%s: %s", __FUNCTION__
, pwmd_strerror (rc
));