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/>.
25 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
32 #include "pwmd-error.h"
33 #include "util-misc.h"
40 #include "util-string.h"
42 static uint8_t pwmd_magic
[5] = { '\177', 'P', 'W', 'M', 'D' };
44 /* Sets the default cipher values for new files. */
46 set_header_defaults (file_header_t
* hdr
)
48 char *s
= config_get_string (NULL
, "cipher");
49 int flags
= cipher_string_to_cipher (s
);
52 memset (hdr
, 0, sizeof (file_header_t
));
53 memcpy (hdr
->magic
, pwmd_magic
, sizeof (hdr
->magic
));
56 ("Invalid 'cipher' in configuration file. Using a default of aes256."));
57 hdr
->flags
= flags
== -1 ? PWMD_CIPHER_AES256
: flags
;
58 hdr
->version
= VERSION_HEX
;
62 hdr
->flags
|= PWMD_FLAG_PKI
;
67 read_header (file_header_t
*hdr
, int fd
)
73 #ifdef WORDS_BIGENDIAN
74 len
= read (fd
, &hdr
->magic
, sizeof(hdr
->magic
));
78 len
= read (fd
, &hdr
->version
, sizeof(hdr
->version
));
82 len
= read (fd
, &hdr
->iterations
, sizeof(hdr
->iterations
));
86 len
= read (fd
, &hdr
->flags
, sizeof(hdr
->flags
));
90 len
= read (fd
, &hdr
->iv
, sizeof(hdr
->iv
));
94 len
= read (fd
, &hdr
->salt
, sizeof(hdr
->salt
));
98 len
= read (fd
, &hdr
->datalen
, sizeof(hdr
->datalen
));
102 len
= read (fd
, &hdr
->magic
, sizeof(hdr
->magic
));
106 len
= read (fd
, &i
, sizeof(hdr
->version
));
109 hdr
->version
= ntohl (i
);
111 len
= read (fd
, &n
, sizeof(hdr
->iterations
));
114 hdr
->iterations
= (((uint64_t) ntohl (n
)) << 32) + ntohl (n
>> 32);
116 len
= read (fd
, &n
, sizeof(hdr
->flags
));
119 hdr
->flags
= (((uint64_t) ntohl (n
)) << 32) + ntohl (n
>> 32);
121 len
= read (fd
, &hdr
->iv
, sizeof(hdr
->iv
));
125 len
= read (fd
, &hdr
->salt
, sizeof(hdr
->salt
));
129 len
= read (fd
, &i
, sizeof(hdr
->datalen
));
132 hdr
->datalen
= ntohl (i
);
136 return len
== -1 ? gpg_error_from_errno (errno
) : 0;
139 /* Read the header of a data file to determine cipher and other. The
140 * header is stored big endian in the file and is converted to little
141 * endian when needed. */
143 read_data_header (const char *filename
, file_header_t
* rhdr
,
144 struct stat
*rst
, int *rfd
)
151 if (lstat (filename
, &st
) == -1)
152 return gpg_error_from_errno (errno
);
154 if (!S_ISREG (st
.st_mode
))
155 return gpg_error (GPG_ERR_ENOANO
);
157 fd
= open (filename
, O_RDONLY
);
159 return gpg_error_from_errno (errno
);
161 rc
= read_header (&hdr
, fd
);
162 if (!rc
&& memcmp (hdr
.magic
, pwmd_magic
, sizeof (hdr
.magic
)))
163 rc
= GPG_ERR_BAD_DATA
;
164 else if (!rc
&& hdr
.version
< 0x030000)
165 rc
= GPG_ERR_UNKNOWN_VERSION
;
181 return gpg_error (rc
);
185 read_data_file (const char *filename
, struct crypto_s
* crypto
)
193 cleanup_crypto_stage1 (crypto
);
194 rc
= read_data_header (filename
, &crypto
->hdr
, &st
, &fd
);
196 return gpg_error (rc
);
198 crypto
->ciphertext_len
= crypto
->hdr
.datalen
;
199 crypto
->ciphertext
= xmalloc (crypto
->hdr
.datalen
);
200 if (!crypto
->ciphertext
)
206 /* The keygrips for PKI files are stored after the header. They are
207 * stored in the file to let file(1) magic(5) show the grips. */
208 if (crypto
->hdr
.flags
& PWMD_FLAG_PKI
)
210 rlen
= read (fd
, crypto
->grip
, 20);
213 rc
= rc
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
217 rlen
= read (fd
, crypto
->sign_grip
, 20);
220 rc
= rc
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
225 len
= read (fd
, crypto
->ciphertext
, crypto
->hdr
.datalen
);
226 if (len
!= crypto
->hdr
.datalen
)
228 rc
= rc
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
232 if (!(crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
237 rc
= GPG_ERR_NOT_IMPLEMENTED
;
242 len
= st
.st_size
- sizeof (file_header_t
) - crypto
->hdr
.datalen
- 40;
250 /* Remaining data file bytes are the encrypted key and XML. */
251 rlen
= read (fd
, buf
, len
);
254 rc
= rc
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
258 rc
= gcry_sexp_new (&crypto
->ciphertext_sexp
, buf
, rlen
, 1);
262 if (crypto
->pkey_sexp
)
263 gcry_sexp_release (crypto
->pkey_sexp
);
265 if (crypto
->sigpkey_sexp
)
266 gcry_sexp_release (crypto
->sigpkey_sexp
);
268 crypto
->pkey_sexp
= crypto
->sigpkey_sexp
= NULL
;
269 rc
= get_pubkey_bin (crypto
, crypto
->grip
, &crypto
->pkey_sexp
);
271 rc
= get_pubkey_bin (crypto
, crypto
->sign_grip
, &crypto
->sigpkey_sexp
);
277 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
283 #define CRYPTO_BLOCKSIZE(c) (blocksize * 1024)
286 * Useful for a large amount of data. Rather than doing all of the data in one
287 * iteration do it in chunks. This lets the command be cancelable rather than
288 * waiting for it to complete.
291 iterate_crypto_once (gcry_cipher_hd_t h
, unsigned char *inbuf
,
292 size_t insize
, size_t blocksize
, status_msg_t which
)
295 off_t len
= CRYPTO_BLOCKSIZE (blocksize
);
296 void *p
= gcry_malloc (len
);
300 return gpg_error (GPG_ERR_ENOMEM
);
302 if (insize
< CRYPTO_BLOCKSIZE (blocksize
))
305 pthread_cleanup_push (gcry_free
, p
);
309 unsigned char *inbuf2
= inbuf
+ total
;
312 if (len
+ total
> insize
)
315 if (which
== STATUS_ENCRYPT
)
316 rc
= gcry_cipher_encrypt (h
, p
, len
, inbuf2
, len
);
318 rc
= gcry_cipher_decrypt (h
, p
, len
, inbuf2
, len
);
324 memmove (tmp
, p
, len
);
329 #ifdef HAVE_PTHREAD_TESTCANCEL
330 pthread_testcancel ();
334 pthread_cleanup_pop (1);
335 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
342 cleanup_cipher (void *arg
)
344 gcry_cipher_close ((gcry_cipher_hd_t
) arg
);
347 /* Decrypt the XML data. For PKI data files the key is retrieved from
348 * gpg-agent and the signature verified. */
350 decrypt_data (assuan_context_t ctx
, struct crypto_s
*crypto
,
351 unsigned char *salted_key
, size_t skeylen
)
354 unsigned char *key
= salted_key
;
355 gcry_cipher_hd_t h
= NULL
;
356 size_t blocksize
, keysize
= 0;
357 int algo
= cipher_to_gcrypt (crypto
->hdr
.flags
);
359 uint64_t n
= crypto
->hdr
.iterations
;
362 size_t keylen
= skeylen
;
363 gcry_sexp_t sig_sexp
;
365 if (crypto
->hdr
.flags
& PWMD_FLAG_PKI
)
367 rc
= agent_extract_key (crypto
, &key
, &keylen
);
371 sig_sexp
= gcry_sexp_find_token (crypto
->ciphertext_sexp
, "sig-val", 0);
375 return GPG_ERR_BAD_DATA
;
378 rc
= agent_verify (crypto
->sigpkey_sexp
, sig_sexp
, crypto
->ciphertext
,
379 crypto
->ciphertext_len
);
380 gcry_sexp_release (sig_sexp
);
386 rc
= gcry_cipher_open (&h
, algo
, GCRY_CIPHER_MODE_CBC
, 0);
389 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
,
393 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_BLKLEN
, NULL
,
397 rc
= gcry_cipher_setiv (h
, crypto
->hdr
.iv
, blocksize
);
400 rc
= gcry_cipher_setkey (h
, key
, keysize
);
407 pthread_cleanup_push (cleanup_cipher
, rc
? NULL
: h
);
411 outbuf
= gcry_malloc (crypto
->hdr
.datalen
);
416 pthread_cleanup_push (gcry_free
, outbuf
);
418 pthread_cleanup_push (gcry_free
, key
);
423 memcpy (outbuf
, crypto
->ciphertext
, crypto
->hdr
.datalen
);
424 rc
= iterate_crypto_once (h
, outbuf
, crypto
->hdr
.datalen
, blocksize
,
429 rc
= gcry_cipher_setkey (h
, key
, keysize
);
435 progress
= config_get_long (NULL
, "cipher_progress");
437 progress
= strtol (DEFAULT_ITERATION_PROGRESS
, NULL
, 10);
440 if (!rc
&& ctx
&& crypto
->hdr
.iterations
)
441 rc
= send_status (ctx
, STATUS_DECRYPT
, "%llu %llu", 0,
442 crypto
->hdr
.iterations
);
444 for (n
= 0; !rc
&& n
< crypto
->hdr
.iterations
; n
++)
446 if (ctx
&& !(n
% progress
))
448 rc
= send_status (ctx
, STATUS_DECRYPT
, "%llu %llu", n
,
449 crypto
->hdr
.iterations
);
454 rc
= gcry_cipher_setiv (h
, crypto
->hdr
.iv
, blocksize
);
458 rc
= iterate_crypto_once (h
, outbuf
, crypto
->hdr
.datalen
, blocksize
,
462 if (!rc
&& ctx
&& crypto
->hdr
.iterations
)
463 rc
= send_status (ctx
, STATUS_DECRYPT
, "%llu %llu", n
,
464 crypto
->hdr
.iterations
);
467 pthread_cleanup_pop (0);
468 if (crypto
->hdr
.flags
& PWMD_FLAG_PKI
)
475 pthread_cleanup_pop (rc
? 1 : 0); /* outbuf */
476 pthread_cleanup_pop (1); /* cipher */
479 char buf
[] = "<?xml ";
481 if (memcmp (outbuf
, buf
, sizeof(buf
)-1))
484 return gpg_error (GPG_ERR_BAD_PASSPHRASE
);
487 crypto
->plaintext
= outbuf
;
488 crypto
->plaintext_len
= crypto
->hdr
.datalen
;
491 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
497 /* The cached document is encrypted with encrypt_xml(). This decrypts
498 * it from the OPEN command. */
500 decrypt_cache (struct crypto_s
* crypto
, const void *data
, size_t len
)
502 gcry_cipher_hd_t h
= NULL
;
505 rc
= gcry_cipher_open (&h
, GCRY_CIPHER_AES
, GCRY_CIPHER_MODE_CBC
, 0);
509 gcry_free (crypto
->plaintext
);
510 crypto
->plaintext
= gcry_malloc (len
);
511 if (!crypto
->plaintext
)
517 rc
= gcry_cipher_setiv (h
, cache_iv
, cache_blocksize
);
521 rc
= gcry_cipher_setkey (h
, cache_key
, cache_keysize
);
525 rc
= gcry_cipher_decrypt (h
, crypto
->plaintext
, len
, data
, len
);
526 if (rc
|| strncmp (crypto
->plaintext
, "<?xml ", 6))
529 rc
= GPG_ERR_BAD_DATA
;
531 gcry_free (crypto
->plaintext
);
532 crypto
->plaintext
= NULL
;
535 crypto
->plaintext_len
= len
;
539 gcry_cipher_close (h
);
544 /* Encrypt the XML data and set 'result'. At least one encryption
545 * iteration is done. */
547 encrypt_xml (assuan_context_t ctx
, void *key
, size_t keylen
,
548 int algo
, const void *xml
, size_t len
, void * *result
,
549 size_t *result_len
, unsigned char **iv
, size_t * iv_len
,
554 size_t blocksize
, keysize
;
558 unsigned char *tmpkey
= NULL
;
559 int init_iv
= *(iv_len
) == 0;
561 rc
= gcry_cipher_open (&h
, algo
, GCRY_CIPHER_MODE_CBC
, 0);
565 pthread_cleanup_push (cleanup_cipher
, h
);
566 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, &keysize
);
568 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_BLKLEN
, NULL
, &blocksize
);
572 *(iv
) = xmalloc (blocksize
);
576 gcry_create_nonce (*(iv
), blocksize
);
584 tmpkey
= gcry_malloc (keysize
);
589 pthread_cleanup_push (gcry_free
, tmpkey
);
593 memcpy (tmpkey
, key
, keysize
);
595 rc
= gcry_cipher_setkey (h
, tmpkey
, keysize
);
599 len
+= blocksize
- (len
% blocksize
);
605 inbuf
= gcry_malloc (len
);
610 pthread_cleanup_push (gcry_free
, inbuf
);
614 long progress
= config_get_long (NULL
, "cipher_progress");
616 memset (inbuf
, 0, len
);
617 memcpy (inbuf
, xml
, olen
);
620 progress
= strtol (DEFAULT_ITERATION_PROGRESS
, NULL
, 10);
622 if (!rc
&& ctx
&& iter
)
623 rc
= send_status (ctx
, STATUS_ENCRYPT
, "%llu %llu", 0, iter
);
625 for (n
= 0; !rc
&& n
< iter
; n
++)
627 if (ctx
&& !(n
% progress
))
629 rc
= send_status (ctx
, STATUS_ENCRYPT
, "%llu %llu", n
, iter
);
634 rc
= gcry_cipher_setiv (h
, *(iv
), blocksize
);
638 rc
= iterate_crypto_once (h
, inbuf
, len
, blocksize
, STATUS_ENCRYPT
);
642 if (!rc
&& ctx
&& iter
)
643 rc
= send_status (ctx
, STATUS_ENCRYPT
, "%llu %llu", n
, iter
);
647 /* Do at least one iteration. */
648 rc
= gcry_cipher_setiv (h
, *(iv
), blocksize
);
651 rc
= gcry_cipher_setkey (h
, key
, keysize
);
653 rc
= iterate_crypto_once (h
, inbuf
, len
, blocksize
,
658 pthread_cleanup_pop (rc
? 1 : 0); /* inbuf */
659 pthread_cleanup_pop (1); /* tmpkey */
660 pthread_cleanup_pop (1); /* cipher */
668 *result
= rc
? NULL
: inbuf
;
674 cleanup_save (struct save_s
*save
)
681 gcry_sexp_release (save
->pkey
);
684 gcry_sexp_release (save
->sigpkey
);
687 memset (save
, 0, sizeof (struct save_s
));
690 /* Keep the agent ctx to retain pinentry options which will be freed in
691 * cleanup_cb(). Also keep .pubkey since it may be needed for a SAVE. */
693 cleanup_crypto_stage1 (struct crypto_s
*cr
)
698 cleanup_save (&cr
->save
);
701 if (cr
->ciphertext_sexp
)
702 gcry_sexp_release (cr
->ciphertext_sexp
);
704 cr
->ciphertext_sexp
= NULL
;
707 gcry_free (cr
->plaintext
);
708 xfree (cr
->ciphertext
);
709 xfree (cr
->filename
);
711 cr
->ciphertext
= NULL
;
712 cr
->ciphertext_len
= 0;
713 cr
->plaintext
= NULL
;
714 cr
->plaintext_len
= 0;
718 cleanup_crypto_stage2 (struct crypto_s
*cr
)
723 cleanup_crypto_stage1 (cr
);
724 set_header_defaults (&cr
->hdr
);
728 cleanup_crypto (struct crypto_s
**c
)
730 struct crypto_s
*cr
= *c
;
735 cleanup_crypto_stage2 (cr
);
739 gcry_sexp_release (cr
->pkey_sexp
);
741 if (cr
->sigpkey_sexp
)
742 gcry_sexp_release (cr
->sigpkey_sexp
);
745 cleanup_agent (cr
->agent
);
753 init_client_crypto (struct crypto_s
**crypto
)
755 struct crypto_s
*new = xcalloc (1, sizeof (struct crypto_s
));
767 rc
= agent_init (&new->agent
);
770 rc
= send_agent_common_options (new->agent
);
772 rc
= agent_set_pinentry_options (new->agent
);
777 cleanup_agent (new->agent
);
784 set_header_defaults (&new->hdr
);
790 try_decrypt (assuan_context_t ctx
, int inquire
, struct crypto_s
*crypto
,
791 const char *filename
, char **key
, size_t *keylen
)
796 char *pin_title
= NULL
;
797 struct client_s
*client
= ctx
? assuan_get_pointer (ctx
) : NULL
;
801 rc
= decrypt_common (ctx
, inquire
, crypto
, filename
, key
, keylen
);
802 if (gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
&& ctx
)
805 pin_title
= client
->pinentry_opts
.title
;
807 client
->pinentry_opts
.title
= str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try
+1, pin_tries
);
810 while (!inquire
&& gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
811 && ++pin_try
<= pin_tries
);
813 if (ctx
&& pin_title
!= client
->pinentry_opts
.title
)
815 xfree (client
->pinentry_opts
.title
);
816 client
->pinentry_opts
.title
= pin_title
;
822 /* Common to both PKI and non-PKI files when saving a data
823 * file. Defers to gpg-agent when needed and handles keyfiles. */
825 export_common (assuan_context_t ctx
, int inquire
, struct crypto_s
* crypto
,
826 const void *data
, size_t datalen
, const char *outfile
,
827 const char *keyfile
, void **rkey
, size_t *rkeylen
,
828 int use_cache
, int force
, int no_passphrase
)
831 void *enc_xml
= NULL
;
832 size_t enc_xml_len
= 0;
833 unsigned char *iv
= NULL
;
835 int algo
= cipher_to_gcrypt (crypto
->save
.hdr
.flags
);
836 void *key
= NULL
, *salted_key
= NULL
;
837 size_t keysize
, keylen
;
840 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, &keysize
);
850 if (stat (keyfile
, &st
) == -1)
851 return gpg_error_from_errno (errno
);
853 buf
= gcry_malloc (st
.st_size
);
855 return GPG_ERR_ENOMEM
;
857 fd
= open (keyfile
, O_RDONLY
);
859 rc
= gpg_error_from_errno (errno
);
862 size_t len
= read (fd
, buf
, st
.st_size
);
864 if (len
!= st
.st_size
)
865 rc
= GPG_ERR_TOO_SHORT
;
873 log_write (_("Using passphrase obtained from file '%s'"), keyfile
);
880 tmp
= gcry_malloc (1);
893 rc
= cache_iscached (outfile
, &defer
);
896 else if (gpg_err_code (rc
) == GPG_ERR_ENOENT
897 || gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
902 struct cache_data_s
*cdata
= cache_get_data_filename (outfile
);
904 salted_key
= gcry_malloc (cdata
->keylen
);
907 memcpy (salted_key
, cdata
->key
, cdata
->keylen
);
908 keysize
= cdata
->keylen
;
921 struct crypto_s
*tmp
= NULL
;
922 gpg_error_t rc
= init_client_crypto (&tmp
);
926 rc
= read_data_file (outfile
, tmp
);
929 rc
= try_decrypt (ctx
, inquire
, tmp
, outfile
,
930 (char **)&key
, &keylen
);
936 cleanup_crypto (&tmp
);
937 if (rc
&& gpg_err_code (rc
) == GPG_ERR_ENOENT
)
943 if (!use_cache
&& !no_passphrase
) // PASSWD or new file
948 rc
= inquire_passphrase (ctx
, "NEW_PASSPHRASE",
949 (unsigned char **)&key
, &keylen
);
953 rc
= getpin_common (ctx
, outfile
, PINENTRY_SAVE
,
954 (char **)&key
, &keylen
);
965 key
= gcry_malloc (keylen
);
966 memset (key
, 0, keylen
);
980 rc
= hash_key (algo
, crypto
->save
.hdr
.salt
,
981 sizeof(crypto
->save
.hdr
.salt
), key
, keylen
, &salted_key
,
987 rc
= encrypt_xml (ctx
, salted_key
, keysize
, algo
, data
, datalen
,
988 &enc_xml
, &enc_xml_len
, &iv
, &iv_len
,
989 crypto
->save
.hdr
.iterations
);
993 if (no_passphrase
&& !(crypto
->save
.hdr
.flags
& PWMD_FLAG_PKI
))
994 crypto
->save
.hdr
.flags
|= PWMD_FLAG_NO_PASSPHRASE
;
996 memcpy (crypto
->save
.hdr
.iv
, iv
, iv_len
);
998 crypto
->save
.hdr
.datalen
= enc_xml_len
;
999 rc
= write_file (crypto
, outfile
, enc_xml
, enc_xml_len
, NULL
, 0, NULL
,
1001 gcry_free (enc_xml
);
1006 memcpy (&crypto
->hdr
, &crypto
->save
.hdr
, sizeof (file_header_t
));
1011 gcry_free (salted_key
);
1017 write_header (struct crypto_s
*crypto
, int fd
)
1023 #ifdef WORDS_BIGENDIAN
1024 len
= write (fd
, &crypto
->save
.hdr
.magic
, sizeof(crypto
->save
.hdr
.magic
));
1028 len
= write (fd
, &crypto
->save
.hdr
.version
, sizeof(crypto
->save
.hdr
.version
));
1032 len
= write (fd
, &crypto
->save
.hdr
.iterations
,
1033 sizeof(crypto
->save
.hdr
.iterations
));
1037 len
= write (fd
, &crypto
->save
.hdr
.flags
, sizeof(crypto
->save
.hdr
.flags
));
1041 len
= write (fd
, &crypto
->save
.hdr
.iv
, sizeof(crypto
->save
.hdr
.iv
));
1045 len
= write (fd
, &crypto
->save
.hdr
.salt
, sizeof(crypto
->save
.hdr
.salt
));
1049 len
= write (fd
, &crypto
->save
.hdr
.datalen
, sizeof(crypto
->save
.hdr
.datalen
));
1053 len
= write (fd
, &crypto
->save
.hdr
.magic
, sizeof(crypto
->save
.hdr
.magic
));
1057 i
= htonl (crypto
->save
.hdr
.version
);
1058 len
= write (fd
, &i
, sizeof(i
));
1062 n
= crypto
->save
.hdr
.iterations
;
1063 x
= (((uint64_t) htonl (n
)) << 32) + htonl (n
>> 32);
1064 len
= write (fd
, &x
, sizeof(x
));
1068 n
= crypto
->save
.hdr
.flags
;
1069 x
= (((uint64_t) htonl (n
)) << 32) + htonl (n
>> 32);
1070 len
= write (fd
, &x
, sizeof(x
));
1074 len
= write (fd
, &crypto
->save
.hdr
.iv
, sizeof(crypto
->save
.hdr
.iv
));
1078 len
= write (fd
, &crypto
->save
.hdr
.salt
, sizeof(crypto
->save
.hdr
.salt
));
1082 i
= htonl (crypto
->save
.hdr
.datalen
);
1083 len
= write (fd
, &i
, sizeof(i
));
1089 return len
== -1 ? gpg_error_from_errno (errno
) : 0;
1092 /* Write the data structures to disk. */
1094 write_file (struct crypto_s
*crypto
, const char *filename
,
1095 void *data
, size_t data_len
, void *sexp
, size_t sexp_len
,
1096 void *pubkey
, void *sigpkey
)
1098 char tmp
[FILENAME_MAX
] = { 0 };
1106 if (lstat (filename
, &st
) == 0)
1108 mode
= st
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
1110 if (!(mode
& S_IWUSR
))
1111 return GPG_ERR_EACCES
;
1113 else if (errno
!= ENOENT
)
1114 return gpg_error_from_errno (errno
);
1116 snprintf (tmp
, sizeof (tmp
), "%s.XXXXXX", filename
);
1120 rc
= gpg_error_from_errno (errno
);
1121 log_write ("%s: %s", tmp
, pwmd_strerror (rc
));
1127 // xml_import() or convert_file() from command line.
1131 pthread_cleanup_push (cleanup_unlink_cb
, tmp
);
1132 crypto
->save
.hdr
.version
= VERSION_HEX
;
1133 rc
= write_header (crypto
, fd
);
1138 if (crypto
->save
.hdr
.flags
& PWMD_FLAG_PKI
)
1140 unsigned char grip
[20];
1142 gcry_pk_get_keygrip ((gcry_sexp_t
) pubkey
, grip
);
1143 len
= write (fd
, grip
, sizeof (grip
));
1144 if (len
== sizeof (grip
))
1146 gcry_pk_get_keygrip ((gcry_sexp_t
) sigpkey
, grip
);
1147 len
= write (fd
, grip
, sizeof (grip
));
1148 if (len
== sizeof (grip
))
1150 len
= write (fd
, data
, data_len
);
1151 if (len
== data_len
)
1153 len
= write (fd
, sexp
, sexp_len
);
1154 if (len
!= sexp_len
)
1155 rc
= gpg_error_from_errno (errno
);
1158 rc
= gpg_error_from_errno (errno
);
1162 rc
= gpg_error_from_errno (errno
);
1166 len
= write (fd
, data
, data_len
);
1167 if (len
!= data_len
)
1168 rc
= gpg_error_from_errno (errno
);
1174 if (fsync (fd
) != -1)
1176 if (filename
&& close (fd
) != -1)
1179 if (mode
&& config_get_boolean (filename
, "backup"))
1181 char tmp2
[FILENAME_MAX
];
1183 snprintf (tmp2
, sizeof (tmp2
), "%s.backup", filename
);
1184 if (rename (filename
, tmp2
) == -1)
1185 rc
= gpg_error_from_errno (errno
);
1189 rc
= gpg_error_from_errno (errno
);
1192 rc
= gpg_error_from_errno (errno
);
1197 if (filename
&& rename (tmp
, filename
) != -1)
1199 if (filename
&& mode
)
1200 chmod (filename
, mode
);
1204 rc
= gpg_error_from_errno (errno
);
1207 pthread_cleanup_pop (rc
? 1 : 0); // unlink
1212 hash_key (int algo
, unsigned char *salt
, size_t salt_len
, const void *key
,
1213 size_t keylen
, void **result
, size_t *rlen
)
1218 /* Be sure the algorithm is available. */
1219 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, rlen
);
1223 /* Always allocate enough for a 256-bit key although the algorithms
1224 themselves may use less. Fixes SAVE --cipher with a different
1225 keylen than the previously saved cipher when cached. */
1227 tmp
= xmalloc (*rlen
);
1229 return GPG_ERR_ENOMEM
;
1231 rc
= gcry_kdf_derive(key
, keylen
, GCRY_KDF_ITERSALTED_S2K
, GCRY_MD_SHA1
,
1232 salt
, salt_len
, DEFAULT_KDFS2K_ITERATIONS
, *rlen
, tmp
);
1241 /* The PASSWD command when not using gpg-agent. */
1243 change_passwd (assuan_context_t ctx
, const char *filename
, int inquire
,
1244 struct crypto_s
**rcrypto
, int no_passphrase
)
1246 unsigned char *key
= NULL
;
1248 struct crypto_s
*crypto
= NULL
;
1249 gpg_error_t rc
= init_client_crypto (&crypto
);
1255 crypto
->client_ctx
= ctx
;
1256 rc
= read_data_file (filename
, crypto
);
1259 cleanup_crypto (&crypto
);
1263 rc
= try_decrypt (ctx
, inquire
, crypto
, filename
, (char **)&key
, &keylen
);
1266 cleanup_crypto (&crypto
);
1275 memcpy (&crypto
->save
.hdr
, &crypto
->hdr
, sizeof (file_header_t
));
1278 crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_NO_PASSPHRASE
;
1280 rc
= export_common (ctx
, inquire
, crypto
, crypto
->plaintext
,
1281 crypto
->plaintext_len
, crypto
->filename
, NULL
,
1282 (void **)&key
, &keylen
, 0, 0, no_passphrase
);
1287 rc
= save_common (filename
, crypto
, crypto
->plaintext
,
1288 crypto
->plaintext_len
, key
, keylen
, &cached
, 1);
1296 cleanup_crypto (&crypto
);
1299 send_status_all (STATUS_CACHE
, NULL
);
1305 save_common (const char *filename
, struct crypto_s
*crypto
,
1306 const unsigned char *data
, size_t datalen
,
1307 const unsigned char *key
, size_t keylen
, int *cached
, int no_agent
)
1309 struct cache_data_s
*cdata
;
1311 gcry_sexp_t pubkey
= NULL
;
1312 gcry_sexp_t sigkey
= NULL
;
1314 /* This is safe since it is a (somewhat) fast operation. */
1316 pthread_cleanup_push (cleanup_cache_mutex
, NULL
);
1317 cdata
= cache_get_data_filename (filename
);
1318 *cached
= cdata
!= NULL
;
1321 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
1324 if (use_agent
&& !no_agent
)
1326 rc
= gcry_sexp_build (&pubkey
, NULL
, "%S", crypto
->pkey_sexp
);
1328 rc
= gcry_sexp_build (&sigkey
, NULL
, "%S", crypto
->sigpkey_sexp
);
1334 gcry_free (cdata
->doc
);
1336 gcry_free (cdata
->key
);
1337 cdata
->key
= (unsigned char *)key
;
1338 cdata
->keylen
= keylen
;
1339 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
, GCRY_CIPHER_AES
, data
,
1340 datalen
, &cdata
->doc
, &cdata
->doclen
, &cache_iv
,
1341 &cache_blocksize
, 0);
1346 unsigned char md5file
[16];
1348 /* Update in case of any --keygrip argument */
1350 gcry_sexp_release (cdata
->pubkey
);
1353 gcry_sexp_release (cdata
->sigkey
);
1355 cdata
->pubkey
= pubkey
;
1356 cdata
->sigkey
= sigkey
;
1357 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
1358 rc
= cache_set_data (md5file
, cdata
, crypto
->grip
);
1363 gcry_sexp_release (pubkey
);
1366 gcry_sexp_release (sigkey
);
1369 pthread_cleanup_pop (1); // mutex unlock
1374 getpin_common (assuan_context_t ctx
, const char *filename
, int which
,
1375 char **rkey
, size_t *rkeylen
)
1378 struct client_s
*client
= ctx
? assuan_get_pointer (ctx
) : NULL
;
1379 struct pinentry_s
*pin
= pinentry_init (filename
);
1382 return GPG_ERR_ENOMEM
;
1385 pinentry_merge_options (&pin
->pinentry_opts
, &client
->pinentry_opts
);
1387 rc
= pinentry_lock (ctx
, pin
);
1389 rc
= pinentry_getpin (pin
, rkey
, which
);
1391 pinentry_deinit (pin
);
1395 *rkeylen
= strlen (*rkey
);
1402 /* Note: 'key' is freed with gcry_free() and not xfree(). Although
1403 * both call xfree(). */
1405 inquire_passphrase (assuan_context_t ctx
, const char *keyword
,
1406 unsigned char **result
, size_t *rlen
)
1410 assuan_begin_confidential (ctx
);
1411 rc
= assuan_inquire (ctx
, keyword
, result
, rlen
, 0);
1412 assuan_end_confidential (ctx
);
1416 /* Common to both PKI and non-PKI files. */
1418 decrypt_common (assuan_context_t ctx
, int inquire
, struct crypto_s
*crypto
,
1419 const char *filename
, char **rkey
, size_t *rkeylen
)
1423 gpg_error_t rc
= read_data_file (filename
, crypto
);
1424 int algo
= cipher_to_gcrypt (crypto
->hdr
.flags
);
1425 void *salted_key
= NULL
;
1430 log_write ("ERR %i: %s", rc
, pwmd_strerror (rc
));
1434 key
= config_get_string (filename
, "passphrase");
1437 log_write (_("Trying the passphrase specified in config ..."));
1438 keylen
= strlen (key
);
1443 key
= config_get_string (filename
, "passphrase_file");
1446 char *tmp
= expand_homedir (key
);
1452 log_write (_("Trying the passphrase using file '%s' ..."), key
);
1453 fd
= open (key
, O_RDONLY
);
1456 rc
= gpg_error_from_errno (errno
);
1457 log_write ("%s: %s", key
, pwmd_strerror (rc
));
1464 key
= xmalloc (st
.st_size
);
1467 if (read (fd
, key
, st
.st_size
) != st
.st_size
)
1469 log_write ("short read() count");
1470 rc
= GPG_ERR_TOO_SHORT
;
1474 rc
= GPG_ERR_ENOMEM
;
1480 log_write ("ERR %i: %s", rc
, pwmd_strerror (rc
));
1485 keylen
= st
.st_size
;
1497 if (!key
&& !IS_PKI (crypto
) && inquire
1498 && !(crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
))
1500 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
1504 log_write ("ERR %i: %s", rc
, pwmd_strerror (rc
));
1511 else if (!key
&& !IS_PKI (crypto
))
1513 if (crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
)
1516 key
= gcry_malloc (keylen
);
1517 memset (key
, 0, keylen
);
1521 rc
= getpin_common (ctx
, filename
, PINENTRY_OPEN
, &key
, &keylen
);
1524 log_write ("ERR %i: %s", rc
, pwmd_strerror (rc
));
1530 else if (key
&& IS_PKI (crypto
))
1532 rc
= set_agent_passphrase (crypto
, key
, keylen
);
1536 log_write ("ERR %i: %s", rc
, pwmd_strerror (rc
));
1540 else if (!key
&& IS_PKI (crypto
))
1542 /* The cache entry has been cleared in save_command(). Will prompt. */
1543 rc
= set_pinentry_mode (crypto
->agent
, inquire
? "loopback" : "ask");
1549 if (key
&& !IS_PKI (crypto
))
1551 rc
= hash_key (algo
, crypto
->hdr
.salt
, sizeof(crypto
->hdr
.salt
), key
,
1552 keylen
, &salted_key
, &keysize
);
1554 key
= (char *)salted_key
;
1563 xfree (crypto
->filename
);
1564 crypto
->filename
= str_dup (filename
);
1565 rc
= decrypt_data (ctx
, crypto
, (unsigned char *)key
, keylen
);
1568 log_write ("ERR %i: %s", rc
, pwmd_strerror (rc
));