1 |
/* wptCardDlg.cpp - Smart Card support |
/* wptCardDlg.cpp - Smart Card support |
2 |
* Copyright (C) 2003, 2004, 2005 Timo Schulz |
* Copyright (C) 2003, 2004, 2005 Timo Schulz |
3 |
|
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
6 |
* |
* |
24 |
#include <ctype.h> |
#include <ctype.h> |
25 |
|
|
26 |
#include "../resource.h" |
#include "../resource.h" |
27 |
|
#include "gpgme.h" |
28 |
#include "wptTypes.h" |
#include "wptTypes.h" |
29 |
#include "wptW32API.h" |
#include "wptW32API.h" |
30 |
#include "wptErrors.h" |
#include "wptErrors.h" |
34 |
#include "wptDlgs.h" |
#include "wptDlgs.h" |
35 |
#include "wptGPG.h" |
#include "wptGPG.h" |
36 |
#include "wptUTF8.h" |
#include "wptUTF8.h" |
37 |
|
#include "wptCardEdit.h" |
38 |
#include "wptCard.h" |
#include "wptCard.h" |
39 |
|
#include "wptContext.h" |
40 |
|
|
41 |
int keygen_check_date (SYSTEMTIME * st); |
int keygen_check_date (SYSTEMTIME * st); |
42 |
|
|
49 |
|
|
50 |
struct { |
struct { |
51 |
int ctlid; |
int ctlid; |
|
gpgme_attr_t attr; |
|
52 |
const char * err; |
const char * err; |
53 |
} attr_tab[] = { |
} attr_tab[] = { |
54 |
{IDC_CEDIT_AID, GPGME_ATTR_CARD_AID, ""}, |
{IDC_CEDIT_AID, ""}, |
55 |
{IDC_CEDIT_VENDOR, GPGME_ATTR_CARD_VENDOR, "No Vendor"}, |
{IDC_CEDIT_VENDOR, "No Vendor"}, |
56 |
{IDC_CEDIT_VERSION,GPGME_ATTR_CARD_VER, "No Version"}, |
{IDC_CEDIT_VERSION,"No Version"}, |
57 |
{IDC_CEDIT_SERIAL, GPGME_ATTR_CARD_SERIAL, "No Serial-No"}, |
{IDC_CEDIT_SERIAL, "No Serial-No"}, |
58 |
{IDC_CEDIT_NAME, GPGME_ATTR_CARD_NAME, "No Name"}, |
{IDC_CEDIT_NAME, "No Name"}, |
59 |
{IDC_CEDIT_NAME2, GPGME_ATTR_CARD_NAME2, "No Surname"}, |
{IDC_CEDIT_NAME2, "No Surname"}, |
60 |
{IDC_CEDIT_KEYURL, GPGME_ATTR_CARD_URL, "No Key-URL"}, |
{IDC_CEDIT_KEYURL, "No Key-URL"}, |
61 |
{IDC_CEDIT_LOGIN, GPGME_ATTR_CARD_LOGIN, "No Login name"}, |
{IDC_CEDIT_LOGIN, "No Login name"}, |
62 |
{0}, |
{0}, |
63 |
}; |
}; |
64 |
|
|
65 |
|
|
66 |
#define card_get_string(card, what) \ |
|
67 |
gpgme_card_get_string_attr ((card), (what), NULL, 0) |
/* XXX: simplify code. */ |
68 |
#define card_get_ulong(card, what) \ |
char* |
69 |
gpgme_card_get_ulong_attr ((card), (what), NULL, 0) |
get_printable_version (const char *version) |
70 |
#define card_get_fpr(card, idx) \ |
{ |
71 |
gpgme_card_get_string_attr ((card), GPGME_ATTR_CARD_FPR, NULL, (idx)-1) |
static char buf[16]; |
72 |
|
char tmp_maj[3]={0}, tmp_min[3]={0}; |
73 |
|
|
74 |
|
strncpy (tmp_maj, version, 2); |
75 |
|
strncpy (tmp_min, version+2, 2); |
76 |
|
sprintf (buf, "%d.%d", atoi (tmp_maj), atoi (tmp_min)); |
77 |
|
return buf; |
78 |
|
} |
79 |
|
|
80 |
|
/* Return all card attributes from @card. @n contains |
81 |
|
the number of items which were returned. */ |
82 |
|
char** |
83 |
|
card_get_items (gpg_card_t card, int *n) |
84 |
|
{ |
85 |
|
char **p; |
86 |
|
|
87 |
|
*n= 8; |
88 |
|
p = (char **)calloc (*n+1, sizeof (char*)); |
89 |
|
if (!p) |
90 |
|
BUG (0); |
91 |
|
p[0] = card->aid; |
92 |
|
p[1] = card->vendor; |
93 |
|
p[2] = get_printable_version (card->version); |
94 |
|
p[3] = card->serial; |
95 |
|
p[4] = card->givenname; |
96 |
|
p[5] = card->surname; |
97 |
|
p[6] = card->url; |
98 |
|
p[7] = card->login; |
99 |
|
return p; |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
static int |
static int |
104 |
idx_from_lang( const char * _lang ) |
idx_from_lang (const char * _lang) |
105 |
{ |
{ |
106 |
const char * s; |
const char * s; |
107 |
int i; |
int i; |
109 |
if (!_lang) |
if (!_lang) |
110 |
return 0; |
return 0; |
111 |
for (i=0; (s = lang[i]); i++) { |
for (i=0; (s = lang[i]); i++) { |
112 |
if( !strcmp( _lang, s ) ) |
if (!strcmp (_lang, s )) |
113 |
return i; |
return i; |
114 |
} |
} |
115 |
return 0; |
return 0; |
116 |
} /* idx_from_lang */ |
} |
117 |
|
|
118 |
|
|
119 |
|
#if 0 /* @unused@ */ |
120 |
int |
int |
121 |
show_card_status (void) |
show_card_status (void) |
122 |
{ |
{ |
134 |
} |
} |
135 |
return 0; |
return 0; |
136 |
} /* show_card_status */ |
} /* show_card_status */ |
137 |
|
#endif |
138 |
|
|
139 |
|
/* Check if there is a card in the reader and analyze the |
140 |
gpgme_card_t |
returned information. |
141 |
gpg_load_scard (void) |
Return value: card context or NULL on error. */ |
142 |
|
gpg_card_t |
143 |
|
gpg_card_load (void) |
144 |
{ |
{ |
145 |
gpgme_error_t rc; |
gpgme_error_t err; |
146 |
gpgme_card_t card = NULL; |
GpgCardEdit *ce; |
147 |
gpgme_editcard_t ec; |
gpg_card_t card = NULL; |
|
gpgme_ctx_t ctx; |
|
|
const char * s; |
|
148 |
struct card_cb_s cb = {0}; |
struct card_cb_s cb = {0}; |
149 |
|
|
150 |
rc = gpgme_new (&ctx); |
ce = new GpgCardEdit (); |
151 |
if (!rc) |
if (!ce) |
|
rc = gpgme_editcard_new (&ec); |
|
|
if (rc) |
|
152 |
BUG (0); |
BUG (0); |
153 |
gpgme_enable_logging (ctx); |
memset (&cb, 0, sizeof (cb)); |
154 |
gpgme_editcard_set_callback (ec, card_callback, &cb); |
ce->setCallback (card_callback, &cb); |
155 |
gpgme_set_edit_ctx (ctx, ec, 0); |
err = ce->getCardStatus (&card); |
156 |
rc = gpgme_op_statuscard (ctx, &card); |
if (err) { |
157 |
if (rc) { |
msg_box (NULL, gpgme_strerror (err), _("Card Manager"), MB_ERR); |
|
gpgme_show_error (NULL, rc, ctx, _("Card Manager"), MB_ERR); |
|
158 |
goto leave; |
goto leave; |
159 |
} |
} |
160 |
s = gpgme_card_get_string_attr (card, GPGME_ATTR_CARD_AID, NULL, 0); |
|
161 |
if (!s || strncmp (s, "D276000124", 10)) { |
if (!card->aid || strncmp (card->aid, "D276000124", 10)) { |
162 |
msg_box (NULL, winpt_strerror (WPTERR_NOPGPCARD), "WinPT", MB_ERR); |
msg_box (NULL, winpt_strerror (WPTERR_NOPGPCARD), "WinPT", MB_ERR); |
163 |
gpgme_card_release (card); |
gpg_card_release (card); |
164 |
card = NULL; |
card = NULL; |
165 |
} |
} |
166 |
|
else { |
167 |
|
struct winpt_key_s key; |
168 |
|
memset (&key, 0, sizeof (key)); |
169 |
|
winpt_get_pubkey (card->fpr[1]+32, &key); |
170 |
|
if (key.ext) { |
171 |
|
key.ext->card_type = strdup (card->card_type); |
172 |
|
if (!key.ext->card_type) |
173 |
|
BUG (NULL); |
174 |
|
/* memory will be released in gpg_keycache_release (). */ |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
leave: |
leave: |
179 |
gpgme_editcard_release (ec); |
delete ce; |
|
gpgme_release (ctx); |
|
180 |
return card; |
return card; |
181 |
} /* gpg_load_scard */ |
} |
182 |
|
|
183 |
|
|
184 |
|
/* Print human friendly fingerprint to control @id in the |
185 |
|
dialog @dlg. @fpr contains the raw fingerprint. */ |
186 |
static void |
static void |
187 |
print_fpr (HWND dlg, int id, const char * fpr) |
print_fpr (HWND dlg, int id, const char * fpr) |
188 |
{ |
{ |
190 |
size_t i, c; |
size_t i, c; |
191 |
|
|
192 |
if (!fpr) |
if (!fpr) |
193 |
strcpy( buf, _("No Fingerprint") ); |
strcpy (buf, _("No Fingerprint")); |
194 |
else { |
else { |
195 |
memset( buf, 0, sizeof (buf) ); |
memset (buf, 0, sizeof (buf)); |
196 |
for( i=0, c=0; i < strlen( fpr ); i++ ) { |
for( i=0, c=0; i < strlen (fpr); i++) { |
197 |
dig[0] = fpr[i]; dig[1] = 0; |
dig[0] = fpr[i]; dig[1] = 0; |
198 |
strcat( buf, dig ); |
strcat (buf, dig); |
199 |
if( ++c == 4 ) { |
if (++c == 4) { |
200 |
strcat( buf, " " ); |
strcat (buf, " "); |
201 |
c=0; |
c=0; |
202 |
} |
} |
203 |
} |
} |
204 |
} |
} |
205 |
SetDlgItemText( dlg, id, buf ); |
SetDlgItemText (dlg, id, buf); |
206 |
} /* print_fpr */ |
} |
207 |
|
|
208 |
|
|
209 |
|
/* Fill in all card information from @card. into the corresponding |
210 |
|
dialog item fields in the dialog @dlg. |
211 |
|
Return value: 0 on success. */ |
212 |
static int |
static int |
213 |
card_status( HWND dlg, gpgme_card_t card ) |
card_status (HWND dlg, gpg_card_t card) |
214 |
{ |
{ |
215 |
static int fprbuf[] = {0, IDC_CEDIT_FPR1, IDC_CEDIT_FPR2, IDC_CEDIT_FPR3, 0}; |
static int fprbuf[] = {IDC_CEDIT_FPR1, IDC_CEDIT_FPR2, IDC_CEDIT_FPR3, 0}; |
216 |
const char * s; |
static int fprtime[] = {IDC_CEDIT_SIG_FPRTIME, IDC_CEDIT_DEC_FPRTIME, IDC_CEDIT_AUTH_FPRTIME, 0}; |
217 |
int idx=0; |
const char *s; |
218 |
u32 t; |
char **attrs; |
219 |
|
char cardinf[128]; |
220 |
|
int idx=0, n=0; |
221 |
|
|
222 |
s = card_get_string( card, GPGME_ATTR_CARD_AID ); |
if (!card->aid) { |
|
if( !s ) { |
|
223 |
msg_box( dlg, _("No OpenPGP smart card detected."), "WinPT", MB_ERR ); |
msg_box( dlg, _("No OpenPGP smart card detected."), "WinPT", MB_ERR ); |
224 |
return -1; |
return -1; |
225 |
} |
} |
226 |
SetDlgItemText( dlg, IDC_CEDIT_AID, s ); |
SetDlgItemText (dlg, IDC_CEDIT_AID, card->aid); |
227 |
|
SetDlgItemInt (dlg, IDC_CEDIT_SIGCOUNT, card->sig_count, TRUE); |
228 |
|
|
229 |
t = card_get_ulong( card, GPGME_ATTR_CARD_SIGCOUNT ); |
for (idx=0; fprbuf[idx]; idx++) { |
230 |
SetDlgItemInt( dlg, IDC_CEDIT_SIGCOUNT, t, TRUE ); |
print_fpr (dlg, fprbuf[idx], card->fpr[idx]); |
231 |
|
SetDlgItemText (dlg, fprtime[idx], card->fpr_created_str[idx]); |
|
for( idx=1; fprbuf[idx]; idx++ ) { |
|
|
s = card_get_fpr( card, idx ); |
|
|
print_fpr( dlg, fprbuf[idx], s ); |
|
232 |
} |
} |
233 |
|
|
234 |
for( idx=1; attr_tab[idx].attr; idx++ ) { |
attrs = card_get_items (card, &n); |
235 |
s = card_get_string( card, attr_tab[idx].attr ); |
for (idx=1; attr_tab[idx].ctlid; idx++) { |
236 |
SetDlgItemText( dlg, attr_tab[idx].ctlid, s && *s? s : attr_tab[idx].err ); |
s = attrs[idx]; |
237 |
|
SetDlgItemText (dlg, attr_tab[idx].ctlid, s && *s? s : attr_tab[idx].err); |
238 |
} |
} |
239 |
|
free (attrs); |
240 |
|
|
241 |
s = card_get_string( card, GPGME_ATTR_CARD_LANG ); |
idx = idx_from_lang (card->lang); |
242 |
idx = idx_from_lang( s ); |
SendDlgItemMessage (dlg, IDC_CEDIT_LANG, CB_SETCURSEL, (WPARAM)idx, 0); |
|
SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_SETCURSEL, (WPARAM)idx, 0 ); |
|
243 |
|
|
244 |
t = card_get_ulong( card, GPGME_ATTR_CARD_SEX ); |
switch (card->sex) { |
|
switch( t ) { |
|
245 |
case 'm': idx=0; break; |
case 'm': idx=0; break; |
246 |
case 'f': idx=1; break; |
case 'f': idx=1; break; |
247 |
default : |
default : |
248 |
case 'u': idx=2; break; |
case 'u': idx=2; break; |
249 |
} |
} |
250 |
SendDlgItemMessage( dlg, IDC_CEDIT_SEX, CB_SETCURSEL, (WPARAM)idx, 0 ); |
SendDlgItemMessage (dlg, IDC_CEDIT_SEX, CB_SETCURSEL, (WPARAM)idx, 0); |
251 |
|
|
252 |
|
s = card->serial; |
253 |
|
while (s && *s == '0') s++; |
254 |
|
_snprintf (cardinf, sizeof (cardinf)-1, |
255 |
|
"Card Edit - %s serial no. %s version %s", |
256 |
|
card->card_type, s, get_printable_version (card->version)); |
257 |
|
SetWindowText (dlg, cardinf); |
258 |
|
|
259 |
return 0; |
return 0; |
260 |
} /* card_status */ |
} |
261 |
|
|
262 |
|
|
263 |
|
/* Initialize the enum combox boxes in dialog @dlg. */ |
264 |
static void |
static void |
265 |
prepare_dialog( HWND dlg ) |
prepare_dialog (HWND dlg) |
266 |
{ |
{ |
267 |
const char * s; |
const char * s; |
268 |
int i; |
int i; |
269 |
|
|
270 |
for( i=0; (s = sex[i]); i++ ) |
for (i=0; (s = sex[i]); i++) |
271 |
SendDlgItemMessage( dlg, IDC_CEDIT_SEX, CB_ADDSTRING, 0, (LPARAM) s ); |
SendDlgItemMessage (dlg, IDC_CEDIT_SEX, CB_ADDSTRING, 0, (LPARAM) s); |
272 |
SendDlgItemMessage( dlg, IDC_CEDIT_SEX, CB_SETCURSEL, 0, 0 ); |
SendDlgItemMessage (dlg, IDC_CEDIT_SEX, CB_SETCURSEL, 0, 0); |
273 |
for( i=0; (s = lang[i]); i++ ) |
for (i=0; (s = lang[i]); i++) |
274 |
SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_ADDSTRING, 0, (LPARAM)s ); |
SendDlgItemMessage (dlg, IDC_CEDIT_LANG, CB_ADDSTRING, 0, (LPARAM)s); |
275 |
SendDlgItemMessage( dlg, IDC_CEDIT_LANG, CB_SETCURSEL, 0, 0 ); |
SendDlgItemMessage (dlg, IDC_CEDIT_LANG, CB_SETCURSEL, 0, 0); |
276 |
} /* prepare_dialog */ |
} |
277 |
|
|
278 |
|
|
279 |
|
/* Return 0 if the given string @str has the proper format. */ |
280 |
static int |
static int |
281 |
check_string( const char * str, int flags ) |
check_string (const char *str, int flags) |
282 |
{ |
{ |
283 |
size_t i; |
size_t i; |
284 |
for( i=0; i < strlen( str ); i++ ) { |
for (i=0; i < strlen (str); i++) { |
285 |
if( flags & 0x02 && !isalpha( str[i] ) ) |
if (flags & 0x02 && !isalpha (str[i])) |
286 |
return -1; |
return -1; |
287 |
} |
} |
288 |
return 0; |
return 0; |
290 |
|
|
291 |
|
|
292 |
static int |
static int |
293 |
do_proc_card_cmds( HWND dlg, struct pin_cb_ctx_s * pincb, gpgme_card_t card ) |
do_proc_card_cmds (HWND dlg, struct pin_cb_ctx_s *pincb, gpg_card_t card) |
294 |
{ |
{ |
295 |
static struct { |
static struct { |
296 |
int id; |
int id; |
298 |
int us_ascii; |
int us_ascii; |
299 |
int changed; |
int changed; |
300 |
} idctl[] = { |
} idctl[] = { |
301 |
{IDC_CEDIT_NAME, GPGME_EDITCARD_NAME, 1, 0}, |
{IDC_CEDIT_NAME, GPG_EDITCARD_NAME, 1, 0}, |
302 |
{IDC_CEDIT_LANG2, GPGME_EDITCARD_LANG, 1, 0}, |
{IDC_CEDIT_LANG2, GPG_EDITCARD_LANG, 1, 0}, |
303 |
{IDC_CEDIT_SEX2, GPGME_EDITCARD_SEX, 1|1,0}, |
{IDC_CEDIT_SEX2, GPG_EDITCARD_SEX, 1|1,0}, |
304 |
{IDC_CEDIT_KEYURL,GPGME_EDITCARD_KEYURL,1, 0}, |
{IDC_CEDIT_KEYURL,GPG_EDITCARD_KEYURL,1|4,0}, |
305 |
{IDC_CEDIT_LOGIN, GPGME_EDITCARD_LOGIN, 1, 0}, |
{IDC_CEDIT_LOGIN, GPG_EDITCARD_LOGIN, 1, 0}, |
306 |
{0} |
{0} |
307 |
}; |
}; |
308 |
gpgme_editcard_t ec; |
gpgme_error_t err; |
309 |
gpgme_ctx_t ctx; |
GpgCardEdit *ce; |
|
gpgme_error_t rc; |
|
310 |
char buf[256], tmp[128]; |
char buf[256], tmp[128]; |
311 |
int errc=0; |
int errc=0, use_arg2 = 0; |
312 |
int i, id, n=0; |
int i, id, n=0; |
313 |
|
|
314 |
/* XXX rewrite the entire function */ |
/* XXX rewrite the entire function */ |
334 |
n++; |
n++; |
335 |
} |
} |
336 |
} |
} |
337 |
if( !pincb || !card ) /* just return the changed elements */ |
if (!pincb || !card) /* just return the changed elements */ |
338 |
return n; |
return n; |
339 |
if( !n ) |
if (!n) |
340 |
|
return 0; |
341 |
|
if (!pincb->apin) { |
342 |
|
msg_box (dlg, _("No PINs found."), _("Card Edit"), MB_ERR); |
343 |
return 0; |
return 0; |
344 |
|
} |
345 |
|
|
346 |
rc = gpgme_editcard_new( &ec ); |
ce = new GpgCardEdit (); |
347 |
if( !rc ) |
if (!ce) |
348 |
rc = gpgme_new( &ctx ); |
BUG (NULL); |
349 |
if( rc ) |
ce->setAdminPIN (pincb->apin); |
350 |
BUG( NULL ); |
/*ce->setPIN (pincb->upin);*/ |
|
gpgme_editcard_control( ec, GPGME_EDITCARD_APIN, pincb->apin ); |
|
|
gpgme_editcard_control( ec, GPGME_EDITCARD_UPIN, pincb->upin ); |
|
351 |
for( i=0; idctl[i].id; i++ ) { |
for( i=0; idctl[i].id; i++ ) { |
352 |
if( idctl[i].changed ) { |
if( idctl[i].changed ) { |
353 |
GetDlgItemText( dlg, idctl[i].id, buf, sizeof (buf)-1 ); |
GetDlgItemText( dlg, idctl[i].id, buf, sizeof (buf)-1 ); |
361 |
_("Card Edit"), MB_ERR ); |
_("Card Edit"), MB_ERR ); |
362 |
errc--; continue; |
errc--; continue; |
363 |
} |
} |
364 |
if( idctl[i].cmd == GPGME_EDITCARD_NAME ) { |
if ((idctl[i].us_ascii & 4) && |
365 |
|
(!strchr (buf, ':') || !strstr (buf, "//"))) { |
366 |
|
/* XXX: better URL check. */ |
367 |
|
msg_box (dlg, _("Invalid URL."), _("Card Edit"), MB_ERR); |
368 |
|
errc--; continue; |
369 |
|
} |
370 |
|
if( idctl[i].cmd == GPG_EDITCARD_NAME ) { |
371 |
/* The "name" command actually needs two fields */ |
/* The "name" command actually needs two fields */ |
372 |
GetDlgItemText( dlg, IDC_CEDIT_NAME2, tmp, sizeof tmp-1 ); |
GetDlgItemText( dlg, IDC_CEDIT_NAME2, tmp, sizeof tmp-1 ); |
373 |
gpgme_editcard_control( ec, GPGME_EDITCARD_NAME2, tmp ); |
use_arg2 = 1; |
374 |
} |
} |
375 |
gpgme_editcard_control( ec, idctl[i].cmd, buf ); |
else |
376 |
gpgme_set_edit_ctx( ctx, ec, idctl[i].cmd ); |
use_arg2 = 0; |
377 |
rc = gpgme_op_editcard( ctx ); |
err = ce->doCmd (idctl[i].cmd, buf, use_arg2? tmp : NULL); |
378 |
if( rc ) { |
if (err) { |
379 |
msg_box( dlg, _("Could not modify card attribute."), |
log_box (_("Card Edit"), MB_ERR, |
380 |
_("Card Edit"), MB_ERR ); |
_("Could not modify card attribute: %s"), |
381 |
|
gpgme_strerror (err)); |
382 |
errc--; |
errc--; |
383 |
|
/* If no card is inserted, we leave the loop. */ |
384 |
|
if (gpgme_err_code (err) == GPG_ERR_CARD_NOT_PRESENT) |
385 |
|
break; |
386 |
} |
} |
387 |
} |
} |
388 |
} |
} |
389 |
if( !errc ) { |
if (!errc) { |
390 |
/* if the operation(s) succeeded, reset the modify flag for each control */ |
/* if the operation(s) succeeded, reset the modify flag for each control */ |
391 |
for( i = 0; idctl[i].id; i++ ) |
for( i = 0; idctl[i].id; i++ ) |
392 |
SendDlgItemMessage( dlg, idctl[i].id, EM_SETMODIFY, (WPARAM)(UINT)FALSE, 0 ); |
SendDlgItemMessage( dlg, idctl[i].id, EM_SETMODIFY, (WPARAM)(UINT)FALSE, 0 ); |
394 |
SetDlgItemText( dlg, IDC_CEDIT_LANG2, "" ); |
SetDlgItemText( dlg, IDC_CEDIT_LANG2, "" ); |
395 |
SetDlgItemText( dlg, IDC_CEDIT_SEX2, "" ); |
SetDlgItemText( dlg, IDC_CEDIT_SEX2, "" ); |
396 |
} |
} |
397 |
gpgme_editcard_release( ec ); |
delete ce; |
|
gpgme_release( ctx ); |
|
398 |
return errc; |
return errc; |
399 |
} /* do_proc_card_cmds */ |
} /* do_proc_card_cmds */ |
400 |
|
|
401 |
|
|
402 |
|
/* Cleanup pin callback @ctx. */ |
403 |
void |
void |
404 |
free_pincb (struct pin_cb_ctx_s * ctx) |
free_pincb (struct pin_cb_ctx_s *ctx) |
405 |
{ |
{ |
406 |
if (!ctx) |
if (!ctx) |
407 |
return; |
return; |
408 |
free_if_alloc (ctx->info_text); |
free_if_alloc (ctx->info_text); |
409 |
free_if_alloc (ctx->upin); |
sfree_if_alloc (ctx->upin); |
410 |
free_if_alloc (ctx->apin); |
sfree_if_alloc (ctx->apin); |
411 |
} /* free_pincb */ |
} |
412 |
|
|
413 |
|
|
414 |
|
/* Request a PIN from the user. @which decided if the |
415 |
|
normal PIN or the admin PIN will be requested. |
416 |
|
@card is used to show some information to the user. |
417 |
|
@pincb is the actuall callback context. |
418 |
|
Return value: 0 on success. */ |
419 |
static int |
static int |
420 |
do_askpin( HWND dlg, gpgme_edit_card_t which, gpgme_card_t card, |
do_askpin (HWND dlg, int which, gpg_card_t card, |
421 |
struct pin_cb_ctx_s * pincb ) |
struct pin_cb_ctx_s *pincb) |
422 |
{ |
{ |
423 |
const char * s, * fmt; |
const char * s, * fmt; |
424 |
const char * n1, * n2, * serial; |
const char * n1, * n2, * serial; |
425 |
char * p; |
char * p; |
426 |
size_t n; |
size_t n; |
427 |
|
|
428 |
if( (which == GPGME_EDITCARD_CHAPIN && pincb->apin) || |
if( (which == CARD_ADMIN_PIN && pincb->apin) || |
429 |
(which == GPGME_EDITCARD_CHUPIN && pincb->upin) ) |
(which == CARD_USER_PIN && pincb->upin) ) |
430 |
return 0; |
return 0; |
431 |
|
|
432 |
if( which == GPGME_EDITCARD_CHAPIN ) |
if (which == CARD_ADMIN_PIN) |
433 |
s = _("Please enter the 'Admin PIN'"); |
s = _("Please enter the 'Admin PIN'"); |
434 |
else if( which == GPGME_EDITCARD_CHUPIN ) |
else if (which == CARD_USER_PIN) |
435 |
s = _("Please enter the 'User PIN'"); |
s = _("Please enter the 'User PIN'"); |
436 |
else |
else |
437 |
s = _("Please enter the PIN"); |
s = _("Please enter the PIN"); |
439 |
free_if_alloc( pincb->info_text ); |
free_if_alloc( pincb->info_text ); |
440 |
if( card ) { |
if( card ) { |
441 |
fmt = _("%s\nName: %s %s\nSerial-No: %s\n"); |
fmt = _("%s\nName: %s %s\nSerial-No: %s\n"); |
442 |
n1 = card_get_string( card, GPGME_ATTR_CARD_NAME ); |
n1 = card->givenname; |
443 |
n2 = card_get_string( card, GPGME_ATTR_CARD_NAME2 ); |
n2 = card->surname; |
444 |
if( !n1 || !n2 ) { |
if( !n1 || !n2 ) { |
445 |
n1 = "No"; n2 = "Name"; |
n1 = "No"; n2 = "Name"; |
446 |
} |
} |
447 |
serial = card_get_string( card, GPGME_ATTR_CARD_SERIAL ); |
serial = card->serial; |
448 |
if( !serial ) |
if (!serial) |
449 |
serial = "No Serial"; |
serial = "No Serial ID"; |
450 |
n = strlen( n1 ) + strlen( n2 ) + strlen( fmt ) + strlen( serial ) + 3; |
n = strlen( n1 ) + strlen( n2 ) + strlen( fmt ) + strlen( serial ) + 3; |
451 |
p = pincb->info_text = new char[strlen( s )+n+1 ]; |
p = pincb->info_text = new char[strlen( s )+n+1 ]; |
452 |
if( !p ) |
if( !p ) |
459 |
BUG (0); |
BUG (0); |
460 |
} |
} |
461 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PIN, dlg, |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PIN, dlg, |
462 |
pin_cb_dlg_proc, (LPARAM)pincb); |
pin_cb_dlg_proc, (LPARAM)pincb); |
463 |
if( !pincb->apin && !pincb->upin) { |
if (!pincb->apin && !pincb->upin) { |
464 |
safe_free (pincb->info_text); |
safe_free (pincb->info_text); |
465 |
return -1; |
return -1; |
466 |
} |
} |
467 |
return 0; |
return 0; |
468 |
} /* do_askpin */ |
} |
469 |
|
|
470 |
|
|
471 |
|
/* Dialog box procedure for card edit. */ |
472 |
BOOL CALLBACK |
BOOL CALLBACK |
473 |
card_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
card_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
474 |
{ |
{ |
475 |
static gpgme_card_t card; |
static gpg_card_t card; |
476 |
char tmp[128]; |
char tmp[128]; |
477 |
size_t n=0; |
size_t n=0; |
478 |
|
|
479 |
switch (msg) { |
switch (msg) { |
480 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
481 |
card = (gpgme_card_t)lparam; |
card = (gpg_card_t)lparam; |
482 |
if (!card) |
if (!card) |
483 |
BUG (0); |
BUG (0); |
484 |
prepare_dialog (dlg); |
prepare_dialog (dlg); |
485 |
if (card_status (dlg, card )) |
if (card_status (dlg, card )) |
486 |
EndDialog (dlg, TRUE); |
EndDialog (dlg, TRUE); |
487 |
center_window (dlg); |
center_window (dlg, NULL); |
488 |
SetForegroundWindow (dlg); |
SetForegroundWindow (dlg); |
489 |
return TRUE; |
return TRUE; |
490 |
|
|
491 |
case WM_DESTROY: |
case WM_DESTROY: |
492 |
free_if_alloc (pincb.info_text); |
free_pincb (&pincb); |
|
free_if_alloc (pincb.apin); |
|
|
free_if_alloc (pincb.upin); |
|
493 |
memset (&pincb, 0, sizeof pincb); |
memset (&pincb, 0, sizeof pincb); |
494 |
break; |
break; |
495 |
|
|
505 |
case IDC_CEDIT_LANG: dstid = IDC_CEDIT_LANG2; break; |
case IDC_CEDIT_LANG: dstid = IDC_CEDIT_LANG2; break; |
506 |
case IDC_CEDIT_SEX: dstid = IDC_CEDIT_SEX2; break; |
case IDC_CEDIT_SEX: dstid = IDC_CEDIT_SEX2; break; |
507 |
} |
} |
508 |
GetDlgItemText (dlg, ctlid, tmp, 127); |
GetDlgItemText (dlg, ctlid, tmp, sizeof (tmp)-1); |
509 |
SetDlgItemText (dlg, dstid, tmp); |
SetDlgItemText (dlg, dstid, tmp); |
510 |
break; |
break; |
511 |
} |
} |
512 |
switch( LOWORD( wparam ) ) { |
switch (LOWORD (wparam)) { |
513 |
case IDC_CEDIT_CHPIN: |
case IDC_CEDIT_CHPIN: |
514 |
DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_CARD_CHPIN, dlg, |
DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_CARD_CHPIN, dlg, |
515 |
card_changepin_dlg_proc, NULL ); |
card_changepin_dlg_proc, NULL ); |
521 |
_("This operation will override the keys on the card.\n" |
_("This operation will override the keys on the card.\n" |
522 |
"Still proceed?"), _("Card Edit"), MB_WARN|MB_YESNO); |
"Still proceed?"), _("Card Edit"), MB_WARN|MB_YESNO); |
523 |
if (id == IDNO) |
if (id == IDNO) |
524 |
return FALSE; |
return TRUE; |
525 |
} |
} |
526 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_KEYGEN, |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_KEYGEN, |
527 |
glob_hwnd, card_keygen_dlg_proc, NULL); |
glob_hwnd, card_keygen_dlg_proc, NULL); |
528 |
break; |
break; |
529 |
|
|
530 |
case IDOK: |
case IDOK: |
531 |
n = do_proc_card_cmds( dlg, NULL, NULL ); |
n = do_proc_card_cmds (dlg, NULL, NULL); |
532 |
if( n ) { |
if (n) { |
533 |
if( do_askpin( dlg, GPGME_EDITCARD_CHAPIN, card, &pincb ) ) |
if (do_askpin (dlg, CARD_ADMIN_PIN, card, &pincb)) |
534 |
EndDialog( dlg, FALSE ); |
EndDialog (dlg, FALSE); |
535 |
if( do_askpin( dlg, GPGME_EDITCARD_CHUPIN, card, &pincb ) ) |
} |
536 |
EndDialog( dlg, FALSE ); |
do_proc_card_cmds (dlg, &pincb, card); |
537 |
} |
free_pincb (&pincb); |
538 |
do_proc_card_cmds( dlg, &pincb, card ); |
if (!n) |
539 |
free_pincb( &pincb ); |
EndDialog (dlg, TRUE); |
|
if( !n ) |
|
|
EndDialog( dlg, TRUE ); |
|
540 |
break; |
break; |
541 |
|
|
542 |
case IDCANCEL: |
case IDCANCEL: |
547 |
} |
} |
548 |
|
|
549 |
return FALSE; |
return FALSE; |
550 |
} /* card_edit_dlg_proc */ |
} |
|
|
|
551 |
|
|
552 |
|
|
553 |
static int /* fixme: works only roughly */ |
static int /* fixme: works only roughly */ |
569 |
} |
} |
570 |
|
|
571 |
|
|
572 |
static void |
/* Dialog box procedure for the key generation on cards. */ |
|
keygen_fill_algbox (HWND dlg) |
|
|
{ |
|
|
SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_ADDSTRING, 0, (LPARAM)(const char*)"RSA"); |
|
|
SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_SETCURSEL, 0, 0); |
|
|
} |
|
|
|
|
|
|
|
573 |
BOOL CALLBACK |
BOOL CALLBACK |
574 |
card_keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
card_keygen_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
575 |
{ |
{ |
|
static int state = 0; |
|
|
static int pwd_state = 0; |
|
|
gpgme_ctx_t ctx; |
|
|
gpgme_editcard_t crd; |
|
576 |
gpgme_error_t err; |
gpgme_error_t err; |
577 |
char name[128], email[128], comment[128], expdate[64]; |
GpgCardEdit *ce; |
578 |
|
char name[128], email[128], comment[128]; |
579 |
char pass[128]; |
char pass[128]; |
580 |
int card_flags = GPGME_CARDFLAG_NONE; |
int card_flags = GPG_CARDFLAG_NONE; |
581 |
int expires=0; |
int expires=0, valid; |
582 |
size_t n; |
size_t n; |
583 |
|
|
584 |
switch (msg) { |
switch (msg) { |
585 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
586 |
state = 0; |
center_window (dlg, NULL); |
|
pwd_state = 1; |
|
|
center_window (dlg); |
|
587 |
CheckDlgButton (dlg, IDC_CKEYGEN_REPLACE, BST_CHECKED); |
CheckDlgButton (dlg, IDC_CKEYGEN_REPLACE, BST_CHECKED); |
588 |
CheckDlgButton (dlg, IDC_CKEYGEN_NEVER, BST_CHECKED); |
CheckDlgButton (dlg, IDC_CKEYGEN_NEVER, BST_CHECKED); |
589 |
CheckDlgButton (dlg, IDC_CKEYGEN_BACKUP, BST_CHECKED); |
CheckDlgButton (dlg, IDC_CKEYGEN_BACKUP, BST_CHECKED); |
590 |
EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), FALSE); |
EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), FALSE); |
591 |
keygen_fill_algbox (dlg); |
SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_ADDSTRING, 0, |
592 |
|
(LPARAM)(const char*)"RSA"); |
593 |
|
SendDlgItemMessage (dlg, IDC_CKEYGEN_ALG, CB_SETCURSEL, 0, 0); |
594 |
SetFocus (GetDlgItem (dlg, IDC_CKEYGEN_NAME)); |
SetFocus (GetDlgItem (dlg, IDC_CKEYGEN_NAME)); |
595 |
SetForegroundWindow (dlg); |
SetForegroundWindow (dlg); |
596 |
return FALSE; |
return FALSE; |
597 |
|
|
598 |
|
case WM_SYSCOMMAND: |
599 |
|
if (LOWORD (wparam) == SC_CLOSE) |
600 |
|
EndDialog (dlg, TRUE); |
601 |
|
return FALSE; |
602 |
|
|
603 |
case WM_COMMAND: |
case WM_COMMAND: |
604 |
if (HIWORD (wparam) == BN_CLICKED) { |
if (HIWORD (wparam) == BN_CLICKED && |
605 |
switch (LOWORD (wparam)) { |
(LOWORD (wparam) == IDC_CKEYGEN_BACKUP) || |
606 |
case IDC_CKEYGEN_NEVER: |
LOWORD (wparam) == IDC_CKEYGEN_NEVER) { |
607 |
state ^= 1; |
EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), |
608 |
EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_VALID), state); |
IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER)? 0: 1); |
609 |
break; |
EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_PASS), |
610 |
|
IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP)? 1 : 0); |
611 |
case IDC_CKEYGEN_BACKUP: |
return TRUE; |
612 |
pwd_state ^= 1; |
} |
|
EnableWindow (GetDlgItem (dlg, IDC_CKEYGEN_PASS), pwd_state); |
|
|
break; |
|
|
} |
|
|
} |
|
613 |
|
|
614 |
switch (LOWORD (wparam)) { |
switch (LOWORD (wparam)) { |
615 |
case IDOK: |
case IDOK: |
616 |
n = item_get_text_length (dlg, IDC_CKEYGEN_NAME); |
n = item_get_text_length (dlg, IDC_CKEYGEN_NAME); |
617 |
if (!n) { |
if (!n) { |
618 |
msg_box (dlg, _("Please enter your name."), _("Card Edit"), MB_ERR); |
msg_box (dlg, _("Please enter your name."), _("Card Edit"), MB_ERR); |
619 |
return FALSE; |
return TRUE; |
620 |
} |
} |
621 |
if (n < 5) { |
if (n < 5) { |
622 |
msg_box (dlg, _("Name must be at least 5 characters long."), |
msg_box (dlg, _("Name must be at least 5 characters long."), |
623 |
_("Card Edit"), MB_INFO); |
_("Card Edit"), MB_INFO); |
624 |
return FALSE; |
return TRUE; |
625 |
} |
} |
626 |
n = item_get_text_length (dlg, IDC_CKEYGEN_EMAIL); |
n = item_get_text_length (dlg, IDC_CKEYGEN_EMAIL); |
627 |
if (!n) { |
if (!n) { |
628 |
msg_box (dlg, _("Please enter your e-mail address."), |
msg_box (dlg, _("Please enter your e-mail address."), |
629 |
_("Card Edit"), MB_ERR); |
_("Card Edit"), MB_ERR); |
630 |
return FALSE; |
return TRUE; |
631 |
} |
} |
632 |
GetDlgItemText (dlg, IDC_CKEYGEN_NAME, name, sizeof (name)-1); |
GetDlgItemText (dlg, IDC_CKEYGEN_NAME, name, sizeof (name)-1); |
633 |
GetDlgItemText (dlg, IDC_CKEYGEN_EMAIL, email, sizeof (email)-1); |
GetDlgItemText (dlg, IDC_CKEYGEN_EMAIL, email, sizeof (email)-1); |
634 |
if (!strchr (email, '@') || n < 3) { |
if (!strchr (email, '@') || n < 3) { |
635 |
msg_box (dlg, _("Please enter a valid e-mail address."), |
msg_box (dlg, _("Please enter a valid e-mail address."), |
636 |
_("Card Edit"), MB_ERR); |
_("Card Edit"), MB_ERR); |
637 |
return FALSE; |
return TRUE; |
638 |
} |
} |
639 |
n = GetDlgItemText (dlg, IDC_CKEYGEN_PASS, pass, sizeof (pass)-1); |
n = GetDlgItemText (dlg, IDC_CKEYGEN_PASS, pass, sizeof (pass)-1); |
640 |
if (!n && pwd_state == 1) { |
if (!n && IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP)) { |
641 |
msg_box (dlg, _("Please enter an off-card passphrase."), _("Card Edit"), MB_ERR); |
msg_box (dlg, _("Please enter an off-card passphrase."), _("Card Edit"), MB_ERR); |
642 |
return FALSE; |
return TRUE; |
643 |
} |
} |
644 |
n = item_get_text_length (dlg, IDC_CKEYGEN_COMMENT); |
n = item_get_text_length (dlg, IDC_CKEYGEN_COMMENT); |
645 |
if (n > 0) |
if (n > 0) |
646 |
GetDlgItemText (dlg, IDC_CKEYGEN_COMMENT, comment, sizeof (comment)-1); |
GetDlgItemText (dlg, IDC_CKEYGEN_COMMENT, comment, sizeof (comment)-1); |
647 |
if (is_8bit_string (name) ||is_8bit_string (comment)) { |
if (is_8bit_string (name) || n > 0 && is_8bit_string (comment)) { |
648 |
msg_box (dlg, _("Please use plain ASCII charset for the fields."), |
msg_box (dlg, _("Please use plain ASCII charset for the fields."), |
649 |
_("Card Edit"), MB_INFO); |
_("Card Edit"), MB_INFO); |
650 |
return FALSE; |
return TRUE; |
651 |
} |
} |
652 |
memset (&pincb, 0, sizeof (pincb)); |
memset (&pincb, 0, sizeof (pincb)); |
653 |
if (do_askpin (dlg, GPGME_EDITCARD_CHAPIN, NULL, &pincb)) { |
if (do_askpin (dlg, CARD_ADMIN_PIN, NULL, &pincb)) { |
654 |
free_pincb (&pincb); |
free_pincb (&pincb); |
655 |
return FALSE; |
return TRUE; |
656 |
} |
} |
657 |
if (do_askpin (dlg, GPGME_EDITCARD_CHUPIN, NULL, &pincb)) { |
if (do_askpin (dlg, CARD_USER_PIN, NULL, &pincb)) { |
658 |
free_pincb (&pincb); |
free_pincb (&pincb); |
659 |
return FALSE; |
return TRUE; |
660 |
} |
} |
661 |
err = gpgme_new (&ctx); |
ce = new GpgCardEdit (); |
662 |
if (!err) |
if (!ce) |
|
err = gpgme_editcard_new (&crd); |
|
|
if (err) |
|
663 |
BUG (0); |
BUG (0); |
664 |
|
|
665 |
expires = !IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER); |
expires = !IsDlgButtonChecked (dlg, IDC_CKEYGEN_NEVER); |
666 |
if (expires) { |
if (expires) { |
667 |
SYSTEMTIME st, ct; |
SYSTEMTIME st, ct; |
669 |
if (!keygen_check_date (&st)) { |
if (!keygen_check_date (&st)) { |
670 |
msg_box (dlg, _("The date you have chosen lies in the past."), |
msg_box (dlg, _("The date you have chosen lies in the past."), |
671 |
_("Card Edit"), MB_ERR); |
_("Card Edit"), MB_ERR); |
672 |
gpgme_release (ctx); |
free_pincb (&pincb); |
673 |
gpgme_editcard_release (crd); |
delete ce; |
674 |
return FALSE; |
return TRUE; |
675 |
} |
} |
676 |
GetSystemTime (&ct); |
GetSystemTime (&ct); |
677 |
/* XXX this is not very precise */ |
/* XXX this is not very precise */ |
678 |
sprintf (expdate, "%d", calc_days (st.wYear, st.wMonth, st.wDay, |
valid = calc_days (st.wYear, st.wMonth, st.wDay, |
679 |
ct.wYear, ct.wMonth, ct.wDay)); |
ct.wYear, ct.wMonth, ct.wDay); |
680 |
} |
} |
681 |
if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_REPLACE)) |
if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_REPLACE)) |
682 |
card_flags |= GPGME_CARDFLAG_REPLACE; |
card_flags |= GPG_CARDFLAG_REPLACE; |
683 |
if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP)) |
if (IsDlgButtonChecked (dlg, IDC_CKEYGEN_BACKUP)) |
684 |
card_flags |= GPGME_CARDFLAG_BAKENC; |
card_flags |= GPG_CARDFLAG_BAKENC; |
685 |
gpgme_editcard_set_keygen_params (crd, card_flags, name, email, |
ce->setKeygenPassphrase (pass); |
686 |
n? comment : NULL, |
ce->setPIN (pincb.upin); |
687 |
expires? expdate : NULL); |
ce->setAdminPIN (pincb.apin); |
688 |
gpgme_editcard_set_passwd (crd, pass); |
|
689 |
gpgme_editcard_control (crd, GPGME_EDITCARD_APIN, pincb.apin); |
SetCursor (LoadCursor (NULL, IDC_WAIT)); |
690 |
gpgme_editcard_control (crd, GPGME_EDITCARD_UPIN, pincb.upin); |
err = ce->genKey (card_flags, name, email, n? comment: NULL, |
691 |
gpgme_set_edit_ctx (ctx, crd, GPGME_EDITCARD_GENKEY); |
expires? valid : 0, NULL); |
692 |
SetCursor( LoadCursor (NULL, IDC_WAIT)); |
|
|
err = gpgme_op_editcard (ctx); |
|
693 |
SetCursor (LoadCursor (NULL, IDC_ARROW)); |
SetCursor (LoadCursor (NULL, IDC_ARROW)); |
694 |
if (err == GPGME_Canceled) |
|
695 |
|
if (gpgme_err_code (err) == GPG_ERR_CANCELED) |
696 |
msg_box (dlg, _("Operation was canceled. It seems that there are " |
msg_box (dlg, _("Operation was canceled. It seems that there are " |
697 |
"existing\nkeys on the cards. You need to mark the " |
"existing\nkeys on the cards. You need to mark the " |
698 |
"'Overwrite' flag."), _("Card Edit"), MB_INFO); |
"'Overwrite' flag."), _("Card Edit"), MB_INFO); |
699 |
else if (err) |
else |
700 |
|
if (err) |
701 |
msg_box (dlg, "The operation does not succeed.\n" |
msg_box (dlg, "The operation does not succeed.\n" |
702 |
"Please make sure you entered the right PIN's." |
"Please make sure you entered the right PIN's." |
703 |
, _("Card Edit"), MB_ERR); |
, _("Card Edit"), MB_ERR); |
706 |
_("Card Edit"), MB_OK); |
_("Card Edit"), MB_OK); |
707 |
memset (pass, 0, sizeof (pass)); |
memset (pass, 0, sizeof (pass)); |
708 |
free_pincb (&pincb); |
free_pincb (&pincb); |
709 |
gpgme_release (ctx); |
delete ce; |
710 |
gpgme_editcard_release (crd); |
return TRUE; |
|
break; |
|
711 |
|
|
712 |
case IDCANCEL: |
case IDCANCEL: |
713 |
EndDialog (dlg, FALSE); |
EndDialog (dlg, FALSE); |
716 |
break; |
break; |
717 |
} |
} |
718 |
return FALSE; |
return FALSE; |
719 |
} /* card_keygen_dlg_proc */ |
} |
720 |
|
|
721 |
|
|
722 |
|
/* Check if the given pinlen is valid. |
723 |
|
@which decided what PIN will be used. |
724 |
|
@pinlen is the pin length entered by the user. |
725 |
|
Return value: 0 on success. */ |
726 |
static int |
static int |
727 |
check_pin_len (int which, int flag, int pinlen) |
check_pin_len (int which, int flag, int pinlen) |
728 |
{ |
{ |
733 |
msg_box (NULL, _("Please enter the new card PIN."), _("Card Edit"), MB_ERR); |
msg_box (NULL, _("Please enter the new card PIN."), _("Card Edit"), MB_ERR); |
734 |
return -1; |
return -1; |
735 |
} |
} |
736 |
if (which == GPGME_EDITCARD_CHAPIN |
if (which == CARD_ADMIN_PIN |
737 |
&& pinlen < 8) { |
&& pinlen < 8) { |
738 |
msg_box (NULL, _("PIN must be minimal 8 characters."), _("Card Edit"), MB_ERR); |
msg_box (NULL, _("Admin PIN must be minimal 8 characters."), _("Card Edit"), MB_ERR); |
739 |
return -1; |
return -1; |
740 |
} |
} |
741 |
if (which == GPGME_EDITCARD_CHUPIN |
if (which == CARD_USER_PIN |
742 |
&& pinlen < 6) { |
&& pinlen < 6) { |
743 |
msg_box (NULL, _("PIN must be minimal 6 characters."), _("Card Edit"), MB_ERR); |
msg_box (NULL, _("PIN must be minimal 6 characters."), _("Card Edit"), MB_ERR); |
744 |
return -1; |
return -1; |
746 |
return 0; |
return 0; |
747 |
} |
} |
748 |
|
|
749 |
|
/* Dialog box procedure to change the PIN. */ |
750 |
BOOL CALLBACK |
BOOL CALLBACK |
751 |
card_changepin_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
card_changepin_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
752 |
{ |
{ |
753 |
static int hide = 1; |
static int hide = 1; |
754 |
gpgme_ctx_t ctx; |
gpgme_error_t err; |
755 |
gpgme_editcard_t chpin; |
GpgCardEdit *ce; |
|
gpgme_error_t rc; |
|
|
gpgme_edit_card_t which; |
|
756 |
char pold[128], pnew[128], pnew2[128]; |
char pold[128], pnew[128], pnew2[128]; |
757 |
|
int which; |
758 |
size_t n; |
size_t n; |
759 |
|
|
760 |
switch( msg ) { |
switch( msg ) { |
761 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
762 |
hide = 1; |
hide = 1; |
763 |
CheckDlgButton (dlg, IDC_CHPIN_HIDE, BST_CHECKED); |
CheckDlgButton (dlg, IDC_CHPIN_HIDE, BST_CHECKED); |
764 |
center_window (dlg); |
center_window (dlg, NULL); |
765 |
CheckDlgButton (dlg, IDC_CHPIN_ISWORK, BST_CHECKED); |
CheckDlgButton (dlg, IDC_CHPIN_ISWORK, BST_CHECKED); |
766 |
SetForegroundWindow (dlg); |
SetForegroundWindow (dlg); |
767 |
break; |
break; |
770 |
if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_CHPIN_HIDE) { |
if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_CHPIN_HIDE) { |
771 |
HWND hwnd; |
HWND hwnd; |
772 |
hide ^= 1; |
hide ^= 1; |
773 |
|
hwnd = GetDlgItem (dlg, IDC_CHPIN_OLDPIN); |
774 |
|
SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0); |
775 |
|
SetFocus (hwnd); |
776 |
hwnd = GetDlgItem (dlg, IDC_CHPIN_NEWPIN); |
hwnd = GetDlgItem (dlg, IDC_CHPIN_NEWPIN); |
777 |
SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0); |
SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0); |
778 |
SetFocus (hwnd); |
SetFocus (hwnd); |
783 |
switch( LOWORD( wparam ) ) { |
switch( LOWORD( wparam ) ) { |
784 |
case IDOK: |
case IDOK: |
785 |
if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISADMIN)) |
if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISADMIN)) |
786 |
which = GPGME_EDITCARD_CHAPIN; |
which = CARD_ADMIN_PIN; |
787 |
else if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISWORK)) |
else if (IsDlgButtonChecked (dlg, IDC_CHPIN_ISWORK)) |
788 |
which = GPGME_EDITCARD_CHUPIN; |
which = CARD_USER_PIN; |
|
else |
|
|
BUG (0); |
|
789 |
|
|
790 |
n = item_get_text_length (dlg, IDC_CHPIN_OLDPIN); |
n = item_get_text_length (dlg, IDC_CHPIN_OLDPIN); |
791 |
if (check_pin_len (which, 1, n)) |
if (check_pin_len (which, 1, n)) |
792 |
return FALSE; |
return TRUE; |
793 |
n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN); |
n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN); |
794 |
if (check_pin_len (which, 0, n)) |
if (check_pin_len (which, 0, n)) |
795 |
return FALSE; |
return TRUE; |
796 |
n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN2); |
n = item_get_text_length (dlg, IDC_CHPIN_NEWPIN2); |
797 |
if (check_pin_len (which, 0, n)) |
if (check_pin_len (which, 0, n)) |
798 |
return FALSE; |
return TRUE; |
799 |
GetDlgItemText (dlg, IDC_CHPIN_OLDPIN, pold, sizeof (pold)-1); |
GetDlgItemText (dlg, IDC_CHPIN_OLDPIN, pold, sizeof (pold)-1); |
800 |
GetDlgItemText (dlg, IDC_CHPIN_NEWPIN, pnew, sizeof (pnew)-1); |
GetDlgItemText (dlg, IDC_CHPIN_NEWPIN, pnew, sizeof (pnew)-1); |
801 |
GetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, pnew2, sizeof (pnew2)-1); |
GetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, pnew2, sizeof (pnew2)-1); |
802 |
if (strcmp (pnew, pnew2)) { |
if (strcmp (pnew, pnew2)) { |
803 |
msg_box (dlg, _("Passphrases do not match. Please try again."), _("Card Edit"), MB_ERR); |
msg_box (dlg, _("Passphrases do not match. Please try again."), |
804 |
return FALSE; |
_("Card Edit"), MB_ERR); |
805 |
|
return TRUE; |
806 |
} |
} |
807 |
rc = gpgme_new (&ctx); |
|
808 |
if (!rc) |
ce = new GpgCardEdit (); |
809 |
rc = gpgme_editcard_new (&chpin); |
if (!ce) |
|
if (rc) |
|
810 |
BUG (0); |
BUG (0); |
811 |
gpgme_changepin_set( chpin, which, pold, pnew ); |
if (which == CARD_ADMIN_PIN) |
812 |
gpgme_set_edit_ctx( ctx, chpin, which ); |
ce->setAdminPIN (pold); |
813 |
rc = gpgme_op_changepin( ctx ); |
else |
814 |
if( rc ) |
ce->setPIN (pold); |
815 |
msg_box( dlg, gpgme_strerror( rc ), _("Card Edit"), MB_ERR ); |
ce->setNewPIN (pnew); |
816 |
|
err = ce->changePIN (which == CARD_ADMIN_PIN? GPG_EDITCARD_CHAPIN : |
817 |
|
GPG_EDITCARD_CHUPIN); |
818 |
|
if (err) |
819 |
|
msg_box (dlg, gpgme_strerror (err), _("Card Edit"), MB_ERR); |
820 |
else { |
else { |
821 |
msg_box( dlg, _("PIN successfully changed."), _("Card Edit"), MB_OK ); |
msg_box (dlg, _("PIN successfully changed."), |
822 |
|
_("Card Edit"), MB_OK); |
823 |
SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, ""); |
SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, ""); |
824 |
SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, ""); |
SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, ""); |
825 |
SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, ""); |
SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, ""); |
826 |
} |
} |
827 |
gpgme_release( ctx ); |
memset (pold, 0, sizeof (pold)); |
828 |
gpgme_editcard_release( chpin ); |
memset (pnew, 0, sizeof (pnew)); |
829 |
|
memset (pnew2, 0, sizeof (pnew2)); |
830 |
|
delete ce; |
831 |
break; |
break; |
832 |
|
|
833 |
case IDCANCEL: |
case IDCANCEL: |
834 |
SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, ""); |
SetDlgItemText (dlg, IDC_CHPIN_NEWPIN, ""); |
835 |
SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, ""); |
SetDlgItemText (dlg, IDC_CHPIN_OLDPIN, ""); |
836 |
SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, ""); |
SetDlgItemText (dlg, IDC_CHPIN_NEWPIN2, ""); |
837 |
EndDialog( dlg, FALSE ); |
EndDialog (dlg, FALSE); |
838 |
break; |
break; |
839 |
} |
} |
840 |
break; |
break; |
841 |
} |
} |
842 |
|
|
843 |
return FALSE; |
return FALSE; |
|
} /* card_changepin_dlg_proc */ |
|
844 |
|
} |