36 |
#include "wptErrors.h" |
#include "wptErrors.h" |
37 |
#include "wptW32API.h" |
#include "wptW32API.h" |
38 |
#include "wptGPG.h" |
#include "wptGPG.h" |
39 |
|
#include "wptTypes.h" |
40 |
|
|
41 |
|
|
42 |
/* Attribute list which holds the image data. */ |
/* Attribute list which holds the image data. */ |
50 |
typedef struct attr_list_s *attr_list_t; |
typedef struct attr_list_s *attr_list_t; |
51 |
|
|
52 |
|
|
53 |
|
/* Free attribute list @ctx. */ |
54 |
void |
void |
55 |
free_attr_list (attr_list_t ctx) |
free_attr_list (attr_list_t ctx) |
56 |
{ |
{ |
83 |
buffer = buf+9+10; |
buffer = buf+9+10; |
84 |
pos = 0; |
pos = 0; |
85 |
c = (attr_list_t)calloc (1, sizeof *c); |
c = (attr_list_t)calloc (1, sizeof *c); |
86 |
|
if (!c) |
87 |
|
BUG (0); |
88 |
p = strtok (buffer, " "); |
p = strtok (buffer, " "); |
89 |
while (p != NULL) { |
while (p != NULL) { |
90 |
switch (pos) { |
switch (pos) { |
106 |
pos++; |
pos++; |
107 |
p = strtok (NULL, " "); |
p = strtok (NULL, " "); |
108 |
} |
} |
|
/*printf ("id=%s octets=%d flags=%d\n", c->fpr, c->octets, c->flags);*/ |
|
109 |
if (!*ctx) |
if (!*ctx) |
110 |
*ctx = c; |
*ctx = c; |
111 |
else { |
else { |
114 |
t->next = c; |
t->next = c; |
115 |
} |
} |
116 |
c->d = (unsigned char*)malloc (c->octets); |
c->d = (unsigned char*)malloc (c->octets); |
117 |
|
if (!c->d) |
118 |
|
BUG (0); |
119 |
memcpy (c->d, data, c->octets); |
memcpy (c->d, data, c->octets); |
120 |
data += c->octets; |
data += c->octets; |
121 |
datlen -= c->octets; |
datlen -= c->octets; |
130 |
parse_attr_data (const char *keyid, attr_list_t *list) |
parse_attr_data (const char *keyid, attr_list_t *list) |
131 |
{ |
{ |
132 |
gpgme_error_t err; |
gpgme_error_t err; |
133 |
FILE *tmp; |
FILE *tmp; |
|
char *status; |
|
134 |
BYTE *data; |
BYTE *data; |
135 |
DWORD ndata; |
char *status, tmpnam[MAX_PATH+1]; |
136 |
|
DWORD ndata = 0; |
137 |
|
|
138 |
err = gpg_get_photoid_data (keyid, &status, &data, &ndata); |
err = gpg_get_photoid_data (keyid, &status, &data, &ndata); |
139 |
if (err) |
if (err) |
140 |
return err; |
return err; |
141 |
|
|
142 |
if (ndata > 0) { |
get_temp_name (tmpnam, MAX_PATH, NULL); |
143 |
tmp = tmpfile (); |
tmp = fopen (tmpnam, "w+b"); |
144 |
|
if (ndata > 0 && tmp != NULL) { |
145 |
fwrite (status, 1, strlen (status), tmp); |
fwrite (status, 1, strlen (status), tmp); |
146 |
fflush (tmp); |
fflush (tmp); |
147 |
rewind (tmp); |
rewind (tmp); |
148 |
|
|
149 |
ndata = parse_attr_list (tmp, data, ndata, list); |
ndata = parse_attr_list (tmp, data, ndata, list); |
150 |
fclose (tmp); |
fclose (tmp); |
151 |
|
DeleteFile (tmpnam); |
152 |
} |
} |
153 |
else |
else |
154 |
*list = NULL; |
*list = NULL; |
178 |
|
|
179 |
gpg_iobuf_ioctl (inp, 3, 1, NULL); |
gpg_iobuf_ioctl (inp, 3, 1, NULL); |
180 |
pkt = (PACKET*)calloc (1, sizeof *pkt); |
pkt = (PACKET*)calloc (1, sizeof *pkt); |
181 |
|
if (!pkt) |
182 |
|
BUG (0); |
183 |
gpg_init_packet (pkt); |
gpg_init_packet (pkt); |
184 |
while (gpg_parse_packet (inp, pkt) != -1) { |
while (gpg_parse_packet (inp, pkt) != -1) { |
185 |
if (pkt->pkttype == PKT_SECRET_KEY) { |
if (pkt->pkttype == PKT_SECRET_KEY) { |
223 |
fnd->attrib.flags = dat->flags; |
fnd->attrib.flags = dat->flags; |
224 |
fnd->attrib.len = dat->octets; |
fnd->attrib.len = dat->octets; |
225 |
fnd->attrib.d = (unsigned char*)malloc (dat->octets); |
fnd->attrib.d = (unsigned char*)malloc (dat->octets); |
226 |
|
if (!fnd->attrib.d) |
227 |
|
BUG (0); |
228 |
memcpy (fnd->attrib.d, dat->d, dat->octets); |
memcpy (fnd->attrib.d, dat->d, dat->octets); |
229 |
return 0; |
return 0; |
230 |
} |
} |
276 |
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
277 |
|
|
278 |
pkt = (PACKET*)calloc (1, sizeof * pkt); |
pkt = (PACKET*)calloc (1, sizeof * pkt); |
279 |
|
if (!pkt) |
280 |
|
BUG (0); |
281 |
gpg_init_packet (pkt); |
gpg_init_packet (pkt); |
282 |
while (gpg_parse_packet (inp, pkt) != -1) { |
while (gpg_parse_packet (inp, pkt) != -1) { |
283 |
if (pkt->pkttype == PKT_PUBLIC_KEY) { |
if (pkt->pkttype == PKT_PUBLIC_KEY) { |
313 |
else if (nsym > 0) { |
else if (nsym > 0) { |
314 |
c->sym_prefs = (unsigned char*)calloc (1, nsym+1); |
c->sym_prefs = (unsigned char*)calloc (1, nsym+1); |
315 |
if (!c->sym_prefs) |
if (!c->sym_prefs) |
316 |
return gpg_error (GPG_ERR_ENOMEM); |
BUG (0); |
317 |
memcpy (c->sym_prefs, sym_prefs, nsym); |
memcpy (c->sym_prefs, sym_prefs, nsym); |
318 |
} |
} |
319 |
} |
} |
355 |
return gpg_error (GPG_ERR_INV_ARG); |
return gpg_error (GPG_ERR_INV_ARG); |
356 |
ctx = (gpg_keycache_t)calloc (1, sizeof *ctx); |
ctx = (gpg_keycache_t)calloc (1, sizeof *ctx); |
357 |
if (!ctx) |
if (!ctx) |
358 |
return gpg_error (GPG_ERR_ENOMEM); |
BUG (0); |
359 |
ctx->secret = 0; |
ctx->secret = 0; |
360 |
ctx->pos = 0; |
ctx->pos = 0; |
361 |
*r_ctx = ctx; |
*r_ctx = ctx; |
376 |
c2 = c->next; |
c2 = c->next; |
377 |
gpgme_key_release (c->key); |
gpgme_key_release (c->key); |
378 |
c->key = NULL; |
c->key = NULL; |
379 |
|
safe_free (c->pref_keyserver); |
380 |
safe_free (c->sym_prefs); |
safe_free (c->sym_prefs); |
381 |
safe_free (c->attrib.d); |
safe_free (c->attrib.d); |
382 |
safe_free (c->card_type); |
safe_free (c->card_type); |
416 |
|
|
417 |
c = (struct keycache_s*)calloc (1, sizeof *c); |
c = (struct keycache_s*)calloc (1, sizeof *c); |
418 |
if (!c) |
if (!c) |
419 |
return gpg_error (GPG_ERR_ENOMEM); |
BUG (0); |
420 |
c->gloflags.is_protected = 1; /*default: assume protection. */ |
c->gloflags.is_protected = 1; /*default: assume protection. */ |
421 |
c->key = key; |
c->key = key; |
422 |
if (!ctx->item) |
if (!ctx->item) |
517 |
|
|
518 |
/* Return the next key which was updated. Before it is |
/* Return the next key which was updated. Before it is |
519 |
returned the update flag is cleared. |
returned the update flag is cleared. |
520 |
|
@r_status is 1 for a new key and 2 for an updated key. |
521 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
522 |
gpgme_error_t |
gpgme_error_t |
523 |
gpg_keycache_next_updated_key (gpg_keycache_t ctx, |
gpg_keycache_next_updated_key (gpg_keycache_t ctx, |
524 |
struct keycache_s **r_obj) |
struct keycache_s **r_obj, |
525 |
|
int *r_status) |
526 |
{ |
{ |
527 |
struct keycache_s *c; |
struct keycache_s *c; |
528 |
|
|
529 |
for (c = ctx->item; c; c = c->next) { |
for (c = ctx->item; c; c = c->next) { |
530 |
if (c->flags == 1) { |
if (c->flags != 0) { |
531 |
|
*r_status = c->flags; |
532 |
c->flags = 0; |
c->flags = 0; |
533 |
*r_obj = c; |
*r_obj = c; |
534 |
return 0; |
return 0; |
552 |
err = gpgme_new (&gctx); |
err = gpgme_new (&gctx); |
553 |
if (err) |
if (err) |
554 |
return err; |
return err; |
555 |
|
gpgme_set_keylist_mode (gctx, GPGME_KEYLIST_MODE_SIGS); |
556 |
err = gpgme_get_key (gctx, keyid, &key, is_sec); |
err = gpgme_get_key (gctx, keyid, &key, is_sec); |
557 |
gpgme_release (gctx); |
gpgme_release (gctx); |
558 |
if (err) |
if (err) |
562 |
log_debug ("keycache update: keyid=%s %p\r\n", keyid, pub); |
log_debug ("keycache update: keyid=%s %p\r\n", keyid, pub); |
563 |
gpgme_key_release (fndkey); |
gpgme_key_release (fndkey); |
564 |
c->key = key; |
c->key = key; |
565 |
c->flags = 1; |
c->flags = KC_FLAG_UPD; |
566 |
if (is_sec && pub != NULL && |
if (is_sec && pub != NULL && |
567 |
!gpg_keycache_find_key (pub, keyid, 0, &fndkey)) { |
!gpg_keycache_find_key (pub, keyid, 0, &fndkey)) { |
568 |
log_debug ("keycache update: set public part %p\r\n", fndkey); |
log_debug ("keycache update: set public part %p\r\n", fndkey); |
586 |
} |
} |
587 |
} |
} |
588 |
if (c) |
if (c) |
589 |
c->flags = 1; |
c->flags = KC_FLAG_ADD; |
590 |
} |
} |
591 |
return 0; |
return 0; |
592 |
} |
} |
694 |
pos++; |
pos++; |
695 |
p = (unsigned char*)calloc (1, pos+1); |
p = (unsigned char*)calloc (1, pos+1); |
696 |
if (!p) |
if (!p) |
697 |
abort (); |
BUG (0); |
698 |
memcpy (p, prefs, pos); |
memcpy (p, prefs, pos); |
699 |
return p; |
return p; |
700 |
} |
} |
715 |
c_sec->gloflags.divert_to_card = c->gloflags.divert_to_card; |
c_sec->gloflags.divert_to_card = c->gloflags.divert_to_card; |
716 |
if (!c->gloflags.divert_to_card) |
if (!c->gloflags.divert_to_card) |
717 |
c->gloflags.divert_to_card = key_divert_to_card (key); |
c->gloflags.divert_to_card = key_divert_to_card (key); |
718 |
c->sym_prefs = copy_uid_prefs (c_sec->sym_prefs); |
if (c_sec->sym_prefs) |
719 |
|
c->sym_prefs = copy_uid_prefs (c_sec->sym_prefs); |
720 |
c->pubpart = c_sec; |
c->pubpart = c_sec; |
721 |
c->pubpart->key = key; |
c->pubpart->key = key; |
722 |
} |
} |
765 |
*r_key = NULL; |
*r_key = NULL; |
766 |
return gpg_error (GPG_ERR_EOF); |
return gpg_error (GPG_ERR_EOF); |
767 |
} |
} |
768 |
|
if (ctx->tmp->flags != 0) |
769 |
|
ctx->tmp->flags = 0; /* reset the 'updated' status. */ |
770 |
|
/* it might be possible there is no public key. */ |
771 |
|
if (flags && ctx->tmp->pubpart == NULL) |
772 |
|
flags = 0; |
773 |
*r_key = flags? ctx->tmp->pubpart->key : ctx->tmp->key; |
*r_key = flags? ctx->tmp->pubpart->key : ctx->tmp->key; |
774 |
*c = ctx->tmp = ctx->tmp->next; |
*c = ctx->tmp = ctx->tmp->next; |
775 |
ctx->pos++; |
ctx->pos++; |
790 |
err = keycache_next_key (ctx, flags, &c, r_key); |
err = keycache_next_key (ctx, flags, &c, r_key); |
791 |
return err; |
return err; |
792 |
} |
} |
793 |
|
|
794 |
|
|
795 |
|
/* Search for a key with the pattern @pattern and mark |
796 |
|
this key as the default signing key if found. |
797 |
|
Return value: 0 on success. */ |
798 |
|
gpgme_error_t |
799 |
|
gpg_keycache_set_default_key (gpg_keycache_t ctx, |
800 |
|
const char *pattern) |
801 |
|
{ |
802 |
|
gpgme_error_t err; |
803 |
|
gpgme_key_t key; |
804 |
|
struct keycache_s *itm; |
805 |
|
|
806 |
|
err = gpg_keycache_find_key2 (ctx, pattern, 0, &key, &itm); |
807 |
|
if (err) |
808 |
|
return err; |
809 |
|
|
810 |
|
if (itm) |
811 |
|
itm->default_key = 1; |
812 |
|
return 0; |
813 |
|
} |
814 |
|
|
815 |
|
/* Return the default key from the cache. If no was |
816 |
|
marked before, NULL is returned in @r_key. |
817 |
|
Return value: 0 on success. */ |
818 |
|
gpgme_error_t |
819 |
|
gpg_keycache_get_default_key (gpg_keycache_t ctx, |
820 |
|
gpgme_key_t *r_key) |
821 |
|
{ |
822 |
|
struct keycache_s *itm; |
823 |
|
|
824 |
|
*r_key = NULL; |
825 |
|
for (itm = ctx->item; itm; itm = itm->next) { |
826 |
|
if (itm->default_key) { |
827 |
|
*r_key = itm->key; |
828 |
|
break; |
829 |
|
} |
830 |
|
} |
831 |
|
if (!*r_key) |
832 |
|
return gpgme_error (GPG_ERR_NOT_FOUND); |
833 |
|
return 0; |
834 |
|
} |
835 |
|
|
836 |
|
|
837 |
|
static gpgme_error_t |
838 |
|
decode_subpacket (const char *subpkt_data, int *type, |
839 |
|
char **out, WORD *outlen) |
840 |
|
{ |
841 |
|
char tmp[128], *p = tmp, *val; |
842 |
|
char *enc; |
843 |
|
size_t pos = 0, i=0; |
844 |
|
|
845 |
|
/* example: spk:24:1:21:http%3A//subkeys.pgp.de */ |
846 |
|
*outlen = 0; |
847 |
|
*out = NULL; |
848 |
|
|
849 |
|
if (strncmp (subpkt_data, "spk:", 4)) |
850 |
|
return gpg_error (GPG_ERR_NO_DATA); |
851 |
|
|
852 |
|
strncpy (tmp, subpkt_data, 62); |
853 |
|
val = strtok (tmp, ":"); |
854 |
|
while (val != NULL) { |
855 |
|
switch (pos++) { |
856 |
|
case 0: |
857 |
|
break; |
858 |
|
|
859 |
|
case 1: |
860 |
|
if (type) |
861 |
|
*type = atoi (val); |
862 |
|
break; |
863 |
|
|
864 |
|
case 2: |
865 |
|
break; |
866 |
|
|
867 |
|
case 3: |
868 |
|
*outlen = atoi (val); |
869 |
|
break; |
870 |
|
|
871 |
|
case 4: |
872 |
|
enc = strdup (val); |
873 |
|
break; |
874 |
|
} |
875 |
|
val = strtok (NULL, ":"); |
876 |
|
} |
877 |
|
if (!enc) |
878 |
|
return gpg_error (GPG_ERR_NO_DATA);; |
879 |
|
*out = (char*)calloc (1, strlen (enc)+1); |
880 |
|
for (pos = 0; pos < strlen (enc); pos++) { |
881 |
|
if (enc[pos] == '%' && enc[pos+1] == '%') |
882 |
|
(*out)[i++] = '%'; |
883 |
|
else if (enc[pos] == '%') { |
884 |
|
char tmp[3]; |
885 |
|
tmp[0] = enc[++pos]; |
886 |
|
tmp[1] = enc[++pos]; |
887 |
|
tmp[2] = 0; |
888 |
|
(*out)[i++] = (char)strtoul (tmp, NULL, 16); |
889 |
|
} |
890 |
|
else |
891 |
|
(*out)[i++] = enc[pos]; |
892 |
|
} |
893 |
|
(*out)[i] = 0; |
894 |
|
free (enc); |
895 |
|
return 0; |
896 |
|
} |
897 |
|
|
898 |
|
|
899 |
|
/* If the attribute given in @attr is not set in the |
900 |
|
key cache object, try to update it. */ |
901 |
|
gpgme_error_t |
902 |
|
gpg_keycache_update_attr (struct keycache_s *item, |
903 |
|
int attr, int force) |
904 |
|
{ |
905 |
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
906 |
|
char *val = NULL; |
907 |
|
WORD n = 0; |
908 |
|
|
909 |
|
switch (attr) { |
910 |
|
case KC_ATTR_PREFSYM: |
911 |
|
if (!force && item->sym_prefs) |
912 |
|
break; |
913 |
|
safe_free (item->sym_prefs); |
914 |
|
err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val); |
915 |
|
if (!err && val != NULL) |
916 |
|
err = decode_subpacket (val, NULL, (char**)&item->sym_prefs, &n); |
917 |
|
break; |
918 |
|
|
919 |
|
case KC_ATTR_PREFKSERV: |
920 |
|
if (!force && item->pref_keyserver) |
921 |
|
break; |
922 |
|
safe_free (item->pref_keyserver); |
923 |
|
err = gpg_find_key_subpacket (item->key->subkeys->keyid+8, attr, &val); |
924 |
|
if (!err && val != NULL) |
925 |
|
err = decode_subpacket (val, NULL, &item->pref_keyserver, &n); |
926 |
|
break; |
927 |
|
} |
928 |
|
safe_free (val); |
929 |
|
return err; |
930 |
|
} |
931 |
|
|