Mercurial > trustbridge
comparison cinst/windowsstore.c @ 161:a4b1c77f3e6a
Change install_certificates_win to generic write_stores_win
This function will also handle the removal to avoid
redundancies in store handling
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Tue, 25 Mar 2014 10:08:31 +0000 |
parents | bd5a5d3e5674 |
children | 8cfcd38a9bb3 |
comparison
equal
deleted
inserted
replaced
160:bf4bfd8843bd | 161:a4b1c77f3e6a |
---|---|
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 LPWSTR bufPtr = NULL; |
8 DWORD err = GetLastError(); | 12 DWORD err = GetLastError(); |
9 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | | 13 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
23 if (!bufPtr) | 27 if (!bufPtr) |
24 printf("Error getting last error\n"); | 28 printf("Error getting last error\n"); |
25 return bufPtr; | 29 return bufPtr; |
26 } | 30 } |
27 | 31 |
28 int install_certificates_win(const char **to_install, bool user_store) | 32 int write_stores_win(char **to_install, char **to_remove, bool user_store) |
29 { | 33 { |
30 int i = 0; | 34 int i = 0; |
35 int ret = -1; | |
31 HCERTSTORE hStore = NULL; | 36 HCERTSTORE hStore = NULL; |
32 | 37 |
33 if (user_store) { | 38 if (user_store) { |
34 hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, | 39 hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, |
35 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root"); | 40 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root"); |
40 | 45 |
41 if (!hStore) { | 46 if (!hStore) { |
42 return ERR_STORE_ACCESS_DENIED; | 47 return ERR_STORE_ACCESS_DENIED; |
43 } | 48 } |
44 | 49 |
45 while (to_install[i]) { | 50 for (i=0; to_install[i]; i++) { |
46 size_t needed_len = 0; | 51 size_t cert_len = strnlen(to_install[i], MAX_LINE_LENGTH), |
47 size_t cert_len = strnlen(to_install[i], MAX_LINE_LENGTH); | 52 buf_size = 0; |
48 int ret = -1; | 53 char *buf = NULL; |
49 unsigned char *buf; | |
50 | 54 |
51 /* Check the needed size for the buffer */ | 55 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 | |
55 if (ret != 0 && ret != POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) { | |
56 return ERR_INVALID_INSTRUCTIONS; | |
57 } | |
58 | |
59 buf = xmalloc(needed_len); | |
60 memset (buf, 0, needed_len); | |
61 | |
62 ret = base64_decode(buf, &needed_len, | |
63 (unsigned char *)to_install[i], cert_len); | |
64 | 56 |
65 if (ret != 0) { | 57 if (ret != 0) { |
66 return ERR_INVALID_INSTRUCTIONS; | 58 return ERR_INVALID_INSTRUCTIONS; |
67 } | 59 } |
68 | 60 |
69 ret = CertAddEncodedCertificateToStore (hStore, | 61 ret = CertAddEncodedCertificateToStore (hStore, |
70 X509_ASN_ENCODING, | 62 X509_ASN_ENCODING, |
71 (PBYTE)buf, | 63 (PBYTE)buf, |
72 needed_len, | 64 buf_size, |
73 CERT_STORE_ADD_ALWAYS, | 65 CERT_STORE_ADD_ALWAYS, |
74 NULL); | 66 NULL); |
75 | 67 |
76 if (ret == 0) { | 68 if (ret == 0) { |
77 LPWSTR error = getLastErrorMsg(); | 69 LPWSTR error = getLastErrorMsg(); |
82 } | 74 } |
83 i++; | 75 i++; |
84 free(buf); | 76 free(buf); |
85 } | 77 } |
86 | 78 |
79 for (i=0; to_remove[i]; i++) { | |
80 // TODO | |
81 } | |
82 | |
87 if(hStore) { | 83 if(hStore) { |
88 CertCloseStore(hStore, 0); | 84 CertCloseStore(hStore, 0); |
89 } | 85 } |
90 return 0; | 86 return 0; |
91 } | 87 } |