comparison cinst/windowsstore.c @ 188:a3bde2aaabd9

merged.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 26 Mar 2014 09:12:10 +0100
parents ee37c085b9f7
children 292e2cb60ef0
comparison
equal deleted inserted replaced
187:0c06a608e15f 188:a3bde2aaabd9
1 #ifdef WIN32 1 #ifdef WIN32
2 2
3 #include <polarssl/base64.h> 3 #include <stdio.h>
4
4 #include "windowsstore.h" 5 #include "windowsstore.h"
6 #include "errorcodes.h"
7 #include "listutil.h"
8 #include "strhelp.h"
5 9
6 static LPWSTR getLastErrorMsg() { 10 static LPWSTR getLastErrorMsg()
7 LPWSTR bufPtr = NULL; 11 {
8 DWORD err = GetLastError(); 12 LPWSTR bufPtr = NULL;
9 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | 13 DWORD err = GetLastError();
10 FORMAT_MESSAGE_FROM_SYSTEM | 14 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
11 FORMAT_MESSAGE_IGNORE_INSERTS, 15 FORMAT_MESSAGE_FROM_SYSTEM |
12 NULL, err, 0, (LPWSTR)&bufPtr, 0, NULL); 16 FORMAT_MESSAGE_IGNORE_INSERTS,
13 if (!bufPtr) { 17 NULL, err, 0, (LPWSTR) &bufPtr, 0, NULL);
14 HMODULE hWinhttp = GetModuleHandleW(L"winhttp"); 18 if (!bufPtr)
15 if (hWinhttp) { 19 {
16 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | 20 HMODULE hWinhttp = GetModuleHandleW (L"crypt32");
17 FORMAT_MESSAGE_FROM_HMODULE | 21 if (hWinhttp)
18 FORMAT_MESSAGE_IGNORE_INSERTS, 22 {
19 hWinhttp, HRESULT_CODE(err), 0, 23 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER |
20 (LPWSTR)&bufPtr, 0, NULL); 24 FORMAT_MESSAGE_FROM_HMODULE |
25 FORMAT_MESSAGE_IGNORE_INSERTS,
26 hWinhttp, HRESULT_CODE (err), 0,
27 (LPWSTR) &bufPtr, 0, NULL);
21 } 28 }
22 } 29 }
23 if (!bufPtr) 30 if (!bufPtr)
24 printf("Error getting last error\n"); 31 printf ("Error getting last error for code: %lx \n", err);
25 return bufPtr; 32 return bufPtr;
26 } 33 }
27 34
28 int install_certificates_win(const char **to_install, bool user_store) 35 int write_stores_win (char **to_install, char **to_remove, bool user_store)
29 { 36 {
30 int i = 0; 37 int i = 0;
31 HCERTSTORE hStore = NULL; 38 int ret = -1;
39 HCERTSTORE hStore = NULL;
32 40
33 if (user_store) { 41 if (user_store)
34 hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 42 {
35 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root"); 43 hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0,
36 } else { 44 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root");
37 hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 45 }
38 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root"); 46 else
47 {
48 hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0,
49 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root");
39 } 50 }
40 51
41 if (!hStore) { 52 if (!hStore)
42 return ERR_STORE_ACCESS_DENIED; 53 {
54 return ERR_STORE_ACCESS_DENIED;
43 } 55 }
44 56
45 while (to_install[i]) { 57 for (i=0; to_install[i]; i++)
46 size_t needed_len = 0; 58 {
47 size_t cert_len = strnlen(to_install[i], MAX_LINE_LENGTH); 59 size_t cert_len = strnlen (to_install[i], MAX_LINE_LENGTH),
48 int ret = -1; 60 buf_size = 0;
49 unsigned char *buf; 61 char *buf = NULL;
50 62
51 /* Check the needed size for the buffer */ 63 ret = str_base64_decode (&buf, &buf_size, to_install[i], cert_len);
52 ret = base64_decode(NULL, &needed_len,
53 (unsigned char *)to_install[i], cert_len);
54 64
55 if (ret != 0 && ret != POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) { 65 if (ret != 0)
56 return ERR_INVALID_INSTRUCTIONS; 66 {
67 return ERR_INVALID_INSTRUCTIONS;
57 } 68 }
58 69
59 buf = xmalloc(needed_len); 70 ret = CertAddEncodedCertificateToStore (hStore,
60 memset (buf, 0, needed_len); 71 X509_ASN_ENCODING,
72 (PBYTE) buf,
73 buf_size,
74 CERT_STORE_ADD_ALWAYS,
75 NULL);
61 76
62 ret = base64_decode(buf, &needed_len, 77 if (ret == 0)
63 (unsigned char *)to_install[i], cert_len); 78 {
64 79 LPWSTR error = getLastErrorMsg();
65 if (ret != 0) { 80 if (error)
66 return ERR_INVALID_INSTRUCTIONS; 81 {
67 } 82 printf ("Failed to add certificate: %S \n", error);
68 83 LocalFree (error);
69 ret = CertAddEncodedCertificateToStore (hStore,
70 X509_ASN_ENCODING,
71 (PBYTE)buf,
72 needed_len,
73 CERT_STORE_ADD_ALWAYS,
74 NULL);
75
76 if (ret == 0) {
77 LPWSTR error = getLastErrorMsg();
78 if (error) {
79 printf("Failed to add certificate: %S \n", error);
80 LocalFree(error);
81 } 84 }
82 } 85 }
83 i++; 86 i++;
84 free(buf); 87 free (buf);
85 } 88 }
86 89
87 if(hStore) { 90 for (i=0; to_remove[i]; i++)
88 CertCloseStore(hStore, 0); 91 {
92 // TODO
89 } 93 }
90 return 0; 94
95 if (hStore)
96 {
97 CertCloseStore (hStore, 0);
98 }
99 return 0;
91 } 100 }
92 #endif // WIN32 101 #endif // WIN32

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