Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTKeypair.cpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTKeypair.cpp
4  *
5  */
6 
7 // A nym contains a list of credentials
8 //
9 // Each credential contains a "master" subkey, and a list of subkeys
10 // signed by that master.
11 //
12 // The same class (subkey) is used because there are master credentials
13 // and subkey credentials, so we're using a single "subkey" class to
14 // encapsulate each credential, both for the master credential and
15 // for each subkey credential.
16 //
17 // Each subkey has 3 key pairs: encryption, signing, and authentication.
18 //
19 // Each key pair has 2 OTAsymmetricKeys (public and private.)
20 
21 /************************************************************
22  -----BEGIN PGP SIGNED MESSAGE-----
23  Hash: SHA1
24 
25  * OPEN TRANSACTIONS
26  *
27  * Financial Cryptography and Digital Cash
28  * Library, Protocol, API, Server, CLI, GUI
29  *
30  * -- Anonymous Numbered Accounts.
31  * -- Untraceable Digital Cash.
32  * -- Triple-Signed Receipts.
33  * -- Cheques, Vouchers, Transfers, Inboxes.
34  * -- Basket Currencies, Markets, Payment Plans.
35  * -- Signed, XML, Ricardian-style Contracts.
36  * -- Scripted smart contracts.
37  *
38  * Copyright (C) 2010-2013 by "Fellow Traveler" (A pseudonym)
39  *
40  * EMAIL:
42  *
43  * BITCOIN: 1NtTPVVjDsUfDWybS4BwvHpG2pdS9RnYyQ
44  *
45  * KEY FINGERPRINT (PGP Key in license file):
46  * 9DD5 90EB 9292 4B48 0484 7910 0308 00ED F951 BB8E
47  *
48  * OFFICIAL PROJECT WIKI(s):
49  * https://github.com/FellowTraveler/Moneychanger
50  * https://github.com/FellowTraveler/Open-Transactions/wiki
51  *
52  * WEBSITE:
53  * http://www.OpenTransactions.org/
54  *
55  * Components and licensing:
56  * -- Moneychanger..A Java client GUI.....LICENSE:.....GPLv3
57  * -- otlib.........A class library.......LICENSE:...LAGPLv3
58  * -- otapi.........A client API..........LICENSE:...LAGPLv3
59  * -- opentxs/ot....Command-line client...LICENSE:...LAGPLv3
60  * -- otserver......Server Application....LICENSE:....AGPLv3
61  * Github.com/FellowTraveler/Open-Transactions/wiki/Components
62  *
63  * All of the above OT components were designed and written by
64  * Fellow Traveler, with the exception of Moneychanger, which
65  * was contracted out to Vicky C ([email protected]).
66  * The open-source community has since actively contributed.
67  *
68  * -----------------------------------------------------
69  *
70  * LICENSE:
71  * This program is free software: you can redistribute it
72  * and/or modify it under the terms of the GNU Affero
73  * General Public License as published by the Free Software
74  * Foundation, either version 3 of the License, or (at your
75  * option) any later version.
76  *
77  * ADDITIONAL PERMISSION under the GNU Affero GPL version 3
78  * section 7: (This paragraph applies only to the LAGPLv3
79  * components listed above.) If you modify this Program, or
80  * any covered work, by linking or combining it with other
81  * code, such other code is not for that reason alone subject
82  * to any of the requirements of the GNU Affero GPL version 3.
83  * (==> This means if you are only using the OT API, then you
84  * don't have to open-source your code--only your changes to
85  * Open-Transactions itself must be open source. Similar to
86  * LGPLv3, except it applies to software-as-a-service, not
87  * just to distributing binaries.)
88  *
89  * Extra WAIVER for OpenSSL, Lucre, and all other libraries
90  * used by Open Transactions: This program is released under
91  * the AGPL with the additional exemption that compiling,
92  * linking, and/or using OpenSSL is allowed. The same is true
93  * for any other open source libraries included in this
94  * project: complete waiver from the AGPL is hereby granted to
95  * compile, link, and/or use them with Open-Transactions,
96  * according to their own terms, as long as the rest of the
97  * Open-Transactions terms remain respected, with regard to
98  * the Open-Transactions code itself.
99  *
100  * Lucre License:
101  * This code is also "dual-license", meaning that Ben Lau-
102  * rie's license must also be included and respected, since
103  * the code for Lucre is also included with Open Transactions.
104  * See Open-Transactions/src/otlib/lucre/LUCRE_LICENSE.txt
105  * The Laurie requirements are light, but if there is any
106  * problem with his license, simply remove the Lucre code.
107  * Although there are no other blind token algorithms in Open
108  * Transactions (yet. credlib is coming), the other functions
109  * will continue to operate.
110  * See Lucre on Github: https://github.com/benlaurie/lucre
111  * -----------------------------------------------------
112  * You should have received a copy of the GNU Affero General
113  * Public License along with this program. If not, see:
114  * http://www.gnu.org/licenses/
115  *
116  * If you would like to use this software outside of the free
117  * software license, please contact FellowTraveler.
118  * (Unfortunately many will run anonymously and untraceably,
119  * so who could really stop them?)
120  *
121  * DISCLAIMER:
122  * This program is distributed in the hope that it will be
123  * useful, but WITHOUT ANY WARRANTY; without even the implied
124  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
125  * PURPOSE. See the GNU Affero General Public License for
126  * more details.
127 
128  -----BEGIN PGP SIGNATURE-----
129  Version: GnuPG v1.4.9 (Darwin)
130 
131  iQIcBAEBAgAGBQJRSsfJAAoJEAMIAO35UbuOQT8P/RJbka8etf7wbxdHQNAY+2cC
132  vDf8J3X8VI+pwMqv6wgTVy17venMZJa4I4ikXD/MRyWV1XbTG0mBXk/7AZk7Rexk
133  KTvL/U1kWiez6+8XXLye+k2JNM6v7eej8xMrqEcO0ZArh/DsLoIn1y8p8qjBI7+m
134  aE7lhstDiD0z8mwRRLKFLN2IH5rAFaZZUvj5ERJaoYUKdn4c+RcQVei2YOl4T0FU
135  LWND3YLoH8naqJXkaOKEN4UfJINCwxhe5Ke9wyfLWLUO7NamRkWD2T7CJ0xocnD1
136  sjAzlVGNgaFDRflfIF4QhBx1Ddl6wwhJfw+d08bjqblSq8aXDkmFA7HeunSFKkdn
137  oIEOEgyj+veuOMRJC5pnBJ9vV+7qRdDKQWaCKotynt4sWJDGQ9kWGWm74SsNaduN
138  TPMyr9kNmGsfR69Q2Zq/FLcLX/j8ESxU+HYUB4vaARw2xEOu2xwDDv6jt0j3Vqsg
139  x7rWv4S/Eh18FDNDkVRChiNoOIilLYLL6c38uMf1pnItBuxP3uhgY6COm59kVaRh
140  nyGTYCDYD2TK+fI9o89F1297uDCwEJ62U0Q7iTDp5QuXCoxkPfv8/kX6lS6T3y9G
141  M9mqIoLbIQ1EDntFv7/t6fUTS2+46uCrdZWbQ5RjYXdrzjij02nDmJAm2BngnZvd
142  kamH0Y/n11lCvo1oQxM+
143  =uSzz
144  -----END PGP SIGNATURE-----
145  **************************************************************/
146 
147 #include "stdafx.hpp"
148 
149 #include "crypto/OTKeypair.hpp"
150 
152 #include "OTContract.hpp"
153 #include "util/OTFolders.hpp"
154 #include "OTLog.hpp"
156 #include "crypto/OTSignature.hpp"
157 #include "OTStorage.hpp"
158 
159 // DONE: Add OTKeypair member for m_pMetadata.
160 // Add method to set the Metadata. Or instead of a member,
161 // just have the method set the public and private keys.
162 //
163 // Then a Subkey can have a similar function which sets the metadata
164 // for its three keypairs (they are identical except for the A|E|S.)
165 //
166 // When a Nym is loaded, load up its master credentials and all their
167 // subcredentials. Since their metadata was supposedly set properly at
168 // creation, verify it at load time.
169 
170 // TODO: on OTNym, change GetPublicKey to GetPublicKeyForVerify or
171 // GetPublicKeyForEncrypt or GetPublicKeyForTransmission. Then
172 // rebuild so that all places using GetPublicKey are forced to choose
173 // one of those. Same with GetPrivateKeyForSigning, GetPrivateKeyForDecrypt,
174 // and GetPrivateKeyForAuthentication.
175 
176 // DONE: add the methods to OTPseudonym for generating a master credential
177 // contract
178 // and a sub contract. Add ability to save / load with this data. Then go from
179 // there.
180 
181 namespace opentxs
182 {
183 
185  : m_pkeyPublic(OTAsymmetricKey::KeyFactory())
186  , m_pkeyPrivate(OTAsymmetricKey::KeyFactory())
187 {
188 }
189 
191 {
192 
193  if (nullptr != m_pkeyPublic) delete m_pkeyPublic; // todo: else error
194  m_pkeyPublic = nullptr;
195 
196  if (nullptr != m_pkeyPrivate) delete m_pkeyPrivate; // todo: else error
197  m_pkeyPrivate = nullptr;
198 }
199 
201 {
202  OT_ASSERT(nullptr != m_pkeyPublic);
203  OT_ASSERT(nullptr != m_pkeyPrivate);
204  OT_ASSERT(nullptr != m_pkeyPublic->m_pMetadata);
205  OT_ASSERT(nullptr != m_pkeyPrivate->m_pMetadata);
206 
207  // Set it for both keys.
208  //
209  *(m_pkeyPublic->m_pMetadata) = theMetadata;
210  *(m_pkeyPrivate->m_pMetadata) = theMetadata;
211 }
212 
214 {
215  OT_ASSERT(nullptr != m_pkeyPublic);
216 
217  return m_pkeyPublic->IsPublic(); // This means it actually has a public key
218  // in it, or tried to.
219 }
220 
222 {
223  OT_ASSERT(nullptr != m_pkeyPrivate);
224 
225  return m_pkeyPrivate->IsPrivate(); // This means it actually has a private
226  // key in it, or tried to.
227 }
228 
230 {
231  OT_ASSERT(nullptr != m_pkeyPublic);
232 
233  return (*m_pkeyPublic);
234 }
235 
237 {
238  OT_ASSERT(nullptr != m_pkeyPrivate);
239 
240  return (*m_pkeyPrivate);
241 }
242 
244  const OTString* pstrReason,
245  const OTPassword* pImportPassword) const
246 {
247  OT_ASSERT(nullptr != m_pkeyPublic);
248 
249  OTString strCert,
250  strReason(nullptr == pstrReason ? "OTKeypair::SaveCertToString"
251  : pstrReason->Get());
252 
253  const bool bSaved =
254  m_pkeyPublic->SaveCertToString(strCert, &strReason, pImportPassword);
255 
256  if (bSaved) strOutput = strCert;
257 
258  return bSaved;
259 }
260 
262  const OTString* pstrReason,
263  const OTPassword* pImportPassword) const
264 {
265  OT_ASSERT(nullptr != m_pkeyPrivate);
266 
267  OTString strPrivateKey;
268 
269  const bool bSaved = m_pkeyPrivate->SavePrivateKeyToString(
270  strPrivateKey, pstrReason, pImportPassword);
271 
272  if (bSaved) strOutput = strPrivateKey;
273 
274  return bSaved;
275 }
276 
278  const OTString* pstrReason,
279  const OTPassword* pImportPassword)
280 {
281  OTString strCert, strPrivateKey;
282 
283  const bool bSaved1 = SaveCertToString(strCert, pstrReason, pImportPassword);
284  const bool bSaved2 =
285  SavePrivateKeyToString(strPrivateKey, pstrReason, pImportPassword);
286 
287  if (bSaved1 && bSaved2)
288  strOutput.Format(const_cast<char*>("%s%s"), strPrivateKey.Get(),
289  strCert.Get());
290 
291  return (bSaved1 && bSaved2);
292 }
293 
295  const OTString& strInput, const OTString* pstrReason,
296  const OTPassword* pImportPassword)
297 {
298  OT_ASSERT(strInput.Exists());
299 
300  // "escaped" means pre-pended with "- " as in: - -----BEGIN CER....
301  //
302  const bool bPublic = LoadPublicKeyFromCertString(
303  strInput, false, // bool bEscaped=true by default
304  pstrReason, pImportPassword);
305  const bool bPrivate = LoadPrivateKeyFromCertString(
306  strInput, false, // bool bEscaped=true by default,
307  pstrReason, pImportPassword);
308  if (!bPublic) {
309  otErr << __FUNCTION__
310  << ": Although the input string apparently exists, "
311  "LoadPublicKeyFromCertString returned false.\n";
312  return false;
313  }
314  else {
315  otInfo << __FUNCTION__
316  << ": Successfully loaded public key from string.\n";
317  }
318 
319  if (!bPrivate) {
320  otErr << __FUNCTION__
321  << ": Although the input string apparently exists, "
322  "LoadPrivateKeyFromCertString returned false.\n";
323  return false;
324  }
325  else {
326  otInfo << __FUNCTION__
327  << ": Successfully loaded private key from string.\n";
328  }
329 
330  return true;
331 }
332 
334  OTString* pstrOutputCert, const OTString* pstrReason,
335  const OTPassword* pImportPassword)
336 {
337  OT_ASSERT(nullptr != m_pkeyPrivate);
338  OT_ASSERT(nullptr != m_pkeyPublic);
339 
340  OTString strOutput;
341  const bool bSuccess =
342  SaveCertAndPrivateKeyToString(strOutput, pstrReason, pImportPassword);
343 
344  if (bSuccess) {
345  // todo security. Revisit this part during security audit.
346  //
347  const OTString strFilename("temp.nym"); // todo stop hardcoding. Plus
348  // this maybe should select a
349  // random number too.
350 
351  if (!OTDB::StorePlainString(strOutput.Get(), OTFolders::Cert().Get(),
352  strFilename.Get())) // temp.nym
353  {
354  otErr << __FUNCTION__
355  << ": Failure storing new cert in temp file: " << strFilename
356  << "\n";
357  return false;
358  }
359 
360  if (!LoadBothKeysFromCertFile(OTFolders::Cert().Get(), strFilename,
361  pstrReason, pImportPassword))
362  return false; // LoadBothKeysFromCertFile already has error logs, no
363  // need to log twice at this point.
364 
365  if (nullptr != pstrOutputCert)
366  pstrOutputCert->Set(strOutput); // Success!
367  }
368 
369  return bSuccess;
370 }
371 
372 // Load from local storage.
373 bool OTKeypair::LoadPrivateKey(const OTString& strFoldername,
374  const OTString& strFilename,
375  const OTString* pstrReason,
376  const OTPassword* pImportPassword)
377 {
378  OT_ASSERT(nullptr != m_pkeyPrivate);
379 
380  return m_pkeyPrivate->LoadPrivateKey(strFoldername, strFilename, pstrReason,
381  pImportPassword);
382 }
383 
384 bool OTKeypair::LoadPublicKey(const OTString& strFoldername,
385  const OTString& strFilename)
386 {
387  OT_ASSERT(nullptr != m_pkeyPublic);
388 
389  return m_pkeyPublic->LoadPublicKey(strFoldername, strFilename);
390 }
391 
392 // LoadPrivateKeyFromCertString
393 //
394 // "escaped" means pre-pended with "- " as in: - -----BEGIN CERTIFICATE....
395 //
397  bool bEscaped,
398  const OTString* pstrReason,
399  const OTPassword* pImportPassword)
400 {
401  OT_ASSERT(nullptr != m_pkeyPrivate);
402 
403  return m_pkeyPrivate->LoadPrivateKeyFromCertString(
404  strCert, bEscaped, pstrReason, pImportPassword);
405 }
406 
407 // Load Public Key from Cert (file or string)
408 //
410  const OTString& strCert, bool bEscaped, const OTString* pstrReason,
411  const OTPassword* pImportPassword) // DOES handle bookends, AND escapes.
412 {
413  OT_ASSERT(nullptr != m_pkeyPublic);
414 
415  return m_pkeyPublic->LoadPublicKeyFromCertString(
416  strCert, bEscaped, pstrReason, pImportPassword);
417 }
418 
420  const OTString& strFoldername, const OTString& strFilename,
421  const OTString* pstrReason,
422  const OTPassword* pImportPassword) // DOES handle bookends.
423 {
424  OT_ASSERT(nullptr != m_pkeyPublic);
425 
426  return m_pkeyPublic->LoadPublicKeyFromCertFile(strFoldername, strFilename,
427  pstrReason, pImportPassword);
428 }
429 
430 bool OTKeypair::MakeNewKeypair(int32_t nBits)
431 {
432  OT_ASSERT(nullptr != m_pkeyPrivate);
433  OT_ASSERT(nullptr != m_pkeyPublic);
434 
435  OTLowLevelKeyData lowLevelData;
436 
437  if (!lowLevelData.MakeNewKeypair(nBits)) {
438  otErr << "OTKeypair::MakeNewKeypair"
439  << ": Failed in a call to OTLowLevelKeyData::MakeNewKeypair("
440  << nBits << ").\n";
441  return false;
442  }
443 
444  return lowLevelData.SetOntoKeypair(*this);
445 
446  // If true is returned:
447  // Success! At this point, theKeypair's public and private keys have been
448  // set.
449  // Keep in mind though, they still won't be "quite right" until saved and
450  // loaded
451  // again, at least according to existing logic. That saving/reloading is
452  // currently
453  // performed in OTPseudonym::GenerateNym().
454 }
455 
457  const OTString& strFilename,
458  const OTString* pstrReason,
459  const OTPassword* pImportPassword)
460 {
461  const char* szFunc = "OTKeypair::LoadBothKeysFromCertFile";
462 
463  OT_ASSERT(nullptr != m_pkeyPublic);
464  OT_ASSERT(nullptr != m_pkeyPrivate);
465 
466  bool bPublic = m_pkeyPublic->LoadPublicKeyFromCertFile(
467  strFoldername.Get(), strFilename.Get(), pstrReason, pImportPassword);
468  bool bPrivate = m_pkeyPrivate->LoadPrivateKey(
469  strFoldername.Get(), strFilename.Get(), pstrReason, pImportPassword);
470  if (!bPublic) {
471  otErr << szFunc << ": Although the ascii-armored file (" << strFilename
472  << ") was read, LoadPublicKeyFromCert "
473  "returned false.\n";
474  return false;
475  }
476  else {
477  otInfo << szFunc << ": Successfully loaded public key from Certfile: "
478  << strFilename << "\n";
479  }
480 
481  if (!bPrivate) {
482  otErr << szFunc << ": Although the ascii-armored file (" << strFilename
483  << ") was read, LoadPrivateKey returned false.\n";
484  return false;
485  }
486  else {
487  otInfo << szFunc << ": Successfully loaded private key from certfile: "
488  << strFilename << "\n";
489  }
490 
491  return true;
492 }
493 
495  const OTPasswordData* pPWData)
496 {
497  OT_ASSERT(nullptr != m_pkeyPrivate);
498 
499  return theContract.SignWithKey(*m_pkeyPrivate, pPWData);
500 }
501 
502 // PUBLIC KEY
503 
504 // * Get the public key in ASCII-armored format -- OTASCIIArmor
505 // * Get the public key in ASCII-armored format WITH bookends -- OTString
506 // - ------- BEGIN PUBLIC KEY --------
507 // Notice the "- " before the rest of the bookend starts.
508 //
510 {
511  OT_ASSERT(nullptr != m_pkeyPublic);
512 
513  return m_pkeyPublic->GetPublicKey(strKey);
514 }
515 
516 bool OTKeypair::GetPublicKey(OTString& strKey, bool bEscaped) const
517 {
518  OT_ASSERT(nullptr != m_pkeyPublic);
519 
520  return m_pkeyPublic->GetPublicKey(strKey, bEscaped);
521 }
522 
523 // (Below) Decodes a public key from ASCII armor into an actual key pointer
524 // and sets that as the m_pKey on this object.
525 //
527 {
528  OT_ASSERT(nullptr != m_pkeyPublic);
529 
530  return m_pkeyPublic->SetPublicKey(strKey);
531 }
532 
533 // Decodes a public key from bookended key string into an actual key
534 // pointer, and sets that as the m_pkeyPublic on this object.
535 // This is the version that will handle the bookends ( -----BEGIN PUBLIC
536 // KEY-----)
537 //
538 bool OTKeypair::SetPublicKey(const OTString& strKey, bool bEscaped)
539 {
540  OT_ASSERT(nullptr != m_pkeyPublic);
541 
542  if (strKey.Contains("PGP PUBLIC KEY")) {
543  OTASCIIArmor theArmor;
544 
545  if (theArmor.LoadFromString(const_cast<OTString&>(strKey), bEscaped)) {
546  // This function expects that the bookends are already removed.
547  // The ascii-armor loading code removes them and handles the escapes
548  // also.
549  return m_pkeyPublic->LoadPublicKeyFromPGPKey(theArmor);
550  }
551  else {
552  otInfo << "OTKeypair::SetPublicKey: Failed extracting PGP public "
553  "key from ascii-armored text.\n";
554  return false;
555  }
556  }
557  else // the below function SetPublicKey (in the return call) expects the
558  // bookends to still be there, and it will handle removing them. (Unlike
559  // PGP code above.)
560  return m_pkeyPublic->SetPublicKey(strKey, bEscaped);
561 }
562 
563 // PRIVATE KEY
564 // Get the private key in ASCII-armored format with bookends
565 // - ------- BEGIN ENCRYPTED PRIVATE KEY --------
566 // Notice the "- " before the rest of the bookend starts.
567 //
568 bool OTKeypair::GetPrivateKey(OTString& strKey, bool bEscaped) const
569 {
570  OT_ASSERT(nullptr != m_pkeyPrivate);
571 
572  return m_pkeyPrivate->GetPrivateKey(strKey, bEscaped);
573 }
574 
575 bool OTKeypair::GetPrivateKey(OTASCIIArmor& strKey) const // Get the private key
576  // in ASCII-armored
577  // format
578 {
579  OT_ASSERT(nullptr != m_pkeyPrivate);
580 
581  return m_pkeyPrivate->GetPrivateKey(strKey);
582 }
583 
584 // Decodes a private key from ASCII armor into an actual key pointer
585 // and sets that as the m_pPrivateKey on this object.
586 // This is the version that will handle the bookends ( -----BEGIN ENCRYPTED
587 // PRIVATE KEY-----)
588 bool OTKeypair::SetPrivateKey(const OTString& strKey, bool bEscaped)
589 {
590  OT_ASSERT(nullptr != m_pkeyPrivate);
591 
592  const char* szOverride = "PGP PRIVATE KEY";
593 
594  if (strKey.Contains(szOverride)) {
595  OTASCIIArmor theArmor;
596 
597  if (theArmor.LoadFromString(const_cast<OTString&>(strKey), bEscaped,
598  szOverride)) // szOverride == "PGP PRIVATE
599  // KEY"
600  {
601  // This function expects that the bookends are already removed.
602  // The ascii-armor loading code removes them and handles the escapes
603  // also.
604  // return
605  // m_pkeyPrivate->LoadPrivateKeyFromPGPKey(theArmor);
606  //
607  otOut << "OTKeypair::SetPrivateKey 1: Failure: PGP private keys "
608  "are NOT YET SUPPORTED.\n\n";
609  // otOut << "OTKeypair::SetPrivateKey 1: Failure: PGP
610  // private keys are NOT YET SUPPORTED:\n\n%s\n\n",
611  // strKey.Get());
612  return false;
613  }
614  else {
615  otOut << "OTKeypair::SetPrivateKey 2: Failure: PGP private keys "
616  "are NOT YET SUPPORTED.\n\n";
617  // otOut << "OTKeypair::SetPrivateKey 2: Failure: PGP
618  // private keys are NOT YET SUPPORTED:\n\n%s\n\n",
619  // strKey.Get());
620  return false;
621  }
622  }
623  else // the below function SetPrivateKey (in the return call) expects the
624  // bookends to still be there, and it will handle removing them. (Unlike
625  // PGP code above.)
626  //
627  return m_pkeyPrivate->SetPrivateKey(strKey, bEscaped);
628 }
629 
630 bool OTKeypair::SetPrivateKey(const OTASCIIArmor& strKey) // Decodes a private
631  // key from ASCII
632  // armor into an
633  // actual key pointer
634  // and sets that as
635  // the m_pKey on this
636  // object.
637 {
638  OT_ASSERT(nullptr != m_pkeyPrivate);
639 
640  return m_pkeyPrivate->SetPrivateKey(strKey);
641 }
642 
643 bool OTKeypair::CalculateID(OTIdentifier& theOutput) const
644 {
645  OT_ASSERT(nullptr != m_pkeyPublic);
646 
647  return m_pkeyPublic->CalculateID(theOutput); // Only works for public keys.
648 }
649 
651  listOfAsymmetricKeys& listOutput, // Inclusive means, return the key even
652  // when theSignature has no metadata.
653  const OTSignature& theSignature, bool bInclusive) const
654 {
655  OT_ASSERT(nullptr != m_pkeyPublic);
656  OT_ASSERT(nullptr != m_pkeyPublic->m_pMetadata);
657 
658  // We know that EITHER exact metadata matches must occur, and the signature
659  // MUST have metadata, (bInclusive=false)
660  // OR if bInclusive=true, we know that metadata is still used to eliminate
661  // keys where possible, but that otherwise,
662  // if the signature has no metadata, then the key is still returned, "just
663  // in case."
664  //
665  if ((false == bInclusive) &&
666  (false == theSignature.getMetaData().HasMetadata()))
667  return 0;
668 
669  // Below this point, metadata is used if it's available.
670  // It's assumed to be "okay" if it's not available, since any non-inclusive
671  // calls would have already returned by now, if that were the case.
672  // (But if it IS available, then it must match, or the key won't be
673  // returned.)
674  //
675  // If the signature has no metadata, or if m_pkeyPublic has no metadata, or
676  // if they BOTH have metadata, and their metadata is a MATCH...
677  if (!theSignature.getMetaData().HasMetadata() ||
678  !m_pkeyPublic->m_pMetadata->HasMetadata() ||
679  (m_pkeyPublic->m_pMetadata->HasMetadata() &&
680  theSignature.getMetaData().HasMetadata() &&
681  (theSignature.getMetaData() == *(m_pkeyPublic->m_pMetadata)))) {
682  // ...Then add m_pkeyPublic as a possible match, to listOutput.
683  //
684  listOutput.push_back(m_pkeyPublic);
685  return 1;
686  }
687  return 0;
688 }
689 
690 // Used when importing/exporting a Nym to/from the wallet.
691 //
692 bool OTKeypair::ReEncrypt(const OTPassword& theExportPassword, bool bImporting,
693  OTString& strOutput)
694 {
695 
696  OT_ASSERT(nullptr != m_pkeyPublic);
697  OT_ASSERT(nullptr != m_pkeyPrivate);
698 
701 
702  // If we were importing, we were in the exported format but now we're in the
703  // internal format.
704  // Therefore we want to use the wallet's internal cached master passphrase
705  // to save. Therefore
706  // strReason will be used for the import case.
707  //
708  // But if we were exporting, then we were in the internal format and just
709  // re-encrypted to the
710  // export format. So we'd want to pass the export passphrase when saving.
711  //
712  const OTString strReasonAbove(
713  bImporting ? "Enter the new export passphrase. (Above "
714  "ReEncryptPrivateKey in OTKeypair::ReEncrypt)"
715  : "Enter your wallet's master passphrase. (Above "
716  "ReEncryptPrivateKey in OTKeypair::ReEncrypt)");
717 
718  const OTString strReasonBelow(
719  bImporting ? "Enter your wallet's master passphrase. (Below "
720  "ReEncryptPrivateKey in OTKeypair::ReEncrypt)"
721  : "Enter the new export passphrase. (Below "
722  "ReEncryptPrivateKey in OTKeypair::ReEncrypt)");
723 
724  // At this point the public key was loaded from a public key, not a cert,
725  // but the private key was loaded from the cert. Therefore we'll save the
726  // public cert from the private key, and then use that to reload the public
727  // key after ReEncrypting. (Otherwise the public key would be there, but it
728  // would be missing the x509, which is only available in the cert, not the
729  // pubkey alone -- and without the x509 being there, the "SaveAndReload"
730  // call
731  // below would fail.
732  // Why don't I just stick the Cert itself into the public data, instead of
733  // sticking the public key in there? Because not all key credentials will
734  // use
735  // certs. Some will use pubkeys from certs, and some will use pubkeys not
736  // from
737  // certs. But I might still just stick it in there, and code things to be
738  // able to
739  // load either indiscriminately. After all, that's what I'm doing already in
740  // the
741  // asset and server contracts. But even in those cases, there will be times
742  // when
743  // only a pubkey is available, not a cert, so I'll probably still find
744  // myself having
745  // to do this. Hmm...
746 
747  const bool bReEncrypted = m_pkeyPrivate->ReEncryptPrivateKey(
748  theExportPassword, bImporting); // <==== IMPORT or EXPORT occurs here.
749  bool bGotCert = false;
750 
751  if (bReEncrypted) {
752 
753  // Keys won't be right until this happens. Todo: eliminate this need.
755  &strOutput, &strReasonBelow,
756  bImporting ? nullptr : &theExportPassword);
757  }
758 
759  const bool bSuccess = (bReEncrypted && bGotCert);
760 
761  if (!bSuccess) {
762  strOutput.Release();
763  otErr << __FUNCTION__ << ": Failure, either when re-encrypting, or "
764  "when subsequently retrieving "
765  "the public/private keys. bImporting == "
766  << (bImporting ? "true" : "false") << "\n";
767  }
768 
769  return bSuccess;
770 }
771 
772 } // namespace opentxs
EXPORT bool LoadCertAndPrivateKeyFromString(const OTString &strInput, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
Definition: OTKeypair.cpp:294
EXPORT bool StorePlainString(std::string strContents, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:698
EXPORT bool ReEncrypt(const OTPassword &theExportPassword, bool bImporting, OTString &strOutput)
Definition: OTKeypair.cpp:692
bool MakeNewKeypair(int32_t nBits=1024)
virtual bool ReEncryptPrivateKey(const OTPassword &theExportPassword, bool bImporting) const =0
std::list< OTAsymmetricKey * > listOfAsymmetricKeys
EXPORT const OTAsymmetricKey & GetPublicKey() const
Definition: OTKeypair.cpp:229
virtual bool LoadPublicKeyFromCertString(const OTString &strCert, bool bEscaped=true, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)=0
EXPORT int32_t GetPublicKeyBySignature(listOfAsymmetricKeys &listOutput, const OTSignature &theSignature, bool bInclusive=false) const
Definition: OTKeypair.cpp:650
bool LoadPublicKey(const OTString &strFoldername, const OTString &strFilename)
EXPORT bool SavePrivateKeyToString(OTString &strOutput, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr) const
Definition: OTKeypair.cpp:261
EXPORT bool SaveAndReloadBothKeysFromTempFile(OTString *pstrOutputCert=nullptr, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
Definition: OTKeypair.cpp:333
OTSignatureMetadata * m_pMetadata
EXPORT bool LoadBothKeysFromCertFile(const OTString &strFoldername, const OTString &strFilename, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
Definition: OTKeypair.cpp:456
OTLOG_IMPORT OTLogStream otOut
EXPORT bool HasPublicKey()
Definition: OTKeypair.cpp:213
EXPORT void SetMetadata(const OTSignatureMetadata &theMetadata)
Definition: OTKeypair.cpp:200
virtual bool LoadPrivateKeyFromCertString(const OTString &strCert, bool bEscaped=true, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)=0
bool LoadPublicKeyFromCertFile(const OTString &strFoldername, const OTString &strFilename, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
virtual bool SaveCertToString(OTString &strOutput, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr) const =0
EXPORT bool Exists() const
Definition: OTString.cpp:1035
bool SetPrivateKey(const OTString &strKey, bool bEscaped=false)
EXPORT bool SignWithKey(const OTAsymmetricKey &theKey, const OTPasswordData *pPWData=nullptr)
Definition: OTContract.cpp:561
static EXPORT bool LoadFromString(OTASCIIArmor &ascArmor, const OTString &strInput, std::string str_bookend="-----BEGIN")
EXPORT bool SetPublicKey(const OTASCIIArmor &strKey)
EXPORT bool SetPublicKey(const OTASCIIArmor &strKey)
Definition: OTKeypair.cpp:526
EXPORT void Format(const char *fmt,...)
Definition: OTString.cpp:1319
virtual bool LoadPublicKeyFromPGPKey(const OTASCIIArmor &strKey)=0
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
bool GetPrivateKey(OTString &strKey, bool bEscaped=true) const
EXPORT bool CalculateID(OTIdentifier &theOutput) const
Definition: OTKeypair.cpp:643
EXPORT bool GetPublicKey(OTASCIIArmor &strKey) const
virtual bool SavePrivateKeyToString(OTString &strOutput, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr) const =0
virtual bool CalculateID(OTIdentifier &theOutput) const
#define OT_ASSERT(x)
Definition: Assert.hpp:150
OTSignatureMetadata & getMetaData()
OTLOG_IMPORT OTLogStream otInfo
EXPORT bool LoadPublicKeyFromCertString(const OTString &strCert, bool bEscaped=true, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
Definition: OTKeypair.cpp:409
EXPORT const OTAsymmetricKey & GetPrivateKey() const
Definition: OTKeypair.cpp:236
EXPORT bool LoadPrivateKeyFromCertString(const OTString &strCert, bool bEscaped=true, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
Definition: OTKeypair.cpp:396
bool LoadPrivateKey(const OTString &strFoldername, const OTString &strFilename, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr
EXPORT bool SetPrivateKey(const OTString &strKey, bool bEscaped=false)
Definition: OTKeypair.cpp:588
EXPORT bool SaveCertToString(OTString &strOutput, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr) const
Definition: OTKeypair.cpp:243
EXPORT bool HasPrivateKey()
Definition: OTKeypair.cpp:221
EXPORT bool SaveCertAndPrivateKeyToString(OTString &strOutput, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
Definition: OTKeypair.cpp:277
bool SetOntoKeypair(OTKeypair &theKeypair)
EXPORT bool LoadPublicKey(const OTString &strFoldername, const OTString &strFilename)
Definition: OTKeypair.cpp:384
EXPORT bool Contains(const char *compare) const
Definition: OTString.cpp:1137
static EXPORT const OTString & Cert()
Definition: OTFolders.cpp:299
EXPORT bool LoadPrivateKey(const OTString &strFoldername, const OTString &strFilename, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
Definition: OTKeypair.cpp:373
EXPORT bool MakeNewKeypair(int32_t nBits=1024)
Definition: OTKeypair.cpp:430
EXPORT bool SignContract(OTContract &theContract, const OTPasswordData *pPWData=nullptr)
Definition: OTKeypair.cpp:494
virtual EXPORT void Release()
Definition: OTString.cpp:765
EXPORT bool LoadPublicKeyFromCertFile(const OTString &strFoldername, const OTString &strFilename, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
Definition: OTKeypair.cpp:419