Mercurial > trustbridge
annotate cinst/windowsstore.c @ 153:252ffe6e27fd
Changed type of MainWindow from QDialog to QMainWindow.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Tue, 25 Mar 2014 08:58:18 +0100 |
parents | 4904fe01055d |
children | bd5a5d3e5674 |
rev | line source |
---|---|
137
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
1 #ifdef WIN32 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
2 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
3 #include <polarssl/base64.h> |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
4 #include "windowsstore.h" |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
5 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
6 static LPWSTR getLastErrorMsg() { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
7 LPWSTR bufPtr = NULL; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
8 DWORD err = GetLastError(); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
9 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
10 FORMAT_MESSAGE_FROM_SYSTEM | |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
11 FORMAT_MESSAGE_IGNORE_INSERTS, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
12 NULL, err, 0, (LPWSTR)&bufPtr, 0, NULL); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
13 if (!bufPtr) { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
14 HMODULE hWinhttp = GetModuleHandleW(L"winhttp"); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
15 if (hWinhttp) { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
16 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
17 FORMAT_MESSAGE_FROM_HMODULE | |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
18 FORMAT_MESSAGE_IGNORE_INSERTS, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
19 hWinhttp, HRESULT_CODE(err), 0, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
20 (LPWSTR)&bufPtr, 0, NULL); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
21 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
22 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
23 if (!bufPtr) |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
24 printf("Error getting last error\n"); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
25 return bufPtr; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
26 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
27 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
28 int install_certificates_win(const char **to_install, int user_store) |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
29 { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
30 int i = 0; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
31 HCERTSTORE hStore = NULL; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
32 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
33 if (user_store) { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
34 // Access user store |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
35 hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
36 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root"); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
37 } else { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
38 // Access machine store |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
39 hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
40 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root"); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
41 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
42 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
43 if (!hStore) { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
44 return ERR_STORE_ACCESS_DENIED; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
45 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
46 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
47 while (to_install[i]) { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
48 size_t needed_len = 0; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
49 size_t cert_len = strnlen(to_install[i], MAX_LINE_LENGTH); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
50 int ret = -1; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
51 unsigned char *buf; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
52 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
53 /* Check the needed size for the buffer */ |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
54 ret = base64_decode(NULL, &needed_len, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
55 (unsigned char *)to_install[i], cert_len); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
56 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
57 if (ret != 0 && ret != POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
58 return ERR_INVALID_INSTRUCTIONS; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
59 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
60 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
61 buf = xmalloc(needed_len); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
62 memset (buf, 0, needed_len); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
63 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
64 ret = base64_decode(buf, &needed_len, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
65 (unsigned char *)to_install[i], cert_len); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
66 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
67 if (ret != 0) { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
68 return ERR_INVALID_INSTRUCTIONS; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
69 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
70 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
71 ret = CertAddEncodedCertificateToStore (hStore, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
72 X509_ASN_ENCODING, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
73 (PBYTE)buf, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
74 needed_len, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
75 CERT_STORE_ADD_ALWAYS, |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
76 NULL); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
77 |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
78 if (ret == 0) { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
79 LPWSTR error = getLastErrorMsg(); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
80 if (error) { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
81 printf("Failed to add certificate: %S \n", error); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
82 LocalFree(error); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
83 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
84 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
85 i++; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
86 free(buf); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
87 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
88 if(hStore) { |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
89 CertCloseStore(hStore, 0); |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
90 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
91 return 0; |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
92 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
93 #endif // WIN32 |