Mercurial > trustbridge
annotate cinst/windowsstore.c @ 258:bf8c74992724
Fixed numbering of errors.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Tue, 01 Apr 2014 14:34:24 +0200 |
parents | 4de97f74d038 |
children | 3595ea4fd3fb |
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 |
222
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
10 static LPWSTR |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
11 getLastErrorMsg() |
163
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
12 { |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
13 LPWSTR bufPtr = NULL; |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
14 DWORD err = GetLastError(); |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
15 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
16 FORMAT_MESSAGE_FROM_SYSTEM | |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
17 FORMAT_MESSAGE_IGNORE_INSERTS, |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
18 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
|
19 if (!bufPtr) |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
20 { |
185
ee37c085b9f7
Try to handle crypt32 errors. Print the error code as fallback
Andre Heinecke <aheinecke@intevation.de>
parents:
163
diff
changeset
|
21 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
|
22 if (hWinhttp) |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
23 { |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
24 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
25 FORMAT_MESSAGE_FROM_HMODULE | |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
26 FORMAT_MESSAGE_IGNORE_INSERTS, |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
27 hWinhttp, HRESULT_CODE (err), 0, |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
28 (LPWSTR) &bufPtr, 0, NULL); |
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 } |
163
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
31 if (!bufPtr) |
185
ee37c085b9f7
Try to handle crypt32 errors. Print the error code as fallback
Andre Heinecke <aheinecke@intevation.de>
parents:
163
diff
changeset
|
32 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
|
33 return bufPtr; |
137
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
34 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
35 |
222
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
36 static PCCERT_CONTEXT |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
37 b64_to_cert_context(char *b64_data, size_t b64_size) |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
38 { |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
39 size_t buf_size = 0; |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
40 char *buf = NULL; |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
41 PCCERT_CONTEXT pCert = NULL; |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
42 int ret = -1; |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
43 |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
44 ret = str_base64_decode (&buf, &buf_size, b64_data, b64_size); |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
45 |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
46 if (ret != 0) |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
47 { |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
48 printf ("decoding certificate failed\n"); |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
49 return NULL; |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
50 } |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
51 |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
52 pCert = CertCreateContext (CERT_STORE_CERTIFICATE_CONTEXT, |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
53 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
54 (const PBYTE) buf, |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
55 (DWORD) buf_size, |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
56 0, |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
57 NULL); |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
58 free (buf); /* Windows has a copy */ |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
59 |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
60 if (pCert == NULL) |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
61 { |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
62 LPWSTR error = getLastErrorMsg(); |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
63 if (error) |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
64 { |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
65 printf ("Failed to create cert context: %S \n", error); |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
66 LocalFree (error); |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
67 } |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
68 return NULL; |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
69 } |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
70 return pCert; |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
71 } |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
72 |
219
57bef180d560
Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents:
218
diff
changeset
|
73 void |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
74 do_remove(HCERTSTORE hStore, char **to_remove) |
137
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
75 { |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
76 PCCERT_CONTEXT pCert = NULL; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
77 unsigned int i = 0; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
78 |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
79 if (!to_remove) |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
80 { |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
81 return; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
82 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
83 |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
84 for (i=0; to_remove[i]; i++) |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
85 { |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
86 PCCERT_CONTEXT pc_to_remove = NULL; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
87 |
222
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
88 pc_to_remove = b64_to_cert_context(to_remove[i], |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
89 strnlen(to_remove[i], MAX_LINE_LENGTH)); |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
90 |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
91 if (pc_to_remove == NULL) |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
92 { |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
93 LPWSTR error = getLastErrorMsg(); |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
94 if (error) |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
95 { |
222
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
96 printf ("Failed to create cert context: %S \n", error); |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
97 LocalFree (error); |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
98 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
99 continue; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
100 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
101 |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
102 pCert = CertFindCertificateInStore (hStore, |
218
8fb12af98960
According to MSDN you Must also provide message encoding flag.
Andre Heinecke <andre.heinecke@intevation.de>
parents:
217
diff
changeset
|
103 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
104 0, |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
105 CERT_FIND_EXISTING, |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
106 pc_to_remove, |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
107 NULL); |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
108 |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
109 CertFreeCertificateContext (pc_to_remove); |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
110 |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
111 if (pCert == NULL) |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
112 { |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
113 printf ("Did not find certificate\n"); |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
114 continue; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
115 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
116 |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
117 if (!CertDeleteCertificateFromStore (pCert)) |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
118 { |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
119 /* From MSDN: |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
120 The CertDeleteCertificateFromStore function always frees |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
121 pCertContext by calling the CertFreeCertificateContext |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
122 function, even if an error is encountered. */ |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
123 LPWSTR error = getLastErrorMsg(); |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
124 printf ("Error deleting certificate. %S", error); |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
125 LocalFree (error); |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
126 continue; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
127 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
128 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
129 return; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
130 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
131 |
219
57bef180d560
Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents:
218
diff
changeset
|
132 void |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
133 do_install(HCERTSTORE hStore, char **to_install) |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
134 { |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
135 int i = 0, |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
136 ret = -1; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
137 |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
138 if (!to_install) |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
139 { |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
140 return; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
141 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
142 |
217
83a015f2e078
Remove leftover incrementation of i from former loop style. Fixes a crash when adding only one
Andre Heinecke <andre.heinecke@intevation.de>
parents:
215
diff
changeset
|
143 for (i = 0; to_install[i]; i++) |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
144 { |
222
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
145 PCCERT_CONTEXT pc_to_add = NULL; |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
146 PCCERT_CONTEXT found_cert = NULL; |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
147 |
222
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
148 pc_to_add = b64_to_cert_context(to_install[i], |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
149 strnlen(to_install[i], MAX_LINE_LENGTH)); |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
150 |
222
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
151 if (pc_to_add == NULL) |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
152 { |
222
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
153 continue; |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
154 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
155 |
222
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
156 found_cert = CertFindCertificateInStore (hStore, |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
157 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
158 0, |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
159 CERT_FIND_EXISTING, |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
160 pc_to_add, |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
161 NULL); |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
162 if (found_cert != NULL) |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
163 { |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
164 printf ("Certificate already in store\n"); |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
165 CertFreeCertificateContext (found_cert); |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
166 CertFreeCertificateContext (pc_to_add); |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
167 continue; |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
168 } |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
169 |
222
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
170 ret = CertAddCertificateContextToStore (hStore, |
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
171 pc_to_add, |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
172 CERT_STORE_ADD_ALWAYS, |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
173 NULL); |
222
53ea9b975d1c
Cleanup windowsstore.c
Andre Heinecke <aheinecke@intevation.de>
parents:
219
diff
changeset
|
174 CertFreeCertificateContext (pc_to_add); |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
175 if (!ret) |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
176 { |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
177 LPWSTR error = getLastErrorMsg(); |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
178 if (error) |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
179 { |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
180 printf ("Failed to add certificate: %S \n", error); |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
181 LocalFree (error); |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
182 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
183 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
184 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
185 return; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
186 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
187 |
247
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
188 static bool is_elevated() { |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
189 HANDLE hToken = NULL; |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
190 bool ret = false; |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
191 if (OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &hToken)) |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
192 { |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
193 TOKEN_ELEVATION elevation; |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
194 DWORD cbSize = sizeof (TOKEN_ELEVATION); |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
195 if (GetTokenInformation (hToken, TokenElevation, &elevation, |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
196 sizeof (TokenElevation), &cbSize)) |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
197 { |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
198 ret = elevation.TokenIsElevated; |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
199 } |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
200 } |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
201 if (hToken) |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
202 CloseHandle (hToken); |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
203 |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
204 return ret; |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
205 } |
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
206 |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
207 int |
247
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
208 write_stores_win (char **to_install, char **to_remove) |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
209 { |
163
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
210 HCERTSTORE hStore = NULL; |
137
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
211 |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
212 if (!to_install && !to_remove) |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
213 { |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
214 /* Nothing to do */ |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
215 return 0; |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
216 } |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
217 |
247
4de97f74d038
Check for process elevation and write into system store accordingly
Andre Heinecke <aheinecke@intevation.de>
parents:
222
diff
changeset
|
218 if (!is_elevated()) |
163
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
219 { |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
220 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
|
221 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
|
222 } |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
223 else |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
224 { |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
225 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
|
226 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root"); |
137
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
227 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
228 |
163
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
229 if (!hStore) |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
230 { |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
231 return ERR_STORE_ACCESS_DENIED; |
137
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
232 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
233 |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
234 /* Do the actual work */ |
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
235 do_install (hStore, to_install); |
137
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
236 |
215
292e2cb60ef0
Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents:
185
diff
changeset
|
237 do_remove (hStore, to_remove); |
161
a4b1c77f3e6a
Change install_certificates_win to generic write_stores_win
Andre Heinecke <aheinecke@intevation.de>
parents:
149
diff
changeset
|
238 |
163
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
239 if (hStore) |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
240 { |
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
241 CertCloseStore (hStore, 0); |
137
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
242 } |
163
8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents:
161
diff
changeset
|
243 return 0; |
137
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
244 } |
4904fe01055d
Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
245 #endif // WIN32 |