2 Copyright (C) 2015-2018 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 as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 Pwmd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
31 #include <sys/resource.h>
33 #include <arpa/inet.h>
34 #include <libxml/tree.h>
35 #include <libxml/parser.h>
36 #include <libxml/xpath.h>
40 #include "pwmd-error.h"
43 #ifdef HAVE_SYS_PRCTL_H
44 #include <sys/prctl.h>
55 #define _(msgid) gettext(msgid)
58 #ifdef HAVE_GETOPT_LONG
63 #include "getopt_long.h"
66 #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 main(int argc
, char **argv
)
419 struct crypto_s
*crypto
= NULL
;
420 char *outfile
= NULL
, *infile
= NULL
, *keyfile
= NULL
;
426 const char *optstring
= "vhk:n";
430 const struct option longopts
[] = {
431 {"force", no_argument
, 0, 0},
432 {"xml", no_argument
, 0, 0},
433 {"no-convert", no_argument
, 0, 'n'},
434 {"keyfile", required_argument
, 0, 'k'},
435 {"version", no_argument
, 0, 'v'},
436 {"help", no_argument
, 0, 'h'},
441 #ifdef HAVE_SETRLIMIT
444 rl
.rlim_cur
= rl
.rlim_max
= 0;
446 if (setrlimit (RLIMIT_CORE
, &rl
) != 0)
447 err (EXIT_FAILURE
, "setrlimit()");
450 #ifdef HAVE_PR_SET_DUMPABLE
451 prctl (PR_SET_DUMPABLE
, 0);
455 if (!gpgrt_check_version (REQUIRE_LIBGPGERROR_VERSION
))
457 fprintf (stderr
, _("gpgrt_check_version(): Incompatible libgpg-error. "
458 "Wanted %s, got %s.\n"), REQUIRE_LIBGPGERROR_VERSION
,
459 gpgrt_check_version (NULL
));
464 //gpgrt_set_alloc_func (xrealloc_gpgrt);
466 if (!gcry_check_version (REQUIRE_LIBGCRYPT_VERSION
))
468 fprintf (stderr
, _("gcry_check_version(): Incompatible libgcrypt. "
469 "Wanted %s, got %s.\n"), REQUIRE_LIBGCRYPT_VERSION
,
470 gcry_check_version (NULL
));
474 gcry_set_allocation_handler (xmalloc
, xmalloc
, NULL
, xrealloc
, xfree
);
475 xmlMemSetup (xfree
, xmalloc
, xrealloc
, str_dup
);
481 while ((opt
= getopt_long (argc
, argv
, optstring
, longopts
, &optindex
)) != -1)
492 usage (argv
[0], EXIT_SUCCESS
);
496 "Copyright (C) 2015-2018\n"
498 "Released under the terms of the GPL v2. Use at your own risk.\n\n"),
499 PACKAGE_STRING
, PACKAGE_BUGREPORT
);
511 usage (argv
[0], EXIT_FAILURE
);
515 usage (argv
[0], EXIT_FAILURE
);
520 if (argc
- optind
!= 2)
521 usage (argv
[0], EXIT_FAILURE
);
523 infile
= argv
[optind
++];
524 outfile
= argv
[optind
];
525 if (strcmp (outfile
, "-"))
527 if (access (outfile
, R_OK
|W_OK
) == 0)
529 fprintf(stderr
, _("Please remove the existing output file '%s'.\n"),
535 rc
= init_client_crypto (&crypto
);
541 rc
= decrypt_common (crypto
, infile
, keyfile
);
549 fd
= open (infile
, O_RDONLY
);
552 rc
= gpg_error_from_syserror ();
556 if (fstat (fd
, &st
) == -1)
558 rc
= gpg_error_from_syserror ();
563 crypto
->plaintext
= gcry_calloc (1, sizeof (char) * st
.st_size
+1);
564 if (!crypto
->plaintext
)
571 crypto
->plaintext_len
= read (fd
, crypto
->plaintext
, st
.st_size
);
573 if (crypto
->plaintext_len
!= st
.st_size
)
575 rc
= GPG_ERR_UNKNOWN_ERRNO
;
584 rc
= parse_doc ((char *) crypto
->plaintext
, crypto
->plaintext_len
, &doc
);
585 cleanup_crypto (&crypto
);
591 FILE *fp
= outfile
[0] == '-' && outfile
[1] == 0 ? stdout
592 : fopen (outfile
, "w");
596 rc
= gpg_error_from_syserror ();
603 xmlNodePtr n
= xmlDocGetRootElement (doc
);
604 rc
= strip_literals (infile
, n
->children
, force
);
612 xmlDocDumpMemory (doc
, &buf
, &len
);
614 fprintf (fp
, "%s", buf
);
624 cleanup_crypto (&crypto
);
628 cleanup_crypto (&crypto
);
629 fprintf(stderr
, "ERR %u: %s\n", rc
, gpg_strerror (rc
));
634 hash_key (int algo
, unsigned char *salt
, size_t salt_len
, const void *key
,
635 size_t keylen
, void **result
, size_t *rlen
, uint64_t iterations
)
640 /* Be sure the algorithm is available. */
641 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
, rlen
);
645 /* Always allocate enough for a 256-bit key although the algorithms
646 themselves may use less. Fixes SAVE --cipher with a different
647 keylen than the previously saved cipher when cached. */
649 tmp
= xmalloc (*rlen
);
651 return GPG_ERR_ENOMEM
;
654 iterations
= DEFAULT_KDFS2K_ITERATIONS
;
656 rc
= gcry_kdf_derive(key
, keylen
, GCRY_KDF_ITERSALTED_S2K
, GCRY_MD_SHA1
,
657 salt
, salt_len
, iterations
, *rlen
, tmp
);
667 * Useful for a large amount of data. Rather than doing all of the data in one
668 * iteration do it in chunks. This lets the command be cancelable rather than
669 * waiting for it to complete.
671 #define CRYPTO_BLOCKSIZE(c) (c * 1024)
673 iterate_crypto_once (gcry_cipher_hd_t h
, unsigned char *inbuf
,
674 size_t insize
, size_t blocksize
)
677 off_t len
= CRYPTO_BLOCKSIZE (blocksize
);
678 void *p
= gcry_malloc (len
);
682 return gpg_error (GPG_ERR_ENOMEM
);
684 if (insize
< CRYPTO_BLOCKSIZE (blocksize
))
687 pthread_cleanup_push (gcry_free
, p
);
691 unsigned char *inbuf2
= inbuf
+ total
;
694 if (len
+ total
> insize
)
697 rc
= gcry_cipher_decrypt (h
, p
, len
, inbuf2
, len
);
702 memmove (tmp
, p
, len
);
707 #ifdef HAVE_PTHREAD_TESTCANCEL
708 pthread_testcancel ();
712 pthread_cleanup_pop (1);
713 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
720 cleanup_cipher (void *arg
)
722 gcry_cipher_close ((gcry_cipher_hd_t
) arg
);
725 /* Decrypt the XML data. For PKI data files the key is retrieved from
726 * gpg-agent and the signature verified. */
728 decrypt_data (struct crypto_s
*crypto
, unsigned char *salted_key
,
732 unsigned char *key
= salted_key
;
733 gcry_cipher_hd_t h
= NULL
;
734 size_t blocksize
, keysize
= 0;
735 int algo
= cipher_to_gcrypt (crypto
->hdr
.flags
);
739 size_t keylen
= skeylen
;
740 gcry_sexp_t sig_sexp
;
742 if (crypto
->hdr
.flags
& PWMD_FLAG_PKI
)
744 rc
= agent_extract_key (crypto
, &key
, &keylen
);
748 sig_sexp
= gcry_sexp_find_token (crypto
->ciphertext_sexp
, "sig-val", 0);
752 return GPG_ERR_BAD_DATA
;
755 rc
= agent_verify (crypto
->sigpkey_sexp
, sig_sexp
, crypto
->ciphertext
,
756 crypto
->ciphertext_len
);
757 gcry_sexp_release (sig_sexp
);
764 rc
= gcry_cipher_open (&h
, algo
, GCRY_CIPHER_MODE_CBC
, 0);
767 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_KEYLEN
, NULL
,
771 rc
= gcry_cipher_algo_info (algo
, GCRYCTL_GET_BLKLEN
, NULL
,
775 rc
= gcry_cipher_setiv (h
, crypto
->hdr
.iv
, blocksize
);
778 rc
= gcry_cipher_setkey (h
, key
, keysize
);
785 pthread_cleanup_push (cleanup_cipher
, rc
? NULL
: h
);
789 outbuf
= gcry_malloc (crypto
->hdr
.datalen
+1);
793 memset (outbuf
, 0, crypto
->hdr
.datalen
+1);
796 pthread_cleanup_push (gcry_free
, outbuf
);
798 pthread_cleanup_push (gcry_free
, key
);
804 rc
= GPG_ERR_INV_PARAMETER
;
807 memcpy (outbuf
, crypto
->ciphertext
, crypto
->hdr
.datalen
);
808 rc
= iterate_crypto_once (h
, outbuf
, crypto
->hdr
.datalen
, blocksize
);
811 if (!rc
&& crypto
->hdr
.version
<= 0x03000e)
814 rc
= gcry_cipher_setkey (h
, key
, keysize
);
818 if (!rc
&& crypto
->hdr
.version
<= 0x03000e)
820 for (n
= 0; !rc
&& n
< crypto
->hdr
.s2k_count
; n
++)
822 rc
= gcry_cipher_setiv (h
, crypto
->hdr
.iv
, blocksize
);
826 rc
= iterate_crypto_once (h
, outbuf
, crypto
->hdr
.datalen
, blocksize
);
831 pthread_cleanup_pop (0);
832 if (crypto
->hdr
.flags
& PWMD_FLAG_PKI
)
834 else if (key
&& crypto
->hdr
.version
<= 0x03000e)
837 if (crypto
->hdr
.version
<= 0x03000e && key
)
840 pthread_cleanup_pop (rc
? 1 : 0); /* outbuf */
841 pthread_cleanup_pop (1); /* cipher */
844 char buf
[] = "<?xml ";
846 if (memcmp (outbuf
, buf
, sizeof(buf
)-1))
849 return gpg_error (GPG_ERR_BAD_PASSPHRASE
);
852 crypto
->plaintext
= outbuf
;
853 crypto
->plaintext_len
= crypto
->hdr
.datalen
;
856 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
863 get_passphrase (const char *filename
, const char *keyfile
, void **r_key
,
866 char buf
[255] = { 0 };
867 struct termios told
, tnew
;
877 fd
= open (keyfile
, O_RDONLY
);
879 return gpg_error_from_syserror ();
881 if (fstat (fd
, &st
) == -1)
883 gpg_error_t rc
= gpg_error_from_syserror ();
889 len
= read (fd
, buf
, sizeof (buf
));
890 if (len
!= st
.st_size
)
893 wipememory (buf
, 0, sizeof (buf
));
894 return gpg_error_from_syserror ();
897 *r_len
= st
.st_size
? st
.st_size
: 1;
898 *r_key
= xmalloc (*r_len
);
900 memcpy (*r_key
, buf
, *r_len
);
901 wipememory (buf
, 0, sizeof (buf
));
903 return *r_key
? 0 : GPG_ERR_ENOMEM
;
906 if (!isatty (STDIN_FILENO
))
907 return GPG_ERR_ENOTTY
;
909 if (tcgetattr (STDIN_FILENO
, &told
) == -1)
910 return gpg_error_from_syserror ();
912 memcpy (&tnew
, &told
, sizeof (struct termios
));
913 tnew
.c_lflag
&= ~(ECHO
);
914 tnew
.c_lflag
|= ICANON
| ECHONL
;
915 if (tcsetattr (STDIN_FILENO
, TCSANOW
, &tnew
) == -1)
919 tcsetattr (STDIN_FILENO
, TCSANOW
, &told
);
920 return gpg_error_from_errno (n
);
923 fprintf(stderr
, "Passphrase for '%s': ", filename
);
924 if (!fgets (buf
, sizeof (buf
), stdin
))
926 tcsetattr (STDIN_FILENO
, TCSANOW
, &told
);
930 tcsetattr (STDIN_FILENO
, TCSANOW
, &told
);
932 if (buf
[strlen(buf
)-1] == '\n')
933 buf
[strlen(buf
)-1] = 0;
934 *r_len
= buf
[0] == 0 ? 1 : strlen (buf
);
935 *r_key
= str_dup (buf
);
936 wipememory (buf
, 0, sizeof (buf
));
940 /* Common to both PKI and non-PKI files. */
942 decrypt_common (struct crypto_s
*crypto
, const char *filename
,
947 gpg_error_t rc
= read_data_file (filename
, crypto
);
948 int algo
= cipher_to_gcrypt (crypto
->hdr
.flags
);
955 rc
= get_passphrase (filename
, keyfile
, &key
, &keylen
);
959 if (key
)// && !IS_PKI (crypto))
961 rc
= hash_key (algo
, crypto
->hdr
.salt
, sizeof(crypto
->hdr
.salt
), key
,
962 keylen
, &skey
, &skeysize
,
963 crypto
->hdr
.version
<= 0x03000e ? COMPAT_KDFS2K_ITERATIONS
: crypto
->hdr
.s2k_count
);
972 xfree (crypto
->filename
);
973 crypto
->filename
= str_dup (filename
);
974 rc
= decrypt_data (crypto
, skey
, skeysize
);
980 read_header (file_header_t
*hdr
, int fd
)
986 #ifdef WORDS_BIGENDIAN
987 len
= read (fd
, &hdr
->magic
, sizeof(hdr
->magic
));
991 len
= read (fd
, &hdr
->version
, sizeof(hdr
->version
));
995 len
= read (fd
, &hdr
->s2k_count
, sizeof(hdr
->s2k_count
));
999 len
= read (fd
, &hdr
->flags
, sizeof(hdr
->flags
));
1003 len
= read (fd
, &hdr
->iv
, sizeof(hdr
->iv
));
1007 len
= read (fd
, &hdr
->salt
, sizeof(hdr
->salt
));
1011 len
= read (fd
, &hdr
->datalen
, sizeof(hdr
->datalen
));
1015 len
= read (fd
, &hdr
->magic
, sizeof(hdr
->magic
));
1019 len
= read (fd
, &i
, sizeof(hdr
->version
));
1022 hdr
->version
= ntohl (i
);
1024 len
= read (fd
, &n
, sizeof(hdr
->s2k_count
));
1027 hdr
->s2k_count
= (((uint64_t) ntohl (n
)) << 32) + ntohl (n
>> 32);
1029 len
= read (fd
, &n
, sizeof(hdr
->flags
));
1032 hdr
->flags
= (((uint64_t) ntohl (n
)) << 32) + ntohl (n
>> 32);
1034 len
= read (fd
, &hdr
->iv
, sizeof(hdr
->iv
));
1038 len
= read (fd
, &hdr
->salt
, sizeof(hdr
->salt
));
1042 len
= read (fd
, &i
, sizeof(hdr
->datalen
));
1045 hdr
->datalen
= ntohl (i
);
1049 return len
== -1 ? gpg_error_from_errno (errno
) : 0;
1052 /* Read the header of a data file to determine cipher and other. The
1053 * header is stored big endian in the file and is converted to little
1054 * endian when needed. */
1056 read_data_header (const char *filename
, file_header_t
* rhdr
,
1057 struct stat
*rst
, int *rfd
)
1064 fd
= open (filename
, O_RDONLY
);
1066 return gpg_error_from_syserror ();
1068 if (fstat (fd
, &st
) == -1)
1070 rc
= gpg_error_from_syserror ();
1075 rc
= read_header (&hdr
, fd
);
1076 if (!rc
&& memcmp (hdr
.magic
, crypto_magic
, sizeof (hdr
.magic
)))
1077 rc
= GPG_ERR_BAD_DATA
;
1078 else if (!rc
&& hdr
.version
< 0x030000)
1079 rc
= GPG_ERR_UNKNOWN_VERSION
;
1095 return gpg_error (rc
);
1099 read_data_file (const char *filename
, struct crypto_s
* crypto
)
1108 return GPG_ERR_INV_PARAMETER
;
1110 cleanup_crypto_stage1 (crypto
);
1111 rc
= read_data_header (filename
, &crypto
->hdr
, &st
, &fd
);
1113 return gpg_error (rc
);
1115 crypto
->ciphertext_len
= crypto
->hdr
.datalen
;
1116 crypto
->ciphertext
= xmalloc (crypto
->hdr
.datalen
);
1117 if (!crypto
->ciphertext
)
1119 rc
= GPG_ERR_ENOMEM
;
1123 /* The keygrips for PKI files are stored after the header. They are
1124 * stored in the file to let file(1) magic(5) show the grips. */
1125 if (crypto
->hdr
.flags
& PWMD_FLAG_PKI
)
1127 rlen
= read (fd
, crypto
->grip
, 20);
1130 rc
= rlen
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
1134 rlen
= read (fd
, crypto
->sign_grip
, 20);
1137 rc
= rlen
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
1142 len
= read (fd
, crypto
->ciphertext
, crypto
->hdr
.datalen
);
1143 if (len
!= crypto
->hdr
.datalen
)
1145 rc
= len
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
1149 if (!(crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
1154 rc
= GPG_ERR_NOT_IMPLEMENTED
;
1159 len
= st
.st_size
- sizeof (file_header_t
) - crypto
->hdr
.datalen
- 40;
1160 buf
= xmalloc (len
);
1163 rc
= GPG_ERR_ENOMEM
;
1167 /* Remaining data file bytes are the encrypted key and XML. */
1168 rlen
= read (fd
, buf
, len
);
1171 rc
= rlen
== -1 ? gpg_error_from_errno (errno
) : GPG_ERR_BAD_DATA
;
1175 rc
= gcry_sexp_new (&crypto
->ciphertext_sexp
, buf
, rlen
, 1);
1179 if (crypto
->pkey_sexp
)
1180 gcry_sexp_release (crypto
->pkey_sexp
);
1182 if (crypto
->sigpkey_sexp
)
1183 gcry_sexp_release (crypto
->sigpkey_sexp
);
1185 crypto
->pkey_sexp
= crypto
->sigpkey_sexp
= NULL
;
1186 rc
= get_pubkey_bin (crypto
, crypto
->grip
, &crypto
->pkey_sexp
);
1188 rc
= get_pubkey_bin (crypto
, crypto
->sign_grip
, &crypto
->sigpkey_sexp
);
1194 if (rc
&& gpg_err_source (rc
) == GPG_ERR_SOURCE_UNKNOWN
)
1195 rc
= gpg_error (rc
);
1201 cleanup_save (struct save_s
*save
)
1208 gcry_sexp_release (save
->pkey
);
1211 gcry_sexp_release (save
->sigpkey
);
1214 memset (save
, 0, sizeof (struct save_s
));
1217 /* Keep the agent ctx to retain pinentry options which will be freed in
1218 * cleanup_cb(). Also keep .pubkey since it may be needed for a SAVE. */
1220 cleanup_crypto_stage1 (struct crypto_s
*cr
)
1225 cleanup_save (&cr
->save
);
1228 if (cr
->ciphertext_sexp
)
1229 gcry_sexp_release (cr
->ciphertext_sexp
);
1231 cr
->ciphertext_sexp
= NULL
;
1234 gcry_free (cr
->plaintext
);
1235 xfree (cr
->ciphertext
);
1236 xfree (cr
->filename
);
1237 cr
->filename
= NULL
;
1238 cr
->ciphertext
= NULL
;
1239 cr
->ciphertext_len
= 0;
1240 cr
->plaintext
= NULL
;
1241 cr
->plaintext_len
= 0;
1245 cleanup_crypto_stage2 (struct crypto_s
*cr
)
1250 cleanup_crypto_stage1 (cr
);
1251 set_header_defaults (&cr
->hdr
);
1255 cleanup_crypto (struct crypto_s
**c
)
1257 struct crypto_s
*cr
= *c
;
1262 cleanup_crypto_stage2 (cr
);
1266 gcry_sexp_release (cr
->pkey_sexp
);
1268 if (cr
->sigpkey_sexp
)
1269 gcry_sexp_release (cr
->sigpkey_sexp
);
1272 cleanup_agent (cr
->agent
);
1279 /* Sets the default cipher values for new files. */
1281 set_header_defaults (file_header_t
* hdr
)
1283 const char *s
= "aes256";
1284 int flags
= cipher_string_to_cipher (s
);
1286 memset (hdr
, 0, sizeof (file_header_t
));
1287 memcpy (hdr
->magic
, crypto_magic
, sizeof (hdr
->magic
));
1289 fprintf(stderr
, _("Invalid 'cipher' in configuration file. Using a default of aes256."));
1291 hdr
->flags
= flags
== -1 ? PWMD_CIPHER_AES256
: flags
;
1292 hdr
->version
= VERSION_HEX
;
1296 hdr
->flags
|= PWMD_FLAG_PKI
;