summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristophe Fergeau <cfergeau@redhat.com>2012-10-04 21:24:36 +0200
committerChristophe Fergeau <cfergeau@redhat.com>2012-10-04 21:24:36 +0200
commite5d2a6a6b6d8bf551d9d8a396bbd21268e18593b (patch)
treed8f6fadeff280ae568c7052da0f2860410e2bd3f
parent5d32e75d3c76dacc69a60b9523649015ebc13d0c (diff)
Add win32 implementation of WriteCAFile
This is not even compile tested...
-rw-r--r--Win/controller.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/Win/controller.cpp b/Win/controller.cpp
index 999e6f6..05028bb 100644
--- a/Win/controller.cpp
+++ b/Win/controller.cpp
@@ -123,3 +123,55 @@ bool SpiceController::StartClient(void)
{
return false;
}
+
+std::string SpiceController::WriteCAFile(const std::string &ca_cert)
+{
+ USES_CONVERSION;
+ SECURITY_ATTRIBUTES sa;
+ SECURITY_DESCRIPTOR sd;
+ PACL dacl = NULL;
+
+
+ // Allow access only to current user
+ if (DWORD err = get_security_attributes(&sa, &sd, &dacl))
+ {
+ warning("failed to get security attributes: %d", HRESULT_FROM_WIN32(err));
+ return std::string();
+ }
+
+ WCHAR szCAFileName[MAX_PATH];
+
+ if (::ExpandEnvironmentStrings(L"%TEMP%\\truststore.pem",
+ szCAFileName, sizeof(szCAFileName)) == 0)
+ {
+ LocalFree(dacl);
+ warning("failed to expand %TEMP%\\truststore.pem: %d", HRESULT_FROM_WIN32(::GetLastError()));
+ return std::string();
+ }
+
+ HANDLE hTrustStore = ::CreateFile(szCAFileName, GENERIC_WRITE,
+ 0, NULL, CREATE_ALWAYS, 0, &sa);
+ LocalFree(dacl);
+ if (hTrustStore == INVALID_HANDLE_VALUE)
+ {
+ return HRESULT_FROM_WIN32(::GetLastError());
+ warning("failed to create %s: %d", szCAFileName, HRESULT_FROM_WIN32(::GetLastError()));
+ return std::string();
+ }
+
+ std::string truststore = W2A(ca_cert.c_str());
+ DWORD dwBytesWritten;
+
+ if ((::WriteFile(hTrustStore, truststore.c_str(),
+ DWORD(truststore.length() + 1), &dwBytesWritten, NULL) == FALSE) ||
+ (dwBytesWritten != (truststore.length() + 1)))
+ {
+ ::CloseHandle(hTrustStore);
+ warning("failed to write to %s: %d", szCAFileName, HRESULT_FROM_WIN32(::GetLastError()));
+ return std::string();
+ }
+
+ ::CloseHandle(hTrustStore);
+
+ return std::string(szCAFileName);
+}