annotate cinst/windowsstore.c @ 165:d47de01d6ad7

Implemented port_realpath for windows.
author Sascha Wilde <wilde@intevation.de>
date Tue, 25 Mar 2014 12:25:39 +0100
parents bd5a5d3e5674
children a4b1c77f3e6a
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
149
bd5a5d3e5674 We decided to use bool. So let's use it.
Andre Heinecke <aheinecke@intevation.de>
parents: 137
diff changeset
28 int install_certificates_win(const char **to_install, bool user_store)
137
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 hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0,
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
35 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root");
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
36 } else {
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
37 hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0,
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
38 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root");
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
39 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
40
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
41 if (!hStore) {
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
42 return ERR_STORE_ACCESS_DENIED;
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
43 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
44
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
45 while (to_install[i]) {
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
46 size_t needed_len = 0;
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
47 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
48 int ret = -1;
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
49 unsigned char *buf;
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
50
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
51 /* Check the needed size for the buffer */
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
52 ret = base64_decode(NULL, &needed_len,
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
53 (unsigned char *)to_install[i], cert_len);
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
54
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
55 if (ret != 0 && ret != POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) {
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
56 return ERR_INVALID_INSTRUCTIONS;
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
57 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
58
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
59 buf = xmalloc(needed_len);
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
60 memset (buf, 0, needed_len);
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
61
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
62 ret = base64_decode(buf, &needed_len,
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
63 (unsigned char *)to_install[i], cert_len);
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
64
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
65 if (ret != 0) {
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
66 return ERR_INVALID_INSTRUCTIONS;
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
67 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
68
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
69 ret = CertAddEncodedCertificateToStore (hStore,
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
70 X509_ASN_ENCODING,
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
71 (PBYTE)buf,
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
72 needed_len,
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
73 CERT_STORE_ADD_ALWAYS,
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
74 NULL);
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
75
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
76 if (ret == 0) {
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
77 LPWSTR error = getLastErrorMsg();
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
78 if (error) {
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
79 printf("Failed to add certificate: %S \n", error);
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
80 LocalFree(error);
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
81 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
82 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
83 i++;
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
84 free(buf);
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
85 }
149
bd5a5d3e5674 We decided to use bool. So let's use it.
Andre Heinecke <aheinecke@intevation.de>
parents: 137
diff changeset
86
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
87 if(hStore) {
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
88 CertCloseStore(hStore, 0);
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
89 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
90 return 0;
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
91 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
92 #endif // WIN32

http://wald.intevation.org/projects/trustbridge/