aheinecke@137: #ifdef WIN32 aheinecke@137: aheinecke@161: #include aheinecke@161: aheinecke@137: #include "windowsstore.h" aheinecke@161: #include "errorcodes.h" aheinecke@161: #include "listutil.h" aheinecke@161: #include "strhelp.h" aheinecke@137: aheinecke@163: static LPWSTR getLastErrorMsg() aheinecke@163: { aheinecke@163: LPWSTR bufPtr = NULL; aheinecke@163: DWORD err = GetLastError(); aheinecke@163: FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | aheinecke@163: FORMAT_MESSAGE_FROM_SYSTEM | aheinecke@163: FORMAT_MESSAGE_IGNORE_INSERTS, aheinecke@163: NULL, err, 0, (LPWSTR) &bufPtr, 0, NULL); aheinecke@163: if (!bufPtr) aheinecke@163: { aheinecke@185: HMODULE hWinhttp = GetModuleHandleW (L"crypt32"); aheinecke@163: if (hWinhttp) aheinecke@163: { aheinecke@163: FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | aheinecke@163: FORMAT_MESSAGE_FROM_HMODULE | aheinecke@163: FORMAT_MESSAGE_IGNORE_INSERTS, aheinecke@163: hWinhttp, HRESULT_CODE (err), 0, aheinecke@163: (LPWSTR) &bufPtr, 0, NULL); aheinecke@137: } aheinecke@137: } aheinecke@163: if (!bufPtr) aheinecke@185: printf ("Error getting last error for code: %lx \n", err); aheinecke@163: return bufPtr; aheinecke@137: } aheinecke@137: aheinecke@163: int write_stores_win (char **to_install, char **to_remove, bool user_store) aheinecke@137: { aheinecke@163: int i = 0; aheinecke@163: int ret = -1; aheinecke@163: HCERTSTORE hStore = NULL; aheinecke@137: aheinecke@163: if (user_store) aheinecke@163: { aheinecke@163: hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0, aheinecke@163: 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root"); aheinecke@163: } aheinecke@163: else aheinecke@163: { aheinecke@163: hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0, aheinecke@163: 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root"); aheinecke@137: } aheinecke@137: aheinecke@163: if (!hStore) aheinecke@163: { aheinecke@163: return ERR_STORE_ACCESS_DENIED; aheinecke@137: } aheinecke@137: aheinecke@163: for (i=0; to_install[i]; i++) aheinecke@163: { aheinecke@163: size_t cert_len = strnlen (to_install[i], MAX_LINE_LENGTH), aheinecke@163: buf_size = 0; aheinecke@163: char *buf = NULL; aheinecke@137: aheinecke@163: ret = str_base64_decode (&buf, &buf_size, to_install[i], cert_len); aheinecke@137: aheinecke@163: if (ret != 0) aheinecke@163: { aheinecke@163: return ERR_INVALID_INSTRUCTIONS; aheinecke@137: } aheinecke@137: aheinecke@163: ret = CertAddEncodedCertificateToStore (hStore, aheinecke@163: X509_ASN_ENCODING, aheinecke@163: (PBYTE) buf, aheinecke@163: buf_size, aheinecke@163: CERT_STORE_ADD_ALWAYS, aheinecke@163: NULL); aheinecke@137: aheinecke@163: if (ret == 0) aheinecke@163: { aheinecke@163: LPWSTR error = getLastErrorMsg(); aheinecke@163: if (error) aheinecke@163: { aheinecke@163: printf ("Failed to add certificate: %S \n", error); aheinecke@163: LocalFree (error); aheinecke@137: } aheinecke@137: } aheinecke@163: i++; aheinecke@163: free (buf); aheinecke@137: } aheinecke@149: aheinecke@163: for (i=0; to_remove[i]; i++) aheinecke@163: { aheinecke@163: // TODO aheinecke@161: } aheinecke@161: aheinecke@163: if (hStore) aheinecke@163: { aheinecke@163: CertCloseStore (hStore, 0); aheinecke@137: } aheinecke@163: return 0; aheinecke@137: } aheinecke@137: #endif // WIN32