Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OTEnvelope Class Reference

#include <OTEnvelope.hpp>

Public Member Functions

EXPORT OTEnvelope ()
 
EXPORT OTEnvelope (const OTASCIIArmor &theArmoredText)
 
EXPORT OTEnvelope (const OTString &strArmorWithBookends)
 
virtual EXPORT ~OTEnvelope ()
 
EXPORT bool Encrypt (const OTString &theInput, OTSymmetricKey &theKey, const OTPassword &thePassword)
 
EXPORT bool Decrypt (OTString &theOutput, const OTSymmetricKey &theKey, const OTPassword &thePassword)
 
EXPORT bool Seal (const OTPseudonym &theRecipient, const OTString &theInput)
 
EXPORT bool Seal (const OTAsymmetricKey &RecipPubKey, const OTString &theInput)
 
EXPORT bool Seal (setOfNyms &theRecipients, const OTString &theInput)
 
EXPORT bool Seal (mapOfAsymmetricKeys &RecipPubKeys, const OTString &theInput)
 
EXPORT bool Open (const OTPseudonym &theRecipient, OTString &theOutput, const OTPasswordData *pPWData=nullptr)
 
EXPORT bool GetAsciiArmoredData (OTASCIIArmor &theArmoredText, bool bLineBreaks=true) const
 
EXPORT bool GetAsBookendedString (OTString &strArmorWithBookends, bool bEscaped=false) const
 
EXPORT bool SetAsciiArmoredData (const OTASCIIArmor &theArmoredText, bool bLineBreaks=true)
 
EXPORT bool SetFromBookendedString (const OTString &strArmorWithBookends, bool bEscaped=false)
 

Friends

class OTPayload
 

Detailed Description

Definition at line 156 of file OTEnvelope.hpp.

Constructor & Destructor Documentation

opentxs::OTEnvelope::OTEnvelope ( )

Definition at line 630 of file OTEnvelope.cpp.

631 {
632 }
opentxs::OTEnvelope::OTEnvelope ( const OTASCIIArmor theArmoredText)

Definition at line 625 of file OTEnvelope.cpp.

626 {
627  SetAsciiArmoredData(theArmoredText);
628 }
EXPORT bool SetAsciiArmoredData(const OTASCIIArmor &theArmoredText, bool bLineBreaks=true)
Definition: OTEnvelope.cpp:178
EXPORT opentxs::OTEnvelope::OTEnvelope ( const OTString strArmorWithBookends)
opentxs::OTEnvelope::~OTEnvelope ( )
virtual

Definition at line 634 of file OTEnvelope.cpp.

635 {
636 }

Member Function Documentation

bool opentxs::OTEnvelope::Decrypt ( OTString theOutput,
const OTSymmetricKey theKey,
const OTPassword thePassword 
)

Definition at line 364 of file OTEnvelope.cpp.

366 {
367  const char* szFunc = "OTEnvelope::Decrypt";
368 
369  OT_ASSERT(
370  (thePassword.isPassword() && (thePassword.getPasswordSize() > 0)) ||
371  (thePassword.isMemory() && (thePassword.getMemorySize() > 0)));
372  OT_ASSERT(theKey.IsGenerated());
373 
374  OTPassword theRawSymmetricKey;
375 
376  if (false ==
377  theKey.GetRawKeyFromPassphrase(thePassword, theRawSymmetricKey)) {
378  otErr << szFunc << ": Failed trying to retrieve raw symmetric key "
379  "using password. (Wrong password?)\n";
380  return false;
381  }
382 
383  uint32_t nRead = 0;
384  uint32_t nRunningTotal = 0;
385 
386  m_dataContents.reset(); // Reset the fread position on this object to 0.
387 
388  //
389  // Read the ENVELOPE TYPE (as network order version -- and convert to host
390  // version.)
391  //
392  // 0 == Error
393  // 1 == Asymmetric Key (this function -- Seal / Open)
394  // 2 == Symmetric Key (other functions -- Encrypt / Decrypt use this.)
395  // Anything else: error.
396  //
397  uint16_t env_type_n = 0;
398 
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";
404  return false;
405  }
406  nRunningTotal += nRead;
407  OT_ASSERT(nRead == static_cast<uint32_t>(sizeof(env_type_n)));
408 
409  // convert that envelope type from network to HOST endian.
410  //
411  const uint16_t env_type =
412  static_cast<uint16_t>(ntohs(static_cast<uint16_t>(env_type_n)));
413  // nRunningTotal += env_type; // NOPE! Just because envelope type is 1
414  // or 2, doesn't mean we add 1 or 2 extra bytes to the length here. Nope!
415 
416  if (2 != env_type) {
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";
420  return false;
421  }
422 
423  // Read network-order IV size (and convert to host version)
424  //
425  const uint32_t max_iv_length =
426  OTCryptoConfig::SymmetricIvSize(); // I believe this is a max length, so
427  // it may not match the actual length
428  // of the IV.
429 
430  // Read the IV SIZE (network order version -- convert to host version.)
431  //
432  uint32_t iv_size_n = 0;
433 
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";
438  return false;
439  }
440  nRunningTotal += nRead;
441  OT_ASSERT(nRead == static_cast<uint32_t>(sizeof(iv_size_n)));
442 
443  // convert that iv size from network to HOST endian.
444  //
445  const uint32_t iv_size_host_order = ntohl(iv_size_n);
446 
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";
452  return false;
453  }
454  // nRunningTotal += iv_size_host_order; // Nope!
455 
456  // Then read the IV (initialization vector) itself.
457  //
458  OTPayload theIV;
459  theIV.SetPayloadSize(iv_size_host_order);
460 
461  if (0 == (nRead = m_dataContents.OTfread(
462  static_cast<uint8_t*>(
463  const_cast<void*>(theIV.GetPayloadPointer())),
464  static_cast<uint32_t>(iv_size_host_order)))) {
465  otErr << szFunc << ": Error reading initialization vector.\n";
466  return false;
467  }
468  nRunningTotal += nRead;
469  OT_ASSERT(nRead == static_cast<uint32_t>(iv_size_host_order));
470 
471  OT_ASSERT(nRead <= max_iv_length);
472 
473  // We create an OTPayload object to store the ciphertext itself, which
474  // begins AFTER the end of the IV.
475  // So we see pointer + nRunningTotal as the starting point for the
476  // ciphertext.
477  // the size of the ciphertext, meanwhile, is the size of the entire thing,
478  // MINUS nRunningTotal.
479  //
480  OTPayload theCipherText(
481  static_cast<const void*>(
482  static_cast<const uint8_t*>(m_dataContents.GetPointer()) +
483  nRunningTotal),
484  m_dataContents.GetSize() - nRunningTotal);
485 
486  // Now we've got all the pieces together, let's try to decrypt it...
487  //
488  OTPayload thePlaintext; // for output.
489 
490  const bool bDecrypted = OTCrypto::It()->Decrypt(
491  theRawSymmetricKey, // The symmetric key, in clear form.
492  static_cast<const char*>(
493  theCipherText.GetPayloadPointer()), // This is the Ciphertext.
494  theCipherText.GetSize(),
495  theIV, thePlaintext); // OUTPUT. (Recovered plaintext.) You can pass
496  // OTPassword& OR OTPayload& here (either will
497  // work.)
498 
499  // theOutput is where we'll put the decrypted data.
500  //
501  theOutput.Release();
502 
503  if (bDecrypted) {
504 
505  // Make sure it's null-terminated...
506  //
507  uint32_t nIndex = thePlaintext.GetSize() - 1;
508  (static_cast<uint8_t*>(
509  const_cast<void*>(thePlaintext.GetPointer())))[nIndex] = '\0';
510 
511  // Set it into theOutput (to return the plaintext to the caller)
512  //
513  theOutput.Set(static_cast<const char*>(thePlaintext.GetPointer()));
514  }
515 
516  return bDecrypted;
517 }
static EXPORT OTCrypto * It()
Definition: OTCrypto.cpp:630
static EXPORT uint32_t SymmetricIvSize()
Definition: OTCrypto.cpp:391
#define OT_ASSERT(x)
Definition: Assert.hpp:150
void reset()
Definition: OTData.hpp:186
friend class OTPayload
Definition: OTEnvelope.hpp:158
OTLOG_IMPORT OTLogStream otErr
const void * GetPointer() const
Definition: OTData.hpp:162
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)
Definition: OTData.cpp:214
uint32_t GetSize() const
Definition: OTData.hpp:174
bool opentxs::OTEnvelope::Encrypt ( const OTString theInput,
OTSymmetricKey theKey,
const OTPassword thePassword 
)

Definition at line 245 of file OTEnvelope.cpp.

247 {
248  OT_ASSERT(
249  (thePassword.isPassword() && (thePassword.getPasswordSize() > 0)) ||
250  (thePassword.isMemory() && (thePassword.getMemorySize() > 0)));
251  OT_ASSERT(theInput.Exists());
252 
253  // Generate a random initialization vector.
254  //
255  OTPayload theIV;
256 
257  if (!theIV.Randomize(OTCryptoConfig::SymmetricIvSize())) {
258  otErr << __FUNCTION__ << ": Failed trying to randomly generate IV.\n";
259  return false;
260  }
261 
262  // If the symmetric key hasn't already been generated, we'll just do that
263  // now...
264  // (The passphrase is used to derive another key that is used to encrypt the
265  // actual symmetric key, and to access it later.)
266  //
267  if ((false == theKey.IsGenerated()) &&
268  (false == theKey.GenerateKey(thePassword))) {
269  otErr << __FUNCTION__
270  << ": Failed trying to generate symmetric key using password.\n";
271  return false;
272  }
273 
274  if (!theKey.HasHashCheck()) {
275  if (!theKey.GenerateHashCheck(thePassword)) {
276  otErr << __FUNCTION__
277  << ": Failed trying to generate hash check using password.\n";
278  return false;
279  }
280  }
281 
282  OT_ASSERT(theKey.HasHashCheck());
283 
284  OTPassword theRawSymmetricKey;
285 
286  if (false ==
287  theKey.GetRawKeyFromPassphrase(thePassword, theRawSymmetricKey)) {
288  otErr << __FUNCTION__ << ": Failed trying to retrieve raw symmetric "
289  "key using password.\n";
290  return false;
291  }
292 
293  OTPayload theCipherText;
294 
295  const bool bEncrypted = OTCrypto::It()->Encrypt(
296  theRawSymmetricKey, // The symmetric key, in clear form.
297  theInput.Get(), // This is the Plaintext.
298  theInput.GetLength() + 1, // for null terminator
299  theIV, // Initialization vector.
300  theCipherText); // OUTPUT. (Ciphertext.)
301 
302  //
303  // Success?
304  //
305  if (!bEncrypted) {
306  otErr << __FUNCTION__ << ": (static) call failed to encrypt. Wrong "
307  "key? (Returning false.)\n";
308  return false;
309  }
310 
311  // This is where the envelope final contents will be placed,
312  // including the envelope type, the size of the IV, the IV
313  // itself, and the ciphertext.
314  //
315  m_dataContents.Release();
316 
317  // Write the ENVELOPE TYPE (network order version.)
318  //
319  // 0 == Error
320  // 1 == Asymmetric Key (other functions -- Seal / Open.)
321  // 2 == Symmetric Key (this function -- Encrypt / Decrypt.)
322  // Anything else: error.
323 
324  uint16_t env_type_n = static_cast<uint16_t>(htons(static_cast<uint16_t>(
325  2))); // Calculate "network-order" version of envelope type 2.
326 
327  m_dataContents.Concatenate(reinterpret_cast<void*>(&env_type_n),
328  // (uint32_t here is the 2nd parameter to
329  // Concatenate, and has nothing to do with
330  // env_type_n being uint16_t)
331  static_cast<uint32_t>(sizeof(env_type_n)));
332 
333  // Write IV size (in network-order)
334  //
335  uint32_t ivlen =
336  OTCryptoConfig::SymmetricIvSize(); // Length of IV for this cipher...
337  OT_ASSERT(ivlen >= theIV.GetSize());
338  uint32_t ivlen_n = htonl(
339  theIV.GetSize()); // Calculate "network-order" version of iv length.
340 
341  m_dataContents.Concatenate(reinterpret_cast<void*>(&ivlen_n),
342  static_cast<uint32_t>(sizeof(ivlen_n)));
343 
344  // Write the IV itself.
345  //
346  m_dataContents.Concatenate(theIV.GetPayloadPointer(),
347  static_cast<uint32_t>(theIV.GetSize()));
348 
349  // Write the Ciphertext.
350  //
351  m_dataContents.Concatenate(theCipherText.GetPayloadPointer(),
352  static_cast<uint32_t>(theCipherText.GetSize()));
353 
354  // We don't write the size of the ciphertext before the ciphertext itself,
355  // since the decryption is able to deduce the size based on the total
356  // envelope
357  // size minus the other pieces. We might still want to add that size here,
358  // however.
359  // (for security / safety reasons.)
360 
361  return true;
362 }
static EXPORT OTCrypto * It()
Definition: OTCrypto.cpp:630
static EXPORT uint32_t SymmetricIvSize()
Definition: OTCrypto.cpp:391
virtual bool Encrypt(const OTPassword &theRawSymmetricKey, const char *szInput, uint32_t lInputLength, const OTPayload &theIV, OTPayload &theEncryptedOutput) const =0
#define OT_ASSERT(x)
Definition: Assert.hpp:150
friend class OTPayload
Definition: OTEnvelope.hpp:158
OTLOG_IMPORT OTLogStream otErr
virtual EXPORT void Release()
Definition: OTData.cpp:257
EXPORT void Concatenate(const void *data, uint32_t size)
Definition: OTData.cpp:333
bool opentxs::OTEnvelope::GetAsBookendedString ( OTString strArmorWithBookends,
bool  bEscaped = false 
) const

Definition at line 184 of file OTEnvelope.cpp.

187 {
188  OTASCIIArmor theArmoredText;
189  // This function will base64 ENCODE m_dataContents, and then
190  // Set() that as the string contents on theArmoredText.
191  const bool bSetData = theArmoredText.SetData(
192  m_dataContents, true); // bLineBreaks=true (by default anyway.)
193 
194  if (bSetData) {
195  const bool bWritten = theArmoredText.WriteArmoredString(
196  strArmorWithBookends, "ENVELOPE", // todo hardcoded
197  bEscaped);
198  if (!bWritten)
199  otErr << __FUNCTION__ << ": Failed while calling: "
200  "theArmoredText.WriteArmoredString\n";
201  else
202  return true;
203  }
204  else
205  otErr << __FUNCTION__
206  << ": Failed while calling: "
207  "theArmoredText.SetData(m_dataContents, true)\n";
208 
209  return false;
210 }
OTLOG_IMPORT OTLogStream otErr
bool opentxs::OTEnvelope::GetAsciiArmoredData ( OTASCIIArmor theArmoredText,
bool  bLineBreaks = true 
) const

Definition at line 162 of file OTEnvelope.cpp.

164 {
165  return theArmoredText.SetData(m_dataContents, bLineBreaks);
166 }
bool opentxs::OTEnvelope::Open ( const OTPseudonym theRecipient,
OTString theOutput,
const OTPasswordData pPWData = nullptr 
)

Definition at line 581 of file OTEnvelope.cpp.

583 {
584  return OTCrypto::It()->Open(m_dataContents, theRecipient, theOutput,
585  pPWData);
586 }
static EXPORT OTCrypto * It()
Definition: OTCrypto.cpp:630
virtual bool Open(OTData &dataInput, const OTPseudonym &theRecipient, OTString &theOutput, const OTPasswordData *pPWData=nullptr) const =0
bool opentxs::OTEnvelope::Seal ( const OTPseudonym theRecipient,
const OTString theInput 
)

Definition at line 521 of file OTEnvelope.cpp.

522 {
523  OTString strNymID;
524  mapOfAsymmetricKeys theKeys;
525  theRecipient.GetIdentifier(strNymID);
526  theKeys.insert(std::pair<std::string, OTAsymmetricKey*>(
527  strNymID.Get(),
528  const_cast<OTAsymmetricKey*>(&(theRecipient.GetPublicEncrKey()))));
529 
530  return Seal(theKeys, theInput);
531 }
EXPORT bool Seal(const OTPseudonym &theRecipient, const OTString &theInput)
Definition: OTEnvelope.cpp:521
std::multimap< std::string, OTAsymmetricKey * > mapOfAsymmetricKeys
Definition: OTCrypto.hpp:155
bool opentxs::OTEnvelope::Seal ( const OTAsymmetricKey RecipPubKey,
const OTString theInput 
)

Definition at line 556 of file OTEnvelope.cpp.

558 {
559  mapOfAsymmetricKeys theKeys;
560  theKeys.insert(std::pair<std::string, OTAsymmetricKey*>(
561  "", // Normally the NymID goes here, but we don't know what it is, in
562  // this case.
563  const_cast<OTAsymmetricKey*>(&RecipPubKey)));
564 
565  return Seal(theKeys, theInput);
566 }
EXPORT bool Seal(const OTPseudonym &theRecipient, const OTString &theInput)
Definition: OTEnvelope.cpp:521
std::multimap< std::string, OTAsymmetricKey * > mapOfAsymmetricKeys
Definition: OTCrypto.hpp:155
bool opentxs::OTEnvelope::Seal ( setOfNyms theRecipients,
const OTString theInput 
)

Definition at line 533 of file OTEnvelope.cpp.

534 {
535  mapOfAsymmetricKeys RecipPubKeys;
536 
537  // Loop through theRecipients, and add the public key of each one to a set
538  // of keys.
539  for (auto& it : theRecipients) {
540  OTPseudonym* pNym = it;
541  OT_ASSERT_MSG(nullptr != pNym,
542  "OTEnvelope::Seal: Assert: nullptr pseudonym pointer.");
543 
544  OTString strNymID;
545  pNym->GetIdentifier(strNymID);
546  RecipPubKeys.insert(std::pair<std::string, OTAsymmetricKey*>(
547  strNymID.Get(),
548  const_cast<OTAsymmetricKey*>(&(pNym->GetPublicEncrKey()))));
549  }
550 
551  if (RecipPubKeys.empty()) return false;
552 
553  return Seal(RecipPubKeys, theInput);
554 }
EXPORT bool Seal(const OTPseudonym &theRecipient, const OTString &theInput)
Definition: OTEnvelope.cpp:521
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
std::multimap< std::string, OTAsymmetricKey * > mapOfAsymmetricKeys
Definition: OTCrypto.hpp:155
bool opentxs::OTEnvelope::Seal ( mapOfAsymmetricKeys RecipPubKeys,
const OTString theInput 
)

Definition at line 570 of file OTEnvelope.cpp.

572 {
573  OT_ASSERT_MSG(!RecipPubKeys.empty(),
574  "OTEnvelope::Seal: ASSERT: RecipPubKeys.size() > 0");
575 
576  return OTCrypto::It()->Seal(RecipPubKeys, theInput, m_dataContents);
577 }
static EXPORT OTCrypto * It()
Definition: OTCrypto.cpp:630
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
virtual bool Seal(mapOfAsymmetricKeys &RecipPubKeys, const OTString &theInput, OTData &dataOutput) const =0
bool opentxs::OTEnvelope::SetAsciiArmoredData ( const OTASCIIArmor theArmoredText,
bool  bLineBreaks = true 
)

Definition at line 178 of file OTEnvelope.cpp.

180 {
181  return theArmoredText.GetData(m_dataContents, bLineBreaks);
182 }
bool opentxs::OTEnvelope::SetFromBookendedString ( const OTString strArmorWithBookends,
bool  bEscaped = false 
)

Definition at line 212 of file OTEnvelope.cpp.

215 {
216  OTASCIIArmor theArmoredText;
217  const bool bLoaded = theArmoredText.LoadFromString(
218  const_cast<OTString&>(strArmorWithBookends),
219  bEscaped); // std::string str_override="-----BEGIN");
220 
221  if (bLoaded) {
222  // This function will base64 DECODE theArmoredText's string contents
223  // and return them as binary in m_dataContents
224  const bool bGotData =
225  theArmoredText.GetData(m_dataContents, true); // bLineBreaks = true
226 
227  if (!bGotData)
228  otErr << __FUNCTION__ << ": Failed while calling: "
229  "theArmoredText.GetData\n";
230  else
231  return true;
232  }
233  else
234  otErr << __FUNCTION__ << ": Failed while calling: "
235  "theArmoredText.LoadFromString\n";
236 
237  return false;
238 }
OTLOG_IMPORT OTLogStream otErr

Friends And Related Function Documentation

friend class OTPayload
friend

Definition at line 158 of file OTEnvelope.hpp.


The documentation for this class was generated from the following files: