2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/types.h>
36 #include "pwmd-error.h"
37 #include "util-misc.h"
43 #include "util-string.h"
47 static unsigned keepalive
;
48 static pthread_mutex_t crypto_mutex
= PTHREAD_MUTEX_INITIALIZER
;
52 #define STATUS_TIMEOUT_INIT(crypto) \
54 crypto->status_timeout = time (NULL); \
55 crypto->progress_rc = 0; \
58 #define STATUS_TIMEOUT_TEST(crypto, rc, s, line) \
60 time_t now = time (NULL); \
61 if (now - crypto->status_timeout >= keepalive && crypto->client_ctx) { \
62 rc = send_status (crypto->client_ctx, s, line); \
63 crypto->status_timeout = now; \
68 get_password_file (const char *keyfile
, unsigned char **result
, size_t *len
)
72 unsigned char *buf
= NULL
;
75 log_write (_ ("obtaining passphrase from passphrase file"));
76 rc
= open_check_file (keyfile
, &fd
, &st
, 0);
80 buf
= xmalloc (st
.st_size
+1);
83 size_t rlen
= read (fd
, buf
, st
.st_size
);
85 if (rlen
!= st
.st_size
)
86 rc
= GPG_ERR_ASS_READ_ERROR
;
90 /* The passphrase may have been truncated do to a nul byte before the
91 * EOF. Better to return an error here rather than passing the
92 * truncated passphrase on. */
93 if (rlen
&& strlen ((char *)buf
) != rlen
)
94 rc
= GPG_ERR_INV_PASSPHRASE
;
114 status_cb (void *data
, const char *keyword
, const char *args
)
116 struct crypto_s
*crypto
= data
;
119 if (!crypto
->client_ctx
)
122 if (!strcmp (keyword
, "INQUIRE_MAXLEN") &&
123 (crypto
->flags
& CRYPTO_FLAG_KEYFILE
))
126 return assuan_write_status (crypto
->client_ctx
, keyword
, args
);
130 passphrase_cb (void *data
, const char *hint
, const char *info
, int bad
, int fd
)
132 struct crypto_s
*crypto
= data
;
133 unsigned char *result
= NULL
;
137 if (crypto
->flags
& CRYPTO_FLAG_KEYFILE
)
138 rc
= get_password_file (crypto
->keyfile
, &result
, &len
);
142 rc
= assuan_write_status (crypto
->client_ctx
, "PASSPHRASE_HINT", hint
);
145 rc
= assuan_write_status (crypto
->client_ctx
, "PASSPHRASE_INFO", info
);
149 const char *keyword
= "PASSPHRASE";
151 if (!bad
&& crypto
->flags
& CRYPTO_FLAG_PASSWD_SIGN
)
152 keyword
= "SIGN_PASSPHRASE";
153 else if (!bad
&& crypto
->flags
& CRYPTO_FLAG_PASSWD_NEW
)
155 crypto
->flags
|= CRYPTO_FLAG_PASSWD_SIGN
;
156 keyword
= "NEW_PASSPHRASE";
158 else if (!bad
&& crypto
->flags
& CRYPTO_FLAG_PASSWD
)
159 crypto
->flags
|= CRYPTO_FLAG_PASSWD_NEW
;
160 else if (!bad
&& crypto
->flags
& CRYPTO_FLAG_NEWFILE
)
162 keyword
= "NEW_PASSPHRASE";
163 if (crypto
->save
.sigkey
)
164 crypto
->flags
|= CRYPTO_FLAG_PASSWD_SIGN
;
167 rc
= assuan_inquire (crypto
->client_ctx
, keyword
, &result
, &len
, 0);
173 pthread_cleanup_push ((void *)xfree
, result
);
176 gpgme_io_writen (fd
, "\n", 1);
179 int nl
= result
[len
-1] == '\n';
182 ret
= gpgme_io_writen (fd
, result
, len
);
184 gpgme_io_writen (fd
, "\n", 1);
186 rc
= GPG_ERR_CANCELED
;
189 pthread_cleanup_pop (1);
196 progress_cb (void *data
, const char *what
, int type
, int current
, int total
)
198 struct crypto_s
*crypto
= data
;
200 crypto
->progress_rc
= send_status (crypto
->client_ctx
, STATUS_GPGME
,
201 "%s %i %i %i", what
, type
, current
, total
);
205 crypto_data_to_buf (const gpgme_data_t data
, unsigned char **result
,
208 off_t ret
= gpgme_data_seek (data
, 0, SEEK_SET
);
210 size_t total
= 0, size
= BUFSIZE
;
211 unsigned char *buf
= NULL
;
217 return gpg_error_from_syserror ();
219 buf
= xmalloc (size
);
221 return GPG_ERR_ENOMEM
;
225 ret
= gpgme_data_read (data
, &buf
[total
], BUFSIZE
);
232 p
= xrealloc (buf
, size
* sizeof (unsigned char));
236 return GPG_ERR_ENOMEM
;
245 rc
= gpgme_err_code_from_syserror ();
260 crypto_list_keys (struct crypto_s
*crypto
, char *keys
[], int secret
,
261 gpgme_key_t
**result
)
264 gpgme_key_t key
= NULL
, *res
= NULL
;
267 rc
= gpgme_op_keylist_ext_start (crypto
->ctx
, (const char **)keys
, secret
, 0);
271 STATUS_TIMEOUT_INIT (crypto
);
276 pthread_cleanup_push ((void *)crypto_free_key_list
, res
);
277 rc
= gpgme_op_keylist_next (crypto
->ctx
, &key
);
278 pthread_cleanup_pop (0);
279 if (rc
&& gpgme_err_code(rc
) != GPG_ERR_EOF
)
288 p
= xrealloc (res
, (total
+2) * sizeof (gpgme_key_t
));
298 pthread_cleanup_push ((void *)crypto_free_key_list
, res
);
299 STATUS_TIMEOUT_TEST (crypto
, rc
, STATUS_KEEPALIVE
, NULL
);
300 pthread_cleanup_pop (0);
305 rc
= gpgme_op_keylist_end (crypto
->ctx
);
309 rc
= total
? 0 : secret
? GPG_ERR_NO_SECKEY
: GPG_ERR_NO_PUBKEY
;
313 crypto_free_key_list (res
);
319 crypto_free_save (struct save_s
*save
)
324 strv_free (save
->pubkey
);
325 xfree (save
->sigkey
);
326 xfree (save
->userid
);
328 crypto_free_key_list (save
->mainkey
);
330 memset (save
, 0, sizeof (struct save_s
));
336 return cache_kill_scd ();
340 free_gpgme_data_cb (void *data
)
342 gpgme_data_t d
= data
;
349 t
= gpgme_data_release_and_get_mem (d
, &len
);
351 wipememory (t
, 0, len
);
357 crypto_free_non_keys (struct crypto_s
*crypto
)
363 gpgme_release (crypto
->ctx
);
366 xfree (crypto
->plaintext
);
367 crypto
->plaintext
= NULL
;
368 crypto
->plaintext_size
= 0;
371 free_gpgme_data_cb (crypto
->cipher
);
373 crypto
->cipher
= NULL
;
374 crypto_free_save (&crypto
->save
);
375 xfree (crypto
->keyfile
);
376 crypto
->keyfile
= NULL
;
377 crypto
->flags
&= ~CRYPTO_FLAG_KEYFILE
;
378 (void)crypto_kill_scd ();
382 crypto_free (struct crypto_s
*crypto
)
387 crypto_free_non_keys (crypto
);
388 strv_free (crypto
->pubkey
);
389 xfree (crypto
->sigkey
);
390 xfree (crypto
->filename
);
395 crypto_init_ctx (struct crypto_s
*crypto
, int no_pinentry
,
396 char *passphrase_file
)
399 gpgme_keylist_mode_t keylist_mode
;
402 rc
= gpgme_new (&ctx
);
406 rc
= gpgme_set_protocol (ctx
, GPGME_PROTOCOL_OPENPGP
);
409 keylist_mode
= gpgme_get_keylist_mode (ctx
);
410 keylist_mode
|= GPGME_KEYLIST_MODE_LOCAL
|GPGME_KEYLIST_MODE_WITH_SECRET
;
411 rc
= gpgme_set_keylist_mode (ctx
, keylist_mode
);
414 if (no_pinentry
|| passphrase_file
)
415 rc
= gpgme_set_pinentry_mode (ctx
, GPGME_PINENTRY_MODE_LOOPBACK
);
417 rc
= gpgme_set_pinentry_mode (ctx
, GPGME_PINENTRY_MODE_DEFAULT
);
421 gpgme_set_passphrase_cb (ctx
, passphrase_cb
, crypto
);
422 gpgme_set_progress_cb (ctx
, progress_cb
, crypto
);
423 gpgme_set_status_cb (ctx
, status_cb
, crypto
);
429 crypto
->keyfile
= passphrase_file
;
430 crypto
->flags
|= CRYPTO_FLAG_KEYFILE
;
443 crypto_set_keepalive ()
445 keepalive
= config_get_integer ("global", "keepalive_interval");
449 crypto_init (struct crypto_s
**crypto
, void *ctx
, const char *filename
,
450 int no_pinentry
, char *passphrase_file
)
452 struct crypto_s
*new = xcalloc (1, sizeof (struct crypto_s
));
455 crypto_set_keepalive ();
458 return GPG_ERR_ENOMEM
;
460 rc
= crypto_init_ctx (new, no_pinentry
, passphrase_file
);
466 new->filename
= str_dup (filename
);
474 new->client_ctx
= ctx
;
483 /* Converts a 40 byte key id to the 16 byte form. Needed for comparison of
484 * gpgme_recipient_t.keyid. */
486 crypto_keyid_to_16b_once (char *key
)
488 size_t len
= strlen (key
);
491 return GPG_ERR_INV_ARG
;
493 memmove (&key
[0], &key
[len
-16], 16);
499 crypto_keyid_to_16b (char **keys
)
503 for (p
= keys
; p
&& *p
; p
++)
505 gpg_error_t rc
= crypto_keyid_to_16b_once (*p
);
514 crypto_decrypt (struct client_s
*client
, struct crypto_s
*crypto
)
521 char **new_recipients
= NULL
;
522 char **new_signers
= NULL
;
524 gpgme_decrypt_result_t result
;
526 rc
= gpgme_data_new (&plain
);
530 pthread_cleanup_push ((void *)free_gpgme_data_cb
, plain
);
531 rc
= open_check_file (crypto
->filename
, &fd
, &st
, 1);
534 pthread_cleanup_push ((void *)close_fd_cb
, &fd
);
535 rc
= gpgme_data_new_from_fd (&cipher
, fd
);
538 pthread_cleanup_push ((void *)free_gpgme_data_cb
, cipher
);
539 rc
= send_status (client
? client
->ctx
: NULL
, STATUS_DECRYPT
, NULL
);
542 MUTEX_TRYLOCK (client
->ctx
, &crypto_mutex
, rc
,
543 client
->lock_timeout
);
546 pthread_cleanup_push (release_mutex_cb
, &crypto_mutex
);
547 rc
= gpgme_op_decrypt_verify_start (crypto
->ctx
, cipher
, plain
);
550 STATUS_TIMEOUT_INIT(crypto
);
553 if (!rc
&& crypto
->progress_rc
)
554 rc
= crypto
->progress_rc
;
556 STATUS_TIMEOUT_TEST (crypto
, rc
, STATUS_DECRYPT
, NULL
);
558 } while (!rc
&& !gpgme_wait (crypto
->ctx
, &rc
, 0) && !rc
);
560 pthread_cleanup_pop (1); // release_mutex_cb
563 pthread_cleanup_pop (1); // free_gpgme_data_cb (cipher)
566 pthread_cleanup_pop (1); // close (fd)
569 pthread_cleanup_pop (0); // free_gpgme_data_cb (plain)
573 free_gpgme_data_cb (plain
);
577 pthread_cleanup_push ((void *)free_gpgme_data_cb
, plain
);
578 result
= gpgme_op_decrypt_result (crypto
->ctx
);
579 if (!result
->unsupported_algorithm
&& !result
->wrong_key_usage
)
583 for (r
= result
->recipients
; r
; r
= r
->next
)
585 pthread_cleanup_push ((void *)strv_free
, new_recipients
);
586 log_write1 (_ ("%s: recipient: %s, status=%u"),
587 crypto
->filename
, r
->keyid
, r
->status
);
588 pthread_cleanup_pop (0);
589 pp
= strv_cat(new_recipients
, str_dup (r
->keyid
));
599 else if (result
->wrong_key_usage
)
600 rc
= GPG_ERR_WRONG_KEY_USAGE
;
601 else if (result
->unsupported_algorithm
)
602 rc
= GPG_ERR_UNSUPPORTED_ALGORITHM
;
606 gpgme_verify_result_t verify
;
609 verify
= gpgme_op_verify_result (crypto
->ctx
);
610 for (s
= verify
->signatures
; s
; s
= s
->next
)
612 unsigned flags
= s
->summary
;
614 pthread_cleanup_push ((void *)strv_free
, new_signers
);
615 log_write1 (_ ("%s: signer: %s, status=%u"),
616 crypto
->filename
, s
->fpr
, s
->status
);
617 pthread_cleanup_pop (0);
619 flags
&= ~(GPGME_SIGSUM_KEY_EXPIRED
|GPGME_SIGSUM_SIG_EXPIRED
);
621 flags
|= GPGME_SIGSUM_VALID
;
623 if (gpg_err_code (s
->status
) == GPG_ERR_KEY_EXPIRED
)
626 if (s
->status
|| s
->wrong_key_usage
|| !(flags
& GPGME_SIGSUM_VALID
))
629 pp
= strv_cat (new_signers
, str_dup (s
->fpr
));
639 if (verify
->signatures
&& !new_signers
)
640 rc
= GPG_ERR_BAD_SIGNATURE
;
644 xfree (crypto
->plaintext
);
645 crypto
->plaintext
= NULL
;
646 crypto
->plaintext_size
= 0;
647 pthread_cleanup_push ((void *)strv_free
, new_recipients
);
648 pthread_cleanup_push ((void *)strv_free
, new_signers
);
649 rc
= crypto_data_to_buf (plain
, &crypto
->plaintext
,
650 &crypto
->plaintext_size
);
651 pthread_cleanup_pop (0);
652 pthread_cleanup_pop (0);
655 strv_free (crypto
->pubkey
);
656 crypto
->pubkey
= new_recipients
;
657 crypto_keyid_to_16b (crypto
->pubkey
);
659 xfree (crypto
->sigkey
);
660 crypto
->sigkey
= NULL
;
663 crypto
->sigkey
= str_dup (*new_signers
);
664 strv_free (new_signers
);
665 crypto_keyid_to_16b_once (crypto
->sigkey
);
673 strv_free (new_recipients
);
674 strv_free (new_signers
);
677 pthread_cleanup_pop (1); // free_gpgme_data_cb (plain)
682 crypto_free_key_list (gpgme_key_t
*keys
)
686 for (i
= 0; keys
&& keys
[i
]; i
++)
687 gpgme_key_unref (keys
[i
]);
692 /* Removes strings in 'prune' from 'a'. */
694 prune_keys (char **a
, char **prune
)
698 for (p
= prune
; p
&& *p
; p
++)
702 for (ap
= a
; ap
&& *ap
; ap
++)
704 if (!strcmp (*ap
, *p
))
719 remove_duplicates (char **a
)
723 for (p
= a
; p
&& *p
; p
++)
727 for (t
= p
+1; t
&& *t
; t
++)
729 if (!strcmp (*p
, *t
))
741 remove_duplicates (a
);
749 crypto_encrypt (struct client_s
*client
, struct crypto_s
*crypto
)
752 gpgme_data_t cipher
= NULL
;
753 gpgme_key_t
*keys
= NULL
;
754 gpgme_key_t
*sigkeys
= NULL
;
758 if (!(crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
))
760 crypto_keyid_to_16b (crypto
->save
.pubkey
);
761 remove_duplicates (crypto
->save
.pubkey
);
762 rc
= crypto_list_keys (crypto
, crypto
->save
.pubkey
, 0, &keys
);
767 pthread_cleanup_push ((void *)crypto_free_key_list
, keys
);
768 rc
= gpgme_data_new (&cipher
);
769 pthread_cleanup_push ((void *)free_gpgme_data_cb
, cipher
);
771 rc
= gpgme_data_set_file_name (cipher
, crypto
->filename
);
773 if (!rc
&& crypto
->save
.sigkey
)
777 crypto_keyid_to_16b_once (crypto
->save
.sigkey
);
778 strv_printf (&tmp
, "%s", crypto
->save
.sigkey
);
779 pthread_cleanup_push ((void *)strv_free
, tmp
);
780 rc
= crypto_list_keys (crypto
, tmp
, 1, &sigkeys
);
781 pthread_cleanup_pop (1);
782 if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
788 gpgme_data_t plain
= NULL
;
790 pthread_cleanup_push ((void *)crypto_free_key_list
, sigkeys
);
791 rc
= gpgme_data_new_from_mem (&plain
, (char *)crypto
->plaintext
,
792 crypto
->plaintext_size
, 0);
793 pthread_cleanup_push ((void *)free_gpgme_data_cb
, plain
);
798 gpgme_signers_clear (crypto
->ctx
);
800 for (i
= 0; sigkeys
&& sigkeys
[i
]; i
++)
801 gpgme_signers_add (crypto
->ctx
, sigkeys
[i
]);
806 gpgme_data_set_encoding (cipher
, GPGME_DATA_ENCODING_ARMOR
);
807 rc
= send_status (client
? client
->ctx
: NULL
, STATUS_ENCRYPT
, NULL
);
812 int f
= config_get_boolean ("global", "encrypt_to");
815 flags
|= GPGME_ENCRYPT_NO_ENCRYPT_TO
;
817 f
= config_get_boolean ("global", "always_trust");
819 flags
|= GPGME_ENCRYPT_ALWAYS_TRUST
;
821 if (!(crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
)
822 || (crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
&& sigkeys
))
827 MUTEX_TRYLOCK (client
->ctx
, &crypto_mutex
, rc
, client
->lock_timeout
);
831 pthread_cleanup_push (release_mutex_cb
, &crypto_mutex
);
833 rc
= gpgme_op_encrypt_sign_start (crypto
->ctx
, keys
, flags
, plain
,
836 rc
= gpgme_op_encrypt_start (crypto
->ctx
, NULL
, flags
, plain
,
840 STATUS_TIMEOUT_INIT (crypto
);
843 if (!rc
&& crypto
->progress_rc
)
844 rc
= crypto
->progress_rc
;
846 STATUS_TIMEOUT_TEST (crypto
, rc
, STATUS_ENCRYPT
, NULL
);
848 } while (!rc
&& !gpgme_wait (crypto
->ctx
, &rc
, 0) && !rc
);
850 pthread_cleanup_pop (1);
854 gpgme_encrypt_result_t result
;
855 gpgme_sign_result_t sresult
;
856 gpgme_invalid_key_t inv
;
857 gpgme_new_signature_t sigs
;
861 result
= gpgme_op_encrypt_result (crypto
->ctx
);
862 inv
= result
->invalid_recipients
;
865 pthread_cleanup_push ((void *)strv_free
, prune
);
866 log_write1 (_ ("%s: invalid recipient: %s, rc=%u"),
867 crypto
->filename
, inv
->fpr
, inv
->reason
);
868 pthread_cleanup_pop (0);
869 p
= strv_cat (prune
, str_dup(inv
->fpr
));
883 p
= prune_keys (crypto
->save
.pubkey
, prune
);
884 crypto
->save
.pubkey
= p
;
888 crypto_keyid_to_16b (crypto
->save
.pubkey
);
889 sresult
= gpgme_op_sign_result (crypto
->ctx
);
890 inv
= sresult
? sresult
->invalid_signers
: NULL
;
893 log_write1 (_ ("%s: invalid signer: %s, rc=%u"),
894 crypto
->filename
, inv
->fpr
, inv
->reason
);
898 sigs
= sresult
? sresult
->signatures
: NULL
;
907 pthread_cleanup_push ((void *)strv_free
, p
);
908 log_write1 (_ ("%s: signer: %s"), crypto
->filename
,
910 pthread_cleanup_pop (0);
912 pp
= strv_cat (p
, str_dup (sigs
->fpr
));
926 xfree (crypto
->save
.sigkey
);
927 crypto
->save
.sigkey
= str_dup (*p
);
929 crypto_keyid_to_16b_once (crypto
->save
.sigkey
);
930 crypto
->cipher
= cipher
;
933 else if (!rc
&& crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
)
935 crypto
->cipher
= cipher
;
939 if (!(crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
)
940 || (crypto
->flags
& CRYPTO_FLAG_SYMMETRIC
&& sigkeys
))
941 rc
= GPG_ERR_NO_SIGNATURE_SCHEME
;
946 pthread_cleanup_pop (1); // free_gpgme_data_cb (plain)
947 pthread_cleanup_pop (1); // crypto_free_key_list (sigkeys)
950 pthread_cleanup_pop (0); // free_gpgme_data_cb (cipher)
951 pthread_cleanup_pop (1); // crypto_free_key_list (keys)
954 free_gpgme_data_cb (cipher
);
960 crypto_passwd (struct client_s
*client
, struct crypto_s
*crypto
)
963 gpgme_key_t
*keys
= NULL
;
965 /* Use SAVE instead for symmetric files. */
966 rc
= crypto_is_symmetric (client
->filename
);
967 if (!rc
|| rc
!= GPG_ERR_BAD_DATA
)
968 return !rc
? GPG_ERR_NOT_SUPPORTED
: rc
;
970 rc
= crypto_list_keys (crypto
, crypto
->pubkey
, 1, &keys
);
974 crypto
->flags
|= CRYPTO_FLAG_PASSWD
;
975 pthread_cleanup_push ((void *)crypto_free_key_list
, keys
);
976 rc
= send_status (client
->ctx
, STATUS_KEEPALIVE
, NULL
);
978 MUTEX_TRYLOCK (client
->ctx
, &crypto_mutex
, rc
, client
->lock_timeout
);
982 pthread_cleanup_push (release_mutex_cb
, &crypto_mutex
);
983 rc
= gpgme_op_passwd_start (crypto
->ctx
, keys
[0], 0);
986 STATUS_TIMEOUT_INIT (crypto
);
989 if (!rc
&& crypto
->progress_rc
)
990 rc
= crypto
->progress_rc
;
992 STATUS_TIMEOUT_TEST (crypto
, rc
, STATUS_KEEPALIVE
, NULL
);
994 } while (!rc
&& !gpgme_wait (crypto
->ctx
, &rc
, 0) && !rc
);
996 pthread_cleanup_pop (1);
999 pthread_cleanup_pop (1);
1003 /* Generate a new key pair. The resulting keyid's are stored in crypto->save.
1006 crypto_genkey (struct client_s
*client
, struct crypto_s
*crypto
)
1010 rc
= send_status (client
? client
->ctx
: NULL
, STATUS_GENKEY
, NULL
);
1012 MUTEX_TRYLOCK ((client
? client
->ctx
: NULL
), &crypto_mutex
, rc
,
1013 (client
? client
->lock_timeout
: 0));
1017 pthread_cleanup_push (release_mutex_cb
, &crypto_mutex
);
1018 if (crypto
->save
.mainkey
)
1020 rc
= gpgme_op_createsubkey_start (crypto
->ctx
,
1021 crypto
->save
.mainkey
[0],
1024 crypto
->save
.expire
,
1025 crypto
->save
.flags
);
1029 rc
= gpgme_op_createkey_start (crypto
->ctx
,
1030 crypto
->save
.userid
,
1033 crypto
->save
.expire
,
1035 crypto
->save
.flags
);
1040 STATUS_TIMEOUT_INIT (crypto
);
1043 if (!rc
&& crypto
->progress_rc
)
1044 rc
= crypto
->progress_rc
;
1046 STATUS_TIMEOUT_TEST (crypto
, rc
, STATUS_GENKEY
, NULL
);
1048 } while (!rc
&& !gpgme_wait (crypto
->ctx
, &rc
, 0) && !rc
);
1050 pthread_cleanup_pop (1);
1055 gpgme_key_t
*keys
= NULL
;
1056 gpgme_genkey_result_t result
;
1058 result
= gpgme_op_genkey_result (crypto
->ctx
);
1059 xfree (crypto
->save
.sigkey
);
1060 crypto
->save
.sigkey
= str_dup (result
->fpr
);
1061 crypto_keyid_to_16b_once (crypto
->save
.sigkey
);
1067 strv_printf (&tmp
, "%s", crypto
->save
.sigkey
);
1068 pthread_cleanup_push ((void *)strv_free
, tmp
);
1069 rc
= crypto_list_keys (crypto
, tmp
, 1, &keys
);
1070 pthread_cleanup_pop (1);
1075 gpgme_subkey_t key
= keys
[0]->subkeys
;
1077 for (; key
; key
= key
->next
)
1079 if (key
->can_encrypt
)
1083 pthread_cleanup_push ((void *)crypto_free_key_list
, keys
);
1084 rc
= send_status (client
? client
->ctx
: NULL
, STATUS_GENKEY
, "%s %s",
1085 crypto
->save
.sigkey
, key
? key
->fpr
: "");
1088 crypto
->save
.pubkey
= strv_cat (crypto
->save
.pubkey
,
1089 str_dup (key
->fpr
));
1090 crypto_keyid_to_16b (crypto
->save
.pubkey
);
1093 pthread_cleanup_pop (1);
1102 acl_free_cb (void *arg
)
1104 acl_t acl
= arg
? (acl_t
) arg
: NULL
;
1111 /* The advisory lock should be obtained before calling this function. */
1113 crypto_write_file (struct crypto_s
*crypto
, unsigned char **r_crc
,
1127 if (crypto
->filename
)
1129 if (lstat (crypto
->filename
, &st
) == 0)
1131 mode
= st
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
1133 if (!(mode
& S_IWUSR
))
1134 return GPG_ERR_EACCES
;
1136 else if (errno
!= ENOENT
)
1137 return gpg_error_from_errno (errno
);
1139 snprintf (tmp
, sizeof (tmp
), "%s.XXXXXX", crypto
->filename
);
1140 mode_t tmode
= umask (0600);
1144 rc
= gpg_error_from_errno (errno
);
1146 log_write ("%s: %s", tmp
, pwmd_strerror (rc
));
1155 rc
= crypto_data_to_buf (crypto
->cipher
, &buf
, &size
);
1158 if (crypto
->filename
)
1163 pthread_cleanup_push ((void *)close_fd_cb
, &fd
);
1165 rc
= get_checksum_memory (buf
, size
, r_crc
, r_crclen
);
1169 pthread_cleanup_push ((void *)xfree
, buf
);
1170 len
= write (fd
, buf
, size
);
1171 pthread_cleanup_pop (1);
1173 rc
= gpg_error_from_errno (errno
);
1178 if (fsync (fd
) != -1)
1180 if (crypto
->filename
&& close (fd
) != -1)
1183 acl
= acl_get_file (crypto
->filename
, ACL_TYPE_ACCESS
);
1184 if (!acl
&& errno
== ENOENT
)
1185 acl
= acl_get_file (".", ACL_TYPE_DEFAULT
);
1187 log_write ("ACL: %s: %s", crypto
->filename
,
1188 pwmd_strerror (gpg_error_from_errno (errno
)));
1189 pthread_cleanup_push ((void *)acl_free_cb
, acl
);
1193 if (config_get_boolean (crypto
->filename
, "backup"))
1195 char tmp2
[PATH_MAX
];
1197 snprintf (tmp2
, sizeof (tmp2
), "%s.backup", crypto
->filename
);
1198 if (rename (crypto
->filename
, tmp2
) == -1)
1199 if (errno
!= ENOENT
)
1200 rc
= gpg_error_from_errno (errno
);
1203 if (!rc
&& rename (tmp
, crypto
->filename
) == -1)
1204 rc
= gpg_error_from_errno (errno
);
1208 if (chmod (crypto
->filename
, mode
) == -1)
1209 log_write ("%s(%u): %s", __FILE__
, __LINE__
,
1210 pwmd_strerror (gpg_error_from_syserror ()));
1213 if (!rc
&& acl
&& acl_set_file (crypto
->filename
,
1214 ACL_TYPE_ACCESS
, acl
))
1215 log_write ("ACL: %s: %s", crypto
->filename
,
1216 pwmd_strerror (gpg_error_from_errno (errno
)));
1217 pthread_cleanup_pop (1);
1220 else if (crypto
->filename
)
1221 rc
= gpg_error_from_errno (errno
);
1223 if (!rc
&& fd
!= -1)
1225 char *datadir
= str_asprintf ("%s/data", homedir
);
1229 int dfd
= open (datadir
, O_RDONLY
);
1233 if (fsync (dfd
) == -1)
1234 log_write ("%s %d: %s", __FILE__
, __LINE__
,
1235 pwmd_strerror (errno
));
1240 log_write ("%s %d: %s", __FILE__
, __LINE__
,
1241 pwmd_strerror (errno
));
1248 rc
= gpg_error_from_errno (errno
);
1251 pthread_cleanup_pop (0); // close (fd)
1259 crypto_key_info (const gpgme_key_t key
)
1261 struct string_s
*string
= string_new (NULL
), *s
;
1264 gpgme_subkey_t subkey
;
1265 gpgme_user_id_t uid
;
1270 for (u
= 0, uid
= key
->uids
; uid
; uid
= uid
->next
)
1273 for (n
= 0, subkey
= key
->subkeys
; subkey
; subkey
= subkey
->next
)
1276 s
= string_append_printf (string
, "%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%i:%s:%s:%s:%i:%u:%u",
1277 key
->revoked
, key
->expired
, key
->disabled
,
1278 key
->invalid
, key
->can_encrypt
, key
->can_sign
,
1279 key
->can_certify
, key
->secret
,
1280 key
->can_authenticate
, key
->is_qualified
,
1282 key
->issuer_serial
? key
->issuer_serial
: "",
1283 key
->issuer_name
? key
->issuer_name
: "",
1284 key
->chain_id
? key
->chain_id
: "",
1285 key
->owner_trust
, u
, n
);
1288 string_free (string
, 1);
1293 for (subkey
= key
->subkeys
; subkey
; subkey
= subkey
->next
)
1297 s
= string_append_printf (string
, ":%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%i:%u",
1298 subkey
->revoked
, subkey
->expired
,
1299 subkey
->disabled
, subkey
->invalid
,
1300 subkey
->can_encrypt
, subkey
->can_sign
,
1301 subkey
->can_certify
, subkey
->secret
,
1302 subkey
->can_authenticate
, subkey
->is_qualified
,
1303 subkey
->is_cardkey
, subkey
->pubkey_algo
,
1307 string_free (string
, 1);
1312 s
= string_append_printf (string
, ":%s:%s:%s:%li:%li:%s",
1313 subkey
->keyid
, subkey
->fpr
,
1314 subkey
->keygrip
? subkey
->keygrip
: "",
1315 subkey
->timestamp
, subkey
->expires
,
1316 subkey
->card_number
? subkey
->card_number
: "0");
1319 string_free (string
, 1);
1324 tmp
= gnupg_escape (subkey
->curve
);
1325 s
= string_append_printf (string
, ":%s", tmp
? tmp
: "");
1329 string_free (string
, 1);
1336 for (uid
= key
->uids
; uid
; uid
= uid
->next
)
1338 char *userid
, *name
, *email
, *comment
;
1340 userid
= gnupg_escape (uid
->uid
);
1341 name
= gnupg_escape (uid
->name
);
1342 email
= gnupg_escape (uid
->email
);
1343 comment
= gnupg_escape (uid
->comment
);
1344 s
= string_append_printf (string
, ":%u:%u:%u:%s:%s:%s:%s",
1345 uid
->revoked
, uid
->invalid
, uid
->validity
,
1346 userid
? userid
: "",
1349 comment
? comment
: "");
1356 string_free (string
, 1);
1364 string_free (string
, 0);
1369 crypto_try_decrypt (struct client_s
*client
, int no_pinentry
)
1371 struct crypto_s
*crypto
;
1373 char *keyfile
= config_get_string (client
->filename
, "passphrase_file");
1375 rc
= crypto_init (&crypto
, client
->ctx
, client
->filename
, no_pinentry
,
1379 pthread_cleanup_push ((void *)crypto_free
, crypto
);
1380 rc
= crypto_decrypt (client
, crypto
);
1381 pthread_cleanup_pop (1);
1389 /* The advisory lock should be obtained before calling this function. */
1391 crypto_is_symmetric (const char *filename
)
1398 rc
= open_check_file (filename
, &fd
, NULL
, 1);
1402 len
= read (fd
, &magic
, sizeof(magic
));
1404 if (len
!= sizeof (magic
))
1405 return GPG_ERR_INV_VALUE
;
1407 // Always read as big endian.
1408 if (magic
[0] != 0x8c || magic
[1] != 0x0d)
1409 return GPG_ERR_BAD_DATA
;
1415 crypto_delete_key (struct client_s
*client
, struct crypto_s
*crypto
,
1416 const gpgme_key_t key
, int secret
)
1420 STATUS_TIMEOUT_INIT (crypto
);
1421 rc
= send_status (client
->ctx
, STATUS_DECRYPT
, NULL
);
1423 rc
= gpgme_op_delete_start (crypto
->ctx
, key
, 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
);