-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcert.cpp
executable file
·218 lines (203 loc) · 5.97 KB
/
cert.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Displays certificates for remote callbacks.
*/
#include "stdafx.h"
typedef struct _LGitCertDialogParams {
LGitContext *ctx;
const char *host;
git_cert *cert;
} LGitCertDialogParams;
extern "C" {
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "cryptui.lib")
/* This is defined in Vista+. Check if they're defined. */
typedef struct tagCRYPTUI_VIEWCERTIFICATE_STRUCTA {
DWORD dwSize;
HWND hwndParent;
DWORD dwFlags;
LPCTSTR szTitle;
PCCERT_CONTEXT pCertContext;
DWORD param6;
DWORD param7;
DWORD param8;
DWORD param9;
DWORD param10;
DWORD param11;
DWORD param12;
DWORD param13;
DWORD param14;
DWORD param15;
DWORD param16;
DWORD param17;
DWORD param18;
} CRYPTUI_VIEWCERTIFICATE_STRUCTA, *PCCRYPTUI_VIEWCERTIFICATE_STRUCT;
typedef struct tagCRYPTUI_VIEWCERTIFICATE_STRUCTW {
DWORD dwSize;
HWND hwndParent;
DWORD dwFlags;
LPCWSTR szTitle;
PCCERT_CONTEXT pCertContext;
DWORD param6;
DWORD param7;
DWORD param8;
DWORD param9;
DWORD param10;
DWORD param11;
DWORD param12;
DWORD param13;
DWORD param14;
DWORD param15;
DWORD param16;
DWORD param17;
DWORD param18;
} CRYPTUI_VIEWCERTIFICATE_STRUCTW, *PCCRYPTUI_VIEWCERTIFICATE_STRUCTW;
WINCRYPT32API
BOOL
WINAPI
CryptUIDlgViewCertificateA(CRYPTUI_VIEWCERTIFICATE_STRUCTA* vcs, BOOL *ps);
WINCRYPT32API
BOOL
WINAPI
CryptUIDlgViewCertificateW(CRYPTUI_VIEWCERTIFICATE_STRUCTW* vcs, BOOL *ps);
#ifdef UNICODE
#define CRYPTUI_VIEWCERTIFICATE_STRUCT CRYPTUI_VIEWCERTIFICATE_STRUCTW
#define PCCRYPTUI_VIEWCERTIFICATE_STRUCT PCCRYPTUI_VIEWCERTIFICATE_STRUCTW
#define CryptUIDlgViewCertificate CryptUIDlgViewCertificateW
#else
#define CRYPTUI_VIEWCERTIFICATE_STRUCT CRYPTUI_VIEWCERTIFICATE_STRUCTA
#define PCCRYPTUI_VIEWCERTIFICATE_STRUCT PCCRYPTUI_VIEWCERTIFICATE_STRUCTA
#define CryptUIDlgViewCertificate CryptUIDlgViewCertificateA
#endif
}
typedef BOOL (WINAPI *DlgViewCertFunc)(CRYPTUI_VIEWCERTIFICATE_STRUCTA*,BOOL*);
/* Workaround for FX!32 */
static BOOL CryptUIDlgViewCertificateWrapper(CRYPTUI_VIEWCERTIFICATE_STRUCTA* vcs, BOOL *ps)
{
static BOOL wrapper_init = FALSE;
static HMODULE cryptui = NULL;
static DlgViewCertFunc func = NULL;
if (!wrapper_init) {
cryptui = LoadLibraryEx("cryptui.dll", NULL, 0);
if (cryptui == NULL) {
LGitLog("!! Failed to load cryptui.dll (%x)", GetLastError());
return FALSE;
}
func = (DlgViewCertFunc)GetProcAddress(cryptui, "CryptUIDlgViewCertificateA");
if (func == NULL) {
LGitLog("!! Failed to load CryptUIDlgViewCertificateA (%x)", GetLastError());
return FALSE;
}
wrapper_init = TRUE;
}
return func(vcs, ps);
}
static void DisplayCertificate(HWND parent,
git_cert_x509 *x509,
const char *host)
{
/* create a certificate store */
HCERTSTORE hMemStore;
OutputDebugString(" ! Opening cert store\n");
hMemStore = CertOpenStore(
CERT_STORE_PROV_MEMORY, // the memory provider type
0, // the encoding type is not needed
NULL, // use the default HCRYPTPROV
0, // accept the default dwFlags
NULL // pvPara is not used
);
/* now the certificate... */
PCCERT_CONTEXT cert_ctx;
OutputDebugString(" ! Adding encoded cert\n");
if (!CertAddEncodedCertificateToStore(hMemStore,
X509_ASN_ENCODING,
(BYTE*)x509->data,
x509->len,
CERT_STORE_ADD_ALWAYS,
&cert_ctx)) {
OutputDebugString("!! Error adding encoded cert\n");
goto fin_close;
}
/* now create the dialog */
BOOL changed;
CRYPTUI_VIEWCERTIFICATE_STRUCT vc_st;
ZeroMemory(&vc_st, sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCT));
vc_st.dwSize = sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCT);
vc_st.pCertContext = cert_ctx;
vc_st.szTitle = host;
vc_st.hwndParent = parent;
OutputDebugString(" ! Displaying cert\n");
if (!CryptUIDlgViewCertificateWrapper(&vc_st, &changed)) {
OutputDebugString("!! Error displaying UI\n");
}
/* cleanup */
CertFreeCertificateContext(cert_ctx);
fin_close:
CertCloseStore(hMemStore, 0);
}
static void InitCertView(HWND hwnd, LGitCertDialogParams *params)
{
/* Could be fancier, I guess */
HICON warning_icon = LoadIcon(NULL, IDI_WARNING);
SendDlgItemMessage(hwnd, IDC_CERT_PROMPT_ICON, STM_SETICON, (WPARAM)warning_icon, 0);
if (params->cert->cert_type != GIT_CERT_X509) {
HWND view_cert = GetDlgItem(hwnd, IDC_VIEW_CERT);
EnableWindow(view_cert, FALSE);
}
wchar_t msg[512], fmt[256], host[256];
LGitUtf8ToWide(params->host, host, 256);
GetDlgItemTextW(hwnd, IDC_CERT_MESSAGE, fmt, 256);
_snwprintf(msg, 512, fmt, host);
SetDlgItemTextW(hwnd, IDC_CERT_MESSAGE, msg);
}
static BOOL CALLBACK CertDialogProc(HWND hwnd,
unsigned int iMsg,
WPARAM wParam,
LPARAM lParam)
{
LGitCertDialogParams *param;
switch (iMsg) {
case WM_INITDIALOG:
param = (LGitCertDialogParams*)lParam;
SetWindowLong(hwnd, GWL_USERDATA, (long)param); /* XXX: 64-bit... */
InitCertView(hwnd, param);
return TRUE;
case WM_COMMAND:
param = (LGitCertDialogParams*)GetWindowLong(hwnd, GWL_USERDATA);
switch (LOWORD(wParam)) {
case IDC_VIEW_CERT:
DisplayCertificate(hwnd, (git_cert_x509*)param->cert, param->host);
return TRUE;
case IDOK:
EndDialog(hwnd, 2);
return TRUE;
case IDCANCEL:
EndDialog(hwnd, 1);
return TRUE;
}
return FALSE;
default:
return FALSE;
}
}
BOOL LGitCertificatePrompt(LGitContext *ctx,
HWND parent,
git_cert *cert,
const char *host)
{
LGitCertDialogParams params;
params.ctx = ctx;
params.host = host;
params.cert = cert;
switch (DialogBoxParamW(ctx->dllInst,
MAKEINTRESOURCEW(IDD_CERT_PROMPT),
parent,
CertDialogProc,
(LPARAM)¶ms)) {
default:
LGitLog(" ! Uh-oh, dialog error\n");
case 1:
return FALSE;
case 2:
return TRUE;
}
}