2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
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>
41 #include "pwmd-error.h"
46 #include "util-misc.h"
47 #include "util-string.h"
53 static uint8_t pwmd_magic
[5] = { '\177', 'P', 'W', 'M', 'D' };
56 mem_realloc_cb (void *data
, const void *buffer
, size_t len
)
58 membuf_t
*mem
= (membuf_t
*) data
;
64 if ((p
= xrealloc (mem
->buf
, mem
->len
+ len
)) == NULL
)
68 memcpy ((char *) mem
->buf
+ mem
->len
, buffer
, len
);
74 status_cb (void *data
, const char *line
)
76 struct agent_s
*agent
= data
;
78 agent
->inquire_maxlen
= 0;
80 if (!strncmp (line
, "INQUIRE_MAXLEN ", 15))
82 agent
->inquire_maxlen
= atoi (line
+ 15);
83 if (!agent
->client_ctx
)
87 return send_status (agent
->client_ctx
, STATUS_AGENT
, "%s", line
);
91 assuan_command (struct agent_s
*a
, char **result
,
92 size_t * len
, const char *cmd
)
102 rc
= assuan_transact (a
->ctx
, cmd
, mem_realloc_cb
, &a
->data
,
103 a
->inquire_cb
, a
->inquire_data
, status_cb
, a
);
111 mem_realloc_cb (&a
->data
, "", 1);
113 *result
= (char *) a
->data
.buf
;
125 /* This commands are sent from launch_agent() after reconnecting to the agent
126 * and also from the initial client connection. */
128 send_agent_common_options (struct agent_s
*agent
)
132 rc
= send_to_agent (agent
, NULL
, NULL
, "OPTION cache-ttl-opt-preset=-1");
137 launch_agent (struct agent_s
*agent
)
140 assuan_context_t ctx
;
141 const char *t
= NULL
;
142 static struct assuan_malloc_hooks mhooks
= { xmalloc
, xrealloc
, xfree
};
143 char *s
, buf
[LINE_MAX
];
146 s
= config_get_string ("global", "agent_env_file");
149 char *ss
= expand_homedir (s
);
152 fp
= fopen (ss
, "r");
157 while ((p
= fgets (buf
, sizeof (buf
), fp
)))
160 if (!strncmp (p
, "GPG_AGENT_INFO=", strlen ("GPG_AGENT_INFO=")))
162 setenv ("GPG_AGENT_INFO", p
+ strlen ("GPG_AGENT_INFO="),
171 log_write ("%s: %s", s
, pwmd_strerror (gpg_error_from_syserror ()));
176 t
= getenv ("GPG_AGENT_INFO");
178 return gpg_error (GPG_ERR_NO_AGENT
);
180 rc
= assuan_new_ext (&ctx
, GPG_ERR_SOURCE_DEFAULT
, &mhooks
,
181 debug_level
? assuan_log_cb
: NULL
, "AGENT ");
185 char **fields
= str_split (t
, ":", 0);
186 rc
= assuan_socket_connect (ctx
, fields
[0], ASSUAN_INVALID_PID
, 0);
190 assuan_release (ctx
);
196 /* Restore the previous agent settings for this new context. */
197 assuan_release (agent
->ctx
);
199 rc
= send_agent_common_options (agent
);
200 if (!rc
&& agent
->display
)
201 rc
= set_agent_option (agent
, "display", agent
->display
);
203 if (!rc
&& agent
->ttyname
)
204 rc
= set_agent_option (agent
, "ttyname", agent
->ttyname
);
206 if (!rc
&& agent
->ttytype
)
207 rc
= set_agent_option (agent
, "ttytype", agent
->ttytype
);
209 if (!rc
&& agent
->lc_messages
)
210 rc
= set_agent_option (agent
, "lc-messages", agent
->lc_messages
);
212 if (!rc
&& agent
->lc_ctype
)
213 rc
= set_agent_option (agent
, "lc-ctype", agent
->lc_ctype
);
221 send_to_agent (struct agent_s
* agent
, char **result
, size_t * len
,
222 const char *fmt
, ...)
230 rc
= launch_agent (agent
);
236 if (str_vasprintf (&cmd
, fmt
, ap
) > 0)
238 rc
= assuan_command (agent
, result
, len
, cmd
);
239 if (!agent
->restart
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_DEFAULT
240 && (gpg_err_code (rc
) == GPG_ERR_ASS_CONNECT_FAILED
241 || gpg_err_code (rc
) == GPG_ERR_EPIPE
))
243 unsetenv ("GPG_AGENT_INFO");
245 rc
= launch_agent (agent
);
247 rc
= assuan_command (agent
, result
, len
, cmd
);
261 agent_disconnect (struct agent_s
*agent
)
267 assuan_release (agent
->ctx
);
273 cleanup_agent (struct agent_s
*agent
)
279 xfree (agent
->display
);
280 xfree (agent
->ttyname
);
281 xfree (agent
->ttytype
);
282 xfree (agent
->lc_messages
);
283 xfree (agent
->lc_ctype
);
286 agent_disconnect (agent
);
292 agent_init (struct agent_s
**agent
)
294 struct agent_s
*new = xcalloc (1, sizeof (struct agent_s
));
297 return GPG_ERR_ENOMEM
;
304 set_header_defaults (file_header_t
* hdr
)
306 char *s
= config_get_string (NULL
, "cipher");
307 int flags
= cipher_string_to_cipher (s
);
310 memset (hdr
, 0, sizeof (file_header_t
));
311 memcpy (hdr
->magic
, pwmd_magic
, sizeof (hdr
->magic
));
314 ("Invalid 'cipher' in configuration file. Using a default of aes256."));
315 hdr
->flags
= flags
== -1 ? PWMD_CIPHER_AES256
: flags
;
316 hdr
->version
= VERSION_HEX
;
320 read_data_header (const char *filename
, file_header_t
* rhdr
,
321 struct stat
*rst
, int *rfd
)
329 if (lstat (filename
, &st
) == -1)
330 return gpg_error_from_syserror ();
332 if (!S_ISREG (st
.st_mode
))
333 return GPG_ERR_ENOANO
;
335 fd
= open (filename
, O_RDONLY
);
337 return gpg_error_from_syserror ();
339 len
= read (fd
, &hdr
, sizeof (file_header_t
));
340 if (len
!= sizeof (file_header_t
))
341 rc
= rc
== -1 ? gpg_error_from_syserror () : GPG_ERR_BAD_DATA
;
343 if (!rc
&& memcmp (hdr
.magic
, pwmd_magic
, sizeof (hdr
.magic
)))
344 rc
= GPG_ERR_BAD_DATA
;
345 else if (hdr
.version
< 0x030000)
346 rc
= GPG_ERR_UNKNOWN_VERSION
;
366 read_data_file (const char *filename
, struct crypto_s
* crypto
)
374 cleanup_crypto_stage1 (crypto
);
375 rc
= read_data_header (filename
, &crypto
->hdr
, &st
, &fd
);
379 crypto
->ciphertext_len
= crypto
->hdr
.datalen
;
380 crypto
->ciphertext
= xmalloc (crypto
->hdr
.datalen
);
381 if (!crypto
->ciphertext
)
387 rlen
= read (fd
, crypto
->grip
, 20);
390 rc
= rc
== -1 ? gpg_error_from_syserror () : GPG_ERR_BAD_DATA
;
394 rlen
= read (fd
, crypto
->sign_grip
, 20);
397 rc
= rc
== -1 ? gpg_error_from_syserror () : GPG_ERR_BAD_DATA
;
401 len
= read (fd
, crypto
->ciphertext
, crypto
->hdr
.datalen
);
402 if (len
!= crypto
->hdr
.datalen
)
404 rc
= rc
== -1 ? gpg_error_from_syserror () : GPG_ERR_BAD_DATA
;
408 len
= st
.st_size
- sizeof (file_header_t
) - crypto
->hdr
.datalen
- 40;
416 rlen
= read (fd
, buf
, len
);
419 rc
= rc
== -1 ? gpg_error_from_syserror () : GPG_ERR_BAD_DATA
;
423 rc
= gcry_sexp_new (&crypto
->ciphertext_sexp
, buf
, rlen
, 1);
427 if (crypto
->pkey_sexp
)
428 gcry_sexp_release (crypto
->pkey_sexp
);
430 if (crypto
->sigpkey_sexp
)
431 gcry_sexp_release (crypto
->sigpkey_sexp
);
433 crypto
->pkey_sexp
= crypto
->sigpkey_sexp
= NULL
;
434 rc
= get_pubkey_bin (crypto
, crypto
->grip
, &crypto
->pkey_sexp
);
436 rc
= get_pubkey_bin (crypto
, crypto
->sign_grip
, &crypto
->sigpkey_sexp
);
445 inquire_cb (void *user
, const char *keyword
)
447 struct inquire_data_s
*idata
= user
;
449 if (!idata
->preset
&& (!strcmp (keyword
, "PASSPHRASE")
450 || !strcmp (keyword
, "NEW_PASSPHRASE")))
452 return agent_loopback_cb (idata
->crypto
, keyword
);
454 // SAVE --inquire-keyparam
455 else if (idata
->preset
&& !strcmp (keyword
, "KEYPARAM"))
458 return agent_loopback_cb (idata
->crypto
, keyword
);
461 if (idata
->crypto
->agent
->inquire_maxlen
462 && idata
->len
> idata
->crypto
->agent
->inquire_maxlen
)
464 log_write (_("Inquired data too large: have=%u, max=%u"), idata
->len
,
465 idata
->crypto
->agent
->inquire_maxlen
);
466 return GPG_ERR_TOO_LARGE
;
469 idata
->crypto
->agent
->inquire_maxlen
= 0;
470 return assuan_send_data (idata
->crypto
->agent
->ctx
, idata
->line
,
475 extract_key (struct crypto_s
*crypto
, unsigned char **result
,
479 gcry_sexp_t enc_sexp
= NULL
, tmp_sexp
;
480 struct inquire_data_s idata
= { 0 };
481 char *hexgrip
= NULL
;
483 size_t keylen
, keysize
;
485 int algo
= cipher_to_gcrypt (crypto
->hdr
.flags
);
488 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, &keysize
);
492 tmp_sexp
= gcry_sexp_find_token (crypto
->ciphertext_sexp
, "enc-val", 0);
494 return GPG_ERR_BAD_DATA
;
496 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
503 rc
= cache_is_shadowed (hexgrip
);
504 if (rc
&& rc
!= GPG_ERR_NO_DATA
)
507 shadowed
= !rc
? 1 : 0;
510 gcry_sexp_t tmp2_sexp
= gcry_sexp_cdr (tmp_sexp
);
511 gcry_sexp_release (tmp_sexp
);
512 tmp_sexp
= gcry_sexp_nth (tmp2_sexp
, 0);
513 gcry_sexp_release (tmp2_sexp
);
514 rc
= gcry_sexp_build (&enc_sexp
, NULL
, "(enc-val (flags pkcs1) %S)",
518 rc
= gcry_sexp_build (&enc_sexp
, NULL
, "%S", tmp_sexp
);
520 gcry_sexp_release (tmp_sexp
);
524 crypto
->agent
->inquire_cb
= inquire_cb
;
525 idata
.crypto
= crypto
;
526 idata
.len
= gcry_sexp_sprint (enc_sexp
, GCRYSEXP_FMT_CANON
, NULL
, 0);
527 idata
.line
= xmalloc (idata
.len
);
534 idata
.len
= gcry_sexp_sprint (enc_sexp
, GCRYSEXP_FMT_CANON
, idata
.line
,
536 crypto
->agent
->inquire_data
= &idata
;
537 gcry_sexp_release (enc_sexp
);
539 log_write1 (_("Keygrip is %s, bits=%i"), hexgrip
,
540 gcry_pk_get_nbits (crypto
->pkey_sexp
));
541 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
, "SETKEY %s", hexgrip
);
545 if (!crypto
->agent
->desc
)
549 ("A %s is required to unlock the secret key for the "
550 "encrypted data file \"%s\". Please enter the %s "
551 "below."), shadowed
? "PIN" : _("passphrase"),
552 crypto
->filename
, shadowed
? "PIN" : _("passphrase"));
555 tmp
= plus_escape (crypto
->agent
->desc
);
557 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
, "SETKEYDESC %s", tmp
);
562 assuan_begin_confidential (crypto
->agent
->ctx
);
563 rc
= send_to_agent (crypto
->agent
, &key
, &keylen
, "PKDECRYPT");
564 assuan_end_confidential (crypto
->agent
->ctx
);
568 rc
= gcry_sexp_new (&tmp_sexp
, key
, keylen
, 1);
573 key
= (char *) gcry_sexp_nth_data (tmp_sexp
, 1, result_len
);
576 *result
= gcry_malloc (*result_len
);
580 memcpy (*result
, key
, *result_len
);
583 rc
= GPG_ERR_BAD_DATA
;
585 gcry_sexp_release (tmp_sexp
);
592 gcry_sexp_release (enc_sexp
);
598 verify (gcry_sexp_t pkey
, gcry_sexp_t sig_sexp
, const void *data
, size_t len
)
601 unsigned hashlen
= gcry_md_get_algo_dlen (GCRY_MD_SHA256
);
603 gcry_sexp_t data_sexp
;
605 hash
= gcry_malloc (hashlen
);
607 return GPG_ERR_ENOMEM
;
609 gcry_md_hash_buffer (GCRY_MD_SHA256
, hash
, data
, len
);
610 rc
= gcry_sexp_build (&data_sexp
, NULL
,
611 "(data (flags pkcs1) (hash sha256 %b))", hashlen
,
616 rc
= gcry_pk_verify (sig_sexp
, data_sexp
, pkey
);
617 gcry_sexp_release (data_sexp
);
623 #define CRYPTO_BLOCKSIZE(c) (blocksize * 1024)
626 * Useful for a large amount of data. Rather than doing all of the data in one
627 * iteration do it in chunks. This lets the command be cancelable rather than
628 * waiting for it to complete.
631 iterate_crypto_once (gcry_cipher_hd_t h
, unsigned char *inbuf
,
632 size_t insize
, size_t blocksize
, status_msg_t which
)
635 off_t len
= CRYPTO_BLOCKSIZE (blocksize
);
636 void *p
= gcry_malloc (len
);
638 unsigned char *inbuf2
;
641 return GPG_ERR_ENOMEM
;
643 if (insize
< CRYPTO_BLOCKSIZE (blocksize
))
646 pthread_cleanup_push (gcry_free
, p
);
650 inbuf2
= inbuf
+ total
;
653 if (len
+ total
> insize
)
656 if (which
== STATUS_ENCRYPT
)
657 rc
= gcry_cipher_encrypt (h
, p
, len
, inbuf2
, len
);
659 rc
= gcry_cipher_decrypt (h
, p
, len
, inbuf2
, len
);
665 memmove (tmp
, p
, len
);
670 #ifdef HAVE_PTHREAD_TESTCANCEL
671 pthread_testcancel ();
675 pthread_cleanup_pop (1);
680 cleanup_cipher (void *arg
)
682 gcry_cipher_close ((gcry_cipher_hd_t
) arg
);
686 decrypt_data (assuan_context_t ctx
, struct crypto_s
*crypto
)
689 unsigned char *key
= NULL
;
691 gcry_cipher_hd_t h
= NULL
;
692 size_t blocksize
, keysize
;
693 int algo
= cipher_to_gcrypt (crypto
->hdr
.flags
);
695 gcry_sexp_t sig_sexp
;
696 uint64_t n
= crypto
->hdr
.iterations
;
699 rc
= extract_key (crypto
, &key
, &keylen
);
703 sig_sexp
= gcry_sexp_find_token (crypto
->ciphertext_sexp
, "sig-val", 0);
707 return GPG_ERR_BAD_DATA
;
710 pthread_cleanup_push (gcry_free
, key
);
712 verify (crypto
->sigpkey_sexp
, sig_sexp
, crypto
->ciphertext
,
713 crypto
->ciphertext_len
);
714 gcry_sexp_release (sig_sexp
);
718 rc
= gcry_cipher_open (&h
, algo
, GCRY_CIPHER_MODE_CBC
, 0);
721 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
,
725 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_BLKLEN
, NULL
,
729 rc
= gcry_cipher_setiv (h
, crypto
->hdr
.iv
,
730 sizeof (crypto
->hdr
.iv
));
733 rc
= gcry_cipher_setkey (h
, key
, keylen
);
740 pthread_cleanup_push (cleanup_cipher
, rc
? NULL
: h
);
744 outbuf
= gcry_malloc (crypto
->hdr
.datalen
);
749 pthread_cleanup_push (gcry_free
, outbuf
);
753 memcpy (outbuf
, crypto
->ciphertext
, crypto
->hdr
.datalen
);
754 rc
= iterate_crypto_once (h
, outbuf
, crypto
->hdr
.datalen
, blocksize
,
759 rc
= gcry_cipher_setkey (h
, key
, keylen
);
765 progress
= config_get_long (NULL
, "cipher_progress");
767 progress
= strtol (DEFAULT_ITERATION_PROGRESS
, NULL
, 10);
770 if (!rc
&& ctx
&& crypto
->hdr
.iterations
)
771 rc
= send_status (ctx
, STATUS_DECRYPT
, "%llu %llu", 0,
772 crypto
->hdr
.iterations
);
774 for (n
= 0; !rc
&& n
< crypto
->hdr
.iterations
; n
++)
776 if (ctx
&& !(n
% progress
))
778 rc
= send_status (ctx
, STATUS_DECRYPT
, "%llu %llu", n
,
779 crypto
->hdr
.iterations
);
784 rc
= gcry_cipher_setiv (h
, crypto
->hdr
.iv
, sizeof (crypto
->hdr
.iv
));
788 rc
= iterate_crypto_once (h
, outbuf
, crypto
->hdr
.datalen
, blocksize
,
792 if (!rc
&& ctx
&& crypto
->hdr
.iterations
)
793 rc
= send_status (ctx
, STATUS_DECRYPT
, "%llu %llu", n
,
794 crypto
->hdr
.iterations
);
796 pthread_cleanup_pop (rc
? 1 : 0); // outbuf
797 pthread_cleanup_pop (1); // cipher
798 pthread_cleanup_pop (1); // key
801 crypto
->plaintext
= outbuf
;
802 crypto
->plaintext_len
= crypto
->hdr
.datalen
;
809 decrypt_xml (struct crypto_s
* crypto
, const void *data
, size_t len
)
811 gcry_cipher_hd_t h
= NULL
;
814 rc
= gcry_cipher_open (&h
, GCRY_CIPHER_AES
, GCRY_CIPHER_MODE_CBC
, 0);
818 gcry_free (crypto
->plaintext
);
819 crypto
->plaintext
= gcry_malloc (len
);
820 if (!crypto
->plaintext
)
826 rc
= gcry_cipher_setiv (h
, cache_iv
, cache_blocksize
);
830 rc
= gcry_cipher_setkey (h
, cache_key
, cache_keysize
);
834 rc
= gcry_cipher_decrypt (h
, crypto
->plaintext
, len
, data
, len
);
837 gcry_free (crypto
->plaintext
);
838 crypto
->plaintext
= NULL
;
841 crypto
->plaintext_len
= len
;
845 gcry_cipher_close (h
);
851 encrypt_xml (assuan_context_t ctx
, void *key
, size_t keylen
,
852 int algo
, const void *xml
, size_t len
, void * *result
, size_t
853 * result_len
, unsigned char **iv
, size_t * iv_len
, uint64_t iter
)
857 size_t blocksize
, keysize
;
862 unsigned char *tmpkey
= NULL
;
863 int free_iv
= *(iv_len
) == 0;
865 rc
= gcry_cipher_open (&h
, algo
, GCRY_CIPHER_MODE_CBC
, 0);
869 pthread_cleanup_push (cleanup_cipher
, h
);
870 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, &keysize
);
872 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_BLKLEN
, NULL
, &blocksize
);
874 if (!rc
&& *(iv_len
) == 0)
876 *(iv
) = xmalloc (blocksize
);
880 gcry_create_nonce (*(iv
), blocksize
);
883 pthread_cleanup_push (xfree
, *(iv_len
) == 0 ? *(iv
) : NULL
);
887 tmpkey
= gcry_malloc (keylen
);
892 pthread_cleanup_push (gcry_free
, tmpkey
);
896 memcpy (tmpkey
, key
, keylen
);
898 rc
= gcry_cipher_setkey (h
, tmpkey
, keylen
);
902 len
+= blocksize
- (len
% blocksize
);
908 inbuf
= gcry_malloc (len
);
913 pthread_cleanup_push (gcry_free
, inbuf
);
917 memset (inbuf
, 0, len
);
918 memcpy (inbuf
, xml
, olen
);
919 progress
= config_get_long (NULL
, "cipher_progress");
921 progress
= strtol (DEFAULT_ITERATION_PROGRESS
, NULL
, 10);
923 if (!rc
&& ctx
&& iter
)
924 rc
= send_status (ctx
, STATUS_ENCRYPT
, "%llu %llu", 0, iter
);
926 for (n
= 0; !rc
&& n
< iter
; n
++)
928 if (ctx
&& !(n
% progress
))
930 rc
= send_status (ctx
, STATUS_ENCRYPT
, "%llu %llu", n
, iter
);
935 rc
= gcry_cipher_setiv (h
, *(iv
), blocksize
);
939 rc
= iterate_crypto_once (h
, inbuf
, len
, blocksize
, STATUS_ENCRYPT
);
943 if (!rc
&& ctx
&& iter
)
944 rc
= send_status (ctx
, STATUS_ENCRYPT
, "%llu %llu", n
, iter
);
948 /* Do at least one iteration. */
949 rc
= gcry_cipher_setiv (h
, *(iv
), blocksize
);
952 rc
= gcry_cipher_setkey (h
, key
, keylen
);
954 rc
= iterate_crypto_once (h
, inbuf
, len
, blocksize
,
959 pthread_cleanup_pop (rc
? 1 : 0); // inbuf
960 pthread_cleanup_pop (1); // tmpkey
961 pthread_cleanup_pop (rc
&& free_iv
? 1 : 0); // iv
962 pthread_cleanup_pop (1); // cipher
963 *result
= rc
? NULL
: inbuf
;
969 agent_loopback_cb (void *user
, const char *keyword
)
971 struct crypto_s
*crypto
= user
;
973 unsigned char *result
;
977 if (!strcmp (keyword
, "KEYPARAM"))
980 rc
= assuan_inquire (crypto
->client_ctx
, keyword
, &result
, &len
, 0);
983 { // PASSPHRASE or NEW_PASSPHRASE
984 assuan_begin_confidential (crypto
->client_ctx
);
985 rc
= assuan_inquire (crypto
->client_ctx
, keyword
, &result
, &len
, 0);
986 assuan_end_confidential (crypto
->client_ctx
);
991 if (keyparam
&& !len
)
993 char *tmp
= default_key_params (crypto
);
996 return gpg_error (GPG_ERR_ENOMEM
);
999 result
= xmalloc (len
);
1000 memcpy (result
, tmp
, len
);
1004 pthread_cleanup_push (xfree
, result
);
1007 rc
= assuan_send_data (crypto
->agent
->ctx
, result
, len
);
1010 assuan_begin_confidential (crypto
->agent
->ctx
);
1011 rc
= assuan_send_data (crypto
->agent
->ctx
, result
, len
);
1012 assuan_end_confidential (crypto
->agent
->ctx
);
1015 pthread_cleanup_pop (1);
1017 else if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
1019 gpg_error_t arc
= assuan_write_line (crypto
->agent
->ctx
, "CAN");
1026 arc
= assuan_read_line (crypto
->agent
->ctx
, &line
, &len
);
1038 sign (gcry_sexp_t
* rsexp
, const char *sign_hexgrip
,
1039 struct crypto_s
*crypto
, const void *data
, size_t len
)
1043 char *tmp
= sign_hexgrip
? str_dup (sign_hexgrip
)
1044 : bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
1046 pthread_cleanup_push (xfree
, tmp
);
1047 log_write1 (_("Signed with keygrip %s"), tmp
);
1048 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
, "SIGKEY %s", tmp
);
1049 pthread_cleanup_pop (1);
1052 unsigned char *hash
;
1053 unsigned hashlen
= gcry_md_get_algo_dlen (GCRY_MD_SHA256
);
1055 hash
= gcry_malloc (hashlen
);
1057 return GPG_ERR_ENOMEM
;
1059 gcry_md_hash_buffer (GCRY_MD_SHA256
, hash
, data
, len
);
1060 tmp
= bin2hex (hash
, hashlen
);
1062 pthread_cleanup_push (xfree
, tmp
);
1063 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
,
1064 "SETHASH --hash=sha256 %s", tmp
);
1065 pthread_cleanup_pop (1);
1070 struct inquire_data_s idata
= { 0 };
1072 idata
.crypto
= crypto
;
1073 crypto
->agent
->inquire_data
= &idata
;
1074 crypto
->agent
->inquire_cb
= inquire_cb
;
1075 rc
= send_to_agent (crypto
->agent
, &result
, &len
, "PKSIGN");
1078 rc
= gcry_sexp_sscan (rsexp
, NULL
, result
, len
);
1088 cleanup_acl (void *arg
)
1090 acl_t acl
= *(acl_t
*) arg
;
1098 write_file (struct crypto_s
*crypto
, const char *filename
,
1099 void *data
, size_t data_len
, void *sexp
, size_t sexp_len
,
1100 gcry_sexp_t pubkey
, gcry_sexp_t sigpkey
)
1102 char tmp
[FILENAME_MAX
] = { 0 };
1114 if (lstat (filename
, &st
) == 0)
1116 mode
= st
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
1118 if (!(mode
& S_IWUSR
))
1119 return GPG_ERR_EACCES
;
1121 else if (errno
!= ENOENT
)
1122 return gpg_error_from_syserror ();
1124 snprintf (tmp
, sizeof (tmp
), "%s.XXXXXX", filename
);
1128 rc
= gpg_error_from_syserror ();
1129 log_write ("%s: %s", tmp
, pwmd_strerror (rc
));
1135 // xml_import() or convert_file() from command line.
1139 pthread_cleanup_push (cleanup_unlink_cb
, tmp
);
1140 crypto
->save
.hdr
.version
= VERSION_HEX
;
1141 len
= write (fd
, &crypto
->save
.hdr
, sizeof (file_header_t
));
1142 if (len
== sizeof (file_header_t
))
1144 unsigned char grip
[20];
1146 gcry_pk_get_keygrip (pubkey
, grip
);
1147 len
= write (fd
, grip
, sizeof (grip
));
1148 if (len
== sizeof (grip
))
1150 gcry_pk_get_keygrip (sigpkey
, grip
);
1151 len
= write (fd
, grip
, sizeof (grip
));
1152 if (len
== sizeof (grip
))
1154 len
= write (fd
, data
, data_len
);
1155 if (len
== data_len
)
1157 len
= write (fd
, sexp
, sexp_len
);
1158 if (len
!= sexp_len
)
1159 rc
= gpg_error_from_syserror ();
1162 rc
= gpg_error_from_syserror ();
1166 rc
= gpg_error_from_syserror ();
1169 rc
= gpg_error_from_syserror ();
1172 pthread_cleanup_push (cleanup_acl
, &acl
);
1176 if (fsync (fd
) != -1)
1178 if (filename
&& close (fd
) != -1)
1181 acl
= acl_get_file (filename
, ACL_TYPE_ACCESS
);
1182 if (!acl
&& errno
== ENOENT
)
1183 acl
= acl_get_file (".", ACL_TYPE_DEFAULT
);
1185 log_write ("ACL: %s: %s", filename
,
1186 pwmd_strerror (gpg_error_from_syserror ()));
1189 if (mode
&& config_get_boolean (filename
, "backup"))
1191 char tmp2
[FILENAME_MAX
];
1193 snprintf (tmp2
, sizeof (tmp2
), "%s.backup", filename
);
1194 if (rename (filename
, tmp2
) == -1)
1195 rc
= gpg_error_from_syserror ();
1199 rc
= gpg_error_from_syserror ();
1202 rc
= gpg_error_from_syserror ();
1207 if (filename
&& rename (tmp
, filename
) != -1)
1210 if (filename
&& mode
)
1211 chmod (filename
, mode
);
1214 if (acl
&& acl_set_file (filename
, ACL_TYPE_ACCESS
, acl
))
1215 log_write ("ACL: %s: %s", filename
,
1216 pwmd_strerror (gpg_error_from_syserror ()));
1220 rc
= gpg_error_from_syserror ();
1224 pthread_cleanup_pop (1);
1226 pthread_cleanup_pop (rc
? 1 : 0); // unlink
1231 encrypt_data_file (assuan_context_t ctx
, struct crypto_s
* crypto
,
1232 gcry_sexp_t pubkey
, gcry_sexp_t sigpkey
,
1233 const char *filename
, const void *xml
, size_t len
)
1237 size_t data_len
= 0;
1238 void *enc_xml
= NULL
;
1239 size_t enc_xml_len
= 0;
1240 unsigned char *iv
= NULL
;
1242 int algo
= cipher_to_gcrypt (crypto
->save
.hdr
.flags
);
1245 unsigned char sig_grip
[20];
1246 unsigned char grip
[20];
1248 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, &keysize
);
1252 pthread_cleanup_push (gcry_free
, key
);
1253 key
= gcry_random_bytes_secure (keysize
, GCRY_STRONG_RANDOM
);
1254 #ifdef HAVE_PTHREAD_TESTCANCEL
1255 pthread_testcancel (); // may have been a long operation
1257 pthread_cleanup_pop (0);
1259 return GPG_ERR_ENOMEM
;
1261 gcry_pk_get_keygrip (pubkey
, grip
);
1262 gcry_pk_get_keygrip (sigpkey
, sig_grip
);
1263 pthread_cleanup_push (xfree
, iv
);
1264 pthread_cleanup_push (gcry_free
, key
);
1265 rc
= encrypt_xml (ctx
, key
, keysize
, algo
, xml
, len
, &enc_xml
, &enc_xml_len
,
1266 &iv
, &iv_len
, crypto
->save
.hdr
.iterations
);
1269 gcry_sexp_t sig_sexp
= NULL
;
1270 char *hexgrip
= bin2hex (grip
, 20);
1272 log_write1 (_("Encrypted with keygrip %s"), hexgrip
);
1274 hexgrip
= bin2hex (sig_grip
, 20);
1275 pthread_cleanup_push (gcry_free
, enc_xml
);
1276 rc
= sign (&sig_sexp
, hexgrip
, crypto
, enc_xml
, enc_xml_len
);
1281 rc
= verify (sigpkey
, sig_sexp
, enc_xml
, enc_xml_len
);
1285 gcry_sexp_t tmp_sexp
;
1287 rc
= gcry_sexp_build (&tmp_sexp
, NULL
,
1288 "(data (flags pkcs1) (value %b))",
1292 gcry_sexp_t key_sexp
;
1294 pthread_cleanup_push ((void (*)(void *)) gcry_sexp_release
,
1296 rc
= gcry_pk_encrypt (&key_sexp
, tmp_sexp
, pubkey
);
1297 pthread_cleanup_pop (0);
1298 gcry_sexp_release (tmp_sexp
);
1302 memcpy (crypto
->save
.hdr
.iv
, iv
, iv_len
);
1303 crypto
->save
.hdr
.datalen
= enc_xml_len
;
1304 rc
= gcry_sexp_build (&tmp_sexp
, NULL
, "%S%S", key_sexp
,
1306 gcry_sexp_release (key_sexp
);
1310 data_len
= gcry_sexp_sprint (tmp_sexp
,
1313 data
= xmalloc (data_len
);
1315 gcry_sexp_sprint (tmp_sexp
, GCRYSEXP_FMT_CANON
,
1318 rc
= GPG_ERR_ENOMEM
;
1320 gcry_sexp_release (tmp_sexp
);
1327 pthread_cleanup_pop (0); // enc_xml
1330 gcry_sexp_release (sig_sexp
);
1333 pthread_cleanup_pop (1); // key
1334 pthread_cleanup_pop (1); // iv
1338 pthread_cleanup_push (gcry_free
, enc_xml
);
1339 rc
= write_file (crypto
, filename
, enc_xml
, enc_xml_len
, data
, data_len
,
1341 pthread_cleanup_pop (1); // enc_xml
1343 memcpy (&crypto
->hdr
, &crypto
->save
.hdr
, sizeof (file_header_t
));
1351 cleanup_save (struct save_s
*save
)
1357 gcry_sexp_release (save
->pkey
);
1360 gcry_sexp_release (save
->sigpkey
);
1362 memset (save
, 0, sizeof (struct save_s
));
1365 /* Keep the agent ctx to retain pinentry options which will be freed in
1366 * cleanup_cb(). Also keep .pubkey since it may be needed for a SAVE. */
1368 cleanup_crypto_stage1 (struct crypto_s
*cr
)
1373 cleanup_save (&cr
->save
);
1375 if (cr
->ciphertext_sexp
)
1376 gcry_sexp_release (cr
->ciphertext_sexp
);
1379 gcry_free (cr
->plaintext
);
1381 xfree (cr
->ciphertext
);
1382 xfree (cr
->filename
);
1383 cr
->filename
= NULL
;
1384 cr
->ciphertext_sexp
= NULL
;
1385 cr
->ciphertext
= NULL
;
1386 cr
->ciphertext_len
= 0;
1387 cr
->plaintext
= NULL
;
1388 cr
->plaintext_len
= 0;
1392 cleanup_crypto_stage2 (struct crypto_s
*cr
)
1397 cleanup_crypto_stage1 (cr
);
1398 set_header_defaults (&cr
->hdr
);
1402 cleanup_crypto (struct crypto_s
**c
)
1404 struct crypto_s
*cr
= *c
;
1409 cleanup_crypto_stage2 (cr
);
1412 gcry_sexp_release (cr
->pkey_sexp
);
1414 if (cr
->sigpkey_sexp
)
1415 gcry_sexp_release (cr
->sigpkey_sexp
);
1418 cleanup_agent (cr
->agent
);
1425 init_client_crypto (struct crypto_s
**crypto
)
1427 struct crypto_s
*new = xcalloc (1, sizeof (struct crypto_s
));
1432 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1433 pwmd_strerror (GPG_ERR_ENOMEM
));
1434 return GPG_ERR_ENOMEM
;
1437 rc
= agent_init (&new->agent
);
1440 rc
= send_agent_common_options (new->agent
);
1442 rc
= set_pinentry_options (new->agent
);
1451 set_header_defaults (&new->hdr
);
1457 generate_key (struct crypto_s
* crypto
, char *sexpstr
, int empty
, int preset
)
1463 if (crypto
->save
.s2k_count
)
1465 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
,
1466 "OPTION s2k-count=%lu", crypto
->save
.s2k_count
);
1471 if (!crypto
->agent
->inquire_cb
)
1472 crypto
->agent
->inquire_cb
= inquire_cb
;
1474 rc
= send_to_agent (crypto
->agent
, &pkey
, &plen
, "GENKEY %s%s",
1475 preset
? "--preset " : "",
1476 empty
? "--no-protection" : "");
1480 if (crypto
->save
.pkey
)
1481 gcry_sexp_release (crypto
->save
.pkey
);
1483 crypto
->save
.pkey
= NULL
;
1484 rc
= gcry_sexp_new (&crypto
->save
.pkey
, pkey
, plen
, 1);
1487 unsigned char grip
[20];
1489 gcry_pk_get_keygrip (crypto
->save
.pkey
, grip
);
1490 char *hexgrip
= bin2hex (grip
, sizeof (grip
));
1491 log_write1 (_("Keygrip is %s"), hexgrip
);
1494 if (!crypto
->save
.sigpkey
)
1496 gcry_sexp_build ((gcry_sexp_t
*) & crypto
->save
.sigpkey
, NULL
, "%S",
1506 set_agent_option (struct agent_s
* agent
, const char *name
, const char *value
)
1508 return send_to_agent (agent
, NULL
, NULL
, "OPTION %s=%s", name
, value
);
1512 set_agent_passphrase (struct crypto_s
* crypto
, const char *key
, size_t len
)
1515 struct inquire_data_s idata
;
1519 /* This is for use with key files or passphrases obtained from an inquire.
1520 * gpg-agent uses strings as passphrases and will truncate the passphrase
1521 * at the first encountered null byte. It's only a warning because the
1522 * passphrase may belong to a key shared outside of pwmd. */
1523 for (i
= 0; i
< len
; i
++)
1527 log_write (_("WARNING: keylen=%i, truncated to %i."), len
, i
);
1532 hexgrip
= bin2hex (crypto
->grip
, 20);
1533 crypto
->agent
->inquire_cb
= inquire_cb
;
1534 crypto
->agent
->inquire_data
= &idata
;
1535 idata
.crypto
= crypto
;
1536 idata
.line
= (char *) key
, idata
.len
= len
;
1538 assuan_begin_confidential (crypto
->agent
->ctx
);
1539 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
,
1540 "PRESET_PASSPHRASE --inquire %s -1", hexgrip
);
1541 assuan_end_confidential (crypto
->agent
->ctx
);
1548 set_pinentry_mode (struct agent_s
* agent
, const char *mode
)
1550 return set_agent_option (agent
, "pinentry-mode", mode
);
1554 get_pubkey_bin (struct crypto_s
* crypto
, const unsigned char *grip
,
1555 gcry_sexp_t
* result
)
1557 char *hexgrip
= bin2hex (grip
, 20);
1561 return GPG_ERR_ENOMEM
;
1563 rc
= get_pubkey (crypto
, hexgrip
, result
);
1569 get_pubkey (struct crypto_s
* crypto
, const char *grip
, gcry_sexp_t
* result
)
1575 rc
= send_to_agent (crypto
->agent
, &pkey
, &plen
, "READKEY %s", grip
);
1577 rc
= gcry_sexp_new (result
, pkey
, plen
, 1);
1584 set_pinentry_options (struct agent_s
* agent
)
1588 if (getenv ("DISPLAY"))
1590 rc
= set_agent_option (agent
, "display", getenv ("DISPLAY"));
1593 xfree (agent
->display
);
1594 agent
->display
= str_dup (getenv ("DISPLAY"));
1597 else if (ttyname (STDOUT_FILENO
))
1599 rc
= set_agent_option (agent
, "ttyname", ttyname (STDOUT_FILENO
));
1602 rc
= set_agent_option (agent
, "ttytype", getenv ("TERM"));
1605 xfree (agent
->ttyname
);
1606 xfree (agent
->ttytype
);
1607 agent
->ttyname
= str_dup (ttyname (STDOUT_FILENO
));
1608 agent
->ttytype
= str_dup (getenv ("TERM"));
1617 inquire_keyfile (void *user
, const char *keyword
)
1619 struct crypto_s
*crypto
= user
;
1620 char *filename
= crypto
->agent
->inquire_data2
;
1621 char *params
= crypto
->agent
->inquire_data3
;
1628 if (!strcmp (keyword
, "KEYPARAM"))
1629 return assuan_send_data (crypto
->agent
->ctx
, params
, strlen (params
));
1631 // This function is only used when generating a new keypair.
1632 if (strcmp (keyword
, "NEW_PASSPHRASE"))
1633 return gpg_error (GPG_ERR_ASS_UNKNOWN_INQUIRE
);
1635 if (stat (filename
, &st
) == -1)
1636 return gpg_error_from_syserror ();
1638 if (crypto
->agent
->inquire_maxlen
1639 && st
.st_size
> crypto
->agent
->inquire_maxlen
)
1641 log_write (_("The passphrase is too large: have=%u, max=%u."),
1642 (unsigned) st
.st_size
, crypto
->agent
->inquire_maxlen
);
1643 return GPG_ERR_TOO_LARGE
;
1646 buf
= gcry_malloc_secure (st
.st_size
);
1648 return GPG_ERR_ENOMEM
;
1650 fd
= open (filename
, O_RDONLY
);
1652 rc
= gpg_error_from_syserror ();
1655 len
= read (fd
, buf
, st
.st_size
);
1656 if (len
== st
.st_size
)
1658 assuan_begin_confidential (crypto
->agent
->ctx
);
1659 rc
= assuan_send_data (crypto
->agent
->ctx
, buf
, len
);
1660 assuan_end_confidential (crypto
->agent
->ctx
);
1663 rc
= gpg_error_from_syserror ();
1665 rc
= GPG_ERR_BUFFER_TOO_SHORT
;
1674 export_common (struct crypto_s
* crypto
, const char *hexgrip
,
1675 const char *sign_hexgrip
, int no_passphrase
,
1676 const void *data
, size_t datalen
, const char *outfile
,
1677 const char *keyparams
, const char *keyfile
)
1681 if (!sign_hexgrip
&& hexgrip
)
1682 sign_hexgrip
= hexgrip
;
1686 if (crypto
->sigpkey_sexp
)
1687 gcry_sexp_release (crypto
->sigpkey_sexp
);
1689 crypto
->sigpkey_sexp
= NULL
;
1690 rc
= get_pubkey (crypto
, sign_hexgrip
, &crypto
->save
.sigpkey
);
1694 gcry_pk_get_keygrip (crypto
->save
.sigpkey
, crypto
->sign_grip
);
1699 rc
= get_pubkey (crypto
, hexgrip
, &crypto
->save
.pkey
);
1701 gcry_pk_get_keygrip (crypto
->save
.pkey
, crypto
->grip
);
1705 struct inquire_data_s idata
= { 0 };
1706 char *params
= keyparams
? str_dup (keyparams
)
1707 : default_key_params (crypto
);
1709 pthread_cleanup_push (xfree
, params
);
1710 log_write (_("Generating a new keypair ..."));
1713 log_write (_("Using passphrase obtained from file '%s'"), keyfile
);
1714 rc
= set_pinentry_mode (crypto
->agent
, "loopback");
1715 crypto
->agent
->inquire_cb
= inquire_keyfile
;
1716 crypto
->agent
->inquire_data
= crypto
;
1717 crypto
->agent
->inquire_data2
= (char *) keyfile
;
1718 crypto
->agent
->inquire_data3
= params
;
1722 idata
.line
= params
;
1723 idata
.len
= strlen (params
);
1724 idata
.crypto
= crypto
;
1725 crypto
->agent
->inquire_cb
= inquire_cb
;
1726 crypto
->agent
->inquire_data
= &idata
;
1731 rc
= generate_key (crypto
, params
, no_passphrase
, 1);
1732 gcry_pk_get_keygrip (crypto
->save
.pkey
, crypto
->grip
);
1735 (void) set_pinentry_mode (crypto
->agent
, "ask");
1736 pthread_cleanup_pop (1);
1741 rc
= encrypt_data_file (NULL
, crypto
, crypto
->save
.pkey
,
1742 crypto
->save
.sigpkey
, outfile
, data
, datalen
);
1745 char *tmp
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
1747 log_write (_("Success! Keygrip is %s."), tmp
);
1748 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
,
1749 "CLEAR_PASSPHRASE --mode=normal %s", tmp
);
1754 tmp
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
1755 log_write (_("Signed with keygrip %s."), tmp
);
1765 default_key_params (struct crypto_s
*crypto
)
1767 int len
= config_get_integer (NULL
, "nbits");
1769 char *algo
= config_get_string (NULL
, "algo");
1772 snprintf (buf
, sizeof (buf
), "%i", len
);
1773 result
= str_asprintf ("(genkey (%s (nbits %lu:%i)))", algo
, strlen (buf
),
1780 agent_passwd (struct crypto_s
* crypto
)
1782 struct inquire_data_s idata
= { 0 };
1784 char *tmp
= bin2hex (crypto
->grip
, 20);
1786 idata
.crypto
= crypto
;
1787 crypto
->agent
->inquire_cb
= inquire_cb
;
1788 crypto
->agent
->inquire_data
= &idata
;
1789 rc
= send_to_agent (crypto
->agent
, NULL
, NULL
, "PASSWD --preset %s", tmp
);
1795 kill_scd (struct agent_s
* agent
)
1799 if (config_get_boolean (NULL
, "kill_scd"))
1801 rc
= send_to_agent (agent
, NULL
, NULL
, "SCD KILLSCD");
1802 if (rc
&& gpg_err_code (rc
) != GPG_ERR_NO_SCDAEMON
)
1803 log_write ("%s: %s", __FUNCTION__
, pwmd_strerror (rc
));