2 Copyright (C) 2006-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/>.
24 #include <sys/types.h>
34 #include "pwmd-error.h"
35 #include "util-misc.h"
41 #include "util-string.h"
45 static unsigned keepalive
;
46 static pthread_mutex_t crypto_mutex
= PTHREAD_MUTEX_INITIALIZER
;
50 #define STATUS_TIMEOUT_INIT(crypto) \
52 crypto->status_timeout = time (NULL); \
53 crypto->progress_rc = 0; \
56 #define STATUS_TIMEOUT_TEST(crypto, rc, s, line) \
58 time_t now = time (NULL); \
59 if (now - crypto->status_timeout >= keepalive && crypto->client_ctx) { \
60 rc = send_status (crypto->client_ctx, s, line); \
61 crypto->status_timeout = now; \
66 get_password_file (const char *keyfile
, unsigned char **result
, size_t *len
)
70 unsigned char *buf
= NULL
;
73 log_write (_ ("obtaining passphrase from passphrase file"));
74 rc
= open_check_file (keyfile
, &fd
, &st
, 0);
78 buf
= xmalloc (st
.st_size
+1);
81 size_t rlen
= read (fd
, buf
, st
.st_size
);
83 if (rlen
!= st
.st_size
)
84 rc
= GPG_ERR_ASS_READ_ERROR
;
88 /* The passphrase may have been truncated do to a nul byte before the
89 * EOF. Better to return an error here rather than passing the
90 * truncated passphrase on. */
91 if (rlen
&& strlen ((char *)buf
) != rlen
)
92 rc
= GPG_ERR_INV_PASSPHRASE
;
112 status_cb (void *data
, const char *keyword
, const char *args
)
114 struct crypto_s
*crypto
= data
;
117 if (!crypto
->client_ctx
)
120 if (!strcmp (keyword
, "INQUIRE_MAXLEN") &&
121 (crypto
->flags
& CRYPTO_FLAG_KEYFILE
))
124 return assuan_write_status (crypto
->client_ctx
, keyword
, args
);
128 passphrase_cb (void *data
, const char *hint
, const char *info
, int bad
, int fd
)
130 struct crypto_s
*crypto
= data
;
131 unsigned char *result
= NULL
;
135 if (crypto
->flags
& CRYPTO_FLAG_KEYFILE
)
136 rc
= get_password_file (crypto
->keyfile
, &result
, &len
);
140 rc
= assuan_write_status (crypto
->client_ctx
, "PASSPHRASE_HINT", hint
);
143 rc
= assuan_write_status (crypto
->client_ctx
, "PASSPHRASE_INFO", info
);
147 const char *keyword
= "PASSPHRASE";
149 if (!bad
&& crypto
->flags
& CRYPTO_FLAG_PASSWD_SIGN
)
150 keyword
= "SIGN_PASSPHRASE";
151 else if (!bad
&& crypto
->flags
& CRYPTO_FLAG_PASSWD_NEW
)
153 crypto
->flags
|= CRYPTO_FLAG_PASSWD_SIGN
;
154 keyword
= "NEW_PASSPHRASE";
156 else if (!bad
&& crypto
->flags
& CRYPTO_FLAG_PASSWD
)
157 crypto
->flags
|= CRYPTO_FLAG_PASSWD_NEW
;
158 else if (!bad
&& crypto
->flags
& CRYPTO_FLAG_NEWFILE
)
160 keyword
= "NEW_PASSPHRASE";
161 if (crypto
->save
.sigkey
)
162 crypto
->flags
|= CRYPTO_FLAG_PASSWD_SIGN
;
165 rc
= assuan_inquire (crypto
->client_ctx
, keyword
, &result
, &len
, 0);
171 pthread_cleanup_push ((void *)xfree
, result
);
174 gpgme_io_writen (fd
, "\n", 1);
177 int nl
= result
[len
-1] == '\n';
180 ret
= gpgme_io_writen (fd
, result
, len
);
182 gpgme_io_writen (fd
, "\n", 1);
184 rc
= GPG_ERR_CANCELED
;
187 pthread_cleanup_pop (1);
194 progress_cb (void *data
, const char *what
, int type
, int current
, int total
)
196 struct crypto_s
*crypto
= data
;
198 crypto
->progress_rc
= send_status (crypto
->client_ctx
, STATUS_GPGME
,
199 "%s %i %i %i", what
, type
, current
, total
);
203 crypto_data_to_buf (const gpgme_data_t data
, unsigned char **result
,
206 off_t ret
= gpgme_data_seek (data
, 0, SEEK_SET
);
208 size_t total
= 0, size
= BUFSIZE
;
209 unsigned char *buf
= NULL
;
215 return gpg_error_from_syserror ();
217 buf
= xmalloc (size
);
219 return GPG_ERR_ENOMEM
;
223 ret
= gpgme_data_read (data
, &buf
[total
], BUFSIZE
);
230 p
= xrealloc (buf
, size
* sizeof (unsigned char));
234 return GPG_ERR_ENOMEM
;
243 rc
= gpgme_err_code_from_syserror ();
258 crypto_list_keys (struct crypto_s
*crypto
, char *keys
[], int secret
,
259 gpgme_key_t
**result
)
262 gpgme_key_t key
= NULL
, *res
= NULL
;
265 rc
= gpgme_op_keylist_ext_start (crypto
->ctx
, (const char **)keys
, secret
, 0);
269 STATUS_TIMEOUT_INIT (crypto
);
274 pthread_cleanup_push ((void *)crypto_free_key_list
, res
);
275 rc
= gpgme_op_keylist_next (crypto
->ctx
, &key
);
276 pthread_cleanup_pop (0);
277 if (rc
&& gpgme_err_code(rc
) != GPG_ERR_EOF
)
286 p
= xrealloc (res
, (total
+2) * sizeof (gpgme_key_t
));
296 pthread_cleanup_push ((void *)crypto_free_key_list
, res
);
297 STATUS_TIMEOUT_TEST (crypto
, rc
, STATUS_KEEPALIVE
, NULL
);
298 pthread_cleanup_pop (0);
303 rc
= gpgme_op_keylist_end (crypto
->ctx
);
307 rc
= total
? 0 : secret
? GPG_ERR_NO_SECKEY
: GPG_ERR_NO_PUBKEY
;
311 crypto_free_key_list (res
);
317 crypto_free_save (struct save_s
*save
)
322 strv_free (save
->pubkey
);
323 xfree (save
->sigkey
);
324 xfree (save
->userid
);
326 crypto_free_key_list (save
->mainkey
);
328 memset (save
, 0, sizeof (struct save_s
));
332 free_gpgme_data_cb (void *data
)
334 gpgme_data_t d
= data
;
341 t
= gpgme_data_release_and_get_mem (d
, &len
);
343 wipememory (t
, 0, len
);
349 crypto_free_non_keys (struct crypto_s
*crypto
)
355 gpgme_release (crypto
->ctx
);
358 xfree (crypto
->plaintext
);
359 crypto
->plaintext
= NULL
;
360 crypto
->plaintext_size
= 0;
363 free_gpgme_data_cb (crypto
->cipher
);
365 crypto
->cipher
= NULL
;
366 crypto_free_save (&crypto
->save
);
367 xfree (crypto
->keyfile
);
368 crypto
->keyfile
= NULL
;
369 crypto
->flags
&= ~CRYPTO_FLAG_KEYFILE
;
373 crypto_free (struct crypto_s
*crypto
)
378 crypto_free_non_keys (crypto
);
379 strv_free (crypto
->pubkey
);
380 xfree (crypto
->sigkey
);
381 xfree (crypto
->filename
);
386 crypto_init_ctx (struct crypto_s
*crypto
, int no_pinentry
,
387 char *passphrase_file
)
390 gpgme_keylist_mode_t keylist_mode
;
393 rc
= gpgme_new (&ctx
);
397 rc
= gpgme_set_protocol (ctx
, GPGME_PROTOCOL_OPENPGP
);
400 keylist_mode
= gpgme_get_keylist_mode (ctx
);
401 keylist_mode
|= GPGME_KEYLIST_MODE_LOCAL
|GPGME_KEYLIST_MODE_WITH_SECRET
;
402 rc
= gpgme_set_keylist_mode (ctx
, keylist_mode
);
405 if (no_pinentry
|| passphrase_file
)
406 rc
= gpgme_set_pinentry_mode (ctx
, GPGME_PINENTRY_MODE_LOOPBACK
);
408 rc
= gpgme_set_pinentry_mode (ctx
, GPGME_PINENTRY_MODE_DEFAULT
);
412 gpgme_set_passphrase_cb (ctx
, passphrase_cb
, crypto
);
413 gpgme_set_progress_cb (ctx
, progress_cb
, crypto
);
414 gpgme_set_status_cb (ctx
, status_cb
, crypto
);
420 crypto
->keyfile
= passphrase_file
;
421 crypto
->flags
|= CRYPTO_FLAG_KEYFILE
;
434 crypto_set_keepalive ()
436 keepalive
= config_get_integer ("global", "keepalive_interval");
440 crypto_init (struct crypto_s
**crypto
, void *ctx
, const char *filename
,
441 int no_pinentry
, char *passphrase_file
)
443 struct crypto_s
*new = xcalloc (1, sizeof (struct crypto_s
));
446 crypto_set_keepalive ();
449 return GPG_ERR_ENOMEM
;
451 rc
= crypto_init_ctx (new, no_pinentry
, passphrase_file
);
457 new->filename
= str_dup (filename
);
465 new->client_ctx
= ctx
;
474 /* Converts a 40 byte key id to the 16 byte form. Needed for comparison of
475 * gpgme_recipient_t.keyid. */
477 crypto_keyid_to_16b_once (char *key
)
479 size_t len
= strlen (key
);
482 return GPG_ERR_INV_ARG
;
484 memmove (&key
[0], &key
[len
-16], 16);
490 crypto_keyid_to_16b (char **keys
)
494 for (p
= keys
; p
&& *p
; p
++)
496 gpg_error_t rc
= crypto_keyid_to_16b_once (*p
);
505 crypto_decrypt (struct client_s
*client
, struct crypto_s
*crypto
)
512 char **new_recipients
= NULL
;
513 char **new_signers
= NULL
;
515 gpgme_decrypt_result_t result
;
517 rc
= gpgme_data_new (&plain
);
521 pthread_cleanup_push ((void *)free_gpgme_data_cb
, plain
);
522 rc
= open_check_file (crypto
->filename
, &fd
, &st
, 1);
525 pthread_cleanup_push ((void *)close_fd_cb
, &fd
);
526 rc
= gpgme_data_new_from_fd (&cipher
, fd
);
529 pthread_cleanup_push ((void *)free_gpgme_data_cb
, cipher
);
530 rc
= send_status (client
? client
->ctx
: NULL
, STATUS_DECRYPT
, NULL
);
533 MUTEX_TRYLOCK (client
->ctx
, &crypto_mutex
, rc
,
534 client
->lock_timeout
);
537 pthread_cleanup_push (release_mutex_cb
, &crypto_mutex
);
538 rc
= gpgme_op_decrypt_verify_start (crypto
->ctx
, cipher
, plain
);
541 STATUS_TIMEOUT_INIT(crypto
);
544 if (!rc
&& crypto
->progress_rc
)
545 rc
= crypto
->progress_rc
;
547 STATUS_TIMEOUT_TEST (crypto
, rc
, STATUS_DECRYPT
, NULL
);
549 } while (!rc
&& !gpgme_wait (crypto
->ctx
, &rc
, 0) && !rc
);
551 pthread_cleanup_pop (1); // release_mutex_cb
554 pthread_cleanup_pop (1); // free_gpgme_data_cb (cipher)
557 pthread_cleanup_pop (1); // close (fd)
560 pthread_cleanup_pop (0); // free_gpgme_data_cb (plain)
564 free_gpgme_data_cb (plain
);
568 pthread_cleanup_push ((void *)free_gpgme_data_cb
, plain
);
569 result
= gpgme_op_decrypt_result (crypto
->ctx
);
570 if (!result
->unsupported_algorithm
&& !result
->wrong_key_usage
)
574 for (r
= result
->recipients
; r
; r
= r
->next
)
576 pthread_cleanup_push ((void *)strv_free
, new_recipients
);
577 log_write1 (_ ("%s: recipient: %s, status=%u"),
578 crypto
->filename
, r
->keyid
, r
->status
);
579 pthread_cleanup_pop (0);
580 pp
= strv_cat(new_recipients
, str_dup (r
->keyid
));
590 else if (result
->wrong_key_usage
)
591 rc
= GPG_ERR_WRONG_KEY_USAGE
;
592 else if (result
->unsupported_algorithm
)
593 rc
= GPG_ERR_UNSUPPORTED_ALGORITHM
;
597 gpgme_verify_result_t verify
;
600 verify
= gpgme_op_verify_result (crypto
->ctx
);
601 for (s
= verify
->signatures
; s
; s
= s
->next
)
603 unsigned flags
= s
->summary
;
605 pthread_cleanup_push ((void *)strv_free
, new_signers
);
606 log_write1 (_ ("%s: signer: %s, status=%u"),
607 crypto
->filename
, s
->fpr
, s
->status
);
608 pthread_cleanup_pop (0);
610 flags
&= ~(GPGME_SIGSUM_KEY_EXPIRED
|GPGME_SIGSUM_SIG_EXPIRED
);
612 flags
|= GPGME_SIGSUM_VALID
;
614 if (gpg_err_code (s
->status
) == GPG_ERR_KEY_EXPIRED
)
617 if (s
->status
|| s
->wrong_key_usage
|| !(flags
& GPGME_SIGSUM_VALID
))
620 pp
= strv_cat (new_signers
, str_dup (s
->fpr
));
630 if (verify
->signatures
&& !new_signers
)
631 rc
= GPG_ERR_BAD_SIGNATURE
;
635 xfree (crypto
->plaintext
);
636 crypto
->plaintext
= NULL
;
637 crypto
->plaintext_size
= 0;
638 pthread_cleanup_push ((void *)strv_free
, new_recipients
);
639 pthread_cleanup_push ((void *)strv_free
, new_signers
);
640 rc
= crypto_data_to_buf (plain
, &crypto
->plaintext
,
641 &crypto
->plaintext_size
);
642 pthread_cleanup_pop (0);
643 pthread_cleanup_pop (0);
646 strv_free (crypto
->pubkey
);
647 crypto
->pubkey
= new_recipients
;
648 crypto_keyid_to_16b (crypto
->pubkey
);
650 xfree (crypto
->sigkey
);
651 crypto
->sigkey
= NULL
;
654 crypto
->sigkey
= str_dup (*new_signers
);
655 strv_free (new_signers
);
656 crypto_keyid_to_16b_once (crypto
->sigkey
);
664 strv_free (new_recipients
);
665 strv_free (new_signers
);
668 pthread_cleanup_pop (1); // free_gpgme_data_cb (plain)
673 crypto_free_key_list (gpgme_key_t
*keys
)
677 for (i
= 0; keys
&& keys
[i
]; i
++)
678 gpgme_key_unref (keys
[i
]);
683 /* Removes strings in 'prune' from 'a'. */
685 prune_keys (char **a
, char **prune
)
689 for (p
= prune
; p
&& *p
; p
++)
693 for (ap
= a
; ap
&& *ap
; ap
++)
695 if (!strcmp (*ap
, *p
))
710 remove_duplicates (char **a
)
714 for (p
= a
; p
&& *p
; p
++)
718 for (t
= p
+1; t
&& *t
; t
++)
720 if (!strcmp (*p
, *t
))
732 remove_duplicates (a
);
740 crypto_encrypt (struct client_s
*client
, struct crypto_s
*crypto
)
743 gpgme_data_t cipher
= NULL
;
744 gpgme_key_t
*keys
= NULL
;
745 gpgme_key_t
*sigkeys
= NULL
;
749 if (!(crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
))
751 crypto_keyid_to_16b (crypto
->save
.pubkey
);
752 remove_duplicates (crypto
->save
.pubkey
);
753 rc
= crypto_list_keys (crypto
, crypto
->save
.pubkey
, 0, &keys
);
758 pthread_cleanup_push ((void *)crypto_free_key_list
, keys
);
759 rc
= gpgme_data_new (&cipher
);
760 pthread_cleanup_push ((void *)free_gpgme_data_cb
, cipher
);
762 rc
= gpgme_data_set_file_name (cipher
, crypto
->filename
);
764 if (!rc
&& crypto
->save
.sigkey
)
768 crypto_keyid_to_16b_once (crypto
->save
.sigkey
);
769 strv_printf (&tmp
, "%s", crypto
->save
.sigkey
);
770 pthread_cleanup_push ((void *)strv_free
, tmp
);
771 rc
= crypto_list_keys (crypto
, tmp
, 1, &sigkeys
);
772 pthread_cleanup_pop (1);
773 if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
779 gpgme_data_t plain
= NULL
;
781 pthread_cleanup_push ((void *)crypto_free_key_list
, sigkeys
);
782 rc
= gpgme_data_new_from_mem (&plain
, (char *)crypto
->plaintext
,
783 crypto
->plaintext_size
, 0);
784 pthread_cleanup_push ((void *)free_gpgme_data_cb
, plain
);
789 gpgme_signers_clear (crypto
->ctx
);
791 for (i
= 0; sigkeys
&& sigkeys
[i
]; i
++)
792 gpgme_signers_add (crypto
->ctx
, sigkeys
[i
]);
797 gpgme_data_set_encoding (cipher
, GPGME_DATA_ENCODING_ARMOR
);
798 rc
= send_status (client
? client
->ctx
: NULL
, STATUS_ENCRYPT
, NULL
);
803 int f
= config_get_boolean ("global", "encrypt_to");
806 flags
|= GPGME_ENCRYPT_NO_ENCRYPT_TO
;
808 f
= config_get_boolean ("global", "always_trust");
810 flags
|= GPGME_ENCRYPT_ALWAYS_TRUST
;
812 if (!(crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
)
813 || (crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
&& sigkeys
))
818 MUTEX_TRYLOCK (client
->ctx
, &crypto_mutex
, rc
, client
->lock_timeout
);
822 pthread_cleanup_push (release_mutex_cb
, &crypto_mutex
);
824 rc
= gpgme_op_encrypt_sign_start (crypto
->ctx
, keys
, flags
, plain
,
827 rc
= gpgme_op_encrypt_start (crypto
->ctx
, NULL
, flags
, plain
,
831 STATUS_TIMEOUT_INIT (crypto
);
834 if (!rc
&& crypto
->progress_rc
)
835 rc
= crypto
->progress_rc
;
837 STATUS_TIMEOUT_TEST (crypto
, rc
, STATUS_ENCRYPT
, NULL
);
839 } while (!rc
&& !gpgme_wait (crypto
->ctx
, &rc
, 0) && !rc
);
841 pthread_cleanup_pop (1);
845 gpgme_encrypt_result_t result
;
846 gpgme_sign_result_t sresult
;
847 gpgme_invalid_key_t inv
;
848 gpgme_new_signature_t sigs
;
852 result
= gpgme_op_encrypt_result (crypto
->ctx
);
853 inv
= result
->invalid_recipients
;
856 pthread_cleanup_push ((void *)strv_free
, prune
);
857 log_write1 (_ ("%s: invalid recipient: %s, rc=%u"),
858 crypto
->filename
, inv
->fpr
, inv
->reason
);
859 pthread_cleanup_pop (0);
860 p
= strv_cat (prune
, str_dup(inv
->fpr
));
874 p
= prune_keys (crypto
->save
.pubkey
, prune
);
875 crypto
->save
.pubkey
= p
;
879 crypto_keyid_to_16b (crypto
->save
.pubkey
);
880 sresult
= gpgme_op_sign_result (crypto
->ctx
);
881 inv
= sresult
? sresult
->invalid_signers
: NULL
;
884 log_write1 (_ ("%s: invalid signer: %s, rc=%u"),
885 crypto
->filename
, inv
->fpr
, inv
->reason
);
889 sigs
= sresult
? sresult
->signatures
: NULL
;
898 pthread_cleanup_push ((void *)strv_free
, p
);
899 log_write1 (_ ("%s: signer: %s"), crypto
->filename
,
901 pthread_cleanup_pop (0);
903 pp
= strv_cat (p
, str_dup (sigs
->fpr
));
917 xfree (crypto
->save
.sigkey
);
918 crypto
->save
.sigkey
= str_dup (*p
);
920 crypto_keyid_to_16b_once (crypto
->save
.sigkey
);
921 crypto
->cipher
= cipher
;
924 else if (!rc
&& crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
)
926 crypto
->cipher
= cipher
;
930 if (!(crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
)
931 || (crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
&& sigkeys
))
932 rc
= GPG_ERR_NO_SIGNATURE_SCHEME
;
937 pthread_cleanup_pop (1); // free_gpgme_data_cb (plain)
938 pthread_cleanup_pop (1); // crypto_free_key_list (sigkeys)
941 pthread_cleanup_pop (0); // free_gpgme_data_cb (cipher)
942 pthread_cleanup_pop (1); // crypto_free_key_list (keys)
945 free_gpgme_data_cb (cipher
);
951 crypto_passwd (struct client_s
*client
, struct crypto_s
*crypto
)
954 gpgme_key_t
*keys
= NULL
;
956 /* Use SAVE instead for symmetric files. */
957 rc
= crypto_is_symmetric (client
->filename
);
958 if (!rc
|| rc
!= GPG_ERR_BAD_DATA
)
959 return !rc
? GPG_ERR_NOT_SUPPORTED
: rc
;
961 /* Cannot change the passphrase of a key stored on a smartcard. Use
962 * gpg --card-edit instead. */
963 rc
= cache_is_shadowed (client
->filename
);
965 return GPG_ERR_NOT_SUPPORTED
;
966 else if (rc
!= GPG_ERR_NO_DATA
)
969 rc
= crypto_list_keys (crypto
, crypto
->pubkey
, 1, &keys
);
973 crypto
->flags
|= CRYPTO_FLAG_PASSWD
;
974 pthread_cleanup_push ((void *)crypto_free_key_list
, keys
);
975 rc
= send_status (client
->ctx
, STATUS_KEEPALIVE
, NULL
);
977 MUTEX_TRYLOCK (client
->ctx
, &crypto_mutex
, rc
, client
->lock_timeout
);
981 pthread_cleanup_push (release_mutex_cb
, &crypto_mutex
);
982 rc
= gpgme_op_passwd_start (crypto
->ctx
, keys
[0], 0);
985 STATUS_TIMEOUT_INIT (crypto
);
988 if (!rc
&& crypto
->progress_rc
)
989 rc
= crypto
->progress_rc
;
991 STATUS_TIMEOUT_TEST (crypto
, rc
, STATUS_KEEPALIVE
, NULL
);
993 } while (!rc
&& !gpgme_wait (crypto
->ctx
, &rc
, 0) && !rc
);
995 pthread_cleanup_pop (1);
998 pthread_cleanup_pop (1);
1002 /* Generate a new key pair. The resulting keyid's are stored in crypto->save.
1005 crypto_genkey (struct client_s
*client
, struct crypto_s
*crypto
)
1009 rc
= send_status (client
? client
->ctx
: NULL
, STATUS_GENKEY
, NULL
);
1011 MUTEX_TRYLOCK ((client
? client
->ctx
: NULL
), &crypto_mutex
, rc
,
1012 (client
? client
->lock_timeout
: 0));
1016 pthread_cleanup_push (release_mutex_cb
, &crypto_mutex
);
1017 if (crypto
->save
.mainkey
)
1019 rc
= gpgme_op_createsubkey_start (crypto
->ctx
,
1020 crypto
->save
.mainkey
[0],
1023 crypto
->save
.expire
,
1024 crypto
->save
.flags
);
1028 rc
= gpgme_op_createkey_start (crypto
->ctx
,
1029 crypto
->save
.userid
,
1032 crypto
->save
.expire
,
1034 crypto
->save
.flags
);
1039 STATUS_TIMEOUT_INIT (crypto
);
1042 if (!rc
&& crypto
->progress_rc
)
1043 rc
= crypto
->progress_rc
;
1045 STATUS_TIMEOUT_TEST (crypto
, rc
, STATUS_GENKEY
, NULL
);
1047 } while (!rc
&& !gpgme_wait (crypto
->ctx
, &rc
, 0) && !rc
);
1049 pthread_cleanup_pop (1);
1054 gpgme_key_t
*keys
= NULL
;
1055 gpgme_genkey_result_t result
;
1057 result
= gpgme_op_genkey_result (crypto
->ctx
);
1058 xfree (crypto
->save
.sigkey
);
1059 crypto
->save
.sigkey
= str_dup (result
->fpr
);
1060 crypto_keyid_to_16b_once (crypto
->save
.sigkey
);
1066 strv_printf (&tmp
, "%s", crypto
->save
.sigkey
);
1067 pthread_cleanup_push ((void *)strv_free
, tmp
);
1068 rc
= crypto_list_keys (crypto
, tmp
, 1, &keys
);
1069 pthread_cleanup_pop (1);
1074 gpgme_subkey_t key
= keys
[0]->subkeys
;
1076 for (; key
; key
= key
->next
)
1078 if (key
->can_encrypt
)
1082 pthread_cleanup_push ((void *)crypto_free_key_list
, keys
);
1083 rc
= send_status (client
? client
->ctx
: NULL
, STATUS_GENKEY
, "%s %s",
1084 crypto
->save
.sigkey
, key
? key
->fpr
: "");
1087 crypto
->save
.pubkey
= strv_cat (crypto
->save
.pubkey
,
1088 str_dup (key
->fpr
));
1089 crypto_keyid_to_16b (crypto
->save
.pubkey
);
1092 pthread_cleanup_pop (1);
1101 acl_free_cb (void *arg
)
1103 acl_t acl
= arg
? (acl_t
) arg
: NULL
;
1110 /* The advisory lock should be obtained before calling this function. */
1112 crypto_write_file (struct crypto_s
*crypto
, unsigned char **r_crc
,
1126 if (crypto
->filename
)
1128 if (lstat (crypto
->filename
, &st
) == 0)
1130 mode
= st
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
1132 if (!(mode
& S_IWUSR
))
1133 return GPG_ERR_EACCES
;
1135 else if (errno
!= ENOENT
)
1136 return gpg_error_from_errno (errno
);
1138 snprintf (tmp
, sizeof (tmp
), "%s.XXXXXX", crypto
->filename
);
1139 mode_t tmode
= umask (0600);
1143 rc
= gpg_error_from_errno (errno
);
1145 log_write ("%s: %s", tmp
, pwmd_strerror (rc
));
1154 rc
= crypto_data_to_buf (crypto
->cipher
, &buf
, &size
);
1157 if (crypto
->filename
)
1162 pthread_cleanup_push ((void *)close_fd_cb
, &fd
);
1164 rc
= get_checksum_memory (buf
, size
, r_crc
, r_crclen
);
1168 pthread_cleanup_push ((void *)xfree
, buf
);
1169 len
= write (fd
, buf
, size
);
1170 pthread_cleanup_pop (1);
1172 rc
= gpg_error_from_errno (errno
);
1177 if (fsync (fd
) != -1)
1179 if (crypto
->filename
&& close (fd
) != -1)
1182 acl
= acl_get_file (crypto
->filename
, ACL_TYPE_ACCESS
);
1183 if (!acl
&& errno
== ENOENT
)
1184 acl
= acl_get_file (".", ACL_TYPE_DEFAULT
);
1186 log_write ("ACL: %s: %s", crypto
->filename
,
1187 pwmd_strerror (gpg_error_from_errno (errno
)));
1188 pthread_cleanup_push ((void *)acl_free_cb
, acl
);
1192 if (config_get_boolean (crypto
->filename
, "backup"))
1194 char tmp2
[PATH_MAX
];
1196 snprintf (tmp2
, sizeof (tmp2
), "%s.backup", crypto
->filename
);
1197 if (rename (crypto
->filename
, tmp2
) == -1)
1198 if (errno
!= ENOENT
)
1199 rc
= gpg_error_from_errno (errno
);
1202 if (!rc
&& rename (tmp
, crypto
->filename
) == -1)
1203 rc
= gpg_error_from_errno (errno
);
1207 if (chmod (crypto
->filename
, mode
) == -1)
1208 log_write ("%s(%u): %s", __FILE__
, __LINE__
,
1209 pwmd_strerror (gpg_error_from_syserror ()));
1212 if (!rc
&& acl
&& acl_set_file (crypto
->filename
,
1213 ACL_TYPE_ACCESS
, acl
))
1214 log_write ("ACL: %s: %s", crypto
->filename
,
1215 pwmd_strerror (gpg_error_from_errno (errno
)));
1216 pthread_cleanup_pop (1);
1219 else if (crypto
->filename
)
1220 rc
= gpg_error_from_errno (errno
);
1222 if (!rc
&& fd
!= -1)
1224 char *datadir
= str_asprintf ("%s/data", homedir
);
1228 int dfd
= open (datadir
, O_RDONLY
);
1232 if (fsync (dfd
) == -1)
1233 log_write ("%s %d: %s", __FILE__
, __LINE__
,
1234 pwmd_strerror (errno
));
1239 log_write ("%s %d: %s", __FILE__
, __LINE__
,
1240 pwmd_strerror (errno
));
1247 rc
= gpg_error_from_errno (errno
);
1250 pthread_cleanup_pop (0); // close (fd)
1258 crypto_key_info (const gpgme_key_t key
)
1260 struct string_s
*string
= string_new (NULL
), *s
;
1263 gpgme_subkey_t subkey
;
1264 gpgme_user_id_t uid
;
1269 for (u
= 0, uid
= key
->uids
; uid
; uid
= uid
->next
)
1272 for (n
= 0, subkey
= key
->subkeys
; subkey
; subkey
= subkey
->next
)
1275 s
= string_append_printf (string
, "%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%i:%s:%s:%s:%i:%u:%u",
1276 key
->revoked
, key
->expired
, key
->disabled
,
1277 key
->invalid
, key
->can_encrypt
, key
->can_sign
,
1278 key
->can_certify
, key
->secret
,
1279 key
->can_authenticate
, key
->is_qualified
,
1281 key
->issuer_serial
? key
->issuer_serial
: "",
1282 key
->issuer_name
? key
->issuer_name
: "",
1283 key
->chain_id
? key
->chain_id
: "",
1284 key
->owner_trust
, u
, n
);
1287 string_free (string
, 1);
1292 for (subkey
= key
->subkeys
; subkey
; subkey
= subkey
->next
)
1296 s
= string_append_printf (string
, ":%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%i:%u",
1297 subkey
->revoked
, subkey
->expired
,
1298 subkey
->disabled
, subkey
->invalid
,
1299 subkey
->can_encrypt
, subkey
->can_sign
,
1300 subkey
->can_certify
, subkey
->secret
,
1301 subkey
->can_authenticate
, subkey
->is_qualified
,
1302 subkey
->is_cardkey
, subkey
->pubkey_algo
,
1306 string_free (string
, 1);
1311 s
= string_append_printf (string
, ":%s:%s:%s:%li:%li:%s",
1312 subkey
->keyid
, subkey
->fpr
,
1313 subkey
->keygrip
? subkey
->keygrip
: "",
1314 subkey
->timestamp
, subkey
->expires
,
1315 subkey
->card_number
? subkey
->card_number
: "0");
1318 string_free (string
, 1);
1323 tmp
= gnupg_escape (subkey
->curve
);
1324 s
= string_append_printf (string
, ":%s", tmp
? tmp
: "");
1328 string_free (string
, 1);
1335 for (uid
= key
->uids
; uid
; uid
= uid
->next
)
1337 char *userid
, *name
, *email
, *comment
;
1339 userid
= gnupg_escape (uid
->uid
);
1340 name
= gnupg_escape (uid
->name
);
1341 email
= gnupg_escape (uid
->email
);
1342 comment
= gnupg_escape (uid
->comment
);
1343 s
= string_append_printf (string
, ":%u:%u:%u:%s:%s:%s:%s",
1344 uid
->revoked
, uid
->invalid
, uid
->validity
,
1345 userid
? userid
: "",
1348 comment
? comment
: "");
1355 string_free (string
, 1);
1363 string_free (string
, 0);
1368 crypto_try_decrypt (struct client_s
*client
, int no_pinentry
)
1370 struct crypto_s
*crypto
;
1372 char *keyfile
= config_get_string (client
->filename
, "passphrase_file");
1374 rc
= crypto_init (&crypto
, client
->ctx
, client
->filename
, no_pinentry
,
1378 pthread_cleanup_push ((void *)crypto_free
, crypto
);
1379 rc
= crypto_decrypt (client
, crypto
);
1380 pthread_cleanup_pop (1);
1388 /* The advisory lock should be obtained before calling this function. */
1390 crypto_is_symmetric (const char *filename
)
1397 rc
= open_check_file (filename
, &fd
, NULL
, 1);
1401 len
= read (fd
, &magic
, sizeof(magic
));
1403 if (len
!= sizeof (magic
))
1404 return GPG_ERR_INV_VALUE
;
1406 // Always read as big endian.
1407 if (magic
[0] != 0x8c || magic
[1] != 0x0d)
1408 return GPG_ERR_BAD_DATA
;
1414 crypto_delete_key (struct client_s
*client
, struct crypto_s
*crypto
,
1415 const gpgme_key_t key
, int secret
)
1419 STATUS_TIMEOUT_INIT (crypto
);
1420 rc
= send_status (client
->ctx
, STATUS_DECRYPT
, NULL
);
1422 rc
= gpgme_op_delete_ext_start (crypto
->ctx
, key
, GPGME_DELETE_FORCE
1423 | GPGME_DELETE_ALLOW_SECRET
);
1430 if (!rc
&& crypto
->progress_rc
)
1431 rc
= crypto
->progress_rc
;
1434 STATUS_TIMEOUT_TEST (crypto
, rc
, STATUS_DECRYPT
, NULL
);
1438 } while (!gpgme_wait (crypto
->ctx
, &rc
, 0) && !rc
);