annotate cinst/windowsstore.c @ 185:ee37c085b9f7

Try to handle crypt32 errors. Print the error code as fallback
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 25 Mar 2014 18:03:49 +0000
parents 8cfcd38a9bb3
children 292e2cb60ef0
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
161
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
3 #include <stdio.h>
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
4
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
5 #include "windowsstore.h"
161
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
6 #include "errorcodes.h"
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
7 #include "listutil.h"
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
8 #include "strhelp.h"
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
9
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
10 static LPWSTR getLastErrorMsg()
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
11 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
12 LPWSTR bufPtr = NULL;
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
13 DWORD err = GetLastError();
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
14 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
15 FORMAT_MESSAGE_FROM_SYSTEM |
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
16 FORMAT_MESSAGE_IGNORE_INSERTS,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
17 NULL, err, 0, (LPWSTR) &bufPtr, 0, NULL);
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
18 if (!bufPtr)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
19 {
185
ee37c085b9f7 Try to handle crypt32 errors. Print the error code as fallback
Andre Heinecke <aheinecke@intevation.de>
parents: 163
diff changeset
20 HMODULE hWinhttp = GetModuleHandleW (L"crypt32");
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
21 if (hWinhttp)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
22 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
23 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
24 FORMAT_MESSAGE_FROM_HMODULE |
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
25 FORMAT_MESSAGE_IGNORE_INSERTS,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
26 hWinhttp, HRESULT_CODE (err), 0,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
27 (LPWSTR) &bufPtr, 0, NULL);
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
28 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
29 }
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
30 if (!bufPtr)
185
ee37c085b9f7 Try to handle crypt32 errors. Print the error code as fallback
Andre Heinecke <aheinecke@intevation.de>
parents: 163
diff changeset
31 printf ("Error getting last error for code: %lx \n", err);
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
32 return bufPtr;
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
33 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
34
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
35 int write_stores_win (char **to_install, char **to_remove, bool user_store)
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
36 {
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
37 int i = 0;
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
38 int ret = -1;
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
39 HCERTSTORE hStore = NULL;
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
40
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
41 if (user_store)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
42 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
43 hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
44 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root");
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
45 }
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
46 else
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
47 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
48 hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
49 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root");
137
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
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
52 if (!hStore)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
53 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
54 return ERR_STORE_ACCESS_DENIED;
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
55 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
56
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
57 for (i=0; to_install[i]; i++)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
58 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
59 size_t cert_len = strnlen (to_install[i], MAX_LINE_LENGTH),
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
60 buf_size = 0;
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
61 char *buf = NULL;
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
62
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
63 ret = str_base64_decode (&buf, &buf_size, to_install[i], cert_len);
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
64
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
65 if (ret != 0)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
66 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
67 return ERR_INVALID_INSTRUCTIONS;
137
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
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
70 ret = CertAddEncodedCertificateToStore (hStore,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
71 X509_ASN_ENCODING,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
72 (PBYTE) buf,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
73 buf_size,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
74 CERT_STORE_ADD_ALWAYS,
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
75 NULL);
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
76
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
77 if (ret == 0)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
78 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
79 LPWSTR error = getLastErrorMsg();
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
80 if (error)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
81 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
82 printf ("Failed to add certificate: %S \n", error);
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
83 LocalFree (error);
137
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 }
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
86 i++;
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
87 free (buf);
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
88 }
149
bd5a5d3e5674 We decided to use bool. So let's use it.
Andre Heinecke <aheinecke@intevation.de>
parents: 137
diff changeset
89
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
90 for (i=0; to_remove[i]; i++)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
91 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
92 // TODO
161
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
93 }
a4b1c77f3e6a Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents: 149
diff changeset
94
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
95 if (hStore)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
96 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
97 CertCloseStore (hStore, 0);
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
98 }
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
99 return 0;
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
100 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
101 #endif // WIN32

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