1 | /* |
---|
2 | * Estonian ID card plugin for web browsers |
---|
3 | * |
---|
4 | * Copyright (C) 2010-2011 Codeborne <info@codeborne.com> |
---|
5 | * |
---|
6 | * This is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the GNU Lesser General Public |
---|
8 | * License as published by the Free Software Foundation; either |
---|
9 | * version 2.1 of the License, or (at your option) any later version. |
---|
10 | * |
---|
11 | * This software is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | * Lesser General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU Lesser General Public |
---|
17 | * License along with this library; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
19 | * |
---|
20 | */ |
---|
21 | |
---|
22 | #include "certselection-win.h" |
---|
23 | #include "esteid_certinfo.h" |
---|
24 | #include <windows.h> |
---|
25 | #include <stdio.h> |
---|
26 | #include <commctrl.h> |
---|
27 | #include "cert-class.h" |
---|
28 | #include "esteid_time.h" |
---|
29 | #include <AtlConv.h> |
---|
30 | #include <atlstr.h> |
---|
31 | #include <Cryptuiapi.h> |
---|
32 | #include "l10n.h" |
---|
33 | |
---|
34 | #pragma comment(lib, "Cryptui.lib") |
---|
35 | #pragma comment(lib, "comctl32.lib") |
---|
36 | |
---|
37 | extern HINSTANCE pluginInstance; |
---|
38 | |
---|
39 | void buildCertificatesList(HWND hwnd) { |
---|
40 | RECT rect; |
---|
41 | HWND certificatesList; |
---|
42 | LV_COLUMN lvC; |
---|
43 | |
---|
44 | certificatesList = GetDlgItem(hwnd, IDC_CERTIFICATESLIST); |
---|
45 | ListView_SetUnicodeFormat(certificatesList, true); |
---|
46 | GetClientRect(certificatesList, &rect); |
---|
47 | int rectWidth = rect.right - rect.left; |
---|
48 | int colSize = rectWidth/6; |
---|
49 | int colWidths[] = {3 * colSize, 2 * colSize, colSize + (rectWidth - (6*colSize)), 0}; |
---|
50 | char* colTitles[] = {"Certificate", "Type", "Valid to", ""}; |
---|
51 | |
---|
52 | ListView_SetExtendedListViewStyle(certificatesList, LVS_EX_FULLROWSELECT); |
---|
53 | lvC.mask = LVCF_WIDTH | LVCF_TEXT; |
---|
54 | lvC.fmt = LVCFMT_LEFT; |
---|
55 | |
---|
56 | |
---|
57 | WCHAR columnName[64]; |
---|
58 | for(int index = 0; index < 4; index++) { |
---|
59 | lvC.iSubItem = index; |
---|
60 | lvC.cx = colWidths[index]; |
---|
61 | |
---|
62 | MultiByteToWideChar(CP_UTF8, 0, l10n(colTitles[index]), -1, columnName, sizeof(columnName) / sizeof(WCHAR)); |
---|
63 | |
---|
64 | lvC.pszText = columnName; |
---|
65 | ListView_InsertColumn(certificatesList, index, &lvC); |
---|
66 | } |
---|
67 | |
---|
68 | LV_ITEM lv; |
---|
69 | lv.mask = LVIF_TEXT; |
---|
70 | |
---|
71 | EstEID_Certs *certs = EstEID_loadCerts(); |
---|
72 | EstEID_log("reading certs @ %p, certs->count = %u ", certs, certs->count); |
---|
73 | |
---|
74 | TCHAR unicodeBuf[512]; |
---|
75 | int currentCertIndex = 0; |
---|
76 | for (int i = 0; i < certs->count; i++) { |
---|
77 | EstEID_Map cert = certs->certs[i]; |
---|
78 | EstEID_logMap(cert); |
---|
79 | if (!EstEID_mapGet(cert, "usageNonRepudiation")) { |
---|
80 | EstEID_log("cert %s is removed from list", EstEID_mapGet(cert, "label")); |
---|
81 | continue; |
---|
82 | } |
---|
83 | |
---|
84 | lv.iSubItem = 0; |
---|
85 | lv.iItem = currentCertIndex; |
---|
86 | |
---|
87 | MultiByteToWideChar(CP_UTF8, 0, EstEID_mapGet(cert, "commonName"), -1, unicodeBuf, sizeof(unicodeBuf) / sizeof(TCHAR)); |
---|
88 | lv.pszText = (LPWSTR)unicodeBuf; |
---|
89 | ListView_InsertItem(certificatesList, &lv); |
---|
90 | |
---|
91 | MultiByteToWideChar(CP_UTF8, 0, EstEID_mapGet(cert, "organizationName"), -1, unicodeBuf, sizeof(unicodeBuf) / sizeof(TCHAR)); |
---|
92 | ListView_SetItemText(certificatesList, currentCertIndex, 1, unicodeBuf); |
---|
93 | |
---|
94 | char* validTo = getDateFromDateTime(EstEID_mapGet(cert, "validTo")); |
---|
95 | MultiByteToWideChar(CP_UTF8, 0, validTo, -1, unicodeBuf, sizeof(unicodeBuf) / sizeof(TCHAR)); |
---|
96 | ListView_SetItemText(certificatesList, currentCertIndex, 2, unicodeBuf); |
---|
97 | free(validTo); |
---|
98 | |
---|
99 | MultiByteToWideChar(CP_UTF8, 0, EstEID_mapGet(cert, "certHash"), -1, unicodeBuf, sizeof(unicodeBuf) / sizeof(TCHAR)); |
---|
100 | ListView_SetItemText(certificatesList, currentCertIndex, 3, unicodeBuf); |
---|
101 | |
---|
102 | currentCertIndex++; |
---|
103 | } |
---|
104 | if (certs->count > 0) { |
---|
105 | ListView_SetItemState(certificatesList, 0, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | INT_PTR CALLBACK CertSelectionDialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { |
---|
110 | static char* selectedCertID; |
---|
111 | |
---|
112 | switch(message) { |
---|
113 | case WM_INITDIALOG: |
---|
114 | HWND previousWindowsHandle; |
---|
115 | |
---|
116 | EstEID_log("current focus is @ %08X", GetFocus()); |
---|
117 | |
---|
118 | WCHAR label[128]; |
---|
119 | MultiByteToWideChar(CP_UTF8, 0, l10n("Select certificate"), -1, label, sizeof(label) / sizeof(WCHAR)); |
---|
120 | SetWindowText(hwnd, label); |
---|
121 | |
---|
122 | MultiByteToWideChar(CP_UTF8, 0, l10n("Select"), -1, label, sizeof(label) / sizeof(WCHAR)); |
---|
123 | SetDlgItemText(hwnd, IDOK, label); |
---|
124 | |
---|
125 | MultiByteToWideChar(CP_UTF8, 0, l10n("Cancel"), -1, label, sizeof(label) / sizeof(WCHAR)); |
---|
126 | SetDlgItemText(hwnd, IDCANCEL, label); |
---|
127 | |
---|
128 | MultiByteToWideChar(CP_UTF8, 0, l10n("By selecting a certificate I accept that my name and personal ID code will be sent to service provider."), -1, label, sizeof(label) / sizeof(WCHAR)); |
---|
129 | SetDlgItemText(hwnd, IDC_WARNING, label); |
---|
130 | |
---|
131 | buildCertificatesList(hwnd); |
---|
132 | selectedCertID = (char*)lParam; |
---|
133 | selectedCertID[0] = '\0'; |
---|
134 | |
---|
135 | EstEID_log("setting focus to certificate selection dialog, hwnd = %08X", hwnd); |
---|
136 | previousWindowsHandle = SetFocus(hwnd); |
---|
137 | if(!previousWindowsHandle) { |
---|
138 | EstEID_log("focus setting returned without previous windows handle, error code = %li", GetLastError()); |
---|
139 | } |
---|
140 | else { |
---|
141 | EstEID_log("focus taken from %08X and set to %08X", previousWindowsHandle, hwnd); |
---|
142 | } |
---|
143 | |
---|
144 | return true; |
---|
145 | case WM_COMMAND: |
---|
146 | switch(LOWORD(wParam)) { |
---|
147 | case IDOK: { |
---|
148 | int index = ListView_GetNextItem(GetDlgItem(hwnd, IDC_CERTIFICATESLIST), -1, LVNI_SELECTED); |
---|
149 | TCHAR unicodeSelectedCertID[256]; |
---|
150 | ListView_GetItemText(GetDlgItem(hwnd, IDC_CERTIFICATESLIST), index, 3, (LPTSTR)unicodeSelectedCertID, 33); |
---|
151 | strcpy(selectedCertID, CW2A(unicodeSelectedCertID)); |
---|
152 | EstEID_log("selected certificate id in dialog proc: %s", selectedCertID); |
---|
153 | } |
---|
154 | default: |
---|
155 | EndDialog(hwnd, LOWORD(wParam)); |
---|
156 | } |
---|
157 | return true; |
---|
158 | case WM_NOTIFY: |
---|
159 | if(wParam == IDC_CERTIFICATESLIST) { |
---|
160 | switch(((LPNMHDR)lParam)->code) { |
---|
161 | case LVN_ITEMCHANGED: |
---|
162 | case NM_CLICK: |
---|
163 | Button_Enable(GetDlgItem(hwnd, IDOK), ((LPNMITEMACTIVATE)lParam)->iItem != -1); |
---|
164 | break; |
---|
165 | case LVN_ITEMACTIVATE: { |
---|
166 | int selectedCertIndex = ((LPNMITEMACTIVATE)lParam)->iItem; |
---|
167 | TCHAR unicodeSelectedCertID[256]; |
---|
168 | ListView_GetItemText(GetDlgItem(hwnd, IDC_CERTIFICATESLIST), selectedCertIndex, 3, (LPTSTR)unicodeSelectedCertID, 33); |
---|
169 | strcpy(selectedCertID, CW2A(unicodeSelectedCertID)); |
---|
170 | EstEID_log("dblcklicked certificate id in dialog proc: %s", selectedCertID); |
---|
171 | EndDialog(hwnd, LOWORD(wParam)); |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | return true; |
---|
176 | default: |
---|
177 | return false; |
---|
178 | } |
---|
179 | } |
---|