VBoxCertUtil.cpp revision 0bc20fbea156fcbb4cd79f14c2a4b6fd2e3d16c5
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <Windows.h>
#include <Wincrypt.h>
#include <iprt/buildconfig.h>
#include <iprt/initterm.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The verbosity level. */
static unsigned g_cVerbosityLevel = 1;
{
switch (dwErr)
{
default:
{
if (pWinComMsg)
return pWinComMsg->pszDefine;
static char s_szErr[32];
return s_szErr;
}
}
}
#if 0 /* hacking */
{
/*
* Open the source.
*/
void *pvFile;
if (RT_FAILURE(rc))
if (pCertCtx)
{
/*
* Open the destination.
*/
NULL /* hCryptProv = default */,
{
#if 0
0 /* dwFlags (reserved) */,
NULL))
{
RTMsgInfo("Successfully added '%s' to the '%ls' store (ctx type %u)", pszFilename, pwszStore, dwContextType);
}
else
#else
{
}
else
#endif
}
else
}
else
return rcExit;
#if 0
#endif
}
#endif /* hacking */
/**
* Reads a certificate from a file, returning a context or a the handle to a
* temporary memory store.
*
* @returns true on success, false on failure (error message written).
* @param pszCertFile The name of the file containing the
* certificates.
* @param ppOutCtx Where to return the certificate context.
* @param phSrcStore Where to return the handle to the temporary
* memory store.
*/
{
*phSrcStore = NULL;
bool fRc = false;
void *pvFile;
if (RT_SUCCESS(rc))
{
if (*ppOutCtx)
rc = true;
else
{
/** @todo figure out if it's some other format... */
RTMsgError("CertCreateCertificateContext returned %s parsing the content of '%s'",
}
}
else
return fRc;
}
/**
* Opens a certificate store.
*
* @returns true on success, false on failure (error message written).
* @param dwDst The destination, like
* CERT_SYSTEM_STORE_LOCAL_MACHINE or
* ERT_SYSTEM_STORE_CURRENT_USER.
* @param pszStoreNm The store name.
*/
{
if (RT_SUCCESS(rc))
{
if (g_cVerbosityLevel > 1)
NULL /* hCryptProv = default */,
RTMsgError("CertOpenStore failed opening %#x:'%s': %s",
}
return hStore;
}
/**
* Adds a certificate to a store.
*
* @returns true on success, false on failure (error message written).
* @param dwDst The destination, like
* CERT_SYSTEM_STORE_LOCAL_MACHINE or
* ERT_SYSTEM_STORE_CURRENT_USER.
* @param pszStoreNm The store name.
* @param pszCertFile The file containing the certificate to add.
* @param dwDisposition The disposition towards existing certificates when
* adding it. CERT_STORE_ADD_NEW is a safe one.
*/
static bool addCertToStore(DWORD dwDst, const char *pszStoreNm, const char *pszCertFile, DWORD dwDisposition)
{
/*
* Read the certificate file first.
*/
return false;
/*
* Open the destination store.
*/
bool fRc = false;
if (hDstStore)
{
if (pSrcCtx)
{
if (g_cVerbosityLevel > 1)
fRc = true;
else
}
else
{
}
}
if (pSrcCtx)
if (hSrcStore)
return fRc;
}
/**
* Worker for cmdDisplayAll.
*/
static BOOL WINAPI displaySystemStoreCallback(const void *pvSystemStore, DWORD dwFlags, PCERT_SYSTEM_STORE_INFO pStoreInfo,
void *pvReserved, void *pvArg)
{
if (g_cVerbosityLevel > 1)
RTPrintf(" pvSystemStore=%p dwFlags=%#x pStoreInfo=%p pvReserved=%p\n", pvSystemStore, dwFlags, pStoreInfo, pvReserved);
{
const CERT_SYSTEM_STORE_RELOCATE_PARA *pRelPara = (const CERT_SYSTEM_STORE_RELOCATE_PARA *)pvSystemStore;
}
else
{
}
/*
* Open the store and list the certificates within.
*/
NULL /* hCryptProv = default */,
if (hStore)
{
{
if (g_cVerbosityLevel > 1)
RTPrintf(" pCertCtx=%p dwCertEncodingType=%#x cbCertEncoded=%#x pCertInfo=%p\n",
{
{
char szNotBefore[80];
szNotBefore, sizeof(szNotBefore));
char szNotAfter[80];
szNotAfter, sizeof(szNotAfter));
{
if (CertGetNameStringW(pCertCtx, CERT_NAME_SIMPLE_DISPLAY_TYPE, CERT_NAME_ISSUER_FLAG, NULL /*pvTypePara*/,
else
}
}
}
else
}
}
else
RTMsgError("CertOpenStore failed opening %#x:'%ls': %s\n", dwDst, pwszStoreNm, errorToString(GetLastError()));
return TRUE;
}
/**
* Worker for cmdDisplayAll.
*/
static BOOL WINAPI displaySystemStoreLocation(LPCWSTR pwszStoreLocation, DWORD dwFlags, void *pvReserved, void *pvArg)
{
RTMsgError("CertEnumSystemStore failed on %#x:'%ls': %s\n",
return TRUE;
}
/**
* Handler for the 'display-all' command.
*/
{
if (argc != 1)
return RTMsgErrorExit(RTEXITCODE_SYNTAX, "CertEnumSystemStoreLocation failed: %s\n", errorToString(GetLastError()));
return RTEXITCODE_SUCCESS;
}
/**
* Handler for the 'add-trusted-publisher' command.
*/
{
/*
* Parse arguments.
*/
static const RTGETOPTDEF s_aOptions[] =
{
};
const char *pszRootCert = NULL;
const char *pszTrustedCert = NULL;
int rc;
{
switch (rc)
{
case 'h':
RTPrintf("Usage: VBoxCertUtil add-trusted-publisher [--root <root-cert>] <trusted-cert>\n");
break;
case 'V':
return RTEXITCODE_SUCCESS;
case 'r':
if (pszRootCert)
return RTMsgErrorExit(RTEXITCODE_SUCCESS,
"You've already specified '%s' as root certificate.",
break;
case VINF_GETOPT_NOT_OPTION:
if (pszTrustedCert)
return RTMsgErrorExit(RTEXITCODE_SUCCESS,
"You've already specified '%s' as trusted certificate.",
break;
default:
}
}
if (!pszTrustedCert)
/*
* Do the job.
*/
if ( pszRootCert
return RTEXITCODE_FAILURE;
if (!addCertToStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, "TrustedPublisher", pszTrustedCert, CERT_STORE_ADD_NEW))
return RTEXITCODE_FAILURE;
if (g_cVerbosityLevel > 0)
{
if (pszRootCert)
RTMsgInfo("Successfully added '%s' as root and '%s' as trusted publisher", pszRootCert, pszTrustedCert);
else
}
return RTEXITCODE_SUCCESS;
}
{
if (RT_FAILURE(rc))
return RTMsgInitFailure(rc);
/*
* Parse arguments up to the command and pass it on to the command handlers.
*/
typedef enum
{
VCUACTION_ADD_TRUSTED_PUBLISHER = 1000,
} VCUACTION;
static const RTGETOPTDEF s_aOptions[] =
{
};
{
switch (rc)
{
case 'v':
break;
case 'q':
if (g_cVerbosityLevel > 0)
break;
case 'h':
RTPrintf("Usage: TODO\n");
break;
case 'V':
return RTEXITCODE_SUCCESS;
case VCUACTION_DISPLAY_ALL:
default:
}
}
RTMsgError("Missing command...\n");
return RTEXITCODE_SYNTAX;
}