From ead24472fda0b5d69497e792a6b4dede30ab319b Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Sun, 15 Aug 2010 17:31:53 +0300 Subject: key generation from module --- main.cpp | 278 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 273 insertions(+), 5 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index 6a58405..9921b65 100644 --- a/main.cpp +++ b/main.cpp @@ -30,13 +30,16 @@ void test() } -HWND hwndFirstRun = NULL, hwndSetDirs = NULL, hwndNewKey = NULL; +HWND hwndFirstRun = NULL, hwndSetDirs = NULL, hwndNewKey = NULL, hwndKeyGen = NULL; int itemnum = 0; +HWND hwndList_g = NULL; + static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM lParam) { HWND hwndList=GetDlgItem(hwndDlg, IDC_KEY_LIST); + hwndList_g = hwndList; LVCOLUMN col = {0}; LVITEM item = {0}; NMLISTVIEW * hdr = (NMLISTVIEW *) lParam; @@ -77,12 +80,12 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM ListView_InsertColumn(hwndList, 4, &col); ListView_SetExtendedListViewStyleEx(hwndList, 0, LVS_EX_FULLROWSELECT); int i = 1, iRow = 0; - { //parse gpg output + { item.mask = LVIF_TEXT; item.iItem = i; item.iSubItem = 0; item.pszText = _T(""); - { + {//parse gpg output string out; DWORD code; string::size_type p = 0, p2 = 0, stop = 0; @@ -149,6 +152,10 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM { switch (LOWORD(wParam)) { + case IDC_GENERATE_KEY: + void ShowKeyGenDialog(); + ShowKeyGenDialog(); + break; case ID_OK: ListView_GetItemText(hwndList, itemnum, 0, fp, 16); { @@ -275,7 +282,7 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP DestroyWindow(hwndDlg); break; case WM_DESTROY: - hwndFirstRun = NULL; + hwndSetDirs = NULL; break; } @@ -333,7 +340,258 @@ static BOOL CALLBACK DlgProcNewKeyDialog(HWND hwndDlg, UINT msg, WPARAM wParam, DestroyWindow(hwndDlg); break; case WM_DESTROY: - hwndFirstRun = NULL; + hwndNewKey = NULL; + break; + + } + return FALSE; +} +static BOOL CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) +{ + switch (msg) + { + case WM_INITDIALOG: + { + TranslateDialogDefault(hwndDlg); + ComboBoxAddStringUtf(GetDlgItem(hwndDlg, IDC_KEY_TYPE), _T("RSA"), 0); + ComboBoxAddStringUtf(GetDlgItem(hwndDlg, IDC_KEY_TYPE), _T("DSA"), 0); + return TRUE; + } + + + case WM_COMMAND: + { + switch (LOWORD(wParam)) + { + case IDCANCEL: + DestroyWindow(hwndDlg); + break; + case IDOK: + { + wstring path; + { //data sanity checks + TCHAR *tmp = new TCHAR [5]; + GetDlgItemText(hwndDlg, IDC_KEY_TYPE, tmp, 5); + if(_tcslen(tmp) < 3) + { + mir_free(tmp); + MessageBox(0, _T("You must set encryption algorythm first"), _T("Error"), MB_OK); + break; + } + mir_free(tmp); + tmp = new TCHAR [5]; + GetDlgItemText(hwndDlg, IDC_KEY_LENGTH, tmp, 5); + int length = _ttoi(tmp); + mir_free(tmp); + if(length < 1024 || length > 4096) + { + MessageBox(0, _T("Key length must be of length from 1024 to 4096 bits"), _T("Error"), MB_OK); + break; + } + tmp = new TCHAR [12]; + GetDlgItemText(hwndDlg, IDC_KEY_EXPIRE_DATE, tmp, 12); + if(_tcslen(tmp) != 10 && tmp[0] != '0') + { + MessageBox(0, _T("Invalid date"), _T("Error"), MB_OK); + delete [] tmp; + break; + } + delete [] tmp; + tmp = new TCHAR [128]; + GetDlgItemText(hwndDlg, IDC_KEY_REAL_NAME, tmp, 128); + if(_tcslen(tmp) < 5) + { + MessageBox(0, _T("Name must contain at least 5 characters"), _T("Error"), MB_OK); + delete [] tmp; + break; + } + delete [] tmp; + tmp = new TCHAR [128]; + GetDlgItemText(hwndDlg, IDC_KEY_EMAIL, tmp, 128); + if((_tcslen(tmp)) < 5 || (!_tcschr(tmp, _T('@'))) || (!_tcschr(tmp, _T('.')))) + { + MessageBox(0, _T("Invalid Email"), _T("Error"), MB_OK); + delete [] tmp; + break; + } + delete [] tmp; + } + { //generating key file + TCHAR *tmp = UniGetContactSettingUtf(NULL, szModuleName, "szHomePath", _T("")); + char *tmp2;// = mir_t2a(tmp); + path = tmp; + mir_free(tmp); + // mir_free(tmp2); + path.append(_T("\\new_key")); + wfstream f(path.c_str(), std::ios::out); + if(!f.is_open()) + { + MessageBox(0, _T("Failed to open file"), _T("Error"), MB_OK); + break; + } + f<<"Key-Type: "; + tmp = new TCHAR [5]; + GetDlgItemText(hwndDlg, IDC_KEY_TYPE, tmp, 5); + tmp2 = mir_t2a(tmp); + char *subkeytype = new char [6]; + if(strstr(tmp2, "RSA")) + strcpy(subkeytype, "RSA"); + else if(strstr(tmp2, "DSA")) //this is useless check for now, but it will be required if someone add another key types support + strcpy(subkeytype, "ELG-E"); + delete [] tmp; + f< 0) + { + f<<"Passphrase: "; + tmp2 = mir_utf8encodeW(tmp); + f< 0) + { + tmp2 = mir_utf8encodeW(tmp); + f<<"Name-Comment: "; + f<", p); + tmp = mir_a2t(out.substr(p,p2-p).c_str()); + ListView_SetItemText(hwndList_g, iRow, 1, tmp); + mir_free(tmp); + p = out.find("ssb ", p2) + 6; + p = out.find(" ", p) + 1; + p2 = out.find("\n", p); + tmp = mir_a2t(out.substr(p,p2-p-1).c_str()); + ListView_SetItemText(hwndList_g, iRow, 3, tmp); + mir_free(tmp); + i++; + } + } + } + break; + default: + break; + } + + break; + } + + case WM_NOTIFY: + { + switch (((LPNMHDR)lParam)->code) + { + } + } + break; + case WM_CLOSE: + DestroyWindow(hwndDlg); + break; + case WM_DESTROY: + hwndKeyGen = NULL; break; } @@ -369,6 +627,16 @@ void ShowNewKeyDialog() SetForegroundWindow(hwndNewKey); } +void ShowKeyGenDialog() +{ + if (hwndKeyGen == NULL) + { + hwndKeyGen = CreateDialog(hInst, MAKEINTRESOURCE(IDD_KEY_GEN), NULL, DlgProcKeyGenDialog); + } + SetForegroundWindow(hwndKeyGen); +} + + void FirstRun() -- cgit v1.2.3