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

#include <OTNymOrSymmetricKey.hpp>

Public Member Functions

EXPORT OTPseudonymGetNym () const
 
EXPORT OTSymmetricKeyGetKey () const
 
EXPORT OTPasswordGetPassword () const
 
EXPORT bool IsNym () const
 
EXPORT bool IsKey () const
 
EXPORT bool HasPassword () const
 
EXPORT void GetIdentifier (OTIdentifier &theIdentifier) const
 
EXPORT void GetIdentifier (OTString &strIdentifier) const
 
EXPORT bool CompareID (const OTNym_or_SymmetricKey &rhs) const
 
EXPORT bool Seal_or_Encrypt (OTEnvelope &outputEnvelope, const OTString &strInput, const OTString *pstrDisplay=nullptr)
 
EXPORT bool Open_or_Decrypt (const OTEnvelope &inputEnvelope, OTString &strOutput, const OTString *pstrDisplay=nullptr)
 
EXPORT ~OTNym_or_SymmetricKey ()
 
EXPORT OTNym_or_SymmetricKey (const OTNym_or_SymmetricKey &rhs)
 
EXPORT OTNym_or_SymmetricKey (const OTPseudonym &theNym, const OTString *pstrDisplay=nullptr)
 
EXPORT OTNym_or_SymmetricKey (const OTSymmetricKey &theKey, const OTString *pstrDisplay=nullptr)
 
EXPORT OTNym_or_SymmetricKey (const OTSymmetricKey &theKey, const OTPassword &thePassword, const OTString *pstrDisplay=nullptr)
 
EXPORT void swap (OTNym_or_SymmetricKey &other)
 
EXPORT OTNym_or_SymmetricKeyoperator= (OTNym_or_SymmetricKey other)
 
EXPORT void Release ()
 
EXPORT void Release_Nym_or_SymmetricKey ()
 

Detailed Description

Definition at line 150 of file OTNymOrSymmetricKey.hpp.

Constructor & Destructor Documentation

opentxs::OTNym_or_SymmetricKey::~OTNym_or_SymmetricKey ( )

Definition at line 166 of file OTNymOrSymmetricKey.cpp.

167 {
168  // We don't own these objects.
169  // Rather, we own a pointer to ONE of them, since we are a wrapper
170  // for this one or that.
171  //
172  m_pNym = nullptr;
173 
174  m_pKey = nullptr;
175 
176  if (m_bCleanupPassword && (nullptr != m_pPassword)) delete m_pPassword;
177  m_pPassword = nullptr; // optional
178 
179  m_pstrDisplay = nullptr;
180 
181  // Since this is merely a wrapper class, we don't even Release() these
182  // things.
183  // However, we DO have a release function, since the programmatic USER of
184  // this class
185  // MAY wish to Release() whatever it is wrapping. That's his choice. But we
186  // don't call
187  // it here in the destructor, because we aren't the owner.
188  //
189  // Release_Nym_or_SymmetricKey();
190 }
opentxs::OTNym_or_SymmetricKey::OTNym_or_SymmetricKey ( const OTNym_or_SymmetricKey rhs)

Definition at line 192 of file OTNymOrSymmetricKey.cpp.

194  : m_pNym(nullptr),
195  m_pKey(nullptr),
196  m_pPassword(nullptr),
197  m_bCleanupPassword(false),
198  m_pstrDisplay(rhs.m_pstrDisplay)
199 {
200  // This class doesn't do any cleanup, it's just a temporary wrapper.
201  // So we won't have anything get deleted twice, because this class won't
202  // even delete it once.
203  //
204  m_pNym = rhs.m_pNym;
205 
206  m_pKey = rhs.m_pKey;
207  m_pPassword = rhs.m_pPassword; // optional
208 
209  // m_bCleanupPassword = rhs.m_bCleanupPassword; // optional
210  //
211  // This is commented out because this object keeps a POINTER to the
212  // password,
213  // which is usually owned by the caller. (So we normally wouldn't delete
214  // it.)
215  // But sometimes, we have to CREATE the password, in which case we store it
216  // until
217  // we destruct, and then destroy it in our destructor. (Having it available
218  // in the
219  // meantime to use, without having to load again.)
220  // m_bCleanupPassword thus normally tells us whether the password was passed
221  // in
222  // by its owner for reference purposes only, or whether we created it
223  // internally and
224  // thus need to clean it up ourselves.
225  // THEREFORE, here in the COPY CONSTRUCTOR, we need to keep a pointer to the
226  // password
227  // from the rhs object, in case we need that password, but we cannot DESTROY
228  // that password,
229  // if rhs is also destroying it! Therefore we copy the password, but we
230  // leave m_bCleanupPassword
231  // as false (its default) since THIS INSTANCE definitely does not own
232  // m_pPassword.
233  //
234 }
opentxs::OTNym_or_SymmetricKey::OTNym_or_SymmetricKey ( const OTPseudonym theNym,
const OTString pstrDisplay = nullptr 
)

Definition at line 236 of file OTNymOrSymmetricKey.cpp.

239  : m_pNym(const_cast<OTPseudonym*>(&theNym)),
240  m_pKey(nullptr),
241  m_pPassword(nullptr),
242  m_bCleanupPassword(false),
243  m_pstrDisplay(pstrDisplay)
244 {
245 }
opentxs::OTNym_or_SymmetricKey::OTNym_or_SymmetricKey ( const OTSymmetricKey theKey,
const OTString pstrDisplay = nullptr 
)

Definition at line 247 of file OTNymOrSymmetricKey.cpp.

250  : m_pNym(nullptr),
251  m_pKey(const_cast<OTSymmetricKey*>(&theKey)),
252  m_pPassword(nullptr),
253  m_bCleanupPassword(false),
254  m_pstrDisplay(pstrDisplay)
255 {
256 }
opentxs::OTNym_or_SymmetricKey::OTNym_or_SymmetricKey ( const OTSymmetricKey theKey,
const OTPassword thePassword,
const OTString pstrDisplay = nullptr 
)

Definition at line 258 of file OTNymOrSymmetricKey.cpp.

262  : m_pNym(nullptr)
263  , m_pKey(const_cast<OTSymmetricKey*>(&theKey))
264  , m_pPassword(const_cast<OTPassword*>(&thePassword))
265  , m_bCleanupPassword(false)
266  , m_pstrDisplay(pstrDisplay)
267 {
268 }

Member Function Documentation

bool opentxs::OTNym_or_SymmetricKey::CompareID ( const OTNym_or_SymmetricKey rhs) const

Definition at line 327 of file OTNymOrSymmetricKey.cpp.

328 {
329  OTIdentifier idTHIS, idRHS;
330 
331  GetIdentifier(idTHIS);
332  rhs.GetIdentifier(idRHS);
333 
334  return (idTHIS == idRHS);
335 }
EXPORT void GetIdentifier(OTIdentifier &theIdentifier) const
void opentxs::OTNym_or_SymmetricKey::GetIdentifier ( OTIdentifier theIdentifier) const

Definition at line 337 of file OTNymOrSymmetricKey.cpp.

338 {
339  if (IsNym()) {
340  m_pNym->GetIdentifier(theIdentifier);
341  }
342  else if (IsKey()) {
343  m_pKey->GetIdentifier(theIdentifier);
344  }
345  else {
346  OT_FAIL; // should never happen
347  }
348 }
EXPORT void GetIdentifier(OTIdentifier &theIdentifier) const
#define OT_FAIL
Definition: Assert.hpp:139
EXPORT void GetIdentifier(OTIdentifier &theIdentifier) const
void opentxs::OTNym_or_SymmetricKey::GetIdentifier ( OTString strIdentifier) const

Definition at line 350 of file OTNymOrSymmetricKey.cpp.

351 {
352  if (IsNym()) {
353  m_pNym->GetIdentifier(strIdentifier);
354  }
355  else if (IsKey()) {
356  m_pKey->GetIdentifier(strIdentifier);
357  }
358  else {
359  OT_FAIL; // should never happen
360  }
361 }
EXPORT void GetIdentifier(OTIdentifier &theIdentifier) const
#define OT_FAIL
Definition: Assert.hpp:139
EXPORT void GetIdentifier(OTIdentifier &theIdentifier) const
EXPORT OTSymmetricKey* opentxs::OTNym_or_SymmetricKey::GetKey ( ) const
inline

Definition at line 175 of file OTNymOrSymmetricKey.hpp.

176  {
177  return m_pKey;
178  }
EXPORT OTPseudonym* opentxs::OTNym_or_SymmetricKey::GetNym ( ) const
inline

Definition at line 171 of file OTNymOrSymmetricKey.hpp.

172  {
173  return m_pNym;
174  }
EXPORT OTPassword* opentxs::OTNym_or_SymmetricKey::GetPassword ( ) const
inline

Definition at line 179 of file OTNymOrSymmetricKey.hpp.

180  {
181  return m_pPassword;
182  } // for symmetric key (optional)
EXPORT bool opentxs::OTNym_or_SymmetricKey::HasPassword ( ) const
inline

Definition at line 192 of file OTNymOrSymmetricKey.hpp.

193  {
194  return (nullptr != m_pPassword);
195  } // for symmetric key (optional)
EXPORT bool opentxs::OTNym_or_SymmetricKey::IsKey ( ) const
inline

Definition at line 188 of file OTNymOrSymmetricKey.hpp.

189  {
190  return (nullptr != m_pKey);
191  }
EXPORT bool opentxs::OTNym_or_SymmetricKey::IsNym ( ) const
inline

Definition at line 184 of file OTNymOrSymmetricKey.hpp.

185  {
186  return (nullptr != m_pNym);
187  }
bool opentxs::OTNym_or_SymmetricKey::Open_or_Decrypt ( const OTEnvelope inputEnvelope,
OTString strOutput,
const OTString pstrDisplay = nullptr 
)

Definition at line 369 of file OTNymOrSymmetricKey.cpp.

372 {
373  const char* szFunc = "OTNym_or_SymmetricKey::Open_or_Decrypt";
374 
375  bool bSuccess = false;
376  bool bHadToInstantiatePassword = false;
377 
378  // Decrypt/Open inputEnvelope into strOutput
379  //
380  if (IsNym()) // *this is a Nym.
381  {
382  bSuccess = (const_cast<OTEnvelope&>(inputEnvelope))
383  .Open(*(GetNym()), strOutput);
384  }
385  else if (IsKey()) // *this is a symmetric key, possibly with a
386  // password already as well.
387  {
388  OTPassword* pPassword = nullptr;
389 
390  if (HasPassword()) // Password is already available. Let's use it.
391  pPassword = GetPassword();
392  else // NO PASSWORD already? let's collect it from the user...
393  {
394  const OTString strDisplay(
395  (nullptr == pstrDisplay) ? szFunc : pstrDisplay->Get());
396  // NOTE: m_pstrDisplay overrides this below.
397 
398  // returns a text OTPassword, or nullptr.
399  //
401  (nullptr == m_pstrDisplay)
402  ? &strDisplay
403  : m_pstrDisplay); // bool bAskTwice=false
404 
405  if (nullptr ==
406  pPassword) // Unable to retrieve passphrase from user.
407  {
408  otOut << szFunc
409  << ": Failed trying to retrieve passphrase for key. "
410  "Returning false.\n";
411  return false;
412  }
413  else // OTNym_or_SymmetricKey stores this, if it creates it.
414  // (And cleans it up on destruction, IF it created it.)
415  //
416  bHadToInstantiatePassword = true;
417  }
418 
419  bSuccess = (const_cast<OTEnvelope&>(inputEnvelope))
420  .Decrypt(strOutput, *(GetKey()), *pPassword);
421 
422  // We only set this, presuming we have to at all, if it was a success.
423  if (bHadToInstantiatePassword) {
424  if (bSuccess) {
425  m_bCleanupPassword = true;
426  m_pPassword = pPassword; // Not bothering to cleanup whatever
427  // was here before, since we only end
428  // up here if m_pPassword was set to
429  // nullptr (according to above logic...)
430  }
431  else // We instantiated the password, but the decrypt failed.
432  // (Need to cleanup the password then.)
433  {
434  delete pPassword;
435  pPassword = nullptr;
436  }
437  }
438  }
439  // else ? should never happen.
440 
441  return bSuccess;
442 }
OTLOG_IMPORT OTLogStream otOut
EXPORT OTPseudonym * GetNym() const
static EXPORT OTPassword * GetPassphraseFromUser(const OTString *pstrDisplay=nullptr, bool bAskTwice=false)
EXPORT OTSymmetricKey * GetKey() const
EXPORT OTPassword * GetPassword() const
OTNym_or_SymmetricKey & opentxs::OTNym_or_SymmetricKey::operator= ( OTNym_or_SymmetricKey  other)

Definition at line 281 of file OTNymOrSymmetricKey.cpp.

283 {
284  // swap this with other
285  swap(other);
286 
287  // by convention, always return *this
288  return *this;
289 }
EXPORT void swap(OTNym_or_SymmetricKey &other)
void opentxs::OTNym_or_SymmetricKey::Release ( )

Definition at line 292 of file OTNymOrSymmetricKey.cpp.

294 {
295  OT_ASSERT((m_pNym != nullptr) ||
296  (m_pKey != nullptr)); // m_pPassword is optional
297 
299 
300  // no need to call ot_super::Release here, since this class has no
301  // superclass.
302 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
void opentxs::OTNym_or_SymmetricKey::Release_Nym_or_SymmetricKey ( )

Definition at line 306 of file OTNymOrSymmetricKey.cpp.

307 {
308  if (nullptr != m_pNym) {
309  // m_pNym->Release(); // no such call on OTPseudonym. (Otherwise it
310  // wouldn't be commented out.)
311  }
312 
313  if (nullptr != m_pKey) m_pKey->Release();
314 
315  if (nullptr != m_pPassword) {
316  m_pPassword->zeroMemory();
317 
318  if (m_bCleanupPassword) // Only in cases where *this is the actual owner
319  // of m_pPassword.
320  {
321  delete m_pPassword;
322  m_pPassword = nullptr;
323  }
324  }
325 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
virtual EXPORT void Release()
bool opentxs::OTNym_or_SymmetricKey::Seal_or_Encrypt ( OTEnvelope outputEnvelope,
const OTString strInput,
const OTString pstrDisplay = nullptr 
)

Definition at line 444 of file OTNymOrSymmetricKey.cpp.

447 {
448  const char* szFunc = "OTNym_or_SymmetricKey::Seal_or_Encrypt";
449 
450  bool bSuccess = false;
451  bool bHadToInstantiatePassword = false;
452 
453  // Encrypt/Seal strInput into outputEnvelope
454  //
455  if (IsNym()) {
456  bSuccess = outputEnvelope.Seal(*(GetNym()), strInput);
457  }
458  else if (IsKey()) {
459  OTPassword* pPassword = nullptr;
460 
461  if (HasPassword()) // Password is already available. Let's use it.
462  pPassword = GetPassword();
463  else // no password? let's collect it from the user...
464  {
465  const OTString strDisplay(
466  (nullptr == pstrDisplay) ? szFunc : pstrDisplay->Get());
467  // NOTE: m_pstrDisplay overrides this below.
468 
469  // returns a text OTPassword, or nullptr.
470  //
472  (nullptr == m_pstrDisplay)
473  ? &strDisplay
474  : m_pstrDisplay); // bool bAskTwice=false
475 
476  if (nullptr ==
477  pPassword) // Unable to retrieve passphrase from user.
478  {
479  otOut << szFunc
480  << ": Failed trying to retrieve passphrase for key. "
481  "Returning false.\n";
482  return false;
483  }
484  else // OTNym_or_SymmetricKey stores this, if it creates it.
485  // (And cleans it up on destruction, IF it created it.)
486  //
487  bHadToInstantiatePassword = true;
488  }
489 
490  bSuccess = outputEnvelope.Encrypt(strInput, *(GetKey()), *pPassword);
491 
492  // We only set this, presuming we have to at all, if it was a success.
493  if (bHadToInstantiatePassword) {
494  if (bSuccess) {
495  m_bCleanupPassword = true;
496  m_pPassword = pPassword; // Not bothering to cleanup whatever
497  // was here before, since we only end
498  // up here if m_pPassword was set to
499  // nullptr (according to above logic...)
500  }
501  else // We instantiated the password, but the encrypt failed.
502  // (Need to cleanup the password then.)
503  {
504  delete pPassword;
505  pPassword = nullptr;
506  }
507  }
508  }
509  // else ? should never happen.
510 
511  return bSuccess;
512 }
OTLOG_IMPORT OTLogStream otOut
EXPORT OTPseudonym * GetNym() const
static EXPORT OTPassword * GetPassphraseFromUser(const OTString *pstrDisplay=nullptr, bool bAskTwice=false)
EXPORT OTSymmetricKey * GetKey() const
EXPORT OTPassword * GetPassword() const
void opentxs::OTNym_or_SymmetricKey::swap ( OTNym_or_SymmetricKey other)

Definition at line 270 of file OTNymOrSymmetricKey.cpp.

271 {
272  if (&other != this) {
273  std::swap(m_pNym, other.m_pNym);
274  std::swap(m_pKey, other.m_pKey);
275  std::swap(m_pPassword, other.m_pPassword);
276  std::swap(m_bCleanupPassword, other.m_bCleanupPassword);
277  std::swap(m_pstrDisplay, other.m_pstrDisplay);
278  }
279 }

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