// This function will base64 DECODE the string contents // and return them as binary in theData bool OTASCIIArmor::GetData(OTData & theData, bool bLineBreaks) const //linebreaks=true { return GetAndUnpackData(theData, bLineBreaks); size_t outSize = 0; uint8_t * pData = NULL; theData.Release(); if (GetLength() < 1) return true; pData = OT_base64_decode(Get(), &outSize, (bLineBreaks ? 1 : 0)); if (pData) { theData.Assign(pData, outSize); delete [] pData; pData=NULL; return true; } else { OTLog::Error("Error while base64_decoding in OTASCIIArmor::GetData.\n"); return false; } }
// Base64-decode bool OTASCIIArmor::GetData(OTData& theData, bool bLineBreaks) const // linebreaks=true { theData.Release(); if (GetLength() < 1) return true; size_t outSize = 0; uint8_t* pData = App::Me().Crypto().Util().Base64Decode(Get(), &outSize, bLineBreaks); // Some versions of OpenSSL will handle input without line breaks when bLineBreaks is true, // other versions of OpenSSL will return a zero-length output. // // Functions which call this method do not always know the correct value for bLineBreaks, since // the input may be too short to warrant a line break. // // To make this funciton less fragile, if the first attempt does not result in the expected // output, try again with the opposite value set for bLineBreaks. if (!pData||(0==outSize)) { pData = App::Me().Crypto().Util().Base64Decode(Get(), &outSize, !bLineBreaks); if (!pData||(0==outSize)) { otErr << __FUNCTION__ << "Base64Decode fail\n"; return false; } } theData.Assign(pData, outSize); delete[] pData; return true; }
// This function will base64 DECODE the string contents // and return them as binary in theData // Additionally it will decompress and unpack the data! // bool OTASCIIArmor::GetAndUnpackData(OTData & theData, bool bLineBreaks) const //linebreaks=true { size_t outSize = 0; uint8_t * pData = NULL; theData.Release(); if (GetLength() < 1) return true; // -------------------------------------------------------------- // pData = OT_base64_decode(Get(), &outSize, (bLineBreaks ? 1 : 0)); if (pData) { // -------------------------------------------------------- OTDB::OTPacker * pPacker = OTASCIIArmor::GetPacker(); // No need to check for failure, since this already ASSERTS. No need to cleanup either. OTDB::PackedBuffer * pBuffer = pPacker->CreateBuffer(); // Need to clean this up. OT_ASSERT(NULL != pBuffer); OTCleanup<OTDB::PackedBuffer> theBufferAngel(*pBuffer); // make sure buffer is deleted. pBuffer->SetData(static_cast<const unsigned char*>(pData), outSize); delete [] pData; pData=NULL; // ----------------------------- OTDB::Blob * pBlob = dynamic_cast<OTDB::Blob *>(OTDB::CreateObject(OTDB::STORED_OBJ_BLOB)); OT_ASSERT(NULL != pBlob); OTCleanup<OTDB::Blob> theBlobAngel(*pBlob); // clean up this blob. bool bUnpacked = pPacker->Unpack(*pBuffer, *pBlob); // ---------------------- if (false == bUnpacked) { OTLog::Error("Failed unpacking data in OTASCIIArmor::GetAndUnpackData.\n"); delete [] pData; pData=NULL; return false; } // -------------------------------------------------------- theData.Assign(pBlob->m_memBuffer.data(), pBlob->m_memBuffer.size()); delete [] pData; pData=NULL; return true; } else { OTLog::Error("Error while base64_decoding in OTASCIIArmor::GetAndUnpackData.\n"); return false; } }
String CryptoUtil::Nonce(const uint32_t size, OTData& rawOutput) const { rawOutput.zeroMemory(); rawOutput.SetSize(size); OTPassword source; source.randomizeMemory(size); String nonce(Base58CheckEncode(source)); rawOutput.Assign(source.getMemory(), source.getMemorySize()); return nonce; }
uint32_t OTPayload::ReadBytesFrom(OTData & theData, uint32_t lSize) { // The size requested to read MUST be less or equal to size of theData if (theData.GetSize() < lSize) abort(); OTPayload & refPayload = (OTPayload &)theData; // Copy from theData to this, up until lSize Assign(refPayload.GetPayloadPointer(), lSize); // Create a temp var, starting from theData+lSize, copying to the end of theData OTData TEMPdata((unsigned char *)refPayload.GetPayloadPointer() + lSize, theData.GetSize() - lSize); // theData is assigned to TEMPdata (thus removing from it the bytes that we just read into this.) theData.Assign(TEMPdata); return lSize; }
// static bool OTKeyring::Windows_StoreSecret(const OTString& strUser, const OTPassword& thePassword, const std::string& str_display) { OT_ASSERT(strUser.Exists()); OT_ASSERT(thePassword.getMemorySize() > 0); DATA_BLOB input; input.pbData = const_cast<BYTE*>( reinterpret_cast<const BYTE*>(thePassword.getMemory())); input.cbData = static_cast<DWORD>(thePassword.getMemorySize()); // CRYPTPROTECT_PROMPTSTRUCT PromptStruct; // ZeroMemory(&PromptStruct, sizeof(PromptStruct)); // PromptStruct.cbSize = sizeof(PromptStruct); // PromptStruct.dwPromptFlags = CRYPTPROTECT_PROMPT_ON_PROTECT; // PromptStruct.szPrompt = L"This is a user prompt."; DATA_BLOB output; BOOL result = CryptProtectData(&input, L"", // description string nullptr, // optional entropy nullptr, // reserved nullptr, //&PromptStruct 0, &output); if (!result) { otErr << __FUNCTION__ << ": Failed calling Win32: CryptProtectData \n"; return false; } // // this does a copy // // std::string ciphertext; // ciphertext.assign(reinterpret_cast<std::string::value_type*>(output.pbData), // output.cbData); OTData theOutput; theOutput.Assign(static_cast<void*>(output.pbData), static_cast<uint32_t>(output.cbData)); LocalFree(output.pbData); // Note: should have a check for nullptr here... ? // And above... // Success encrypting to ciphertext (std::string or OTData) // // Write it to local storage. // if (theOutput.IsEmpty()) { otErr << __FUNCTION__ << ": Error: Output of Win32 CryptProtectData was empty.\n"; } else { OTASCIIArmor ascData(theOutput); const OTString strFoldername("win32_data"); // todo hardcoding. if (ascData.Exists()) return ascData.WriteArmoredFile(strFoldername, strUser, // this is filename "WINDOWS KEYRING MASTERKEY"); } return false; }