annotate cinst/windowsstore.c @ 219:57bef180d560

Add debug output and make windowsstore linkable from C++ code
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 26 Mar 2014 20:16:50 +0100
parents 8fb12af98960
children 53ea9b975d1c
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
219
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
35 void
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
36 do_remove(HCERTSTORE hStore, char **to_remove)
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
37 {
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
38 PCCERT_CONTEXT pCert = NULL;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
39 unsigned int i = 0;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
40
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
41 if (!to_remove)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
42 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
43 return;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
44 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
45
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
46 for (i=0; to_remove[i]; i++)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
47 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
48 char *asn1_data = NULL;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
49 size_t asn1_size = 0;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
50 int ret = -1;
219
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
51 DWORD j;
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
52 PCCERT_CONTEXT pc_to_remove = NULL;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
53
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
54 ret = str_base64_decode (&asn1_data, &asn1_size, to_remove[i],
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
55 strnlen(to_remove[i], MAX_LINE_LENGTH));
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
56 /* Decoding / parsing errors in here should not happen at all.
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
57 The only errors which are not a bug would be out of memory or
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
58 if the signed certificate list contained an invalid certificate. */
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
59 if (ret != 0)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
60 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
61 printf ("Error base64 certificate.\n");
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
62 continue;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
63 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
64
219
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
65 printf ("Deleting cert %s\n", to_remove[i]);
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
66 pc_to_remove = CertCreateContext (CERT_STORE_CERTIFICATE_CONTEXT,
218
8fb12af98960 According to MSDN you Must also provide message encoding flag.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 217
diff changeset
67 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
68 (const PBYTE) asn1_data,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
69 (DWORD) asn1_size,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
70 0,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
71 NULL);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
72 free (asn1_data); /* Windows has a copy */
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
73 if (pc_to_remove == NULL)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
74 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
75 LPWSTR error = getLastErrorMsg();
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
76 if (error)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
77 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
78 printf ("Failed to add certificate: %S \n", error);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
79 LocalFree (error);
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 continue;
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 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
85 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
86 0,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
87 CERT_FIND_EXISTING,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
88 pc_to_remove,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
89 NULL);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
90
219
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
91
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
92 {
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
93 char pszNameString[256];
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
94 if(CertGetNameString(
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
95 pc_to_remove,
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
96 CERT_NAME_SIMPLE_DISPLAY_TYPE,
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
97 0,
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
98 NULL,
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
99 pszNameString,
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
100 128))
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
101 {
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
102 printf("wanted: %s \n",pszNameString);
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
103 }
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
104 if(CertGetNameString(
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
105 pCert,
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
106 CERT_NAME_SIMPLE_DISPLAY_TYPE,
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
107 0,
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
108 NULL,
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
109 pszNameString,
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
110 128))
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
111 {
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
112 printf("got: %s \n",pszNameString);
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
113 }
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
114 }
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
115
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
116 CertFreeCertificateContext (pc_to_remove);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
117
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
118 if (pCert == NULL)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
119 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
120 printf ("Did not find certificate\n");
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
121 continue;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
122 }
219
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
123 for (j = 0; j < pCert->cbCertEncoded; j++) {
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
124 if (asn1_data[j] != pCert->pbCertEncoded[j]) {
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
125 printf("%1x", (unsigned)(unsigned char)pCert->pbCertEncoded[j]);
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
126 }
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
127 }
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
128 printf("\nWanted: \n");
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
129 for (j = 0; j < pCert->cbCertEncoded; j++) {
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
130 if (asn1_data[j] != pCert->pbCertEncoded[j]) {
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
131 printf("%1x", (unsigned)(unsigned char)asn1_data[j]);
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
132 }
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
133 }
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
134 printf("\n");
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
135
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
136 if (!CertDeleteCertificateFromStore (pCert))
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 /* From MSDN:
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
139 The CertDeleteCertificateFromStore function always frees
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
140 pCertContext by calling the CertFreeCertificateContext
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
141 function, even if an error is encountered. */
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
142 LPWSTR error = getLastErrorMsg();
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
143 printf ("Error deleting certificate. %S", error);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
144 LocalFree (error);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
145 continue;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
146 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
147 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
148 return;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
149 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
150
219
57bef180d560 Add debug output and make windowsstore linkable from C++ code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 218
diff changeset
151 void
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
152 do_install(HCERTSTORE hStore, char **to_install)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
153 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
154 int i = 0,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
155 ret = -1;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
156
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
157 if (!to_install)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
158 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
159 return;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
160 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
161
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
162 for (i = 0; to_install[i]; i++)
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
163 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
164 size_t cert_len = strnlen (to_install[i], MAX_LINE_LENGTH),
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
165 buf_size = 0;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
166 char *buf = NULL;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
167
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
168 ret = str_base64_decode (&buf, &buf_size, to_install[i], cert_len);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
169
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
170 if (ret != 0)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
171 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
172 printf ("decoding certificate failed\n");
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
173 return;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
174 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
175
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
176 printf ("Adding cert %s\n", to_install[i]);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
177
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
178 ret = CertAddEncodedCertificateToStore (hStore,
218
8fb12af98960 According to MSDN you Must also provide message encoding flag.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 217
diff changeset
179 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
180 (PBYTE) buf,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
181 buf_size,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
182 CERT_STORE_ADD_ALWAYS,
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
183 NULL);
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 if (!ret)
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 LPWSTR error = getLastErrorMsg();
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
188 if (error)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
189 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
190 printf ("Failed to add certificate: %S \n", error);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
191 LocalFree (error);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
192 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
193 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
194 free (buf);
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
195 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
196 return;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
197 }
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
198
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
199 int
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
200 write_stores_win (char **to_install, char **to_remove, bool user_store)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
201 {
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
202 HCERTSTORE hStore = NULL;
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
203
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
204 if (!to_install && !to_remove)
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
205 {
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
206 /* Nothing to do */
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
207 return 0;
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
208 }
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 if (user_store)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
211 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
212 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
213 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
214 }
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
215 else
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
216 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
217 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
218 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root");
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
219 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
220
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
221 if (!hStore)
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 return ERR_STORE_ACCESS_DENIED;
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
224 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
225
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
226 /* Do the actual work */
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
227 do_install (hStore, to_install);
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
228
215
292e2cb60ef0 Add removal of certificates
Andre Heinecke <andre.heinecke@intevation.de>
parents: 185
diff changeset
229 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
230
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
231 if (hStore)
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
232 {
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
233 CertCloseStore (hStore, 0);
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
234 }
163
8cfcd38a9bb3 Change coding style for cinst main / windowsstore to GNU
Andre Heinecke <aheinecke@intevation.de>
parents: 161
diff changeset
235 return 0;
137
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
236 }
4904fe01055d Factor out windows specific parts
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
237 #endif // WIN32

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