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/>.
25 #include <sys/types.h>
34 #include "pwmd-error.h"
35 #include "util-misc.h"
42 #include "util-string.h"
44 static uint8_t pwmd_magic
[5] = { '\177', 'P', 'W', 'M', 'D' };
47 set_header_defaults (file_header_t
* hdr
)
49 char *s
= config_get_string (NULL
, "cipher");
50 int flags
= cipher_string_to_cipher (s
);
53 memset (hdr
, 0, sizeof (file_header_t
));
54 memcpy (hdr
->magic
, pwmd_magic
, sizeof (hdr
->magic
));
57 ("Invalid 'cipher' in configuration file. Using a default of aes256."));
58 hdr
->flags
= flags
== -1 ? PWMD_CIPHER_AES256
: flags
;
59 hdr
->version
= VERSION_HEX
;
63 hdr
->flags
|= PWMD_FLAG_PKCS
;
68 read_data_header (const char *filename
, file_header_t
* rhdr
,
69 struct stat
*rst
, int *rfd
)
77 if (lstat (filename
, &st
) == -1)
78 return gpg_error_from_syserror ();
80 if (!S_ISREG (st
.st_mode
))
81 return GPG_ERR_ENOANO
;
83 fd
= open (filename
, O_RDONLY
);
85 return gpg_error_from_syserror ();
87 len
= read (fd
, &hdr
, sizeof (file_header_t
));
88 if (len
!= sizeof (file_header_t
))
89 rc
= rc
== -1 ? gpg_error_from_syserror () : GPG_ERR_BAD_DATA
;
91 if (!rc
&& memcmp (hdr
.magic
, pwmd_magic
, sizeof (hdr
.magic
)))
92 rc
= GPG_ERR_BAD_DATA
;
93 else if (hdr
.version
< 0x030000)
94 rc
= GPG_ERR_UNKNOWN_VERSION
;
114 read_data_file (const char *filename
, struct crypto_s
* crypto
)
122 cleanup_crypto_stage1 (crypto
);
123 rc
= read_data_header (filename
, &crypto
->hdr
, &st
, &fd
);
127 crypto
->ciphertext_len
= crypto
->hdr
.datalen
;
128 crypto
->ciphertext
= xmalloc (crypto
->hdr
.datalen
);
129 if (!crypto
->ciphertext
)
135 if (crypto
->hdr
.flags
& PWMD_FLAG_PKCS
)
137 rlen
= read (fd
, crypto
->grip
, 20);
140 rc
= rc
== -1 ? gpg_error_from_syserror () : GPG_ERR_BAD_DATA
;
144 rlen
= read (fd
, crypto
->sign_grip
, 20);
147 rc
= rc
== -1 ? gpg_error_from_syserror () : GPG_ERR_BAD_DATA
;
152 len
= read (fd
, crypto
->ciphertext
, crypto
->hdr
.datalen
);
153 if (len
!= crypto
->hdr
.datalen
)
155 rc
= rc
== -1 ? gpg_error_from_syserror () : GPG_ERR_BAD_DATA
;
159 if (!(crypto
->hdr
.flags
& PWMD_FLAG_PKCS
))
163 rc
= GPG_ERR_NOT_IMPLEMENTED
;
167 return GPG_ERR_NOT_IMPLEMENTED
;
169 len
= st
.st_size
- sizeof (file_header_t
) - crypto
->hdr
.datalen
- 40;
177 rlen
= read (fd
, buf
, len
);
180 rc
= rc
== -1 ? gpg_error_from_syserror () : GPG_ERR_BAD_DATA
;
184 rc
= gcry_sexp_new (&crypto
->ciphertext_sexp
, buf
, rlen
, 1);
188 if (crypto
->pkey_sexp
)
189 gcry_sexp_release (crypto
->pkey_sexp
);
191 if (crypto
->sigpkey_sexp
)
192 gcry_sexp_release (crypto
->sigpkey_sexp
);
194 crypto
->pkey_sexp
= crypto
->sigpkey_sexp
= NULL
;
195 rc
= get_pubkey_bin (crypto
, crypto
->grip
, &crypto
->pkey_sexp
);
197 rc
= get_pubkey_bin (crypto
, crypto
->sign_grip
, &crypto
->sigpkey_sexp
);
206 #define CRYPTO_BLOCKSIZE(c) (blocksize * 1024)
209 * Useful for a large amount of data. Rather than doing all of the data in one
210 * iteration do it in chunks. This lets the command be cancelable rather than
211 * waiting for it to complete.
214 iterate_crypto_once (gcry_cipher_hd_t h
, unsigned char *inbuf
,
215 size_t insize
, size_t blocksize
, status_msg_t which
)
218 off_t len
= CRYPTO_BLOCKSIZE (blocksize
);
219 void *p
= gcry_malloc (len
);
221 unsigned char *inbuf2
;
224 return GPG_ERR_ENOMEM
;
226 if (insize
< CRYPTO_BLOCKSIZE (blocksize
))
229 pthread_cleanup_push (gcry_free
, p
);
233 inbuf2
= inbuf
+ total
;
236 if (len
+ total
> insize
)
239 if (which
== STATUS_ENCRYPT
)
240 rc
= gcry_cipher_encrypt (h
, p
, len
, inbuf2
, len
);
242 rc
= gcry_cipher_decrypt (h
, p
, len
, inbuf2
, len
);
248 memmove (tmp
, p
, len
);
253 #ifdef HAVE_PTHREAD_TESTCANCEL
254 pthread_testcancel ();
258 pthread_cleanup_pop (1);
263 cleanup_cipher (void *arg
)
265 gcry_cipher_close ((gcry_cipher_hd_t
) arg
);
269 decrypt_data (assuan_context_t ctx
, struct crypto_s
*crypto
,
270 unsigned char *salted_key
, size_t skeylen
)
273 unsigned char *key
= salted_key
;
274 gcry_cipher_hd_t h
= NULL
;
275 size_t blocksize
, keysize
= 0;
276 int algo
= cipher_to_gcrypt (crypto
->hdr
.flags
);
278 uint64_t n
= crypto
->hdr
.iterations
;
281 size_t keylen
= skeylen
;
282 gcry_sexp_t sig_sexp
;
284 if (crypto
->hdr
.flags
& PWMD_FLAG_PKCS
)
286 rc
= agent_extract_key (crypto
, &key
, &keylen
);
290 sig_sexp
= gcry_sexp_find_token (crypto
->ciphertext_sexp
, "sig-val", 0);
294 return GPG_ERR_BAD_DATA
;
297 rc
= agent_verify (crypto
->sigpkey_sexp
, sig_sexp
, crypto
->ciphertext
,
298 crypto
->ciphertext_len
);
299 gcry_sexp_release (sig_sexp
);
305 rc
= gcry_cipher_open (&h
, algo
, GCRY_CIPHER_MODE_CBC
, 0);
308 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
,
312 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_BLKLEN
, NULL
,
316 rc
= gcry_cipher_setiv (h
, crypto
->hdr
.iv
,
317 sizeof (crypto
->hdr
.iv
));
320 rc
= gcry_cipher_setkey (h
, key
, keysize
);
327 pthread_cleanup_push (cleanup_cipher
, rc
? NULL
: h
);
331 outbuf
= gcry_malloc (crypto
->hdr
.datalen
);
336 pthread_cleanup_push (gcry_free
, outbuf
);
338 pthread_cleanup_push (gcry_free
, key
);
343 memcpy (outbuf
, crypto
->ciphertext
, crypto
->hdr
.datalen
);
344 rc
= iterate_crypto_once (h
, outbuf
, crypto
->hdr
.datalen
, blocksize
,
349 rc
= gcry_cipher_setkey (h
, key
, keysize
);
355 progress
= config_get_long (NULL
, "cipher_progress");
357 progress
= strtol (DEFAULT_ITERATION_PROGRESS
, NULL
, 10);
360 if (!rc
&& ctx
&& crypto
->hdr
.iterations
)
361 rc
= send_status (ctx
, STATUS_DECRYPT
, "%llu %llu", 0,
362 crypto
->hdr
.iterations
);
364 for (n
= 0; !rc
&& n
< crypto
->hdr
.iterations
; n
++)
366 if (ctx
&& !(n
% progress
))
368 rc
= send_status (ctx
, STATUS_DECRYPT
, "%llu %llu", n
,
369 crypto
->hdr
.iterations
);
374 rc
= gcry_cipher_setiv (h
, crypto
->hdr
.iv
, sizeof (crypto
->hdr
.iv
));
378 rc
= iterate_crypto_once (h
, outbuf
, crypto
->hdr
.datalen
, blocksize
,
382 if (!rc
&& ctx
&& crypto
->hdr
.iterations
)
383 rc
= send_status (ctx
, STATUS_DECRYPT
, "%llu %llu", n
,
384 crypto
->hdr
.iterations
);
387 pthread_cleanup_pop (0);
388 if (crypto
->hdr
.flags
& PWMD_FLAG_PKCS
)
395 pthread_cleanup_pop (rc
? 1 : 0); /* outbuf */
396 pthread_cleanup_pop (1); /* cipher */
399 char buf
[] = "<?xml ";
401 if (memcmp (outbuf
, buf
, sizeof(buf
)-1))
404 return gpg_error (GPG_ERR_BAD_PASSPHRASE
);
407 crypto
->plaintext
= outbuf
;
408 crypto
->plaintext_len
= crypto
->hdr
.datalen
;
415 decrypt_xml (struct crypto_s
* crypto
, const void *data
, size_t len
)
417 gcry_cipher_hd_t h
= NULL
;
420 rc
= gcry_cipher_open (&h
, GCRY_CIPHER_AES
, GCRY_CIPHER_MODE_CBC
, 0);
424 gcry_free (crypto
->plaintext
);
425 crypto
->plaintext
= gcry_malloc (len
);
426 if (!crypto
->plaintext
)
432 rc
= gcry_cipher_setiv (h
, cache_iv
, cache_blocksize
);
436 rc
= gcry_cipher_setkey (h
, cache_key
, cache_keysize
);
440 rc
= gcry_cipher_decrypt (h
, crypto
->plaintext
, len
, data
, len
);
441 if (rc
|| strncmp (crypto
->plaintext
, "<?xml ", 6))
444 rc
= GPG_ERR_BAD_DATA
;
446 gcry_free (crypto
->plaintext
);
447 crypto
->plaintext
= NULL
;
450 crypto
->plaintext_len
= len
;
454 gcry_cipher_close (h
);
460 encrypt_xml (assuan_context_t ctx
, void *key
, size_t keylen
,
461 int algo
, const void *xml
, size_t len
, void * *result
,
462 size_t *result_len
, unsigned char **iv
, size_t * iv_len
,
467 size_t blocksize
, keysize
;
472 unsigned char *tmpkey
= NULL
;
473 int free_iv
= *(iv_len
) == 0;
475 rc
= gcry_cipher_open (&h
, algo
, GCRY_CIPHER_MODE_CBC
, 0);
479 pthread_cleanup_push (cleanup_cipher
, h
);
480 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, &keysize
);
482 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_BLKLEN
, NULL
, &blocksize
);
484 if (!rc
&& *(iv_len
) == 0)
486 *(iv
) = xmalloc (blocksize
);
490 gcry_create_nonce (*(iv
), blocksize
);
493 pthread_cleanup_push (xfree
, *(iv_len
) == 0 ? *(iv
) : NULL
);
497 tmpkey
= gcry_malloc (keysize
);
502 pthread_cleanup_push (gcry_free
, tmpkey
);
506 memcpy (tmpkey
, key
, keysize
);
508 rc
= gcry_cipher_setkey (h
, tmpkey
, keysize
);
512 len
+= blocksize
- (len
% blocksize
);
518 inbuf
= gcry_malloc (len
);
523 pthread_cleanup_push (gcry_free
, inbuf
);
527 memset (inbuf
, 0, len
);
528 memcpy (inbuf
, xml
, olen
);
529 progress
= config_get_long (NULL
, "cipher_progress");
531 progress
= strtol (DEFAULT_ITERATION_PROGRESS
, NULL
, 10);
533 if (!rc
&& ctx
&& iter
)
534 rc
= send_status (ctx
, STATUS_ENCRYPT
, "%llu %llu", 0, iter
);
536 for (n
= 0; !rc
&& n
< iter
; n
++)
538 if (ctx
&& !(n
% progress
))
540 rc
= send_status (ctx
, STATUS_ENCRYPT
, "%llu %llu", n
, iter
);
545 rc
= gcry_cipher_setiv (h
, *(iv
), blocksize
);
549 rc
= iterate_crypto_once (h
, inbuf
, len
, blocksize
, STATUS_ENCRYPT
);
553 if (!rc
&& ctx
&& iter
)
554 rc
= send_status (ctx
, STATUS_ENCRYPT
, "%llu %llu", n
, iter
);
558 /* Do at least one iteration. */
559 rc
= gcry_cipher_setiv (h
, *(iv
), blocksize
);
562 rc
= gcry_cipher_setkey (h
, key
, keysize
);
564 rc
= iterate_crypto_once (h
, inbuf
, len
, blocksize
,
569 pthread_cleanup_pop (rc
? 1 : 0); /* inbuf */
570 pthread_cleanup_pop (1); /* tmpkey */
571 pthread_cleanup_pop (rc
&& free_iv
? 1 : 0); /* iv */
572 pthread_cleanup_pop (1); /* cipher */
573 *result
= rc
? NULL
: inbuf
;
579 cleanup_save (struct save_s
*save
)
586 gcry_sexp_release (save
->pkey
);
589 gcry_sexp_release (save
->sigpkey
);
592 memset (save
, 0, sizeof (struct save_s
));
595 /* Keep the agent ctx to retain pinentry options which will be freed in
596 * cleanup_cb(). Also keep .pubkey since it may be needed for a SAVE. */
598 cleanup_crypto_stage1 (struct crypto_s
*cr
)
603 cleanup_save (&cr
->save
);
606 if (cr
->ciphertext_sexp
)
607 gcry_sexp_release (cr
->ciphertext_sexp
);
609 cr
->ciphertext_sexp
= NULL
;
612 gcry_free (cr
->plaintext
);
613 xfree (cr
->ciphertext
);
614 xfree (cr
->filename
);
616 cr
->ciphertext
= NULL
;
617 cr
->ciphertext_len
= 0;
618 cr
->plaintext
= NULL
;
619 cr
->plaintext_len
= 0;
623 cleanup_crypto_stage2 (struct crypto_s
*cr
)
628 cleanup_crypto_stage1 (cr
);
629 set_header_defaults (&cr
->hdr
);
633 cleanup_crypto (struct crypto_s
**c
)
635 struct crypto_s
*cr
= *c
;
640 cleanup_crypto_stage2 (cr
);
644 gcry_sexp_release (cr
->pkey_sexp
);
646 if (cr
->sigpkey_sexp
)
647 gcry_sexp_release (cr
->sigpkey_sexp
);
650 cleanup_agent (cr
->agent
);
658 init_client_crypto (struct crypto_s
**crypto
)
660 struct crypto_s
*new = xcalloc (1, sizeof (struct crypto_s
));
672 rc
= agent_init (&new->agent
);
675 rc
= send_agent_common_options (new->agent
);
677 rc
= agent_set_pinentry_options (new->agent
);
688 set_header_defaults (&new->hdr
);
694 export_common (assuan_context_t ctx
, int inquire
, struct crypto_s
* crypto
,
695 const void *data
, size_t datalen
, const char *outfile
,
696 const char *keyfile
, void **rkey
, size_t *rkeylen
,
697 int use_cache
, int force
)
700 void *enc_xml
= NULL
;
701 size_t enc_xml_len
= 0;
702 unsigned char *iv
= NULL
;
704 int algo
= cipher_to_gcrypt (crypto
->save
.hdr
.flags
);
705 void *key
= NULL
, *salted_key
= NULL
;
706 size_t keysize
, keylen
;
709 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, &keysize
);
720 if (stat (keyfile
, &st
) == -1)
721 return gpg_error_from_syserror ();
723 buf
= gcry_malloc (st
.st_size
);
725 return GPG_ERR_ENOMEM
;
727 fd
= open (keyfile
, O_RDONLY
);
729 rc
= gpg_error_from_syserror ();
732 len
= read (fd
, buf
, st
.st_size
);
733 if (len
!= st
.st_size
)
734 rc
= GPG_ERR_TOO_SHORT
;
742 log_write (_("Using passphrase obtained from file '%s'"), keyfile
);
749 tmp
= gcry_malloc (1);
760 cached
= cache_iscached (outfile
, NULL
) == 0;
763 struct cache_data_s
*cdata
= cache_get_data_filename (outfile
);
765 salted_key
= gcry_malloc (cdata
->keylen
);
768 memcpy (salted_key
, cdata
->key
, cdata
->keylen
);
769 keysize
= cdata
->keylen
;
782 struct crypto_s
*tmp
= NULL
;
783 gpg_error_t rc
= init_client_crypto (&tmp
);
787 rc
= read_data_file (outfile
, tmp
);
790 rc
= decrypt_common (ctx
, inquire
, tmp
, outfile
,
791 (char **)&key
, &keylen
);
797 cleanup_crypto (&tmp
);
798 if (rc
&& gpg_err_code (rc
) == GPG_ERR_ENOENT
)
804 if (!use_cache
) // PASSWD or new file
809 rc
= inquire_passphrase (ctx
, "NEW_PASSPHRASE",
810 (unsigned char **)&key
, &keylen
);
814 rc
= getpin_common (ctx
, outfile
, PINENTRY_SAVE
,
815 (char **)&key
, &keylen
);
832 rc
= hash_key (algo
, crypto
->save
.hdr
.salt
,
833 sizeof(crypto
->save
.hdr
.salt
), key
, keylen
, &salted_key
,
839 rc
= encrypt_xml (ctx
, salted_key
, keysize
, algo
, data
, datalen
,
840 &enc_xml
, &enc_xml_len
, &iv
, &iv_len
,
841 crypto
->save
.hdr
.iterations
);
845 memcpy (crypto
->save
.hdr
.iv
, iv
, iv_len
);
847 crypto
->save
.hdr
.datalen
= enc_xml_len
;
848 rc
= write_file (crypto
, outfile
, enc_xml
, enc_xml_len
, NULL
, 0, NULL
,
855 memcpy (&crypto
->hdr
, &crypto
->save
.hdr
, sizeof (file_header_t
));
860 gcry_free (salted_key
);
867 cleanup_acl (void *arg
)
869 acl_t acl
= *(acl_t
*) arg
;
877 write_file (struct crypto_s
*crypto
, const char *filename
,
878 void *data
, size_t data_len
, void *sexp
, size_t sexp_len
,
879 void *pubkey
, void *sigpkey
)
881 char tmp
[FILENAME_MAX
] = { 0 };
893 if (lstat (filename
, &st
) == 0)
895 mode
= st
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
897 if (!(mode
& S_IWUSR
))
898 return GPG_ERR_EACCES
;
900 else if (errno
!= ENOENT
)
901 return gpg_error_from_syserror ();
903 snprintf (tmp
, sizeof (tmp
), "%s.XXXXXX", filename
);
907 rc
= gpg_error_from_syserror ();
908 log_write ("%s: %s", tmp
, pwmd_strerror (rc
));
914 // xml_import() or convert_file() from command line.
918 pthread_cleanup_push (cleanup_unlink_cb
, tmp
);
919 crypto
->save
.hdr
.version
= VERSION_HEX
;
920 len
= write (fd
, &crypto
->save
.hdr
, sizeof (file_header_t
));
921 if (len
== sizeof (file_header_t
))
923 if (crypto
->save
.hdr
.flags
& PWMD_FLAG_PKCS
)
925 unsigned char grip
[20];
927 gcry_pk_get_keygrip ((gcry_sexp_t
) pubkey
, grip
);
928 len
= write (fd
, grip
, sizeof (grip
));
929 if (len
== sizeof (grip
))
931 gcry_pk_get_keygrip ((gcry_sexp_t
) sigpkey
, grip
);
932 len
= write (fd
, grip
, sizeof (grip
));
933 if (len
== sizeof (grip
))
935 len
= write (fd
, data
, data_len
);
938 len
= write (fd
, sexp
, sexp_len
);
940 rc
= gpg_error_from_syserror ();
943 rc
= gpg_error_from_syserror ();
947 rc
= gpg_error_from_syserror ();
951 len
= write (fd
, data
, data_len
);
953 rc
= gpg_error_from_syserror ();
957 rc
= gpg_error_from_syserror ();
960 pthread_cleanup_push (cleanup_acl
, &acl
);
964 if (fsync (fd
) != -1)
966 if (filename
&& close (fd
) != -1)
969 acl
= acl_get_file (filename
, ACL_TYPE_ACCESS
);
970 if (!acl
&& errno
== ENOENT
)
971 acl
= acl_get_file (".", ACL_TYPE_DEFAULT
);
973 log_write ("ACL: %s: %s", filename
,
974 pwmd_strerror (gpg_error_from_syserror ()));
977 if (mode
&& config_get_boolean (filename
, "backup"))
979 char tmp2
[FILENAME_MAX
];
981 snprintf (tmp2
, sizeof (tmp2
), "%s.backup", filename
);
982 if (rename (filename
, tmp2
) == -1)
983 rc
= gpg_error_from_syserror ();
987 rc
= gpg_error_from_syserror ();
990 rc
= gpg_error_from_syserror ();
995 if (filename
&& rename (tmp
, filename
) != -1)
998 if (filename
&& mode
)
999 chmod (filename
, mode
);
1002 if (acl
&& acl_set_file (filename
, ACL_TYPE_ACCESS
, acl
))
1003 log_write ("ACL: %s: %s", filename
,
1004 pwmd_strerror (gpg_error_from_syserror ()));
1008 rc
= gpg_error_from_syserror ();
1012 pthread_cleanup_pop (1);
1014 pthread_cleanup_pop (rc
? 1 : 0); // unlink
1019 hash_key (int algo
, unsigned char *salt
, size_t salt_len
, const void *key
,
1020 size_t keylen
, void **result
, size_t *rlen
)
1025 /* Be sure the algorithm is available. */
1026 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, rlen
);
1030 /* Always allocate enough for a 256-bit key although the algorithms
1031 themselves may use less. Fixes SAVE --cipher with a different
1032 keylen than the previously saved cipher when cached. */
1034 tmp
= xmalloc (*rlen
);
1036 return GPG_ERR_ENOMEM
;
1038 rc
= gcry_kdf_derive(key
, keylen
, GCRY_KDF_ITERSALTED_S2K
, GCRY_MD_SHA1
,
1039 salt
, salt_len
, DEFAULT_KDFS2K_ITERATIONS
, *rlen
, tmp
);
1049 change_passwd (assuan_context_t ctx
, const char *filename
, int inquire
,
1050 struct crypto_s
**rcrypto
)
1052 unsigned char *key
= NULL
;
1054 struct crypto_s
*crypto
= NULL
;
1055 gpg_error_t rc
= init_client_crypto (&crypto
);
1060 crypto
->client_ctx
= ctx
;
1061 rc
= read_data_file (filename
, crypto
);
1064 cleanup_crypto (&crypto
);
1068 rc
= decrypt_common (ctx
, inquire
, crypto
, filename
, (char **)&key
, &keylen
);
1073 cleanup_crypto (&crypto
);
1079 memcpy (&crypto
->save
.hdr
, &crypto
->hdr
, sizeof (file_header_t
));
1080 rc
= export_common (ctx
, inquire
, crypto
, crypto
->plaintext
,
1081 crypto
->plaintext_len
, crypto
->filename
, NULL
,
1082 (void **)&key
, &keylen
, 0, 0);
1089 rc
= save_common (filename
, crypto
, crypto
->plaintext
,
1090 crypto
->plaintext_len
, key
, keylen
, &cached
, 1);
1098 cleanup_crypto (&crypto
);
1104 // FIXME gcry_sexp_t is updated in cdata. make a temp var in case of failure.
1106 save_common (const char *filename
, struct crypto_s
*crypto
,
1107 const unsigned char *data
, size_t datalen
,
1108 const unsigned char *key
, size_t keylen
, int *cached
, int no_agent
)
1110 struct cache_data_s
*cdata
;
1113 /* This is safe since it is a fast operation. */
1115 pthread_cleanup_push (cleanup_cache_mutex
, NULL
);
1116 cdata
= cache_get_data_filename (filename
);
1117 *cached
= cdata
!= NULL
;
1120 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
1123 if (use_agent
&& !no_agent
)
1125 /* Update in case of any --keygrip argument */
1127 gcry_sexp_release (cdata
->pubkey
);
1129 gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
, "%S",
1133 gcry_sexp_release (cdata
->sigkey
);
1135 gcry_sexp_build ((gcry_sexp_t
*) & cdata
->sigkey
, NULL
, "%S",
1136 crypto
->sigpkey_sexp
);
1140 gcry_free (cdata
->doc
);
1142 cdata
->key
= (unsigned char *)key
;
1143 cdata
->keylen
= keylen
;
1144 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
, GCRY_CIPHER_AES
, data
,
1145 datalen
, &cdata
->doc
, &cdata
->doclen
, &cache_iv
,
1146 &cache_blocksize
, 0);
1149 unsigned char md5file
[16];
1151 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
1152 rc
= cache_set_data (md5file
, cdata
, crypto
->grip
);
1155 pthread_cleanup_pop (1); // mutex unlock
1160 getpin_common (assuan_context_t ctx
, const char *filename
, int which
,
1161 char **rkey
, size_t *rkeylen
)
1164 struct client_s
*client
= ctx
? assuan_get_pointer (ctx
) : NULL
;
1165 struct pinentry_s
*pin
= pinentry_init (filename
);
1168 return GPG_ERR_ENOMEM
;
1171 pinentry_merge_options (&pin
->pinentry_opts
, &client
->pinentry_opts
);
1173 rc
= pinentry_lock (ctx
, pin
);
1175 rc
= pinentry_getpin (pin
, rkey
, which
);
1177 pinentry_deinit (pin
);
1181 *rkeylen
= strlen (*rkey
);
1188 /* Note: 'key' is freed with gcry_free() and not xfree(). Although
1189 * both call xfree(). */
1191 inquire_passphrase (assuan_context_t ctx
, const char *keyword
,
1192 unsigned char **result
, size_t *rlen
)
1196 assuan_begin_confidential (ctx
);
1197 rc
= assuan_inquire (ctx
, keyword
, result
, rlen
, 0);
1198 assuan_end_confidential (ctx
);
1203 decrypt_common (assuan_context_t ctx
, int inquire
, struct crypto_s
*crypto
,
1204 const char *filename
, char **rkey
, size_t *rkeylen
)
1208 gpg_error_t rc
= read_data_file (filename
, crypto
);
1209 int algo
= cipher_to_gcrypt (crypto
->hdr
.flags
);
1210 void *salted_key
= NULL
;
1215 log_write ("ERR %i: %s", rc
, pwmd_strerror (rc
));
1219 key
= config_get_string (filename
, "passphrase");
1222 log_write (_("Trying the passphrase specified in config ..."));
1223 keylen
= strlen (key
);
1228 key
= config_get_string (filename
, "passphrase_file");
1231 char *tmp
= expand_homedir (key
);
1237 log_write (_("Trying the passphrase using file '%s' ..."), key
);
1238 fd
= open (key
, O_RDONLY
);
1241 rc
= gpg_error_from_syserror ();
1242 log_write ("%s: %s", key
, pwmd_strerror (rc
));
1249 key
= xmalloc (st
.st_size
);
1252 if (read (fd
, key
, st
.st_size
) != st
.st_size
)
1254 log_write ("short read() count");
1255 rc
= GPG_ERR_TOO_SHORT
;
1259 rc
= GPG_ERR_ENOMEM
;
1265 log_write ("%s", pwmd_strerror (rc
));
1270 keylen
= st
.st_size
;
1282 if (!key
&& !IS_PKCS (crypto
) && inquire
)
1284 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
1288 log_write ("ERR %i: %s", rc
, pwmd_strerror (rc
));
1295 else if (!key
&& !IS_PKCS (crypto
))
1297 rc
= getpin_common (ctx
, filename
, PINENTRY_OPEN
, &key
, &keylen
);
1300 log_write ("%s", pwmd_strerror (rc
));
1305 else if (key
&& IS_PKCS (crypto
))
1307 rc
= set_agent_passphrase (crypto
, key
, keylen
);
1311 log_write ("ERR %i: %s", rc
, pwmd_strerror (rc
));
1317 if (key
&& !IS_PKCS (crypto
))
1319 rc
= hash_key (algo
, crypto
->hdr
.salt
, sizeof(crypto
->hdr
.salt
), key
,
1320 keylen
, &salted_key
, &keysize
);
1322 key
= (char *)salted_key
;
1331 xfree (crypto
->filename
);
1332 crypto
->filename
= str_dup (filename
);
1333 rc
= decrypt_data (NULL
, crypto
, (unsigned char *)key
, keylen
);
1336 log_write ("ERR %i: %s", rc
, pwmd_strerror (rc
));