395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
struct sessionsaver_data {
union control *editbox, *listbox, *loadbutton, *savebutton, *delbutton;
union control *okbutton, *cancelbutton;
struct sesslist sesslist;
int midsession;
};
/*
* Helper function to load the session selected in the list box, if
* any, as this is done in more than one place below. Returns 0 for
* failure.
*/
static int load_selected_session(struct sessionsaver_data *ssd,
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
struct sessionsaver_data {
union control *editbox, *listbox, *loadbutton, *savebutton, *delbutton;
union control *okbutton, *cancelbutton;
struct sesslist sesslist;
int midsession;
};
/* PuTTY SC start */
void *m_label_dlg = NULL;
void *m_cert_dlg = NULL;
void *m_keystring_dlg = NULL;
void *sc_get_label_dialog() {
return m_label_dlg;
}
void *m_label_ctrl = NULL;
void *m_cert_ctrl = NULL;
void *m_keystring_ctrl = NULL;
void *sc_get_label_ctrl() {
return m_label_ctrl;
}
void sc_cert_handler(union control *ctrl, void *dlg, void *data, int event);
void sc_tokenlabel_handler(union control *ctrl, void *dlg, void *data, int event ) {
Config *cfg = (Config *)data;
m_label_dlg = dlg;
m_label_ctrl = ctrl;
if(event == EVENT_REFRESH) {
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
if(filename_is_null(cfg->pkcs11_libfile)) {
strcpy(cfg->pkcs11_token_label, "");
dlg_listbox_add(ctrl, dlg, "<E: SELECT LIBRARY FIRST!>");
} else {
int i;
CK_RV rv = 0;
HINSTANCE hLib = LoadLibrary((char *)&cfg->pkcs11_libfile);
CK_C_GetFunctionList pGFL = (CK_RV (*)(CK_FUNCTION_LIST_PTR_PTR))GetProcAddress(hLib, "C_GetFunctionList");
if (pGFL == NULL) {
strcpy(cfg->pkcs11_token_label, "");
dlg_listbox_add(ctrl, dlg, "<E: WRONG LIBRARY!>");
} else {
CK_FUNCTION_LIST_PTR fl = 0;
rv = pGFL(&fl);
if(rv != CKR_OK) {
strcpy(cfg->pkcs11_token_label, "");
dlg_listbox_add(ctrl, dlg, "<E: ACCESS TO LIBRARY FAILED!>");
} else {
int rv1, rv2;
unsigned long slot_count = 16;
CK_SLOT_ID slots[16];
rv1 = fl->C_Initialize(0);
rv2 = fl->C_GetSlotList(TRUE, slots, &slot_count);
if((rv1 != CKR_OK) || (rv2 != CKR_OK)) {
strcpy(cfg->pkcs11_token_label, "");
dlg_listbox_add(ctrl, dlg, "<E: NO SLOTS FOUND!>");
} else {
if(slot_count == 0) {
strcpy(cfg->pkcs11_token_label, "");
dlg_listbox_add(ctrl, dlg, "<E: NO TOKEN FOUND!>");
}
for(i=0; i<slot_count; i++) {
CK_TOKEN_INFO token_info;
CK_SLOT_ID slot = 64;
slot = slots[i];
fl->C_GetTokenInfo(slot,&token_info);
{
char buf[40];
int j;
memset(buf, 0, 40);
strncpy(buf, token_info.label, 30);
for(j=29;j>0;j--) {
if(buf[j] == ' ') {
buf[j] = '\0';
} else {
break;
}
}
dlg_listbox_add(ctrl, dlg, buf);
}
}
}
fl->C_Finalize(0);
}
}
FreeLibrary(hLib);
}
dlg_editbox_set(ctrl, dlg, cfg->pkcs11_token_label);
dlg_update_done(ctrl, dlg);
} else if (event == EVENT_VALCHANGE) {
char buf[70];
dlg_editbox_get(ctrl, dlg, buf,
sizeof(buf));
if(strncmp(buf, "<E: ", 4) != 0){
strcpy(cfg->pkcs11_token_label, buf);
}
}
if(m_cert_dlg != NULL) {
sc_cert_handler(m_cert_ctrl, m_cert_dlg, data, EVENT_REFRESH);
}
}
void sc_keystring_handler(union control *ctrl, void *dlg, void *data, int event ) {
m_keystring_dlg = dlg;
m_keystring_ctrl = ctrl;
}
void sc_cert_handler(union control *ctrl, void *dlg, void *data, int event ) {
Config *cfg = (Config *)data;
m_cert_dlg = dlg;
m_cert_ctrl = ctrl;
if(event == EVENT_REFRESH) {
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
if(cfg->pkcs11_token_label == NULL ||
strlen(cfg->pkcs11_token_label) == 0) {
strcpy(cfg->pkcs11_token_label, "");
dlg_listbox_add(ctrl, dlg, "<E: SELECT TOKEN FIRST!>");
} else {
sc_lib *sclib;
if (cfg->sclib == NULL) { cfg->sclib = calloc(sizeof(sc_lib), 1); }
sclib = cfg->sclib;
sc_init_library(NULL, 0, sclib, &cfg->pkcs11_libfile);
if(sclib->m_fl) {
CK_SESSION_HANDLE session = sc_get_session(NULL, 0, sclib->m_fl, cfg->pkcs11_token_label);
if(session) {
char msg[1024] = "";
sc_cert_list *pcl;
sc_cert_list *cl = sc_get_cert_list(sclib, session, msg);
pcl = cl;
while(pcl != NULL) {
char *p_buf;
p_buf = calloc(1,pcl->cert_attr[0].ulValueLen+1);
strncpy(p_buf, pcl->cert_attr[0].pValue, pcl->cert_attr[0].ulValueLen);
dlg_listbox_add(ctrl, dlg, p_buf);
free(p_buf);
pcl = pcl->next;
}
sc_free_cert_list(cl);
// sclib->m_fl->C_CloseSession(session);
}
// sclib->m_fl->C_Finalize(0);
}
// free(sclib);
}
dlg_editbox_set(ctrl, dlg, cfg->pkcs11_cert_label);
dlg_update_done(ctrl, dlg);
} else if (event == EVENT_VALCHANGE) {
sc_lib *sclib;
char token_label[70];
char cert_label[70];
int blob_len;
char *algorithm; /* minor memory leak */
sclib = cfg->sclib;
dlg_editbox_get(m_label_ctrl, m_label_dlg, token_label, sizeof(token_label));
dlg_editbox_get(ctrl, dlg, cert_label, sizeof(cert_label));
if(strncmp(cert_label, "<E: ", 4) != 0) {
strcpy(cfg->pkcs11_cert_label, cert_label);
sc_get_pub(NULL, 0, sclib, token_label, cert_label, &algorithm, &blob_len);
if (m_keystring_dlg != NULL && sclib && sclib->keystring != NULL) {
dlg_editbox_set(m_keystring_ctrl, m_keystring_dlg, sclib->keystring);
}
if (sclib == NULL) {
dlg_editbox_set(m_keystring_ctrl, m_keystring_dlg, "no sclib");
} else
if (sclib->keystring == NULL) {
dlg_editbox_set(m_keystring_ctrl, m_keystring_dlg, "no sclib keystring");
}
}
}
}
/* PuTTY SC end */
/* PuTTY CAPI begin */
#ifdef _WINDOWS
struct capi_data {
union control *certstore_droplist, *certID_text, *cert_browse, *keystring_text;
};
void capi_certstore_handler(union control *ctrl, void *dlg, void *data, int event ) {
Config *cfg = (Config *)data;
struct capi_data *capid = (struct capi_data *)ctrl->generic.context.p;
if (event == EVENT_REFRESH) {
if (ctrl == capid->certstore_droplist) {
dlg_update_start(ctrl, dlg);
dlg_listbox_clear(ctrl, dlg);
dlg_listbox_add(ctrl, dlg, "User\\MY (Personal Certificates)");
dlg_listbox_add(ctrl, dlg, "System\\MY (Personal Certificates)");
if (strncmp(cfg->capi_certID, "System\\MY", 9) == 0)
dlg_listbox_select(ctrl, dlg, 1);
else
dlg_listbox_select(ctrl, dlg, 0); /* *shrug* */
dlg_update_done(ctrl, dlg);
}
}
}
void capi_certID_handler(union control *ctrl, void *dlg, void *data, int event ) {
Config *cfg = (Config *)data;
struct capi_data *capid = (struct capi_data *)ctrl->generic.context.p;
char* tmpKeystring = NULL;
if (event == EVENT_REFRESH) {
dlg_editbox_set(ctrl, dlg, cfg->capi_certID);
} else if (event == EVENT_VALCHANGE) {
dlg_editbox_get(ctrl, dlg, cfg->capi_certID, sizeof(cfg->capi_certID));
}
if (cfg->capi_certID[0]) {
if ((tmpKeystring = capi_get_key_string(cfg->capi_certID)) != NULL) {
dlg_editbox_set(capid->keystring_text, dlg, tmpKeystring);
free(tmpKeystring);
tmpKeystring = NULL;
}
}
}
typedef BOOL (WINAPI *PCertSelectCertificateA)(
__inout PCERT_SELECT_STRUCT_A pCertSelectInfo
);
void capi_certstore_browse_handler(union control *ctrl, void *dlg, void *data, int event ) {
Config *cfg = (Config *)data;
struct capi_data *capid = (struct capi_data *)ctrl->generic.context.p;
HCERTSTORE hStore = NULL;
CERT_SELECT_STRUCT_A* css = NULL;
CERT_CONTEXT** acc = NULL;
unsigned int tmpSHA1size = 0, dwCertStoreUser;
unsigned char tmpSHA1[20];
char tmpSHA1hex[41] = "";
char tmpCertID[100] = "";
char* tmpKeystring = NULL;
HMODULE hCertDlgDLL = NULL;
PCertSelectCertificateA f_csca = NULL;
int i;
if (event == EVENT_ACTION) {
i = dlg_listbox_index(capid->certstore_droplist, dlg);
if (i < 0)
goto cleanup;
if ((hCertDlgDLL = LoadLibrary("CryptDlg.dll")) == NULL)
goto cleanup;
if ((f_csca = (PCertSelectCertificateA) GetProcAddress(hCertDlgDLL, "CertSelectCertificateA")) == NULL)
goto cleanup;
dwCertStoreUser = CERT_SYSTEM_STORE_CURRENT_USER;
if (i == 1)
dwCertStoreUser = CERT_SYSTEM_STORE_LOCAL_MACHINE;
if ((hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, 0 /*hCryptProv*/, dwCertStoreUser | CERT_STORE_READONLY_FLAG | CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_ENUM_ARCHIVED_FLAG, "MY")) == NULL)
goto cleanup;
acc = (CERT_CONTEXT**) malloc(sizeof(CERT_CONTEXT*));
acc[0] = NULL;
css = (CERT_SELECT_STRUCT_A*) malloc(sizeof(CERT_SELECT_STRUCT_A));
memset(css, 0, sizeof(CERT_SELECT_STRUCT_A));
css->dwSize = sizeof(CERT_SELECT_STRUCT_A);
css->hwndParent = ((struct dlgparam *) dlg)->hwnd;
css->hInstance = NULL;
css->pTemplateName = NULL;
css->dwFlags = 0;
css->szTitle = "PuTTY: Select Certificate for CAPI Auth";
css->cCertStore = 1;
css->arrayCertStore = &hStore;
css->szPurposeOid = szOID_PKIX_KP_CLIENT_AUTH;
css->cCertContext = 1; // count of arrayCertContext indexes allocated
css->arrayCertContext = acc;
if (!f_csca(css)) // GetProcAddress(hCertDlgDLL, "CertSelectCertificateA")
goto cleanup;
if (css->cCertContext != 1)
goto cleanup;
if (acc[0] == NULL)
goto cleanup;
tmpSHA1size = sizeof(tmpSHA1);
if (!CertGetCertificateContextProperty(acc[0], CERT_HASH_PROP_ID, tmpSHA1, &tmpSHA1size))
memset(tmpSHA1, 0, sizeof(tmpSHA1));
_snprintf(tmpSHA1hex, sizeof(tmpSHA1hex)-1, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", tmpSHA1[0], tmpSHA1[1], tmpSHA1[2], tmpSHA1[3], tmpSHA1[4], tmpSHA1[5], tmpSHA1[6], tmpSHA1[7], tmpSHA1[8], tmpSHA1[9], tmpSHA1[10], tmpSHA1[11], tmpSHA1[12], tmpSHA1[13], tmpSHA1[14], tmpSHA1[15], tmpSHA1[16], tmpSHA1[17], tmpSHA1[18], tmpSHA1[19]);
tmpSHA1hex[sizeof(tmpSHA1hex)-1] = '\0';
_snprintf(tmpCertID, sizeof(tmpCertID)-1, "%s\\%s", i == 1 ? "Machine\\MY" : "User\\MY", tmpSHA1hex);
tmpCertID[sizeof(tmpCertID)-1] = '\0';
dlg_editbox_set(capid->certID_text, dlg, tmpCertID);
strncpy(cfg->capi_certID, tmpCertID, sizeof(cfg->capi_certID));
cfg->capi_certID[sizeof(cfg->capi_certID)-1] = '\0';
if ((tmpKeystring = capi_get_key_string(tmpCertID)) != NULL) {
dlg_editbox_set(capid->keystring_text, dlg, tmpKeystring);
free(tmpKeystring);
tmpKeystring = NULL;
}
}
cleanup:
if (hCertDlgDLL) {
FreeLibrary(hCertDlgDLL);
f_csca = NULL;
hCertDlgDLL = NULL;
}
if (acc) {
if (acc[0])
CertFreeCertificateContext(acc[0]);
acc[0] = NULL;
free(acc);
acc = NULL;
}
if (css)
free(css);
css = NULL;
if (hStore)
CertCloseStore(hStore, 0);
hStore = NULL;
return;
}
#endif
/* PuTTY CAPI end */
/*
* Helper function to load the session selected in the list box, if
* any, as this is done in more than one place below. Returns 0 for
* failure.
*/
static int load_selected_session(struct sessionsaver_data *ssd,
|
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
|
I(offsetof(Config,ssh_rekey_data)),
I(16));
ctrl_text(s, "(Use 1M for 1 megabyte, 1G for 1 gigabyte etc)",
HELPCTX(ssh_kex_repeat));
}
if (!midsession) {
/*
* The Connection/SSH/Auth panel.
*/
ctrl_settitle(b, "Connection/SSH/Auth",
"Options controlling SSH authentication");
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
|
I(offsetof(Config,ssh_rekey_data)),
I(16));
ctrl_text(s, "(Use 1M for 1 megabyte, 1G for 1 gigabyte etc)",
HELPCTX(ssh_kex_repeat));
}
if (!midsession) {
/* PuTTY SC start */
/*
* The Connection/SSH/Pkcs11 panel.
*/
ctrl_settitle(b, "Connection/SSH/Pkcs11",
"Options controlling PKCS11 SSH authentication");
s = ctrl_getset(b, "Connection/SSH/Pkcs11", "methods",
"Authentication methods");
ctrl_checkbox(s, "Use Windows event log", NO_SHORTCUT,
HELPCTX(ssh_write_syslog),
dlg_stdcheckbox_handler,
I(offsetof(Config,try_write_syslog)));
ctrl_checkbox(s, "Attempt \"PKCS#11 smartcard\" auth (SSH-2)", NO_SHORTCUT,
HELPCTX(ssh_auth_pkcs11),
dlg_stdcheckbox_handler,
I(offsetof(Config,try_pkcs11_auth)));
s = ctrl_getset(b, "Connection/SSH/Pkcs11", "params",
"Authentication parameters");
ctrl_filesel(s, "PKCS#11 library for authentication:", NO_SHORTCUT,
FALSE , FALSE, "Select PKCS#11 library file",
HELPCTX(ssh_auth_pkcs11_libfile),
sc_dlg_stdfilesel_handler11, I(offsetof(Config, pkcs11_libfile)));
m_label_ctrl = ctrl_combobox(s, "Token label:",
NO_SHORTCUT, 70, HELPCTX(ssh_auth_pkcs11_token_label),
sc_tokenlabel_handler, P(NULL), P(NULL));
m_cert_ctrl = ctrl_combobox(s, "Certificate label:",
NO_SHORTCUT, 70, HELPCTX(ssh_auth_pkcs11_cert_label),
sc_cert_handler, P(NULL), P(NULL));
m_keystring_ctrl = ctrl_editbox(s, "SSH keystring:",
NO_SHORTCUT, 100, HELPCTX(ssh_auth_pkcs11_cert_label),
sc_keystring_handler, P(NULL), P(NULL));
/* PuTTY SC end */
/* PuTTY CAPI start */
#ifdef _WINDOWS
/*
* The Connection/SSH/CAPI panel.
*/
ctrl_settitle(b, "Connection/SSH/CAPI",
"Options controlling MS CAPI SSH authentication");
capid = (struct capi_data *) ctrl_alloc(b, sizeof(struct capi_data));
s = ctrl_getset(b, "Connection/SSH/CAPI", "methods",
"Authentication methods");
ctrl_checkbox(s, "Attempt \"CAPI Certificate\" (Key-only) auth (SSH-2)", NO_SHORTCUT,
HELPCTX(ssh_auth_capi),
dlg_stdcheckbox_handler,
I(offsetof(Config,try_capi_auth)));
s = ctrl_getset(b, "Connection/SSH/CAPI", "params",
"Authentication parameters");
capid->certstore_droplist = ctrl_droplist(s, "Store:", NO_SHORTCUT, 85,
HELPCTX(ssh_auth_capi_certstore_label),
capi_certstore_handler, P(capid));
ctrl_columns(s, 2, 75, 25);
capid->certID_text =
ctrl_editbox(s, "Cert:", NO_SHORTCUT, 80,
HELPCTX(ssh_auth_capi_certstore_label),
capi_certID_handler
, P(capid), P(NULL)
);
capid->certID_text->generic.column = 0;
capid->cert_browse = ctrl_pushbutton(s, "Browse", NO_SHORTCUT,
HELPCTX(ssh_auth_capi),
capi_certstore_browse_handler, P(capid));
capid->cert_browse->generic.column = 1;
capid->keystring_text = ctrl_editbox(s, "SSH keystring:",
NO_SHORTCUT, 100, HELPCTX(ssh_auth_capi),
dlg_stdeditbox_handler, P(NULL), P(NULL));
/* m_label_ctrl = ctrl_combobox(s, "Certificate Store:",
NO_SHORTCUT, 70, HELPCTX(ssh_auth_capi_certstore_label),
capi_certstore_handler, P(NULL), P(NULL));
m_cert_ctrl = ctrl_combobox(s, "Certificate fingerprint:",
NO_SHORTCUT, 70, HELPCTX(ssh_auth_capi_certfingerprint_label),
capi_certfingerprint_handler, P(NULL), P(NULL));
m_keystring_ctrl = ctrl_editbox(s, "SSH keystring:",
NO_SHORTCUT, 100, HELPCTX(ssh_auth_pkcs11_cert_label),
capi_keystring_handler, P(NULL), P(NULL));
*/
#endif
/* PuTTY CAPI end */
/*
* The Connection/SSH/Auth panel.
*/
ctrl_settitle(b, "Connection/SSH/Auth",
"Options controlling SSH authentication");
|