2 Copyright (C) 2015-2022 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 Pwmd is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
30 #include <sys/resource.h>
32 #include <arpa/inet.h>
33 #include <libxml/tree.h>
34 #include <libxml/parser.h>
35 #include <libxml/xpath.h>
39 #include "pwmd-error.h"
42 #ifdef HAVE_SYS_PRCTL_H
43 #include <sys/prctl.h>
54 #define _(msgid) gettext(msgid)
57 #ifdef HAVE_GETOPT_LONG
62 #include "getopt_long.h"
65 #include "util-string.h"
75 #define PWMD_CIPHER_OFFSET (1)
76 #define PWMD_CIPHER(n) (PWMD_CIPHER_OFFSET << n)
77 #define PWMD_CIPHER_AES128 PWMD_CIPHER (0)
78 #define PWMD_CIPHER_AES192 PWMD_CIPHER (1)
79 #define PWMD_CIPHER_AES256 PWMD_CIPHER (2)
80 #define PWMD_CIPHER_SERPENT128 PWMD_CIPHER (3)
81 #define PWMD_CIPHER_SERPENT192 PWMD_CIPHER (4)
82 #define PWMD_CIPHER_SERPENT256 PWMD_CIPHER (5)
83 #define PWMD_CIPHER_CAMELLIA128 PWMD_CIPHER (6)
84 #define PWMD_CIPHER_CAMELLIA192 PWMD_CIPHER (7)
85 #define PWMD_CIPHER_CAMELLIA256 PWMD_CIPHER (8)
86 #define PWMD_CIPHER_3DES PWMD_CIPHER (9)
87 #define PWMD_CIPHER_CAST5 PWMD_CIPHER (10)
88 #define PWMD_CIPHER_BLOWFISH PWMD_CIPHER (11)
89 #define PWMD_CIPHER_TWOFISH PWMD_CIPHER (12)
90 #define PWMD_CIPHER_TWOFISH128 PWMD_CIPHER (13)
92 #define PWMD_FLAG_OFFSET (PWMD_CIPHER_OFFSET << 15)
93 #define PWMD_FLAG(n) (PWMD_FLAG_OFFSET << n)
94 #define PWMD_FLAG_PKI PWMD_FLAG (1)
95 #define PWMD_FLAG_NO_PASSPHRASE PWMD_FLAG (2)
107 uint32_t datalen
; /* of the encrypted xml */
108 } __attribute__ ((packed
)) file_header_t
;
112 gcry_sexp_t pkey
; /* SAVE --keygrip */
113 gcry_sexp_t sigpkey
; /* SAVE --sign-keygrip */
119 //assuan_context_t client_ctx;
121 struct agent_s
*agent
;
124 gcry_sexp_t pkey_sexp
;
125 unsigned char grip
[20];
126 gcry_sexp_t sigpkey_sexp
;
127 unsigned char sign_grip
[20];
128 gcry_sexp_t ciphertext_sexp
;
130 size_t ciphertext_len
;
132 size_t plaintext_len
;
134 char *filename
; /* the currently opened data file */
137 #define DEFAULT_KDFS2K_ITERATIONS 5000000
138 #define COMPAT_KDFS2K_ITERATIONS 1000
140 const char *reserved_attributes
[] = {
141 "_name", "_mtime", "_ctime", "_acl", "_target",
144 static unsigned char crypto_magic
[5] = "\177PWMD";
145 static int use_agent
;
147 static void set_header_defaults (file_header_t
* hdr
);
148 static gpg_error_t
decrypt_common (struct crypto_s
*crypto
,
149 const char *filename
, const char *keyfile
);
150 static gpg_error_t
read_data_file (const char *filename
,
151 struct crypto_s
*crypto
);
152 static void cleanup_crypto_stage1 (struct crypto_s
*cr
);
153 static void cleanup_crypto (struct crypto_s
**c
);
156 cipher_to_gcrypt (int flags
)
161 if (flags
& PWMD_CIPHER_AES128
)
162 return GCRY_CIPHER_AES128
;
163 else if (flags
& PWMD_CIPHER_AES192
)
164 return GCRY_CIPHER_AES192
;
165 else if (flags
& PWMD_CIPHER_AES256
)
166 return GCRY_CIPHER_AES256
;
167 else if (flags
& PWMD_CIPHER_SERPENT128
)
168 return GCRY_CIPHER_SERPENT128
;
169 else if (flags
& PWMD_CIPHER_SERPENT192
)
170 return GCRY_CIPHER_SERPENT192
;
171 else if (flags
& PWMD_CIPHER_SERPENT256
)
172 return GCRY_CIPHER_SERPENT256
;
173 else if (flags
& PWMD_CIPHER_CAMELLIA128
)
174 return GCRY_CIPHER_CAMELLIA128
;
175 else if (flags
& PWMD_CIPHER_CAMELLIA192
)
176 return GCRY_CIPHER_CAMELLIA192
;
177 else if (flags
& PWMD_CIPHER_CAMELLIA256
)
178 return GCRY_CIPHER_CAMELLIA256
;
179 else if (flags
& PWMD_CIPHER_BLOWFISH
)
180 return GCRY_CIPHER_BLOWFISH
;
181 else if (flags
& PWMD_CIPHER_3DES
)
182 return GCRY_CIPHER_3DES
;
183 else if (flags
& PWMD_CIPHER_CAST5
)
184 return GCRY_CIPHER_CAST5
;
185 else if (flags
& PWMD_CIPHER_TWOFISH
)
186 return GCRY_CIPHER_TWOFISH
;
187 else if (flags
& PWMD_CIPHER_TWOFISH128
)
188 return GCRY_CIPHER_TWOFISH128
;
194 cipher_string_to_cipher (const char *str
)
198 if (!strcasecmp (str
, "aes128"))
199 flags
= PWMD_CIPHER_AES128
;
200 else if (!strcasecmp (str
, "aes192"))
201 flags
= PWMD_CIPHER_AES192
;
202 else if (!strcasecmp (str
, "aes256"))
203 flags
= PWMD_CIPHER_AES256
;
204 else if (!strcasecmp (str
, "serpent128"))
205 flags
= PWMD_CIPHER_SERPENT128
;
206 else if (!strcasecmp (str
, "serpent192"))
207 flags
= PWMD_CIPHER_SERPENT192
;
208 else if (!strcasecmp (str
, "serpent256"))
209 flags
= PWMD_CIPHER_SERPENT256
;
210 else if (!strcasecmp (str
, "camellia128"))
211 flags
= PWMD_CIPHER_CAMELLIA128
;
212 else if (!strcasecmp (str
, "camellia192"))
213 flags
= PWMD_CIPHER_CAMELLIA192
;
214 else if (!strcasecmp (str
, "camellia256"))
215 flags
= PWMD_CIPHER_CAMELLIA256
;
216 else if (!strcasecmp (str
, "blowfish"))
217 flags
= PWMD_CIPHER_BLOWFISH
;
218 else if (!strcasecmp (str
, "cast5"))
219 flags
= PWMD_CIPHER_CAST5
;
220 else if (!strcasecmp (str
, "3des"))
221 flags
= PWMD_CIPHER_3DES
;
222 else if (!strcasecmp (str
, "twofish256"))
223 flags
= PWMD_CIPHER_TWOFISH
;
224 else if (!strcasecmp (str
, "twofish128"))
225 flags
= PWMD_CIPHER_TWOFISH128
;
233 usage (const char *pn
, int rc
)
235 fprintf(rc
== EXIT_SUCCESS
? stdout
: stderr
,
236 "%s [-hvn] [--force] [-k <filename>] [--xml] <infile> <outfile>\n"
237 " --no-convert, -n don't discard data for elements with targets\n"
238 " --force don't abort when converting\n"
239 " --xml read a raw pwmd XML document\n"
240 " --keyfile, -k file containing the passphrase to use for decryption\n"
243 "Use - as <outfile> to write to standard output.\n",
249 init_client_crypto (struct crypto_s
**crypto
)
251 struct crypto_s
*new = xcalloc (1, sizeof (struct crypto_s
));
263 rc
= agent_init (&new->agent
);
266 rc
= send_agent_common_options (new->agent
);
268 rc
= agent_set_pinentry_options (new->agent
);
273 cleanup_agent (new->agent
);
280 set_header_defaults (&new->hdr
);
286 parse_doc (const char *xml
, size_t len
, xmlDocPtr
*result
)
290 xmlResetLastError ();
291 doc
= xmlReadMemory (xml
, len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
292 if (!doc
&& xmlGetLastError ())
293 return GPG_ERR_BAD_DATA
;
296 return !doc
? GPG_ERR_ENOMEM
: 0;
300 xml_attribute_value (xmlNodePtr n
, xmlChar
* attr
)
302 xmlAttrPtr a
= xmlHasProp (n
, attr
);
307 if (!a
->children
|| !a
->children
->content
)
310 return xmlGetProp (n
, attr
);
314 xml_reserved_attribute (const char *name
)
318 for (i
= 0; reserved_attributes
[i
]; i
++)
320 if (!strcmp (name
, reserved_attributes
[i
]))
327 remove_non_reserved_attributes (xmlNodePtr n
)
331 for (a
= n
->properties
; a
; a
= a
->next
)
333 if (xml_reserved_attribute ((char *)a
->name
))
341 strip_literals (const char *filename
, xmlNodePtr n
, int force
)
345 for (; n
; n
= n
->next
)
349 if (n
->type
!= XML_ELEMENT_NODE
)
352 if (!xmlStrEqual (n
->name
, (xmlChar
*) "element"))
354 xmlNodePtr tmp
= n
->next
;
358 return strip_literals (filename
, tmp
, force
);
361 target
= xml_attribute_value (n
, (xmlChar
*)"_target");
364 xmlChar lastc
= 0, *p
;
366 remove_non_reserved_attributes (n
);
368 for (lastc
= 0, p
= target
; *p
;)
370 if (*p
== '!' && (p
== target
|| lastc
== '\t'))
384 if (!xmlSetProp (n
, (xmlChar
*) "_target", target
))
387 return GPG_ERR_INV_VALUE
;
392 if (n
->children
&& !force
)
394 fprintf(stderr
, _("%s: aborting do to literal child elements (use --force)\n"), filename
);
395 return GPG_ERR_CANCELED
;
397 else if (n
->children
)
399 xmlNodePtr tmp
= n
->children
;
401 xmlUnlinkNode (n
->children
);
402 xmlFreeNodeList (tmp
);
406 rc
= strip_literals (filename
, n
->children
, force
);
415 recurse_rename_attribute (xmlNodePtr root
, const char *old
, const char *name
)
420 for (n
= root
; n
; n
= n
->next
)
422 xmlAttrPtr attr
= xmlHasProp (n
, (xmlChar
*)old
);
426 xmlChar
*a
= xml_attribute_value (n
, (xmlChar
*)old
);
428 if (a
&& !xmlSetProp (n
, (xmlChar
*) name
, a
))
429 rc
= GPG_ERR_BAD_DATA
;
435 if (xmlRemoveProp (attr
) == -1)
436 rc
= GPG_ERR_BAD_DATA
;
443 rc
= recurse_rename_attribute (n
->children
, old
, name
);
452 update_to_version (xmlDocPtr doc
)
454 xmlNodePtr root
= xmlDocGetRootElement (doc
);
455 xmlChar
*v
= xml_attribute_value (root
, (xmlChar
*)"_version");
458 // Pwmd 3.2.0 or later. No updates needed ATM.
465 rc
= recurse_rename_attribute (root
->children
, "target", "_target");
467 rc
= recurse_rename_attribute (root
->children
, "expire", "_expire");
470 rc
= recurse_rename_attribute (root
->children
, "expire_increment", "_age");
476 main(int argc
, char **argv
)
480 struct crypto_s
*crypto
= NULL
;
481 char *outfile
= NULL
, *infile
= NULL
, *keyfile
= NULL
;
487 const char *optstring
= "vhk:n";
491 const struct option longopts
[] = {
492 {"force", no_argument
, 0, 0},
493 {"xml", no_argument
, 0, 0},
494 {"no-convert", no_argument
, 0, 'n'},
495 {"keyfile", required_argument
, 0, 'k'},
496 {"version", no_argument
, 0, 'v'},
497 {"help", no_argument
, 0, 'h'},
502 #ifdef HAVE_SETRLIMIT
505 rl
.rlim_cur
= rl
.rlim_max
= 0;
507 if (setrlimit (RLIMIT_CORE
, &rl
) != 0)
508 err (EXIT_FAILURE
, "setrlimit()");
511 #ifdef HAVE_PR_SET_DUMPABLE
512 prctl (PR_SET_DUMPABLE
, 0);
516 if (!gpgrt_check_version (REQUIRE_LIBGPGERROR_VERSION
))
518 fprintf (stderr
, _("gpgrt_check_version(): Incompatible libgpg-error. "
519 "Wanted %s, got %s.\n"), REQUIRE_LIBGPGERROR_VERSION
,
520 gpgrt_check_version (NULL
));
525 //gpgrt_set_alloc_func (xrealloc_gpgrt);
527 if (!gcry_check_version (REQUIRE_LIBGCRYPT_VERSION
))
529 fprintf (stderr
, _("gcry_check_version(): Incompatible libgcrypt. "
530 "Wanted %s, got %s.\n"), REQUIRE_LIBGCRYPT_VERSION
,
531 gcry_check_version (NULL
));
535 gcry_set_allocation_handler (xmalloc
, xmalloc
, NULL
, xrealloc
, xfree
);
536 xmlMemSetup (xfree
, xmalloc
, xrealloc
, str_dup
);
542 while ((opt
= getopt_long (argc
, argv
, optstring
, longopts
, &optindex
)) != -1)
553 usage (argv
[0], EXIT_SUCCESS
);
557 "Copyright (C) 2015-2022\n"
559 "Released under the terms of the GPL v2. Use at your own risk.\n\n"),
560 PACKAGE_STRING PWMD_GIT_HASH
, PACKAGE_BUGREPORT
);
572 usage (argv
[0], EXIT_FAILURE
);
576 usage (argv
[0], EXIT_FAILURE
);
581 if (argc
- optind
!= 2)
582 usage (argv
[0], EXIT_FAILURE
);
584 infile
= argv
[optind
++];
585 outfile
= argv
[optind
];
586 if (strcmp (outfile
, "-"))
588 if (access (outfile
, R_OK
|W_OK
) == 0)
590 fprintf(stderr
, _("Please remove the existing output file '%s'.\n"),
596 rc
= init_client_crypto (&crypto
);
602 rc
= decrypt_common (crypto
, infile
, keyfile
);
610 fd
= open (infile
, O_RDONLY
);
613 rc
= gpg_error_from_syserror ();
617 if (fstat (fd
, &st
) == -1)
619 rc
= gpg_error_from_syserror ();
624 crypto
->plaintext
= gcry_calloc (1, sizeof (char) * st
.st_size
+1);
625 if (!crypto
->plaintext
)
632 crypto
->plaintext_len
= read (fd
, crypto
->plaintext
, st
.st_size
);
634 if (crypto
->plaintext_len
!= st
.st_size
)
636 rc
= GPG_ERR_UNKNOWN_ERRNO
;
645 rc
= parse_doc ((char *) crypto
->plaintext
, crypto
->plaintext_len
, &doc
);
646 cleanup_crypto (&crypto
);
653 FILE *fp
= outfile
[0] == '-' && outfile
[1] == 0 ? stdout
654 : fopen (outfile
, "w");
658 rc
= gpg_error_from_syserror ();
665 n
= xmlDocGetRootElement (doc
);
666 rc
= strip_literals (infile
, n
->children
, force
);
673 rc
= update_to_version (doc
);
680 n
= xmlDocGetRootElement (doc
);
681 if (!xmlSetProp (n
, (xmlChar
*) "_version",
682 (xmlChar
*)PACKAGE_VERSION
))
683 rc
= GPG_ERR_BAD_DATA
;
691 xmlDocDumpMemory (doc
, &buf
, &len
);
693 fprintf (fp
, "%s", buf
);
703 cleanup_crypto (&crypto
);
707 cleanup_crypto (&crypto
);
708 fprintf(stderr
, "ERR %u: %s\n", rc
, gpg_strerror (rc
));
713 hash_key (int algo
, unsigned char *salt
, size_t salt_len
, const void *key
,
714 size_t keylen
, void **result
, size_t *rlen
, uint64_t iterations
)
719 /* Be sure the algorithm is available. */
720 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, rlen
);
724 /* Always allocate enough for a 256-bit key although the algorithms
725 themselves may use less. Fixes SAVE --cipher with a different
726 keylen than the previously saved cipher when cached. */
728 tmp
= xmalloc (*rlen
);
730 return GPG_ERR_ENOMEM
;
733 iterations
= DEFAULT_KDFS2K_ITERATIONS
;
735 rc
= gcry_kdf_derive(key
, keylen
, GCRY_KDF_ITERSALTED_S2K
, GCRY_MD_SHA1
,
736 salt
, salt_len
, iterations
, *rlen
, tmp
);
746 * Useful for a large amount of data. Rather than doing all of the data in one
747 * iteration do it in chunks. This lets the command be cancelable rather than
748 * waiting for it to complete.
750 #define CRYPTO_BLOCKSIZE(c) (c * 1024)
752 iterate_crypto_once (gcry_cipher_hd_t h
, unsigned char *inbuf
,
753 size_t insize
, size_t blocksize
)
756 off_t len
= CRYPTO_BLOCKSIZE (blocksize
);
757 void *p
= gcry_malloc (len
);
761 return gpg_error (GPG_ERR_ENOMEM
);
763 if (insize
< CRYPTO_BLOCKSIZE (blocksize
))
766 pthread_cleanup_push (gcry_free
, p
);
770 unsigned char *inbuf2
= inbuf
+ total
;
773 if (len
+ total
> insize
)
776 rc
= gcry_cipher_decrypt (h
, p
, len
, inbuf2
, len
);
781 memmove (tmp
, p
, len
);
786 #ifdef HAVE_PTHREAD_TESTCANCEL
787 pthread_testcancel ();
791 pthread_cleanup_pop (1);
792 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
799 cleanup_cipher (void *arg
)
801 gcry_cipher_close ((gcry_cipher_hd_t
) arg
);
804 /* Decrypt the XML data. For PKI data files the key is retrieved from
805 * gpg-agent and the signature verified. */
807 decrypt_data (struct crypto_s
*crypto
, unsigned char *salted_key
,
811 unsigned char *key
= salted_key
;
812 gcry_cipher_hd_t h
= NULL
;
813 size_t blocksize
, keysize
= 0;
814 int algo
= cipher_to_gcrypt (crypto
->hdr
.flags
);
818 size_t keylen
= skeylen
;
819 gcry_sexp_t sig_sexp
;
821 if (crypto
->hdr
.flags
& PWMD_FLAG_PKI
)
823 rc
= agent_extract_key (crypto
, &key
, &keylen
);
827 sig_sexp
= gcry_sexp_find_token (crypto
->ciphertext_sexp
, "sig-val", 0);
831 return GPG_ERR_BAD_DATA
;
834 rc
= agent_verify (crypto
->sigpkey_sexp
, sig_sexp
, crypto
->ciphertext
,
835 crypto
->ciphertext_len
);
836 gcry_sexp_release (sig_sexp
);
843 rc
= gcry_cipher_open (&h
, algo
, GCRY_CIPHER_MODE_CBC
, 0);
846 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
,
850 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_BLKLEN
, NULL
,
854 rc
= gcry_cipher_setiv (h
, crypto
->hdr
.iv
, blocksize
);
857 rc
= gcry_cipher_setkey (h
, key
, keysize
);
864 pthread_cleanup_push (cleanup_cipher
, rc
? NULL
: h
);
868 outbuf
= gcry_malloc (crypto
->hdr
.datalen
+1);
872 memset (outbuf
, 0, crypto
->hdr
.datalen
+1);
875 pthread_cleanup_push (gcry_free
, outbuf
);
877 pthread_cleanup_push (gcry_free
, key
);
883 rc
= GPG_ERR_INV_PARAMETER
;
886 memcpy (outbuf
, crypto
->ciphertext
, crypto
->hdr
.datalen
);
887 rc
= iterate_crypto_once (h
, outbuf
, crypto
->hdr
.datalen
, blocksize
);
890 if (!rc
&& crypto
->hdr
.version
<= 0x03000e)
893 rc
= gcry_cipher_setkey (h
, key
, keysize
);
897 if (!rc
&& crypto
->hdr
.version
<= 0x03000e)
899 for (n
= 0; !rc
&& n
< crypto
->hdr
.s2k_count
; n
++)
901 rc
= gcry_cipher_setiv (h
, crypto
->hdr
.iv
, blocksize
);
905 rc
= iterate_crypto_once (h
, outbuf
, crypto
->hdr
.datalen
, blocksize
);
910 pthread_cleanup_pop (0);
911 if (crypto
->hdr
.flags
& PWMD_FLAG_PKI
)
913 else if (key
&& crypto
->hdr
.version
<= 0x03000e)
916 if (crypto
->hdr
.version
<= 0x03000e && key
)
919 pthread_cleanup_pop (rc
? 1 : 0); /* outbuf */
920 pthread_cleanup_pop (1); /* cipher */
923 char buf
[] = "<?xml ";
925 if (memcmp (outbuf
, buf
, sizeof(buf
)-1))
928 return gpg_error (GPG_ERR_BAD_PASSPHRASE
);
931 crypto
->plaintext
= outbuf
;
932 crypto
->plaintext_len
= crypto
->hdr
.datalen
;
935 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
942 get_passphrase (const char *filename
, const char *keyfile
, void **r_key
,
945 char buf
[255] = { 0 };
946 struct termios told
, tnew
;
956 fd
= open (keyfile
, O_RDONLY
);
958 return gpg_error_from_syserror ();
960 if (fstat (fd
, &st
) == -1)
962 gpg_error_t rc
= gpg_error_from_syserror ();
968 len
= read (fd
, buf
, sizeof (buf
));
969 if (len
!= st
.st_size
)
972 wipememory (buf
, 0, sizeof (buf
));
973 return gpg_error_from_syserror ();
976 *r_len
= st
.st_size
? st
.st_size
: 1;
977 *r_key
= xmalloc (*r_len
);
979 memcpy (*r_key
, buf
, *r_len
);
980 wipememory (buf
, 0, sizeof (buf
));
982 return *r_key
? 0 : GPG_ERR_ENOMEM
;
985 if (!isatty (STDIN_FILENO
))
986 return GPG_ERR_ENOTTY
;
988 if (tcgetattr (STDIN_FILENO
, &told
) == -1)
989 return gpg_error_from_syserror ();
991 memcpy (&tnew
, &told
, sizeof (struct termios
));
992 tnew
.c_lflag
&= ~(ECHO
);
993 tnew
.c_lflag
|= ICANON
| ECHONL
;
994 if (tcsetattr (STDIN_FILENO
, TCSANOW
, &tnew
) == -1)
998 tcsetattr (STDIN_FILENO
, TCSANOW
, &told
);
999 return gpg_error_from_errno (n
);
1002 fprintf(stderr
, "Passphrase for '%s': ", filename
);
1003 if (!fgets (buf
, sizeof (buf
), stdin
))
1005 tcsetattr (STDIN_FILENO
, TCSANOW
, &told
);
1009 tcsetattr (STDIN_FILENO
, TCSANOW
, &told
);
1011 if (buf
[strlen(buf
)-1] == '\n')
1012 buf
[strlen(buf
)-1] = 0;
1013 *r_len
= buf
[0] == 0 ? 1 : strlen (buf
);
1014 *r_key
= str_dup (buf
);
1015 wipememory (buf
, 0, sizeof (buf
));
1019 /* Common to both PKI and non-PKI files. */
1021 decrypt_common (struct crypto_s
*crypto
, const char *filename
,
1022 const char *keyfile
)
1026 gpg_error_t rc
= read_data_file (filename
, crypto
);
1027 int algo
= cipher_to_gcrypt (crypto
->hdr
.flags
);
1029 size_t skeysize
= 0;
1034 rc
= get_passphrase (filename
, keyfile
, &key
, &keylen
);
1038 if (key
)// && !IS_PKI (crypto))
1040 rc
= hash_key (algo
, crypto
->hdr
.salt
, sizeof(crypto
->hdr
.salt
), key
,
1041 keylen
, &skey
, &skeysize
,
1042 crypto
->hdr
.version
<= 0x03000e ? COMPAT_KDFS2K_ITERATIONS
: crypto
->hdr
.s2k_count
);
1051 xfree (crypto
->filename
);
1052 crypto
->filename
= str_dup (filename
);
1053 rc
= decrypt_data (crypto
, skey
, skeysize
);
1059 read_header (file_header_t
*hdr
, int fd
)
1065 #ifdef WORDS_BIGENDIAN
1066 len
= read (fd
, &hdr
->magic
, sizeof(hdr
->magic
));
1070 len
= read (fd
, &hdr
->version
, sizeof(hdr
->version
));
1074 len
= read (fd
, &hdr
->s2k_count
, sizeof(hdr
->s2k_count
));
1078 len
= read (fd
, &hdr
->flags
, sizeof(hdr
->flags
));
1082 len
= read (fd
, &hdr
->iv
, sizeof(hdr
->iv
));
1086 len
= read (fd
, &hdr
->salt
, sizeof(hdr
->salt
));
1090 len
= read (fd
, &hdr
->datalen
, sizeof(hdr
->datalen
));
1094 len
= read (fd
, &hdr
->magic
, sizeof(hdr
->magic
));
1098 len
= read (fd
, &i
, sizeof(hdr
->version
));
1101 hdr
->version
= ntohl (i
);
1103 len
= read (fd
, &n
, sizeof(hdr
->s2k_count
));
1106 hdr
->s2k_count
= (((uint64_t) ntohl (n
)) << 32) + ntohl (n
>> 32);
1108 len
= read (fd
, &n
, sizeof(hdr
->flags
));
1111 hdr
->flags
= (((uint64_t) ntohl (n
)) << 32) + ntohl (n
>> 32);
1113 len
= read (fd
, &hdr
->iv
, sizeof(hdr
->iv
));
1117 len
= read (fd
, &hdr
->salt
, sizeof(hdr
->salt
));
1121 len
= read (fd
, &i
, sizeof(hdr
->datalen
));
1124 hdr
->datalen
= ntohl (i
);
1128 return len
== -1 ? gpg_error_from_errno (errno
) : 0;
1131 /* Read the header of a data file to determine cipher and other. The
1132 * header is stored big endian in the file and is converted to little
1133 * endian when needed. */
1135 read_data_header (const char *filename
, file_header_t
* rhdr
,
1136 struct stat
*rst
, int *rfd
)
1146 fd
= open (filename
, O_RDONLY
);
1148 return gpg_error_from_syserror ();
1150 if (fstat (fd
, &st
) == -1)
1152 rc
= gpg_error_from_syserror ();
1157 rc
= read_header (&hdr
, fd
);
1158 if (!rc
&& memcmp (hdr
.magic
, crypto_magic
, sizeof (hdr
.magic
)))
1159 rc
= GPG_ERR_BAD_DATA
;
1160 else if (!rc
&& hdr
.version
< 0x030000)
1161 rc
= GPG_ERR_UNKNOWN_VERSION
;
1177 return gpg_error (rc
);
1181 read_data_file (const char *filename
, struct crypto_s
* crypto
)
1190 return GPG_ERR_INV_PARAMETER
;
1192 cleanup_crypto_stage1 (crypto
);
1193 rc
= read_data_header (filename
, &crypto
->hdr
, &st
, &fd
);
1195 return gpg_error (rc
);
1197 crypto
->ciphertext_len
= crypto
->hdr
.datalen
;
1198 crypto
->ciphertext
= xmalloc (crypto
->hdr
.datalen
);
1199 if (!crypto
->ciphertext
)
1201 rc
= GPG_ERR_ENOMEM
;
1205 /* The keygrips for PKI files are stored after the header. They are
1206 * stored in the file to let file(1) magic(5) show the grips. */
1207 if (crypto
->hdr
.flags
& PWMD_FLAG_PKI
)
1209 rlen
= read (fd
, crypto
->grip
, 20);
1212 rc
= rlen
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
1216 rlen
= read (fd
, crypto
->sign_grip
, 20);
1219 rc
= rlen
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
1224 len
= read (fd
, crypto
->ciphertext
, crypto
->hdr
.datalen
);
1225 if (len
!= crypto
->hdr
.datalen
)
1227 rc
= len
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
1231 if (!(crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
1236 rc
= GPG_ERR_NOT_IMPLEMENTED
;
1241 len
= st
.st_size
- sizeof (file_header_t
) - crypto
->hdr
.datalen
- 40;
1242 buf
= xmalloc (len
);
1245 rc
= GPG_ERR_ENOMEM
;
1249 /* Remaining data file bytes are the encrypted key and XML. */
1250 rlen
= read (fd
, buf
, len
);
1253 rc
= rlen
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
1257 rc
= gcry_sexp_new (&crypto
->ciphertext_sexp
, buf
, rlen
, 1);
1261 if (crypto
->pkey_sexp
)
1262 gcry_sexp_release (crypto
->pkey_sexp
);
1264 if (crypto
->sigpkey_sexp
)
1265 gcry_sexp_release (crypto
->sigpkey_sexp
);
1267 crypto
->pkey_sexp
= crypto
->sigpkey_sexp
= NULL
;
1268 rc
= get_pubkey_bin (crypto
, crypto
->grip
, &crypto
->pkey_sexp
);
1270 rc
= get_pubkey_bin (crypto
, crypto
->sign_grip
, &crypto
->sigpkey_sexp
);
1276 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
1277 rc
= gpg_error (rc
);
1283 cleanup_save (struct save_s
*save
)
1290 gcry_sexp_release (save
->pkey
);
1293 gcry_sexp_release (save
->sigpkey
);
1296 memset (save
, 0, sizeof (struct save_s
));
1299 /* Keep the agent ctx to retain pinentry options which will be freed in
1300 * cleanup_cb(). Also keep .pubkey since it may be needed for a SAVE. */
1302 cleanup_crypto_stage1 (struct crypto_s
*cr
)
1307 cleanup_save (&cr
->save
);
1310 if (cr
->ciphertext_sexp
)
1311 gcry_sexp_release (cr
->ciphertext_sexp
);
1313 cr
->ciphertext_sexp
= NULL
;
1316 gcry_free (cr
->plaintext
);
1317 xfree (cr
->ciphertext
);
1318 xfree (cr
->filename
);
1319 cr
->filename
= NULL
;
1320 cr
->ciphertext
= NULL
;
1321 cr
->ciphertext_len
= 0;
1322 cr
->plaintext
= NULL
;
1323 cr
->plaintext_len
= 0;
1327 cleanup_crypto_stage2 (struct crypto_s
*cr
)
1332 cleanup_crypto_stage1 (cr
);
1333 set_header_defaults (&cr
->hdr
);
1337 cleanup_crypto (struct crypto_s
**c
)
1339 struct crypto_s
*cr
= *c
;
1344 cleanup_crypto_stage2 (cr
);
1348 gcry_sexp_release (cr
->pkey_sexp
);
1350 if (cr
->sigpkey_sexp
)
1351 gcry_sexp_release (cr
->sigpkey_sexp
);
1354 cleanup_agent (cr
->agent
);
1361 /* Sets the default cipher values for new files. */
1363 set_header_defaults (file_header_t
* hdr
)
1365 const char *s
= "aes256";
1366 int flags
= cipher_string_to_cipher (s
);
1368 memset (hdr
, 0, sizeof (file_header_t
));
1369 memcpy (hdr
->magic
, crypto_magic
, sizeof (hdr
->magic
));
1371 fprintf(stderr
, _("Invalid 'cipher' in configuration file. Using a default of aes256."));
1373 hdr
->flags
= flags
== -1 ? PWMD_CIPHER_AES256
: flags
;
1374 hdr
->version
= VERSION_HEX
;
1378 hdr
->flags
|= PWMD_FLAG_PKI
;