27 |
#include <ctype.h> |
#include <ctype.h> |
28 |
|
|
29 |
#include "resource.h" |
#include "resource.h" |
|
#include "wptKeyserver.h" |
|
30 |
#include "wptErrors.h" |
#include "wptErrors.h" |
31 |
#include "wptTypes.h" |
#include "wptTypes.h" |
32 |
#include "wptCommonCtl.h" |
#include "wptCommonCtl.h" |
35 |
#include "wptVersion.h" |
#include "wptVersion.h" |
36 |
#include "wptGPG.h" |
#include "wptGPG.h" |
37 |
#include "wptGPGME.h" |
#include "wptGPGME.h" |
38 |
|
#include "wptKeyserver.h" |
39 |
#include "wptContext.h" /* for passphrase_s */ |
#include "wptContext.h" /* for passphrase_s */ |
40 |
#include "wptKeyManager.h" |
#include "wptKeyManager.h" |
41 |
#include "wptDlgs.h" |
#include "wptDlgs.h" |
42 |
#include "wptUTF8.h" |
#include "wptUTF8.h" |
43 |
|
#include "wptRegistry.h" |
44 |
|
#include "wptKeylist.h" |
45 |
|
|
46 |
char* get_reg_entry_keyserver (const char *); |
char* get_reg_entry_keyserver (const char *); |
47 |
int set_reg_entry_keyserver (const char *, const char *); |
int set_reg_entry_keyserver (const char *, const char *); |
173 |
keyserver_recv_key (HWND dlg, const char *kserver, WORD port, |
keyserver_recv_key (HWND dlg, const char *kserver, WORD port, |
174 |
const char *pattern, int proto, int flags, |
const char *pattern, int proto, int flags, |
175 |
char **r_fpr) |
char **r_fpr) |
176 |
{ |
{ |
|
gpgme_ctx_t ctx = NULL; |
|
|
gpgme_data_t keydata = NULL; |
|
177 |
gpgme_import_result_t import_res = NULL; |
gpgme_import_result_t import_res = NULL; |
178 |
gpgme_error_t err; |
gpgme_error_t err; |
179 |
|
GPGME *gpg = NULL; |
180 |
char *rawkey = NULL; |
char *rawkey = NULL; |
181 |
int keylen = 0; |
int keylen = 0; |
182 |
int rc; |
int rc; |
214 |
_("Keyserver"), MB_ERR); |
_("Keyserver"), MB_ERR); |
215 |
goto leave; |
goto leave; |
216 |
} |
} |
217 |
if (gpgme_new (&ctx)) |
gpg = new GPGME (); |
218 |
BUG (NULL); |
err = gpg->importFromBuffer (rawkey); |
|
gpgme_data_new_from_mem (&keydata, rawkey, strlen (rawkey), 1); |
|
|
err = gpgme_op_import (ctx, keydata); |
|
219 |
if (err) { |
if (err) { |
220 |
msg_box (dlg, gpgme_strerror (err), _("Import"), MB_ERR); |
msg_box (dlg, gpgme_strerror (err), _("Import"), MB_ERR); |
221 |
goto leave; |
goto leave; |
222 |
} |
} |
223 |
import_res = gpgme_op_import_result (ctx); |
import_res = gpg->importGetResult (); |
224 |
if (import_res && r_fpr) |
if (import_res && r_fpr) |
225 |
*r_fpr = m_strdup (import_res->imports->fpr); |
*r_fpr = m_strdup (import_res->imports->fpr); |
226 |
|
|
227 |
/* if we use the refresh mode, a lot of keys will be fetched and thus only |
/* if we use the refresh mode, a lot of keys will be fetched and thus only |
228 |
a summarize at the end is presented and not for each key. */ |
a summarize at the end is presented and not for each key. */ |
229 |
if (!(flags & KM_KS_REFRESH)) { |
if (import_res && !(flags & KM_KS_REFRESH)) { |
230 |
show_imported_keys (import_res); |
show_imported_keys (import_res); |
231 |
if (import_res && import_res->unchanged == import_res->considered) { |
if (import_res->unchanged == import_res->considered) { |
232 |
rc = WPTERR_GENERAL; /* no keys updated. */ |
rc = WPTERR_GENERAL; /* no keys updated. */ |
233 |
goto leave; |
goto leave; |
234 |
} |
} |
236 |
|
|
237 |
leave: |
leave: |
238 |
free_if_alloc (rawkey); |
free_if_alloc (rawkey); |
239 |
if (ctx != NULL) |
if (gpg != NULL) |
240 |
gpgme_release (ctx); |
delete gpg; |
|
if (keydata != NULL) |
|
|
gpgme_data_release (keydata); |
|
241 |
return rc; |
return rc; |
242 |
} |
} |
243 |
|
|
261 |
} |
} |
262 |
|
|
263 |
|
|
264 |
|
/* Utility function to fetch a key from the keyserver |
265 |
|
based on a given signature (@sig). |
266 |
|
Return 0 on success. */ |
267 |
|
int |
268 |
|
fetch_key_from_keyserver (HWND dlg, gpgme_signature_t sig) |
269 |
|
{ |
270 |
|
const char *keyid; |
271 |
|
int id; |
272 |
|
|
273 |
|
if (!sig->fpr) |
274 |
|
return FALSE; |
275 |
|
keyid = get_keyid_from_fpr (sig->fpr); |
276 |
|
id = log_box (_("Verify"), MB_INFO|MB_YESNO, |
277 |
|
_("Signature made %s using %s key ID 0x%s\n" |
278 |
|
"Cannot check signature: public key not found\n\n" |
279 |
|
"Do you want to try to retrieve the key from the keyserver?"), |
280 |
|
strtimestamp (sig->timestamp), |
281 |
|
get_key_pubalgo (sig->pubkey_algo), keyid); |
282 |
|
if (id == IDNO) { |
283 |
|
msg_box (dlg, get_gpg_sigstat (GPGME_SIGSUM_KEY_MISSING), |
284 |
|
_("Verify"), MB_WARN); |
285 |
|
return -1; |
286 |
|
} |
287 |
|
|
288 |
|
if (!hkp_recv_key (dlg, default_keyserver, default_keyserver_port, |
289 |
|
keyid, 0, 0)) { |
290 |
|
keycache_update (0, keyid); |
291 |
|
return 0; |
292 |
|
} |
293 |
|
return -1; |
294 |
|
} |
295 |
|
|
296 |
/* Check if the given pattern are either a valid |
/* Check if the given pattern are either a valid |
297 |
email address, a {long, short} keyid or a fingerprint. |
email address, a {long, short} keyid or a fingerprint. |
298 |
Return 0 on success. */ |
Return 0 on success. */ |
591 |
return FALSE; |
return FALSE; |
592 |
} |
} |
593 |
|
|
594 |
|
|
595 |
|
/* XXX: factor out the pattern code for generic use. */ |
596 |
|
|
597 |
|
/* Add search pattern @patt to the combo box if it is |
598 |
|
not already available in the list. */ |
599 |
|
static void |
600 |
|
add_pattern_to_combox (HWND dlg, int ctlid, const char *patt) |
601 |
|
{ |
602 |
|
int err; |
603 |
|
|
604 |
|
err = SendDlgItemMessage (dlg, ctlid, CB_FINDSTRINGEXACT, |
605 |
|
0, (LPARAM)(LPCSTR)patt); |
606 |
|
if (err != CB_ERR) |
607 |
|
return; |
608 |
|
SendDlgItemMessage (dlg, IDC_KEYSERVER_SEARCH, CB_ADDSTRING, |
609 |
|
0, (LPARAM)(LPCSTR)patt); |
610 |
|
} |
611 |
|
|
612 |
|
|
613 |
|
/* Restore saved pattern from the registry back to the combo box. |
614 |
|
If no pattern were saved, do nothing and just return. */ |
615 |
|
static void |
616 |
|
load_pattern_to_combox (HWND dlg, int ctlid, const char *rkey_name) |
617 |
|
{ |
618 |
|
char *p, *tok; |
619 |
|
|
620 |
|
p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", rkey_name); |
621 |
|
if (!p || strlen (p) < 2) { |
622 |
|
free_if_alloc (p); |
623 |
|
return; |
624 |
|
} |
625 |
|
tok = strtok (p, "$"); |
626 |
|
while (tok != NULL) { |
627 |
|
SendDlgItemMessage (dlg, ctlid, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)tok); |
628 |
|
tok = strtok (NULL, "$"); |
629 |
|
} |
630 |
|
free_if_alloc (p); |
631 |
|
} |
632 |
|
|
633 |
|
|
634 |
|
/* Save all entries from the combo box given in @ctlid to the |
635 |
|
registry. Concat all values and use '$' as a separator. */ |
636 |
|
static void |
637 |
|
save_pattern_from_combox (HWND dlg, int ctlid, const char *rkey_name) |
638 |
|
{ |
639 |
|
char *p, tmp[64]; |
640 |
|
int i; |
641 |
|
int n, len=0; |
642 |
|
|
643 |
|
n = SendDlgItemMessage (dlg, ctlid, CB_GETCOUNT, 0, 0); |
644 |
|
if (n == 0 || n == CB_ERR) |
645 |
|
return; |
646 |
|
/* it is very unlikely that the combox contain hundred of pattern |
647 |
|
but even so we limit the value to 16 which should be sufficient. */ |
648 |
|
if (n > 16) n = 16; |
649 |
|
for (i=0; i < n; i++) |
650 |
|
len += SendDlgItemMessage (dlg, ctlid, CB_GETLBTEXTLEN, |
651 |
|
(WPARAM)i, 0) + 1 + 1; |
652 |
|
p = new char[len+1]; |
653 |
|
memset (p, 0, len+1); |
654 |
|
for (i=0; i < n; i++) { |
655 |
|
memset (tmp, 0, sizeof (tmp)); |
656 |
|
SendDlgItemMessage (dlg, ctlid, CB_GETLBTEXT, |
657 |
|
(WPARAM)i, (LPARAM)tmp); |
658 |
|
strcat (p, tmp); |
659 |
|
strcat (p, "$"); |
660 |
|
} |
661 |
|
set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", rkey_name, p); |
662 |
|
free_if_alloc (p); |
663 |
|
} |
664 |
|
|
665 |
|
|
666 |
/* Dialog box procedure to access keyservers. */ |
/* Dialog box procedure to access keyservers. */ |
667 |
BOOL CALLBACK |
BOOL CALLBACK |
668 |
keyserver_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
keyserver_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
688 |
SetDlgItemText (dlg, IDC_KEYSERVER_DEFAULT, _("Set &default")); |
SetDlgItemText (dlg, IDC_KEYSERVER_DEFAULT, _("Set &default")); |
689 |
SetDlgItemText (dlg, IDCANCEL, _("&Close")); |
SetDlgItemText (dlg, IDCANCEL, _("&Close")); |
690 |
|
|
691 |
|
load_pattern_to_combox (dlg, IDC_KEYSERVER_SEARCH, "KSsearch"); |
692 |
keyserver_list_build (&lv, GetDlgItem (dlg, IDC_KEYSERVER_LIST)); |
keyserver_list_build (&lv, GetDlgItem (dlg, IDC_KEYSERVER_LIST)); |
693 |
set_default_ks (lv); |
set_default_ks (lv); |
694 |
center_window (dlg, NULL); |
center_window (dlg, NULL); |
734 |
lv = NULL; |
lv = NULL; |
735 |
} |
} |
736 |
lv_idx = 0; |
lv_idx = 0; |
737 |
|
save_pattern_from_combox (dlg, IDC_KEYSERVER_SEARCH, "KSsearch"); |
738 |
return FALSE; |
return FALSE; |
739 |
|
|
740 |
case WM_SYSCOMMAND: |
case WM_SYSCOMMAND: |
757 |
if (!GetDlgItemText (dlg, IDC_KEYSERVER_SEARCH, pattern, sizeof (pattern)-1)) { |
if (!GetDlgItemText (dlg, IDC_KEYSERVER_SEARCH, pattern, sizeof (pattern)-1)) { |
758 |
msg_box (dlg, _("Please enter the search pattern."), |
msg_box (dlg, _("Please enter the search pattern."), |
759 |
_("Keyserver"), MB_INFO); |
_("Keyserver"), MB_INFO); |
760 |
return FALSE; |
return TRUE; |
761 |
} |
} |
762 |
|
|
763 |
if (lv_idx != -1) { |
if (lv_idx != -1) { |
775 |
hkpsearch_dlg_proc, (LPARAM) &ksc); |
hkpsearch_dlg_proc, (LPARAM) &ksc); |
776 |
return TRUE; |
return TRUE; |
777 |
|
|
778 |
case IDC_KEYSERVER_RECV: |
case IDC_KEYSERVER_RECV: |
779 |
memset (&kserver, 0, sizeof (kserver)); |
memset (&kserver, 0, sizeof (kserver)); |
780 |
if (!lv_idx) { |
if (!lv_idx) { |
781 |
lv_idx = kserver_get_pos (lv); |
lv_idx = kserver_get_pos (lv); |
782 |
if (lv_idx == -1) { |
if (lv_idx == -1) { |
783 |
msg_box (dlg, _("Please select one of the keyservers."), |
msg_box (dlg, _("Please select one of the keyservers."), |
784 |
_("Keyserver"), MB_INFO); |
_("Keyserver"), MB_INFO); |
785 |
return FALSE; |
return TRUE; |
786 |
} |
} |
787 |
} |
} |
788 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
809 |
if (strchr (pattern, '@') || strchr (pattern, ' ')) { |
if (strchr (pattern, '@') || strchr (pattern, ' ')) { |
810 |
msg_box (dlg, _("Only enter the name of the user."), |
msg_box (dlg, _("Only enter the name of the user."), |
811 |
_("Keyserver"), MB_INFO); |
_("Keyserver"), MB_INFO); |
812 |
return FALSE; |
return TRUE; |
813 |
} |
} |
814 |
} |
} |
815 |
else if (check_pattern (pattern)) { |
else if (check_pattern (pattern)) { |
816 |
msg_box (dlg, _("Only email addresses or keyids are allowed."), |
msg_box (dlg, _("Only email addresses or keyids are allowed."), |
817 |
_("Keyserver"), MB_INFO); |
_("Keyserver"), MB_INFO); |
818 |
return FALSE; |
return TRUE; |
819 |
} |
} |
820 |
rc = keyserver_recv_key (dlg, kserver, kserver_get_port (lv), |
rc = keyserver_recv_key (dlg, kserver, kserver_get_port (lv), |
821 |
pattern, proto_nr, 0, |
pattern, proto_nr, 0, |
824 |
keycache_update (0, fpr); |
keycache_update (0, fpr); |
825 |
free_if_alloc (fpr); |
free_if_alloc (fpr); |
826 |
} |
} |
827 |
|
if (!rc) |
828 |
|
add_pattern_to_combox (dlg, IDC_KEYSERVER_SEARCH, pattern); |
829 |
return TRUE; |
return TRUE; |
830 |
|
|
831 |
case IDC_KEYSERVER_DEFAULT: |
case IDC_KEYSERVER_DEFAULT: |