/** * @interface_method_impl(PDMINVRAM,pfnStoreNvramValue) */ DECLCALLBACK(int) drvNvram_pfnStoreNvramValue(PPDMINVRAM pInterface, int idxVariable, RTUUID *pVendorUuid, const char *pcszVariableName, size_t cbVariableName, uint8_t *pu8Value, size_t cbValue) { int rc = VINF_SUCCESS; char szExtraDataKey[256]; char szExtraDataValue[1024]; LogFlowFunc(("ENTER: pVendorUuid:%RTuuid, pcszVariableName:%s, cbVariableName:%d, pu8Value:%.*Rhxs, cbValue:%d\n", pVendorUuid, pcszVariableName, cbVariableName, cbValue, pu8Value, cbValue)); PNVRAM pThis = RT_FROM_MEMBER(pInterface, NVRAM, INvram); if (!pThis->fPermanentSave) { LogFlowFuncLeaveRC(rc); return rc; } bool fFlushVariable = (!pu8Value); RT_ZERO(szExtraDataKey); RT_ZERO(szExtraDataValue); RTStrPrintf(szExtraDataKey, 256, "VBoxInternal/Devices/efi/0/LUN#0/Config/NVRAM/%d/VariableName", idxVariable); if (!fFlushVariable) RTStrPrintf(szExtraDataValue, 1024, "%s", pcszVariableName); pThis->pNvram->getParent()->machine()->SetExtraData(Bstr(szExtraDataKey).raw(), Bstr(szExtraDataValue).raw()); RT_ZERO(szExtraDataKey); RT_ZERO(szExtraDataValue); RTStrPrintf(szExtraDataKey, 256, "VBoxInternal/Devices/efi/0/LUN#0/Config/NVRAM/%d/VendorGuid", idxVariable); if (!fFlushVariable) RTUuidToStr(pVendorUuid, szExtraDataValue, 1024); pThis->pNvram->getParent()->machine()->SetExtraData(Bstr(szExtraDataKey).raw(), Bstr(szExtraDataValue).raw()); RT_ZERO(szExtraDataKey); RT_ZERO(szExtraDataValue); RTStrPrintf(szExtraDataKey, 256, "VBoxInternal/Devices/efi/0/LUN#0/Config/NVRAM/%d/VariableValueLength", idxVariable); if (!fFlushVariable) RTStrPrintf(szExtraDataValue, 1024, "%d", cbValue); pThis->pNvram->getParent()->machine()->SetExtraData(Bstr(szExtraDataKey).raw(), Bstr(szExtraDataValue).raw()); RT_ZERO(szExtraDataKey); RT_ZERO(szExtraDataValue); RTStrPrintf(szExtraDataKey, 256, "VBoxInternal/Devices/efi/0/LUN#0/Config/NVRAM/%d/VariableValue", idxVariable); size_t cbActualSize; if (pu8Value) rc = RTBase64Encode(pu8Value, cbValue, szExtraDataValue, 1024, &cbActualSize); AssertRCReturn(rc, rc); pThis->pNvram->getParent()->machine()->SetExtraData(Bstr(szExtraDataKey).raw(), Bstr(szExtraDataValue).raw()); LogFlowFuncLeaveRC(rc); return rc; }
static void tstBase64(const void *pvData, size_t cbData, const char *pszEnc, size_t cchEnc, int fTextData, int fNormalEnc) { char szOut[0x10000]; size_t cchOut = 0; /* * Test decoding. */ int rc = RTBase64Decode(pszEnc, szOut, cbData, &cchOut, NULL); if (RT_FAILURE(rc)) RTTestIFailed("RTBase64Decode -> %Rrc", rc); else if (cchOut != cbData) RTTestIFailed("RTBase64Decode returned %zu bytes, expected %zu.", cchOut, cbData); else if (memcmp(szOut, pvData, cchOut)) { if (fTextData) RTTestIFailed("RTBase64Decode returned:\n%.*s\nexpected:\n%s\n", (int)cchOut, szOut, pvData); else RTTestIFailed("RTBase64Decode return mismatching output\n"); } cchOut = RTBase64DecodedSize(pszEnc, NULL); if (cchOut != cbData) RTTestIFailed("RTBase64DecodedSize returned %zu bytes, expected %zu.\n", cchOut, cbData); /* * Test encoding. */ rc = RTBase64Encode(pvData, cbData, szOut, cchEnc + 1, &cchOut); if (RT_FAILURE(rc)) RTTestIFailed("RTBase64Encode -> %Rrc\n", rc); else if (fNormalEnc && cchOut != cchEnc) RTTestIFailed("RTBase64Encode returned %zu bytes, expected %zu.\n", cchOut, cchEnc); else if (fNormalEnc && memcmp(szOut, pszEnc, cchOut + 1)) RTTestIFailed("RTBase64Encode returned:\n%*s\nexpected:\n%s\n", szOut, pszEnc); size_t cchOut2 = RTBase64EncodedLength(cbData); if (cchOut != cchOut2) RTTestIFailed("RTBase64EncodedLength returned %zu bytes, expected %zu.\n", cchOut2, cchOut); /** @todo negative testing. */ }
/** * Converts the binary to a CFGM overlay binary string. * * @returns Pointer to a heap buffer (hand it to RTMemFree when done). * @param pvBuf The binary data to convert. * @param cbBuf The number of bytes to convert. */ static char *drvNvram_binaryToCfgmString(void const *pvBuf, size_t cbBuf) { static char s_szPrefix[] = "bytes:"; size_t cbStr = RTBase64EncodedLength(cbBuf) + sizeof(s_szPrefix); char *pszStr = (char *)RTMemAlloc(cbStr); if (pszStr) { memcpy(pszStr, s_szPrefix, sizeof(s_szPrefix) - 1); int rc = RTBase64Encode(pvBuf, cbBuf, &pszStr[sizeof(s_szPrefix) - 1], cbStr - sizeof(s_szPrefix) + 1, NULL); if (RT_FAILURE(rc)) { RTMemFree(pszStr); pszStr = NULL; } } return pszStr; }
static char* rtS3CreateSignature(PRTS3INTERNAL pS3Int, const char* pszAction, const char* pszBucket, const char* pszKey, char** papszHeadEnts, size_t cHeadEnts) { /* Create a string we can sign */ char* pszSig = rtS3Canonicalize(pszAction, pszBucket, pszKey, papszHeadEnts, cHeadEnts); // printf ("Sig %s\n", pszSig); /* Sign the string by creating a SHA1 finger print */ char pszSigEnc[1024]; unsigned int cSigEnc = sizeof(pszSigEnc); HMAC(EVP_sha1(), pS3Int->pszSecretKey, (int)strlen(pS3Int->pszSecretKey), (const unsigned char*)pszSig, strlen(pszSig), (unsigned char*)pszSigEnc, &cSigEnc); RTStrFree(pszSig); /* Convert the signature to Base64 */ size_t cSigBase64Enc = RTBase64EncodedLength(cSigEnc) + 1; /* +1 for the 0 */ char *pszSigBase64Enc = (char*)RTMemAlloc(cSigBase64Enc); size_t cRes; RTBase64Encode(pszSigEnc, cSigEnc, pszSigBase64Enc, cSigBase64Enc, &cRes); return pszSigBase64Enc; }
int main() { RTTEST hTest; int rc = RTTestInitAndCreate("tstRTBase64", &hTest); if (rc) return rc; RTTestBanner(hTest); /* * Series of simple tests. */ static const struct { const char *pszText; size_t cchText; const char *pszEnc; size_t cchEnc; } g_aTests[] = { #define TEST_ENTRY(szText, szEnc) { szText, sizeof(szText) - 1, szEnc, sizeof(szEnc) - 1 } TEST_ENTRY("Hey", "SGV5"), TEST_ENTRY("Base64", "QmFzZTY0"), TEST_ENTRY("Call me Ishmael.", "Q2FsbCBtZSBJc2htYWVsLg=="), TEST_ENTRY( "Man is distinguished, not only by his reason, but by this singular passion " "from other animals, which is a lust of the mind, that by a perseverance of " "delight in the continued and indefatigable generation of knowledge, exceeds " "the short vehemence of any carnal pleasure." /* Thomas Hobbes's Leviathan */, "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1" MY_NL "dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3" MY_NL "aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFu" MY_NL "Y2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxl" MY_NL "IGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhl" MY_NL "bWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" ) #undef TEST_ENTRY }; for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++) { RTTestSubF(hTest, "Test 1-%u", i); tstBase64(g_aTests[i].pszText, g_aTests[i].cchText, g_aTests[i].pszEnc, g_aTests[i].cchEnc, 1 /* fTextData */, 1 /* fNormalEnc */); } /* * Try with some more junk in the encoding and different line length. */ RTTestSub(hTest, "Test 2"); static const char s_szText2[] = "Man is distinguished, not only by his reason, but by this singular passion " "from other animals, which is a lust of the mind, that by a perseverance of " "delight in the continued and indefatigable generation of knowledge, exceeds " "the short vehemence of any carnal pleasure."; /* Thomas Hobbes's Leviathan */ static const char s_szEnc2[] = " TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n" " IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n\r\t\t\t\v" "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n" "\tdWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n\r" " ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\n \n \r \n \t"; tstBase64(s_szText2, sizeof(s_szText2) - 1, s_szEnc2, sizeof(s_szEnc2) - 1, 1 /* fTextData */, 0 /* fNormalEnc */); /* * Test for buffer overruns. */ RTTestSubF(hTest, "Test 3"); static uint8_t s_abData4[32768]; for (size_t i = 0; i < sizeof(s_abData4); i++) s_abData4[i] = i % 256; for (size_t cbSrc = 1; cbSrc <= sizeof(s_abData4); cbSrc++) { char szEnc[49152]; memset(szEnc, '\0', sizeof(szEnc)); size_t cchEnc = RTBase64EncodedLength(cbSrc); if (cchEnc >= sizeof(szEnc)) RTTestIFailed("RTBase64EncodedLength(%zu) returned %zu bytes, too big\n", cbSrc, cchEnc); size_t cchOut = 0; rc = RTBase64Encode(s_abData4, cbSrc, szEnc, cchEnc, &cchOut); if (rc != VERR_BUFFER_OVERFLOW) RTTestIFailed("RTBase64Encode(,%zu,) has no buffer overflow with too small buffer -> %Rrc\n", cbSrc, rc); rc = RTBase64Encode(s_abData4, cbSrc, szEnc, cchEnc + 1, &cchOut); if (RT_FAILURE(rc)) RTTestIFailed("RTBase64Encode -> %Rrc\n", rc); if (cchOut != cchEnc) RTTestIFailed("RTBase64EncodedLength(%zu) returned %zu bytes, expected %zu.\n", cbSrc, cchEnc, cchOut); if (szEnc[cchOut + 1] != '\0') RTTestIFailed("RTBase64Encode(,%zu,) returned string which is not zero terminated\n", cbSrc); if (strlen(szEnc) != cchOut) RTTestIFailed("RTBase64Encode(,%zu,) returned incorrect string, length %lu\n", cbSrc, cchOut); } /* * Finally, a more extensive test. */ RTTestSub(hTest, "Test 4"); static uint8_t s_abData3[12*256]; for (unsigned i = 0; i < 256; i++) { unsigned j = i*12; s_abData3[j + 0] = i; /* */ s_abData3[j + 1] = 0xff; s_abData3[j + 2] = i; s_abData3[j + 3] = 0xff; /* */ s_abData3[j + 4] = i; s_abData3[j + 5] = 0xff; s_abData3[j + 6] = i; /* */ s_abData3[j + 7] = 0x00; s_abData3[j + 8] = i; s_abData3[j + 9] = 0x00; /* */ s_abData3[j + 10]= i; s_abData3[j + 11]= 0x00; } static const char s_szEnc3[] = "AP8A/wD/AAAAAAAAAf8B/wH/AQABAAEAAv8C/wL/AgACAAIAA/8D/wP/AwADAAMA" MY_NL "BP8E/wT/BAAEAAQABf8F/wX/BQAFAAUABv8G/wb/BgAGAAYAB/8H/wf/BwAHAAcA" MY_NL "CP8I/wj/CAAIAAgACf8J/wn/CQAJAAkACv8K/wr/CgAKAAoAC/8L/wv/CwALAAsA" MY_NL "DP8M/wz/DAAMAAwADf8N/w3/DQANAA0ADv8O/w7/DgAOAA4AD/8P/w//DwAPAA8A" MY_NL "EP8Q/xD/EAAQABAAEf8R/xH/EQARABEAEv8S/xL/EgASABIAE/8T/xP/EwATABMA" MY_NL "FP8U/xT/FAAUABQAFf8V/xX/FQAVABUAFv8W/xb/FgAWABYAF/8X/xf/FwAXABcA" MY_NL "GP8Y/xj/GAAYABgAGf8Z/xn/GQAZABkAGv8a/xr/GgAaABoAG/8b/xv/GwAbABsA" MY_NL "HP8c/xz/HAAcABwAHf8d/x3/HQAdAB0AHv8e/x7/HgAeAB4AH/8f/x//HwAfAB8A" MY_NL "IP8g/yD/IAAgACAAIf8h/yH/IQAhACEAIv8i/yL/IgAiACIAI/8j/yP/IwAjACMA" MY_NL "JP8k/yT/JAAkACQAJf8l/yX/JQAlACUAJv8m/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/9r/2gDaANoA2//b/9v/2wDbANsA" MY_NL "3P/c/9z/3ADcANwA3f/d/93/3QDdAN0A3v/e/97/3gDeAN4A3//f/9//3wDfAN8A" MY_NL "4P/g/+D/4ADgAOAA4f/h/+H/4QDhAOEA4v/i/+L/4gDiAOIA4//j/+P/4wDjAOMA" MY_NL "5P/k/+T/5ADkAOQA5f/l/+X/5QDlAOUA5v/m/+b/5gDmAOYA5//n/+f/5wDnAOcA" MY_NL "6P/o/+j/6ADoAOgA6f/p/+n/6QDpAOkA6v/q/+r/6gDqAOoA6//r/+v/6wDrAOsA" MY_NL "7P/s/+z/7ADsAOwA7f/t/+3/7QDtAO0A7v/u/+7/7gDuAO4A7//v/+//7wDvAO8A" MY_NL "8P/w//D/8ADwAPAA8f/x//H/8QDxAPEA8v/y//L/8gDyAPIA8//z//P/8wDzAPMA" MY_NL "9P/0//T/9AD0APQA9f/1//X/9QD1APUA9v/2//b/9gD2APYA9//3//f/9wD3APcA" MY_NL "+P/4//j/+AD4APgA+f/5//n/+QD5APkA+v/6//r/+gD6APoA+//7//v/+wD7APsA" MY_NL "/P/8//z//AD8APwA/f/9//3//QD9AP0A/v/+//7//gD+AP4A/////////wD/AP8A"; tstBase64(s_abData3, sizeof(s_abData3), s_szEnc3, sizeof(s_szEnc3) - 1, 0 /* fTextData */, 0 /* fNormalEnc */); /* * Summary. */ return RTTestSummaryAndDestroy(hTest); }
RTDECL(int) RTCrStoreCertExportAsPem(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename) { /* * Validate input. */ AssertReturn(!fFlags, VERR_INVALID_FLAGS); /* * Start the enumeration first as this validates the store handle. */ RTCRSTORECERTSEARCH Search; int rc = RTCrStoreCertFindAll(hStore, &Search); if (RT_SUCCESS(rc)) { /* * Open the file for writing. * * Note! We must use text and no binary here, because the base-64 API * below will use host specific EOL markers, not CRLF as PEM * specifies. */ PRTSTREAM hStrm; rc = RTStrmOpen(pszFilename, "w", &hStrm); if (RT_SUCCESS(rc)) { /* * Enumerate the certificates in the store, writing them out one by one. */ size_t cbBase64 = 0; char *pszBase64 = NULL; PCRTCRCERTCTX pCertCtx; while ((pCertCtx = RTCrStoreCertSearchNext(hStore, &Search)) != NULL) { const char *pszMarker; switch (pCertCtx->fFlags & RTCRCERTCTX_F_ENC_MASK) { case RTCRCERTCTX_F_ENC_X509_DER: pszMarker = "CERTIFICATE"; break; case RTCRCERTCTX_F_ENC_TAF_DER: pszMarker = "TRUST ANCHOR"; break; default: pszMarker = NULL; break; } if (pszMarker && pCertCtx->cbEncoded > 0) { /* * Do the base64 conversion first. */ size_t cchEncoded = RTBase64EncodedLength(pCertCtx->cbEncoded); if (cchEncoded < cbBase64) { /* likely */ } else { size_t cbNew = RT_ALIGN(cchEncoded + 64, 128); void *pvNew = RTMemRealloc(pszBase64, cbNew); if (!pvNew) { rc = VERR_NO_MEMORY; break; } cbBase64 = cbNew; pszBase64 = (char *)pvNew; } rc = RTBase64Encode(pCertCtx->pabEncoded, pCertCtx->cbEncoded, pszBase64, cbBase64, &cchEncoded); if (RT_FAILURE(rc)) break; RTStrmPrintf(hStrm, "-----BEGIN %s-----\n", pszMarker); RTStrmWrite(hStrm, pszBase64, cchEncoded); rc = RTStrmPrintf(hStrm, "\n-----END %s-----\n", pszMarker); if (RT_FAILURE(rc)) break; } RTCrCertCtxRelease(pCertCtx); } if (pCertCtx) RTCrCertCtxRelease(pCertCtx); RTMemFree(pszBase64); /* * Flush the output file before closing. */ int rc2 = RTStrmFlush(hStrm); if (RT_FAILURE(rc2) && RT_SUCCESS(rc)) rc = rc2; RTStrmClearError(hStrm); /** @todo fix RTStrmClose... */ rc2 = RTStrmClose(hStrm); if (RT_FAILURE(rc2) && RT_SUCCESS(rc)) rc = rc2; } int rc2 = RTCrStoreCertSearchDestroy(hStore, &Search); AssertRC(rc2); } return rc; }