Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTNymOrSymmetricKey.cpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTNymOrSymmetricKey.cpp
4  *
5  */
6 
7 /************************************************************
8  -----BEGIN PGP SIGNED MESSAGE-----
9  Hash: SHA1
10 
11  * OPEN TRANSACTIONS
12  *
13  * Financial Cryptography and Digital Cash
14  * Library, Protocol, API, Server, CLI, GUI
15  *
16  * -- Anonymous Numbered Accounts.
17  * -- Untraceable Digital Cash.
18  * -- Triple-Signed Receipts.
19  * -- Cheques, Vouchers, Transfers, Inboxes.
20  * -- Basket Currencies, Markets, Payment Plans.
21  * -- Signed, XML, Ricardian-style Contracts.
22  * -- Scripted smart contracts.
23  *
24  * Copyright (C) 2010-2013 by "Fellow Traveler" (A pseudonym)
25  *
26  * EMAIL:
28  *
29  * BITCOIN: 1NtTPVVjDsUfDWybS4BwvHpG2pdS9RnYyQ
30  *
31  * KEY FINGERPRINT (PGP Key in license file):
32  * 9DD5 90EB 9292 4B48 0484 7910 0308 00ED F951 BB8E
33  *
34  * OFFICIAL PROJECT WIKI(s):
35  * https://github.com/FellowTraveler/Moneychanger
36  * https://github.com/FellowTraveler/Open-Transactions/wiki
37  *
38  * WEBSITE:
39  * http://www.OpenTransactions.org/
40  *
41  * Components and licensing:
42  * -- Moneychanger..A Java client GUI.....LICENSE:.....GPLv3
43  * -- otlib.........A class library.......LICENSE:...LAGPLv3
44  * -- otapi.........A client API..........LICENSE:...LAGPLv3
45  * -- opentxs/ot....Command-line client...LICENSE:...LAGPLv3
46  * -- otserver......Server Application....LICENSE:....AGPLv3
47  * Github.com/FellowTraveler/Open-Transactions/wiki/Components
48  *
49  * All of the above OT components were designed and written by
50  * Fellow Traveler, with the exception of Moneychanger, which
51  * was contracted out to Vicky C ([email protected]).
52  * The open-source community has since actively contributed.
53  *
54  * -----------------------------------------------------
55  *
56  * LICENSE:
57  * This program is free software: you can redistribute it
58  * and/or modify it under the terms of the GNU Affero
59  * General Public License as published by the Free Software
60  * Foundation, either version 3 of the License, or (at your
61  * option) any later version.
62  *
63  * ADDITIONAL PERMISSION under the GNU Affero GPL version 3
64  * section 7: (This paragraph applies only to the LAGPLv3
65  * components listed above.) If you modify this Program, or
66  * any covered work, by linking or combining it with other
67  * code, such other code is not for that reason alone subject
68  * to any of the requirements of the GNU Affero GPL version 3.
69  * (==> This means if you are only using the OT API, then you
70  * don't have to open-source your code--only your changes to
71  * Open-Transactions itself must be open source. Similar to
72  * LGPLv3, except it applies to software-as-a-service, not
73  * just to distributing binaries.)
74  *
75  * Extra WAIVER for OpenSSL, Lucre, and all other libraries
76  * used by Open Transactions: This program is released under
77  * the AGPL with the additional exemption that compiling,
78  * linking, and/or using OpenSSL is allowed. The same is true
79  * for any other open source libraries included in this
80  * project: complete waiver from the AGPL is hereby granted to
81  * compile, link, and/or use them with Open-Transactions,
82  * according to their own terms, as long as the rest of the
83  * Open-Transactions terms remain respected, with regard to
84  * the Open-Transactions code itself.
85  *
86  * Lucre License:
87  * This code is also "dual-license", meaning that Ben Lau-
88  * rie's license must also be included and respected, since
89  * the code for Lucre is also included with Open Transactions.
90  * See Open-Transactions/src/otlib/lucre/LUCRE_LICENSE.txt
91  * The Laurie requirements are light, but if there is any
92  * problem with his license, simply remove the Lucre code.
93  * Although there are no other blind token algorithms in Open
94  * Transactions (yet. credlib is coming), the other functions
95  * will continue to operate.
96  * See Lucre on Github: https://github.com/benlaurie/lucre
97  * -----------------------------------------------------
98  * You should have received a copy of the GNU Affero General
99  * Public License along with this program. If not, see:
100  * http://www.gnu.org/licenses/
101  *
102  * If you would like to use this software outside of the free
103  * software license, please contact FellowTraveler.
104  * (Unfortunately many will run anonymously and untraceably,
105  * so who could really stop them?)
106  *
107  * DISCLAIMER:
108  * This program is distributed in the hope that it will be
109  * useful, but WITHOUT ANY WARRANTY; without even the implied
110  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
111  * PURPOSE. See the GNU Affero General Public License for
112  * more details.
113 
114  -----BEGIN PGP SIGNATURE-----
115  Version: GnuPG v1.4.9 (Darwin)
116 
117  iQIcBAEBAgAGBQJRSsfJAAoJEAMIAO35UbuOQT8P/RJbka8etf7wbxdHQNAY+2cC
118  vDf8J3X8VI+pwMqv6wgTVy17venMZJa4I4ikXD/MRyWV1XbTG0mBXk/7AZk7Rexk
119  KTvL/U1kWiez6+8XXLye+k2JNM6v7eej8xMrqEcO0ZArh/DsLoIn1y8p8qjBI7+m
120  aE7lhstDiD0z8mwRRLKFLN2IH5rAFaZZUvj5ERJaoYUKdn4c+RcQVei2YOl4T0FU
121  LWND3YLoH8naqJXkaOKEN4UfJINCwxhe5Ke9wyfLWLUO7NamRkWD2T7CJ0xocnD1
122  sjAzlVGNgaFDRflfIF4QhBx1Ddl6wwhJfw+d08bjqblSq8aXDkmFA7HeunSFKkdn
123  oIEOEgyj+veuOMRJC5pnBJ9vV+7qRdDKQWaCKotynt4sWJDGQ9kWGWm74SsNaduN
124  TPMyr9kNmGsfR69Q2Zq/FLcLX/j8ESxU+HYUB4vaARw2xEOu2xwDDv6jt0j3Vqsg
125  x7rWv4S/Eh18FDNDkVRChiNoOIilLYLL6c38uMf1pnItBuxP3uhgY6COm59kVaRh
126  nyGTYCDYD2TK+fI9o89F1297uDCwEJ62U0Q7iTDp5QuXCoxkPfv8/kX6lS6T3y9G
127  M9mqIoLbIQ1EDntFv7/t6fUTS2+46uCrdZWbQ5RjYXdrzjij02nDmJAm2BngnZvd
128  kamH0Y/n11lCvo1oQxM+
129  =uSzz
130  -----END PGP SIGNATURE-----
131  **************************************************************/
132 
133 #include "stdafx.hpp"
134 
136 
137 #include "crypto/OTEnvelope.hpp"
138 #include "OTLog.hpp"
139 #include "crypto/OTPassword.hpp"
140 #include "OTPseudonym.hpp"
141 #include "crypto/OTSymmetricKey.hpp"
142 
143 extern "C" {
144 #if defined(OPENTXS_HAVE_NETINET_IN_H)
145 #include <netinet/in.h>
146 #endif
147 }
148 
149 namespace opentxs
150 {
151 
152 // There are certain cases where we want the option to pass a Nym OR a
153 // symmetric key, and the function should be able to handle either.
154 // This class is used to make that possible.
155 //
157 OTNym_or_SymmetricKey::OTNym_or_SymmetricKey()
158  : m_pNym(nullptr)
159  , m_pKey(nullptr)
160  , m_pPassword(nullptr)
161  , m_bCleanupPassword(false)
162  , m_pstrDisplay(nullptr)
163 {
164 }
165 
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 }
191 
192 OTNym_or_SymmetricKey::OTNym_or_SymmetricKey(
193  const OTNym_or_SymmetricKey& rhs) // same type
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 }
235 
236 OTNym_or_SymmetricKey::OTNym_or_SymmetricKey(
237  const OTPseudonym& theNym,
238  const OTString* pstrDisplay) // construct with nym
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 }
246 
247 OTNym_or_SymmetricKey::OTNym_or_SymmetricKey(
248  const OTSymmetricKey& theKey,
249  const OTString* pstrDisplay) // construct with key
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 }
257 
258 OTNym_or_SymmetricKey::OTNym_or_SymmetricKey(
259  const OTSymmetricKey& theKey,
260  const OTPassword& thePassword, // construct with key and password.
261  const OTString* pstrDisplay)
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 }
269 
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 }
280 
282  OTNym_or_SymmetricKey other) // passed by value.
283 {
284  // swap this with other
285  swap(other);
286 
287  // by convention, always return *this
288  return *this;
289 }
290 
291 // This is just a wrapper class.
292 void OTNym_or_SymmetricKey::Release() // Someday make this virtual, if we ever
293  // subclass it.
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 }
303 
304 // This is just a wrapper class. (Destructor doesn't call this because we aren't
305 // the owner.)
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 }
326 
328 {
329  OTIdentifier idTHIS, idRHS;
330 
331  GetIdentifier(idTHIS);
332  rhs.GetIdentifier(idRHS);
333 
334  return (idTHIS == idRHS);
335 }
336 
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 }
349 
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 }
362 
363 // From OTEnvelope:
364 // bool GetAsBookendedString (OTString& strArmorWithBookends,
365 // bool bEscaped = false ) const;
366 // bool SetFromBookendedString(const OTString& strArmorWithBookends,
367 // bool bEscaped = false );
368 
370  OTString& strOutput,
371  const OTString* pstrDisplay)
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 }
443 
445  const OTString& strInput,
446  const OTString* pstrDisplay)
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 }
513 
514 } // namespace opentxs
EXPORT void GetIdentifier(OTIdentifier &theIdentifier) const
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
OTLOG_IMPORT OTLogStream otOut
EXPORT OTPseudonym * GetNym() const
EXPORT bool Seal(const OTPseudonym &theRecipient, const OTString &theInput)
Definition: OTEnvelope.cpp:521
EXPORT bool Encrypt(const OTString &theInput, OTSymmetricKey &theKey, const OTPassword &thePassword)
Definition: OTEnvelope.cpp:245
EXPORT OTNym_or_SymmetricKey & operator=(OTNym_or_SymmetricKey other)
static EXPORT OTPassword * GetPassphraseFromUser(const OTString *pstrDisplay=nullptr, bool bAskTwice=false)
EXPORT bool Seal_or_Encrypt(OTEnvelope &outputEnvelope, const OTString &strInput, const OTString *pstrDisplay=nullptr)
EXPORT OTSymmetricKey * GetKey() const
#define OT_ASSERT(x)
Definition: Assert.hpp:150
#define OT_FAIL
Definition: Assert.hpp:139
EXPORT void swap(OTNym_or_SymmetricKey &other)
EXPORT const char * Get() const
Definition: OTString.cpp:1045
EXPORT void GetIdentifier(OTIdentifier &theIdentifier) const
EXPORT OTPassword * GetPassword() const
virtual EXPORT void Release()
EXPORT bool CompareID(const OTNym_or_SymmetricKey &rhs) const
EXPORT void GetIdentifier(OTIdentifier &theIdentifier) const
EXPORT bool Open_or_Decrypt(const OTEnvelope &inputEnvelope, OTString &strOutput, const OTString *pstrDisplay=nullptr)