146 #include <winsock2.h>
147 #pragma comment(lib, "ws2_32.lib")
149 #include <netinet/in.h>
163 bool bLineBreaks)
const
165 return theArmoredText.
SetData(m_dataContents, bLineBreaks);
181 return theArmoredText.
GetData(m_dataContents, bLineBreaks);
191 const bool bSetData = theArmoredText.
SetData(
192 m_dataContents,
true);
196 strArmorWithBookends,
"ENVELOPE",
199 otErr << __FUNCTION__ <<
": Failed while calling: "
200 "theArmoredText.WriteArmoredString\n";
205 otErr << __FUNCTION__
206 <<
": Failed while calling: "
207 "theArmoredText.SetData(m_dataContents, true)\n";
213 const OTString& strArmorWithBookends,
218 const_cast<OTString&>(strArmorWithBookends),
224 const bool bGotData =
225 theArmoredText.
GetData(m_dataContents,
true);
228 otErr << __FUNCTION__ <<
": Failed while calling: "
229 "theArmoredText.GetData\n";
234 otErr << __FUNCTION__ <<
": Failed while calling: "
235 "theArmoredText.LoadFromString\n";
258 otErr << __FUNCTION__ <<
": Failed trying to randomly generate IV.\n";
269 otErr << __FUNCTION__
270 <<
": Failed trying to generate symmetric key using password.\n";
276 otErr << __FUNCTION__
277 <<
": Failed trying to generate hash check using password.\n";
288 otErr << __FUNCTION__ <<
": Failed trying to retrieve raw symmetric "
289 "key using password.\n";
306 otErr << __FUNCTION__ <<
": (static) call failed to encrypt. Wrong "
307 "key? (Returning false.)\n";
324 uint16_t env_type_n =
static_cast<uint16_t
>(htons(static_cast<uint16_t>(
327 m_dataContents.
Concatenate(reinterpret_cast<void*>(&env_type_n),
331 static_cast<uint32_t>(
sizeof(env_type_n)));
338 uint32_t ivlen_n = htonl(
341 m_dataContents.
Concatenate(reinterpret_cast<void*>(&ivlen_n),
342 static_cast<uint32_t>(
sizeof(ivlen_n)));
346 m_dataContents.
Concatenate(theIV.GetPayloadPointer(),
347 static_cast<uint32_t
>(theIV.GetSize()));
352 static_cast<uint32_t
>(theCipherText.
GetSize()));
367 const char* szFunc =
"OTEnvelope::Decrypt";
378 otErr << szFunc <<
": Failed trying to retrieve raw symmetric key "
379 "using password. (Wrong password?)\n";
384 uint32_t nRunningTotal = 0;
386 m_dataContents.
reset();
397 uint16_t env_type_n = 0;
399 if (0 == (nRead = m_dataContents.
OTfread(
400 reinterpret_cast<uint8_t*>(&env_type_n),
401 static_cast<uint32_t>(
sizeof(env_type_n))))) {
402 otErr << szFunc <<
": Error reading Envelope Type. Expected "
403 "asymmetric(1) or symmetric (2).\n";
406 nRunningTotal += nRead;
407 OT_ASSERT(nRead == static_cast<uint32_t>(
sizeof(env_type_n)));
411 const uint16_t env_type =
412 static_cast<uint16_t
>(ntohs(static_cast<uint16_t>(env_type_n)));
417 const uint32_t l_env_type =
static_cast<uint32_t
>(env_type);
418 otErr << szFunc <<
": Error: Expected Envelope for Symmetric key (type "
419 "2) but instead found type: " << l_env_type <<
".\n";
425 const uint32_t max_iv_length =
432 uint32_t iv_size_n = 0;
434 if (0 == (nRead = m_dataContents.
OTfread(
435 reinterpret_cast<uint8_t*>(&iv_size_n),
436 static_cast<uint32_t>(
sizeof(iv_size_n))))) {
437 otErr << szFunc <<
": Error reading IV Size.\n";
440 nRunningTotal += nRead;
441 OT_ASSERT(nRead == static_cast<uint32_t>(
sizeof(iv_size_n)));
445 const uint32_t iv_size_host_order = ntohl(iv_size_n);
447 if (iv_size_host_order > max_iv_length) {
448 otErr << szFunc <<
": Error: iv_size ("
449 <<
static_cast<int64_t
>(iv_size_host_order)
450 <<
") is larger than max_iv_length ("
451 << static_cast<int64_t>(max_iv_length) <<
").\n";
461 if (0 == (nRead = m_dataContents.
OTfread(
462 static_cast<uint8_t*>(
464 static_cast<uint32_t
>(iv_size_host_order)))) {
465 otErr << szFunc <<
": Error reading initialization vector.\n";
468 nRunningTotal += nRead;
469 OT_ASSERT(nRead == static_cast<uint32_t>(iv_size_host_order));
481 static_cast<const void*>(
482 static_cast<const uint8_t*>(m_dataContents.
GetPointer()) +
484 m_dataContents.
GetSize() - nRunningTotal);
492 static_cast<const char*>(
493 theCipherText.GetPayloadPointer()),
494 theCipherText.GetSize(),
495 theIV, thePlaintext);
507 uint32_t nIndex = thePlaintext.GetSize() - 1;
508 (
static_cast<uint8_t*
>(
509 const_cast<void*
>(thePlaintext.GetPointer())))[nIndex] =
'\0';
513 theOutput.
Set(static_cast<const char*>(thePlaintext.GetPointer()));
526 theKeys.insert(std::pair<std::string, OTAsymmetricKey*>(
530 return Seal(theKeys, theInput);
539 for (
auto& it : theRecipients) {
542 "OTEnvelope::Seal: Assert: nullptr pseudonym pointer.");
546 RecipPubKeys.insert(std::pair<std::string, OTAsymmetricKey*>(
551 if (RecipPubKeys.empty())
return false;
553 return Seal(RecipPubKeys, theInput);
560 theKeys.insert(std::pair<std::string, OTAsymmetricKey*>(
563 const_cast<OTAsymmetricKey*>(&RecipPubKey)));
565 return Seal(theKeys, theInput);
574 "OTEnvelope::Seal: ASSERT: RecipPubKeys.size() > 0");
static EXPORT OTCrypto * It()
static EXPORT uint32_t SymmetricIvSize()
virtual bool Encrypt(const OTPassword &theRawSymmetricKey, const char *szInput, uint32_t lInputLength, const OTPayload &theIV, OTPayload &theEncryptedOutput) const =0
EXPORT uint32_t getMemorySize() const
EXPORT void GetIdentifier(OTIdentifier &theIdentifier) const
EXPORT bool Decrypt(OTString &theOutput, const OTSymmetricKey &theKey, const OTPassword &thePassword)
EXPORT const OTAsymmetricKey & GetPublicEncrKey() const
EXPORT bool GetAsBookendedString(OTString &strArmorWithBookends, bool bEscaped=false) const
EXPORT bool isPassword() const
EXPORT uint32_t GetLength() const
std::set< OTPseudonym * > setOfNyms
EXPORT bool SetAsciiArmoredData(const OTASCIIArmor &theArmoredText, bool bLineBreaks=true)
EXPORT bool Open(const OTPseudonym &theRecipient, OTString &theOutput, const OTPasswordData *pPWData=nullptr)
EXPORT bool Seal(const OTPseudonym &theRecipient, const OTString &theInput)
EXPORT bool WriteArmoredString(OTString &strOutput, const std::string str_type, bool bEscaped=false) const
EXPORT bool Encrypt(const OTString &theInput, OTSymmetricKey &theKey, const OTPassword &thePassword)
EXPORT bool GetRawKeyFromPassphrase(const OTPassword &thePassphrase, OTPassword &theRawKeyOutput, OTPassword *pDerivedKey=nullptr) const
EXPORT bool Exists() const
static EXPORT bool LoadFromString(OTASCIIArmor &ascArmor, const OTString &strInput, std::string str_bookend="-----BEGIN")
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
EXPORT bool GetAsciiArmoredData(OTASCIIArmor &theArmoredText, bool bLineBreaks=true) const
virtual EXPORT ~OTEnvelope()
EXPORT bool GenerateKey(const OTPassword &thePassphrase, OTPassword **ppDerivedKey=nullptr)
#define OT_ASSERT_MSG(x, s)
EXPORT void SetPayloadSize(uint32_t lNewSize)
EXPORT uint32_t getPasswordSize() const
EXPORT bool GetData(OTData &theData, bool bLineBreaks=true) const
std::multimap< std::string, OTAsymmetricKey * > mapOfAsymmetricKeys
EXPORT bool SetFromBookendedString(const OTString &strArmorWithBookends, bool bEscaped=false)
virtual bool Seal(mapOfAsymmetricKeys &RecipPubKeys, const OTString &theInput, OTData &dataOutput) const =0
EXPORT const char * Get() const
virtual bool Open(OTData &dataInput, const OTPseudonym &theRecipient, OTString &theOutput, const OTPasswordData *pPWData=nullptr) const =0
EXPORT const void * GetPayloadPointer() const
OTLOG_IMPORT OTLogStream otErr
const void * GetPointer() const
EXPORT bool isMemory() const
EXPORT bool GenerateHashCheck(const OTPassword &thePassphrase)
virtual bool Decrypt(const OTPassword &theRawSymmetricKey, const char *szInput, uint32_t lInputLength, const OTPayload &theIV, OTCrypto_Decrypt_Output theDecryptedOutput) const =0
EXPORT uint32_t OTfread(uint8_t *data, uint32_t size)
EXPORT bool SetData(const OTData &theData, bool bLineBreaks=true)
virtual EXPORT void Release()
bool HasHashCheck() const
EXPORT void Concatenate(const void *data, uint32_t size)
virtual EXPORT void Release()