36 |
#include "wptW32API.h" |
#include "wptW32API.h" |
37 |
#include "wptGPG.h" |
#include "wptGPG.h" |
38 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
39 |
|
#include "wptUTF8.h" |
40 |
|
|
41 |
/* Map net_errno to a winsock error. */ |
/* Map net_errno to a winsock error. */ |
42 |
#define net_errno ((int)WSAGetLastError ()) |
#define net_errno ((int)WSAGetLastError ()) |
45 |
keyserver server[MAX_KEYSERVERS] = {0}; |
keyserver server[MAX_KEYSERVERS] = {0}; |
46 |
keyserver_proxy_s proxy = {0}; |
keyserver_proxy_s proxy = {0}; |
47 |
static const char *server_list[] = { |
static const char *server_list[] = { |
|
"hkp://gnv.us.ks.cryptnet.net", |
|
|
"hkp://keyserver.kjsl.com", |
|
48 |
"hkp://sks.keyserver.penguin.de", |
"hkp://sks.keyserver.penguin.de", |
49 |
"hkp://subkeys.pgp.net", |
"hkp://subkeys.pgp.net", |
50 |
"ldap://keyserver.pgp.com", |
"ldap://keyserver.pgp.com", |
62 |
/* Default socket timeout. */ |
/* Default socket timeout. */ |
63 |
static int default_socket_timeout = 6; |
static int default_socket_timeout = 6; |
64 |
|
|
65 |
|
|
66 |
|
/* Wrapper for safe memory allocation. */ |
67 |
|
static char* |
68 |
|
safe_alloc (DWORD n) |
69 |
|
{ |
70 |
|
char *p; |
71 |
|
|
72 |
|
p = new char[n+1]; |
73 |
|
if (!p) |
74 |
|
BUG (0); |
75 |
|
memset (p, 0, n); |
76 |
|
return p; |
77 |
|
} |
78 |
|
|
79 |
/* Basic64 encode the input @inbuf to @outbuf. */ |
/* Basic64 encode the input @inbuf to @outbuf. */ |
80 |
static void |
static void |
81 |
base64_encode (const char *inbuf, char *outbuf) |
base64_encode (const char *inbuf, char *outbuf) |
121 |
} |
} |
122 |
|
|
123 |
|
|
124 |
|
/* Check that the given buffer contains a valid keyserver URL |
125 |
|
and return the prefix length, 0 in case of an error. */ |
126 |
|
static int |
127 |
|
check_URL (const char *buf) |
128 |
|
{ |
129 |
|
const char *proto[] = { |
130 |
|
"ldap://", |
131 |
|
"http://", |
132 |
|
"finger://", |
133 |
|
"hkp://", |
134 |
|
NULL}; |
135 |
|
int i; |
136 |
|
|
137 |
|
if (strlen (buf) < 7) |
138 |
|
return 0; |
139 |
|
|
140 |
|
for (i=0; proto[i] != NULL; i++) { |
141 |
|
if (strstr (buf, proto[i])) |
142 |
|
return strlen (proto[i]); |
143 |
|
} |
144 |
|
return 0; |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
/* Skip the URL schema and return only the host part of it. */ |
/* Skip the URL schema and return only the host part of it. */ |
149 |
static const char* |
static const char* |
150 |
skip_type_prefix (const char *hostname) |
skip_type_prefix (const char *hostname) |
151 |
{ |
{ |
152 |
if (hostname && !strncmp (hostname, "http://", 7)) |
int pos; |
153 |
hostname += 7; |
|
154 |
else if (hostname && !strncmp (hostname, "hkp://", 6)) |
if (!hostname) |
155 |
hostname += 6; |
return hostname; |
156 |
else if (hostname && !strncmp (hostname, "finger://", 9)) |
|
157 |
hostname += 9; |
pos = check_URL (hostname); |
158 |
else if (hostname && !strncmp (hostname, "ldap://", 7)) |
if (pos > 0) |
159 |
hostname += 7; |
hostname += pos; |
160 |
return hostname; |
return hostname; |
161 |
} |
} |
162 |
|
|
163 |
|
/* Parse the keyserver response and extract the human |
164 |
|
readable text passages. */ |
165 |
|
static int |
166 |
|
parse_keyserver_error (const char *resp, char *txt, size_t txtlen) |
167 |
|
{ |
168 |
|
char *p, *p2; |
169 |
|
|
170 |
|
/* If we find no 'Error' substring, we assume a success. */ |
171 |
|
if (!stristr (resp, "Error")) |
172 |
|
return -1; |
173 |
|
|
174 |
|
memset (txt, 0, txtlen); |
175 |
|
p = strstr (resp, "\r\n\r\n"); |
176 |
|
if (!p) |
177 |
|
return -1; |
178 |
|
resp += (p-resp); |
179 |
|
p = strstr (resp, "<h1>"); |
180 |
|
if (!p) |
181 |
|
return -1; |
182 |
|
resp += (p-resp)+strlen ("<h1>"); |
183 |
|
p = strstr (resp, "</h1>"); |
184 |
|
if (!p) |
185 |
|
return -1; |
186 |
|
resp += (p-resp)+strlen ("</h1>"); |
187 |
|
p2 = strstr (resp, "</body>"); |
188 |
|
if (p2 != NULL) |
189 |
|
memcpy (txt, resp, (p2-resp)); |
190 |
|
else { |
191 |
|
if (!strnicmp (resp, "<p>", 3)) |
192 |
|
resp += 3; |
193 |
|
while (resp && *resp == '\r' || *resp == '\n') |
194 |
|
resp++; |
195 |
|
memcpy (txt, resp, strlen (resp)); |
196 |
|
} |
197 |
|
return 0; |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
/* Check that the keyserver response indicates an OK. |
/* Check that the keyserver response indicates an OK. |
202 |
Return 0 on success. */ |
Return 0 on success. */ |
203 |
static int |
static int |
204 |
check_hkp_response (const char *resp, int recv) |
check_hkp_response (const char *resp, int recv) |
205 |
{ |
{ |
206 |
char *p, *end; |
int ec; |
|
int ec, len; |
|
207 |
|
|
208 |
|
log_debug ("check_hkp_response: '%s'\r\n", resp); |
209 |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
ec = recv ? WPTERR_WINSOCK_RECVKEY : WPTERR_WINSOCK_SENDKEY; |
210 |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
if (!strstr (resp, "HTTP/1.0 200 OK") && |
211 |
!strstr (resp, "HTTP/1.1 200 OK")) /* http error */ |
!strstr (resp, "HTTP/1.1 200 OK") && |
212 |
return ec; |
!strstr (resp, "HTTP/1.0 500 OK") && |
213 |
if (strstr (resp, "Public Key Server -- Error") |
!strstr (resp, "HTTP/1.1 500 OK")) |
214 |
|| strstr (resp, "Public Key Server -- Error") |
return ec; /* http error */ |
215 |
|| strstr (resp, "No matching keys in database")) { |
|
216 |
p = strstr (resp, "<p>"); |
if (!parse_keyserver_error (resp, hkp_errmsg, sizeof (hkp_errmsg)-2)) { |
217 |
if (p && strlen (p) < sizeof (hkp_errmsg)) { |
if (!strlen (hkp_errmsg)) |
218 |
end = strstr (p, "</p>"); |
_snprintf (hkp_errmsg, sizeof (hkp_errmsg)-1, |
219 |
len = end? (end - p + 1) : strlen (p); |
"Unknown keyserver error"); |
220 |
memset (hkp_errmsg, 0, sizeof (hkp_errmsg)); |
hkp_err = 1; |
|
strncpy (hkp_errmsg, p, len); |
|
|
hkp_err = 1; |
|
|
} |
|
221 |
return ec; |
return ec; |
222 |
} |
} |
223 |
return 0; |
return 0; |
418 |
} |
} |
419 |
kserver_proxy_release (&proxy); |
kserver_proxy_release (&proxy); |
420 |
WSACleanup (); |
WSACleanup (); |
421 |
|
log_debug ("wsock_end: cleanup finished.\r\n"); |
422 |
} |
} |
423 |
|
|
424 |
|
|
466 |
void |
void |
467 |
kserver_set_socket_timeout (int nsec) |
kserver_set_socket_timeout (int nsec) |
468 |
{ |
{ |
469 |
if (nsec < 0) |
if (nsec < 0 || nsec > 3600) |
470 |
nsec = 0; |
nsec = 0; |
471 |
default_socket_timeout = nsec; |
default_socket_timeout = nsec; |
472 |
} |
} |
492 |
return default_keyserver; |
return default_keyserver; |
493 |
} |
} |
494 |
else if (!type && idx < DIM (server_list)) { |
else if (!type && idx < DIM (server_list)) { |
495 |
|
/* XXX: handle non HKP servers. */ |
496 |
*port = HKP_PORT; |
*port = HKP_PORT; |
497 |
return server_list[idx]; |
return server_list[idx]; |
498 |
} |
} |
511 |
closesocket (fd); |
closesocket (fd); |
512 |
return 0; |
return 0; |
513 |
} |
} |
514 |
|
log_debug ("kserver_check_inet_connection: no inet connection.\r\n"); |
515 |
return -1; |
return -1; |
516 |
} |
} |
517 |
|
|
523 |
const char* |
const char* |
524 |
kserver_check_keyid (const char *keyid) |
kserver_check_keyid (const char *keyid) |
525 |
{ |
{ |
526 |
static char id[21]; |
static char id[22]; |
527 |
|
|
528 |
if (strstr (keyid, "@")) |
if (strstr (keyid, "@")) |
529 |
return keyid; /* email address */ |
return keyid; /* email address */ |
545 |
|
|
546 |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
n = 4*strlen (proxy_user) / 3 + 32 + strlen (proxy_pass) + 2; |
547 |
free_if_alloc (proxy.base64_user); |
free_if_alloc (proxy.base64_user); |
548 |
proxy.base64_user = new char[n]; |
proxy.base64_user = safe_alloc (n+1); |
|
if (!proxy.base64_user) |
|
|
BUG (0); |
|
549 |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
_snprintf (t, sizeof (t)-1, "%s:%s", proxy_user, proxy_pass); |
550 |
base64_encode (t, proxy.base64_user); |
base64_encode (t, proxy.base64_user); |
551 |
free_if_alloc (proxy.user); |
free_if_alloc (proxy.user); |
553 |
proxy.user = m_strdup (proxy_user); |
proxy.user = m_strdup (proxy_user); |
554 |
proxy.pass = m_strdup (proxy_pass); |
proxy.pass = m_strdup (proxy_pass); |
555 |
} |
} |
|
|
|
|
|
|
|
/* Check that the given buffer contains a valid keyserver URL. */ |
|
|
static int |
|
|
check_URL (const char * buf) |
|
|
{ |
|
|
if (strlen (buf) < 7) |
|
|
return -1; |
|
|
if (!strstr (buf, "ldap://") |
|
|
&& !strstr (buf, "http://") |
|
|
&& !strstr (buf, "finger://") |
|
|
&& !strstr (buf, "hkp://")) |
|
|
return -1; |
|
|
return 0; |
|
|
} |
|
556 |
|
|
557 |
|
|
558 |
/* Get the port number from the given protocol. */ |
/* Get the port number from the given protocol. */ |
560 |
port_from_proto (int proto) |
port_from_proto (int proto) |
561 |
{ |
{ |
562 |
switch (proto) { |
switch (proto) { |
563 |
case KSPROTO_LDAP: return 0; |
case KSPROTO_LDAP: return LDAP_PORT; |
564 |
case KSPROTO_FINGER: return FINGER_PORT; |
case KSPROTO_FINGER: return FINGER_PORT; |
565 |
case KSPROTO_HTTP: return HKP_PORT; |
case KSPROTO_HTTP: return HKP_PORT; |
566 |
} |
} |
567 |
return 0; |
return 0; |
568 |
} |
} |
572 |
static int |
static int |
573 |
proto_from_URL (const char *buf) |
proto_from_URL (const char *buf) |
574 |
{ |
{ |
575 |
if (strstr( buf, "ldap")) |
if (strstr (buf, "ldap")) |
576 |
return KSPROTO_LDAP; |
return KSPROTO_LDAP; |
577 |
else if (strstr( buf, "finger")) |
else if (strstr (buf, "finger")) |
578 |
return KSPROTO_FINGER; |
return KSPROTO_FINGER; |
579 |
return KSPROTO_HKP; |
return KSPROTO_HKP; |
580 |
} |
} |
581 |
|
|
582 |
|
|
583 |
|
/* Set the default keyserver. |
584 |
|
If @hostname is NULL, use the predefined keyserver. */ |
585 |
void |
void |
586 |
keyserver_set_default (const char *hostname, WORD port) |
keyserver_set_default (const char *hostname, WORD port) |
587 |
{ |
{ |
588 |
|
if (!port) |
589 |
|
port = HKP_PORT; |
590 |
if (hostname != NULL) { |
if (hostname != NULL) { |
591 |
free_if_alloc (default_keyserver); |
free_if_alloc (default_keyserver); |
592 |
default_keyserver = m_strdup (hostname); |
default_keyserver = m_strdup (hostname); |
593 |
default_keyserver_port = port; |
default_keyserver_port = port; |
594 |
} |
} |
595 |
if (!port) |
if (!default_keyserver) { |
|
port = HKP_PORT; |
|
|
if (!default_keyserver) |
|
596 |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
default_keyserver = m_strdup (DEF_HKP_KEYSERVER); |
597 |
|
default_keyserver_port = HKP_PORT; |
598 |
|
} |
599 |
server[0].name = m_strdup (default_keyserver); |
server[0].name = m_strdup (default_keyserver); |
600 |
server[0].used = 1; |
server[0].used = 1; |
601 |
server[0].port = port; |
server[0].port = port; |
647 |
} |
} |
648 |
|
|
649 |
|
|
650 |
|
/* Return the specified keyserver config setting @key as an integer. */ |
651 |
|
static int |
652 |
|
get_conf_kserver_int (const char *key) |
653 |
|
{ |
654 |
|
char *p; |
655 |
|
int val = 0; |
656 |
|
|
657 |
|
p = get_reg_entry_keyserver (key); |
658 |
|
if (p && *p) |
659 |
|
val = atoi (p); |
660 |
|
free_if_alloc (p); |
661 |
|
return val; |
662 |
|
} |
663 |
|
|
664 |
|
|
665 |
|
/* Read the proxy configuration and store it into @prox. */ |
666 |
|
static void |
667 |
|
read_proxy_config (keyserver_proxy_t prox) |
668 |
|
{ |
669 |
|
char *proto; |
670 |
|
|
671 |
|
if (!prox) |
672 |
|
return; |
673 |
|
|
674 |
|
proto = get_reg_entry_keyserver("Proto"); |
675 |
|
if (proto != NULL && strlen (proto) > 0) |
676 |
|
prox->proto = atoi (proto); |
677 |
|
else |
678 |
|
prox->proto = PROXY_PROTO_NONE; |
679 |
|
free_if_alloc (proto); |
680 |
|
free_if_alloc (prox->host); |
681 |
|
prox->host = get_reg_entry_keyserver ("Host"); |
682 |
|
free_if_alloc (prox->user); |
683 |
|
prox->user = get_reg_entry_keyserver ("User"); |
684 |
|
free_if_alloc (prox->pass); |
685 |
|
prox->pass = get_reg_entry_keyserver ("Pass"); |
686 |
|
prox->port = get_conf_kserver_int ("Port"); |
687 |
|
} |
688 |
|
|
689 |
|
|
690 |
/* Load the keyserver config file @conf. */ |
/* Load the keyserver config file @conf. */ |
691 |
int |
int |
692 |
kserver_load_conf (const char *conf) |
kserver_load_conf (const char *conf) |
711 |
} |
} |
712 |
no_config=1; |
no_config=1; |
713 |
} |
} |
714 |
get_reg_proxy_prefs (&proxy); |
read_proxy_config (&proxy); |
715 |
if (user && pass) |
if (user && pass) |
716 |
update_proxy_user (user, pass); |
update_proxy_user (user, pass); |
717 |
else if (user && !pass || !user && pass) { |
else if (user && !pass || !user && pass) { |
718 |
msg_box (NULL, _("Invalid proxy configuration." |
msg_box (NULL, _("Invalid proxy configuration. " |
719 |
"You need to set a user and a password" |
"You need to set a user and a password " |
720 |
"to use proxy authentication!"), |
"to use proxy authentication!"), |
721 |
_("Proxy Error"), MB_ERR); |
_("Proxy Error"), MB_ERR); |
722 |
} |
} |
741 |
s = (char *)skip_whitespace (buf); |
s = (char *)skip_whitespace (buf); |
742 |
if (*s == '#' || strlen (s) < 7) |
if (*s == '#' || strlen (s) < 7) |
743 |
continue; |
continue; |
744 |
if (check_URL (s)) { |
chk_pos = check_URL (s); |
745 |
|
if (!chk_pos) { |
746 |
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
msg_box (NULL, _("All entries of this file must have a valid prefix.\n" |
747 |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
"Currently HKP/HTTP, LDAP and FINGER are supported.\n"), |
748 |
_("Keyserver Error"), MB_ERR); |
_("Keyserver Error"), MB_ERR); |
749 |
continue; |
continue; |
750 |
} |
} |
|
chk_pos = 6; |
|
|
if (strstr (s, "finger")) |
|
|
chk_pos = 10; |
|
751 |
p = strchr (s+chk_pos, ':'); |
p = strchr (s+chk_pos, ':'); |
752 |
server[pos].used = 1; |
server[pos].used = 1; |
753 |
server[pos].proto = proto_from_URL (s); |
server[pos].proto = proto_from_URL (s); |
758 |
server[pos].port = atol (p+1); |
server[pos].port = atol (p+1); |
759 |
if (server[pos].port < 0 || server[pos].port > 65536) |
if (server[pos].port < 0 || server[pos].port > 65536) |
760 |
server[pos].port = HKP_PORT; |
server[pos].port = HKP_PORT; |
761 |
server[pos].name = new char [(p-s)+2]; |
server[pos].name = safe_alloc ((p-s)+2); |
|
if (!server[pos].name) |
|
|
BUG (0); |
|
762 |
memset (server[pos].name, 0, (p-s)+2); |
memset (server[pos].name, 0, (p-s)+2); |
763 |
memcpy (server[pos].name, s, (p-s)); |
memcpy (server[pos].name, s, (p-s)); |
764 |
} |
} |
850 |
} |
} |
851 |
|
|
852 |
|
|
853 |
|
/* Check if the URL needs to be encoded or not. */ |
854 |
static bool |
static bool |
855 |
URL_must_encoded (const char *url) |
URL_must_encoded (const char *url) |
856 |
{ |
{ |
862 |
|
|
863 |
/* Perform URL encoding of the given data. */ |
/* Perform URL encoding of the given data. */ |
864 |
static char* |
static char* |
865 |
URL_encode (const char *url, size_t ulen, size_t *ret_nlen) |
URL_encode (const char *url, DWORD ulen, DWORD *ret_nlen) |
866 |
{ |
{ |
867 |
gpgme_data_t hd; |
gpgme_data_t hd; |
868 |
char numbuf[5], *p; |
char numbuf[5], *p; |
892 |
/* Format a request for the given keyid (send). */ |
/* Format a request for the given keyid (send). */ |
893 |
static char* |
static char* |
894 |
kserver_send_request (const char *hostname, WORD port, |
kserver_send_request (const char *hostname, WORD port, |
895 |
const char *pubkey, int octets) |
const char *pubkey, DWORD octets) |
896 |
{ |
{ |
897 |
char *request = NULL; |
const char *fmt; |
898 |
char *enc_pubkey = NULL; |
char *request; |
899 |
size_t enc_octets; |
char *enc_pubkey; |
900 |
|
DWORD enc_octets; |
901 |
int reqlen; |
int reqlen; |
902 |
|
|
903 |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
log_debug ("kserver_send_request: %s:%d\r\n", hostname, port); |
904 |
|
|
905 |
if (!port) |
if (!port) |
906 |
port = HKP_PORT; |
port = HKP_PORT; |
|
reqlen = 512 + strlen (hostname) + 2*strlen (pubkey); |
|
|
if (proxy.proto == PROXY_PROTO_HTTP && proxy.base64_user) |
|
|
reqlen += strlen (proxy.base64_user) + 1; |
|
|
request = new char[reqlen]; |
|
|
if (!request) |
|
|
BUG (0); |
|
907 |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
enc_pubkey = URL_encode (pubkey, octets, &enc_octets); |
908 |
if (!enc_pubkey || !enc_octets) { |
reqlen = 2*strlen (hostname) + 2*32/*port*/ + enc_octets + 32 /*len*/ + 1; |
|
free_if_alloc (request); |
|
|
return NULL; |
|
|
} |
|
909 |
|
|
910 |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
911 |
_snprintf (request, reqlen-1, |
fmt = "POST http://%s:%d/pks/add HTTP/1.0\r\n" |
912 |
"POST http://%s:%d/pks/add HTTP/1.0\r\n" |
"Referer: \r\n" |
913 |
"Referer: \r\n" |
"User-Agent: WinPT/W32\r\n" |
914 |
"User-Agent: WinPT/W32\r\n" |
"Host: %s:%d\r\n" |
915 |
"Host: %s:%d\r\n" |
"Proxy-Authorization: Basic %s\r\n" |
916 |
"Proxy-Authorization: Basic %s\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
917 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-length: %d\r\n" |
918 |
"Content-length: %d\r\n" |
"\r\n" |
919 |
"\r\n" |
"keytext=%s" |
920 |
"keytext=%s" |
"\r\n"; |
921 |
"\r\n", |
reqlen += strlen (fmt) + strlen (proxy.base64_user) + 1; |
922 |
skip_type_prefix (hostname), port, hostname, port, |
request = safe_alloc (reqlen+1); |
923 |
|
_snprintf (request, reqlen, fmt, |
924 |
|
skip_type_prefix (hostname), port, hostname, port, |
925 |
proxy.base64_user, enc_octets+9, enc_pubkey); |
proxy.base64_user, enc_octets+9, enc_pubkey); |
926 |
} |
} |
927 |
else { |
else { |
928 |
_snprintf (request, reqlen-1, |
fmt = "POST /pks/add HTTP/1.0\r\n" |
929 |
"POST /pks/add HTTP/1.0\r\n" |
"Referer: \r\n" |
930 |
"Referer: \r\n" |
"User-Agent: WinPT/W32\r\n" |
931 |
"User-Agent: WinPT/W32\r\n" |
"Host: %s:%d\r\n" |
932 |
"Host: %s:%d\r\n" |
"Content-type: application/x-www-form-urlencoded\r\n" |
933 |
"Content-type: application/x-www-form-urlencoded\r\n" |
"Content-length: %d\r\n" |
934 |
"Content-length: %d\r\n" |
"\r\n" |
935 |
"\r\n" |
"keytext=%s" |
936 |
"keytext=%s" |
"\r\n"; |
937 |
"\r\n", |
reqlen += strlen (fmt)+1; |
938 |
skip_type_prefix (hostname), port, |
request = safe_alloc (reqlen+1); |
939 |
|
_snprintf (request, reqlen, fmt, |
940 |
|
skip_type_prefix (hostname), port, |
941 |
enc_octets+9, enc_pubkey); |
enc_octets+9, enc_pubkey); |
942 |
} |
} |
|
free_if_alloc (enc_pubkey); |
|
943 |
|
|
944 |
log_debug ("%s\r\n", request); |
free_if_alloc (enc_pubkey); |
945 |
|
log_debug ("request:\r\n%s\r\n", request); |
946 |
return request; |
return request; |
947 |
} |
} |
948 |
|
|
952 |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
kserver_recvkey (const char *hostname, WORD port, const char *keyid, |
953 |
char **r_key, int *r_keylen) |
char **r_key, int *r_keylen) |
954 |
{ |
{ |
955 |
|
const char *fmt; |
956 |
char *request = NULL; |
char *request = NULL; |
957 |
int conn_fd; |
int conn_fd; |
958 |
int rc; |
int rc, reqlen; |
959 |
|
|
960 |
if (!port) |
if (!port) |
961 |
port = HKP_PORT; |
port = HKP_PORT; |
962 |
hkp_err = 0; /* reset */ |
hkp_err = 0; /* reset */ |
963 |
rc = kserver_connect (hostname, port, &conn_fd); |
rc = kserver_connect (hostname, port, &conn_fd); |
964 |
if (rc) |
if (rc) |
965 |
goto leave; |
goto leave; |
966 |
|
|
967 |
request = new char[300+1]; |
reqlen = strlen (hostname)+32+strlen (keyid)+1; |
|
if (!request) |
|
|
BUG (0); |
|
|
|
|
968 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
969 |
_snprintf (request, 300, |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
970 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n" |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
971 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
reqlen =+ strlen (fmt) + strlen (proxy.base64_user)+1; |
972 |
skip_type_prefix (hostname), port, keyid, proxy.base64_user); |
request = safe_alloc (reqlen+1); |
973 |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
974 |
|
keyid, proxy.base64_user); |
975 |
} |
} |
976 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
977 |
_snprintf (request, 300, |
fmt = "GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
978 |
"GET http://%s:%d/pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", |
reqlen += strlen (fmt)+1; |
979 |
skip_type_prefix (hostname), port, keyid); |
request = safe_alloc (reqlen+1); |
980 |
} |
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), |
981 |
|
port, keyid); |
982 |
|
} |
983 |
else { |
else { |
984 |
_snprintf (request, 300, |
fmt = "GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n"; |
985 |
"GET /pks/lookup?op=get&search=%s HTTP/1.0\r\n\r\n", keyid); |
reqlen += strlen (fmt)+1; |
986 |
|
request = safe_alloc (reqlen+1); |
987 |
|
_snprintf (request, reqlen, fmt, keyid); |
988 |
} |
} |
989 |
|
|
990 |
log_debug ("%s\r\n", request); |
log_debug ("request:\r\n%s\r\n", request); |
991 |
|
|
992 |
rc = sock_write (conn_fd, request, strlen (request)); |
rc = sock_write (conn_fd, request, strlen (request)); |
993 |
if (rc == SOCKET_ERROR) { |
if (rc == SOCKET_ERROR) { |
1001 |
goto leave; |
goto leave; |
1002 |
} |
} |
1003 |
|
|
1004 |
log_debug ("%s\r\n", *r_key); |
log_debug ("response:\r\n%s\r\n", *r_key); |
1005 |
rc = check_hkp_response (*r_key, 1); |
rc = check_hkp_response (*r_key, 1); |
1006 |
if (rc) |
if (rc) |
1007 |
goto leave; |
goto leave; |
1069 |
int n=0; |
int n=0; |
1070 |
|
|
1071 |
/* parse response 'HTTP/1.0 500 OK' */ |
/* parse response 'HTTP/1.0 500 OK' */ |
1072 |
if (sock_getline (fd, buf, 127, &n)) |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
1073 |
return WPTERR_KEYSERVER_NOTFOUND; |
return WPTERR_KEYSERVER_NOTFOUND; |
1074 |
|
|
1075 |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
log_debug ("kserver_search_chkpresp: %s\r\n", buf); |
1090 |
} |
} |
1091 |
|
|
1092 |
|
|
1093 |
/* Begin keyserver search procedure. */ |
/* Extract the amount of keys from the info record. */ |
1094 |
|
static int |
1095 |
|
count_keys_in_response (char *buf) |
1096 |
|
{ |
1097 |
|
char *p; |
1098 |
|
int recno = 0; |
1099 |
|
int n = 0; |
1100 |
|
|
1101 |
|
/* info:1:4 */ |
1102 |
|
if (strncmp (buf, "info", 4)) |
1103 |
|
return 0; |
1104 |
|
p = strtok (buf, ":"); |
1105 |
|
while (p != NULL) { |
1106 |
|
recno++; |
1107 |
|
if (recno == 3) |
1108 |
|
n = atoi (p); |
1109 |
|
p = strtok (NULL, ":"); |
1110 |
|
} |
1111 |
|
return n; |
1112 |
|
} |
1113 |
|
|
1114 |
|
|
1115 |
|
/* Start the keyserver search. |
1116 |
|
Connect to host @hostname and port @port. |
1117 |
|
The pattern are given in @pattern. |
1118 |
|
The socket is returned in @conn_fd and @nkeys contains |
1119 |
|
the amount of keys which were found. */ |
1120 |
int |
int |
1121 |
kserver_search_begin (const char *hostname, WORD port, |
kserver_search_begin (const char *hostname, WORD port, |
1122 |
const char *pattern, int *conn_fd) |
const char *pattern, int *conn_fd, int *nkeys) |
1123 |
{ |
{ |
1124 |
|
const char *fmt; |
1125 |
char *request = NULL; |
char *request = NULL; |
1126 |
char *enc_patt = NULL; |
char *enc_patt = NULL; |
1127 |
int n; |
char status[128]; |
1128 |
int rc, sock_fd; |
int rc, sock_fd; |
1129 |
|
int reqlen; |
1130 |
|
|
1131 |
|
*conn_fd = 0; |
1132 |
|
|
1133 |
rc = kserver_connect (hostname, port, &sock_fd); |
rc = kserver_connect (hostname, port, &sock_fd); |
1134 |
if (rc) { |
if (rc) |
|
*conn_fd = 0; |
|
1135 |
goto leave; |
goto leave; |
|
} |
|
1136 |
|
|
1137 |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
enc_patt = URL_encode (pattern, strlen (pattern), NULL); |
1138 |
n = 140 + strlen (enc_patt) + strlen (hostname) + 32 + 2; |
reqlen = strlen (enc_patt) + strlen (hostname) + 32 + 1; |
1139 |
if (proxy.base64_user) |
|
|
n += strlen (proxy.base64_user) + 1; |
|
|
request = new char[n+1]; |
|
|
if (!request) |
|
|
BUG (0); |
|
|
|
|
1140 |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
if (proxy.host && proxy.user && proxy.proto == PROXY_PROTO_HTTP) { |
1141 |
_snprintf (request, n, |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n" |
1142 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n" |
"Proxy-Authorization: Basic %s\r\n\r\n"; |
1143 |
"Proxy-Authorization: Basic %s\r\n\r\n", |
reqlen += strlen (proxy.base64_user) + strlen (fmt) + 1; |
1144 |
skip_type_prefix (hostname), port, enc_patt, proxy.base64_user); |
request = safe_alloc (reqlen+1); |
1145 |
|
_snprintf (request, reqlen, fmt, skip_type_prefix (hostname), port, |
1146 |
|
enc_patt, proxy.base64_user); |
1147 |
} |
} |
1148 |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
else if (proxy.host && proxy.proto == PROXY_PROTO_HTTP) { |
1149 |
_snprintf (request, n, |
fmt = "GET http://%s:%d/pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1150 |
"GET http://%s:%d/pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
reqlen += strlen (fmt)+1; |
1151 |
skip_type_prefix (hostname), port, enc_patt); |
request = safe_alloc (reqlen+1); |
1152 |
|
_snprintf (request, reqlen, skip_type_prefix (hostname), port, |
1153 |
|
enc_patt); |
1154 |
} |
} |
1155 |
else { |
else { |
1156 |
_snprintf (request, n, |
fmt = "GET /pks/lookup?options=mr&op=index&search=%s HTTP/1.0\r\n\r\n"; |
1157 |
"GET /pks/lookup?op=index&search=%s HTTP/1.0\r\n\r\n", |
reqlen += strlen (fmt)+1; |
1158 |
enc_patt); |
request = safe_alloc (reqlen+1); |
1159 |
|
_snprintf (request, reqlen, fmt, enc_patt); |
1160 |
} |
} |
1161 |
|
|
1162 |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
log_debug ("kserver_search_begin:\r\n%s\r\n", request); |
|
|
|
1163 |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
if (sock_write (sock_fd, request, strlen (request)) == SOCKET_ERROR) { |
1164 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1165 |
goto leave; |
goto leave; |
1169 |
if (rc) { |
if (rc) { |
1170 |
closesocket (sock_fd); |
closesocket (sock_fd); |
1171 |
sock_fd = 0; |
sock_fd = 0; |
1172 |
|
goto leave; |
1173 |
|
} |
1174 |
|
|
1175 |
|
/* Skip all lines until we reach the "info:" record. */ |
1176 |
|
for (;;) { |
1177 |
|
if (sock_getline (sock_fd, status, sizeof (status)-1, &reqlen)) { |
1178 |
|
log_debug ("kserver_search_begin: retrieving status line failed.\r\n"); |
1179 |
|
closesocket (sock_fd); |
1180 |
|
sock_fd = 0; |
1181 |
|
rc = WPTERR_GENERAL; |
1182 |
|
break; |
1183 |
|
} |
1184 |
|
if (!strncmp (status, "info:", 5)) |
1185 |
|
break; |
1186 |
} |
} |
1187 |
|
|
1188 |
|
if (!rc) |
1189 |
|
*nkeys = count_keys_in_response (status); |
1190 |
*conn_fd = sock_fd; |
*conn_fd = sock_fd; |
1191 |
|
|
1192 |
leave: |
leave: |
1196 |
} |
} |
1197 |
|
|
1198 |
|
|
1199 |
|
/* Parse a single pub record returned by the keyserver. */ |
1200 |
|
static void |
1201 |
|
parse_pub_record (keyserver_key_s *key, char *buf) |
1202 |
|
{ |
1203 |
|
enum pub_rec_t {ID=1, KEYID, ALGO, SIZE, CREATE, EXPIRE}; |
1204 |
|
char *p; |
1205 |
|
int recno = 0; |
1206 |
|
|
1207 |
|
/* pub:BF3DF9B4:17:1024:925411133:: */ |
1208 |
|
p = strtok (buf, ":"); |
1209 |
|
while (p != NULL) { |
1210 |
|
recno++; |
1211 |
|
switch (recno) { |
1212 |
|
case ID: |
1213 |
|
break; |
1214 |
|
|
1215 |
/* Convert an iso date @iso_date (YYYY-MM-DD) into the locale |
case KEYID: |
1216 |
representation and store it into @loc_date. |
free_if_alloc (key->keyid); |
1217 |
Return value: 0 on success. */ |
key->keyid = m_strdup (p); |
1218 |
static int |
break; |
1219 |
parse_iso_date (const char *iso_date, char *loc_date, size_t loclen) |
|
1220 |
|
case ALGO: |
1221 |
|
key->algo = atoi (p); |
1222 |
|
break; |
1223 |
|
|
1224 |
|
case SIZE: |
1225 |
|
key->bits = atoi (p); |
1226 |
|
break; |
1227 |
|
|
1228 |
|
case CREATE: |
1229 |
|
key->creation = strtoul (p, NULL, 10); |
1230 |
|
break; |
1231 |
|
|
1232 |
|
case EXPIRE: |
1233 |
|
key->expires = strtoul (p, NULL, 10); |
1234 |
|
break; |
1235 |
|
} |
1236 |
|
p = strtok (NULL, ":"); |
1237 |
|
} |
1238 |
|
|
1239 |
|
} |
1240 |
|
|
1241 |
|
|
1242 |
|
/* Parse a single user id returned by the keyserver. */ |
1243 |
|
static void |
1244 |
|
parse_uid_record (keyserver_key_s *key, char *buf) |
1245 |
{ |
{ |
1246 |
SYSTEMTIME st; |
enum uid_rec_t {ID=1, UID, CREATE, EXPIRE}; |
1247 |
char buf[16] = {0}, *p; |
keyserver_uid_s *u, *n; |
1248 |
int pos=0; |
char *p, *raw; |
1249 |
|
int recno = 0; |
1250 |
|
|
1251 |
|
/* uid:Timo Schulz <[email protected]>:1138440360:: */ |
1252 |
|
u = new keyserver_uid_s; |
1253 |
|
memset (u, 0, sizeof *u); |
1254 |
|
|
1255 |
strncpy (buf, iso_date, sizeof (buf)-1); |
p = strtok (buf, ":"); |
|
p = strtok (buf, "-"); |
|
1256 |
while (p != NULL) { |
while (p != NULL) { |
1257 |
switch (pos) { |
recno++; |
1258 |
case 0: st.wYear = (WORD)atoi (p); pos++; break; |
|
1259 |
case 1: st.wMonth = (WORD)atoi (p); pos++; break; |
switch (recno) { |
1260 |
case 2: st.wDay = (WORD)atoi (p); pos++; break; |
case ID: |
1261 |
default: break; |
break; |
1262 |
|
|
1263 |
|
case UID: |
1264 |
|
unhexify_buffer (p, &raw); |
1265 |
|
u->uid = utf8_to_native (raw); |
1266 |
|
free_if_alloc (raw); |
1267 |
|
break; |
1268 |
|
|
1269 |
|
case CREATE: |
1270 |
|
u->creation = strtoul (p, NULL, 10); |
1271 |
|
break; |
1272 |
|
|
1273 |
|
case EXPIRE: |
1274 |
|
u->expires = strtoul (p, NULL, 10); |
1275 |
|
break; |
1276 |
} |
} |
1277 |
p = strtok (NULL, "-"); |
|
1278 |
|
p = strtok (NULL, ":"); |
1279 |
|
} |
1280 |
|
if (!key->uids) |
1281 |
|
key->uids = u; |
1282 |
|
else { |
1283 |
|
for (n = key->uids; n->next; n=n->next) |
1284 |
|
; |
1285 |
|
n->next = u; |
1286 |
} |
} |
|
if (pos != 3) |
|
|
return -1; |
|
|
|
|
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
|
|
NULL, loc_date, loclen)) |
|
|
return -1; |
|
|
return 0; |
|
1287 |
} |
} |
1288 |
|
|
1289 |
|
|
1290 |
|
/* Peek the next 3 bytes to check if the next line |
1291 |
|
would be a new "pub" record. In this case, return |
1292 |
|
-1 as an EOF indication, 0 otherwise. */ |
1293 |
|
static int |
1294 |
|
is_key_eof (int fd) |
1295 |
|
{ |
1296 |
|
char buf[64]; |
1297 |
|
int n; |
1298 |
|
|
1299 |
|
n = recv (fd, buf, 3, MSG_PEEK); |
1300 |
|
if (n < 3 || strncmp (buf, "pub", 3)) |
1301 |
|
return 0; |
1302 |
|
return -1; |
1303 |
|
} |
1304 |
|
|
1305 |
|
|
1306 |
|
/* Return the next key from the search response. */ |
1307 |
int |
int |
1308 |
kserver_search_next (int fd, keyserver_key *key) |
kserver_search_next (int fd, keyserver_key_s **r_key) |
1309 |
{ |
{ |
1310 |
char buf[1024], *p; |
keyserver_uid_s *uid, *u = NULL; |
1311 |
int uidlen, nbytes, pos = 0; |
keyserver_key_s *key; |
1312 |
|
char buf[512]; |
1313 |
|
int n = 0; |
1314 |
|
long max = 0; |
1315 |
|
|
1316 |
log_debug ("keyserver_search_next:\r\n"); |
log_debug ("keyserver_search_next:\r\n"); |
1317 |
|
*r_key = NULL; |
|
if (sock_getline (fd, buf, sizeof (buf) - 1, &nbytes)) |
|
|
return WPTERR_GENERAL; |
|
1318 |
|
|
1319 |
/* XXX: use maschine readable option. */ |
key = new keyserver_key_s; |
1320 |
log_debug ("%s\r\n", buf); |
memset (key, 0, sizeof *key); |
1321 |
|
for (;;) { |
1322 |
if (!strncmp (buf, "pub", 3)) { |
if (sock_getline (fd, buf, sizeof (buf)-1, &n)) |
1323 |
int revoked = strstr (buf, "KEY REVOKED") != NULL? 1 : 0; |
break; |
1324 |
key->bits = atol (buf+3); |
/*log_debug ("record: '%s'\r\n", buf); */ |
1325 |
p = strchr (buf, '>'); |
if (!strncmp (buf, "pub", 3)) |
1326 |
if (!p) |
parse_pub_record (key, buf); |
1327 |
goto fail; |
else |
1328 |
pos = p - buf + 1; |
parse_uid_record (key, buf); |
1329 |
memcpy (key->keyid, buf + pos, 8); |
if (is_key_eof (fd)) |
1330 |
key->keyid[8] = '\0'; |
break; |
1331 |
p = strstr (buf, "</a>"); |
} |
|
if (!p) |
|
|
goto fail; |
|
|
pos = p - buf + 5; |
|
|
memcpy (key->date, buf + pos, 10); |
|
|
key->date[10] = '\0'; |
|
|
parse_iso_date (key->date, key->date, sizeof (key->date)-1); |
|
|
if (revoked) { |
|
|
strcpy (key->uid, "KEY REVOKED: not checked"); |
|
|
return 0; |
|
|
} |
|
|
pos += 10; |
|
|
p = buf + pos + 1; |
|
|
while (p && *p != '>') |
|
|
p++; |
|
|
p++; |
|
|
uidlen = strlen (p) - 10; |
|
|
if (!strstr (p, "<") && !strstr (p, ">")) { |
|
|
pos=0; |
|
|
while (p && *p && pos < sizeof (key->uid)-1) { |
|
|
if (*p == '<') |
|
|
break; |
|
|
key->uid[pos++] = *p++; |
|
|
} |
|
|
key->uid[pos] ='\0'; |
|
|
return 0; |
|
|
} |
|
1332 |
|
|
1333 |
if (uidlen > sizeof (key->uid)-1) |
/* the uid with the newest self sig is used as the |
1334 |
uidlen = sizeof (key->uid)-1; |
primary user id. */ |
1335 |
memcpy (key->uid, p, uidlen); |
for (uid = key->uids; uid; uid = uid->next) { |
1336 |
key->uid[uidlen] = '\0'; |
if (uid->creation > max) { |
1337 |
strcat (key->uid, ">"); |
max = uid->creation; |
1338 |
p = strchr (key->uid, '&'); |
u = uid; |
|
if (p) { |
|
|
key->uid[(p-key->uid)] = '<'; |
|
|
memmove (key->uid+(p-key->uid)+1, key->uid+(p-key->uid)+4, |
|
|
strlen (key->uid)-3); |
|
1339 |
} |
} |
|
return 0; |
|
1340 |
} |
} |
1341 |
|
|
1342 |
fail: |
key->main_uid = u? u : key->uids; |
1343 |
key->bits = 0; |
*r_key = key; |
|
memset (key, 0, sizeof *key); |
|
1344 |
return 0; |
return 0; |
1345 |
} |
} |
1346 |
|
|
1347 |
|
|
1348 |
|
/* Release the keyserver key @key and all its elements. */ |
1349 |
|
void |
1350 |
|
kserver_release_key (keyserver_key_s *key) |
1351 |
|
{ |
1352 |
|
keyserver_uid_s *u; |
1353 |
|
|
1354 |
|
while (key->uids) { |
1355 |
|
u = key->uids->next; |
1356 |
|
free_if_alloc (key->uids->uid); |
1357 |
|
free_if_alloc (key->uids); |
1358 |
|
key->uids = u; |
1359 |
|
} |
1360 |
|
free_if_alloc (key->keyid); |
1361 |
|
free_if_alloc (key); |
1362 |
|
} |
1363 |
|
|
1364 |
|
|
1365 |
/* Release mbmers in the proxy context @ctx. */ |
/* Release mbmers in the proxy context @ctx. */ |
1366 |
void |
void |
1367 |
kserver_proxy_release (keyserver_proxy_t ctx) |
kserver_proxy_release (keyserver_proxy_t ctx) |
1411 |
|
|
1412 |
p = get_gnupg_prog (); |
p = get_gnupg_prog (); |
1413 |
n = strlen (p) + 1 + 128; |
n = strlen (p) + 1 + 128; |
1414 |
ksprg = new char[n+1]; |
ksprg = safe_alloc (n+1); |
1415 |
if (!ksprg) |
if (!ksprg) |
1416 |
BUG (0); |
BUG (0); |
1417 |
sep = strrchr (p, '\\'); |
sep = strrchr (p, '\\'); |
1445 |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
host? skip_type_prefix (host): "64.94.85.200", keyid); |
1446 |
fclose (fp); |
fclose (fp); |
1447 |
|
|
1448 |
p = new char[strlen (ksprg) + strlen (inf) + strlen (outf) + 32]; |
p = safe_alloc (strlen (ksprg) + strlen (inf) + strlen (outf) + 32); |
|
if (!p) |
|
|
BUG (NULL); |
|
1449 |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
sprintf (p, "%s -o %s %s", ksprg, outf, inf); |
1450 |
if (spawn_application (p)) { |
if (spawn_application (p)) { |
1451 |
fp = NULL; |
fp = NULL; |
1572 |
} |
} |
1573 |
return 0; |
return 0; |
1574 |
} |
} |
1575 |
|
|
1576 |
|
|
1577 |
|
/* Split the URL @r_keyserver into the host and the port |
1578 |
|
part if possible. */ |
1579 |
|
gpgme_error_t |
1580 |
|
parse_keyserver_url (char **r_keyserver, unsigned short *r_port) |
1581 |
|
{ |
1582 |
|
char *p; |
1583 |
|
char *url = *r_keyserver; |
1584 |
|
int off = 0; |
1585 |
|
|
1586 |
|
/* no port is given so use the default port. */ |
1587 |
|
p = strrchr (url, ':'); |
1588 |
|
if (p == strchr (url, ':')) { |
1589 |
|
int port = port_from_proto (proto_from_URL (url)); |
1590 |
|
if (!port) |
1591 |
|
port = HKP_PORT; |
1592 |
|
*r_port = port; |
1593 |
|
return 0; |
1594 |
|
} |
1595 |
|
|
1596 |
|
if (url[(p-url)-1] == '/') /* remove / in .de/:11371 */ |
1597 |
|
off = 1; |
1598 |
|
|
1599 |
|
*r_keyserver = substr (url, 0, (p-url)-off); |
1600 |
|
*r_port = atoi (url+(p-url)+1); |
1601 |
|
free_if_alloc (url); |
1602 |
|
return 0; |
1603 |
|
} |