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

#include <OTLowLevelKeyData.hpp>

Collaboration diagram for opentxs::OTLowLevelKeyData:

Classes

class  OTLowLevelKeyDataOpenSSLdp
 

Public Member Functions

bool MakeNewKeypair (int32_t nBits=1024)
 
void Cleanup ()
 
bool SetOntoKeypair (OTKeypair &theKeypair)
 
 OTLowLevelKeyData ()
 
 ~OTLowLevelKeyData ()
 

Public Attributes

bool m_bCleanup
 
OTLowLevelKeyDataOpenSSLdpdp
 

Detailed Description

OTLowLevelKeyData Used for passing x509's and EVP_PKEYs around, so a replacement crypto engine will not require changes to any function parameters throughout the rest of OT.

Definition at line 186 of file OTLowLevelKeyData.hpp.

Constructor & Destructor Documentation

opentxs::OTLowLevelKeyData::OTLowLevelKeyData ( )

Definition at line 171 of file OTLowLevelKeyData.cpp.

172  : m_bCleanup(true)
173 {
174  dp = new OTLowLevelKeyDataOpenSSLdp();
175  dp->m_pX509 = nullptr;
176  dp->m_pKey = nullptr;
177 }
OTLowLevelKeyDataOpenSSLdp * dp
opentxs::OTLowLevelKeyData::~OTLowLevelKeyData ( )

Definition at line 163 of file OTLowLevelKeyData.cpp.

164 {
165  if (m_bCleanup) Cleanup();
166  if (nullptr != dp) delete (dp);
167 }
OTLowLevelKeyDataOpenSSLdp * dp

Member Function Documentation

void opentxs::OTLowLevelKeyData::Cleanup ( )

Definition at line 183 of file OTLowLevelKeyData.cpp.

184 {
185  if (nullptr != dp->m_pKey) EVP_PKEY_free(dp->m_pKey);
186  dp->m_pKey = nullptr;
187  if (nullptr != dp->m_pX509) X509_free(dp->m_pX509);
188  dp->m_pX509 = nullptr;
189 }
OTLowLevelKeyDataOpenSSLdp * dp
bool opentxs::OTLowLevelKeyData::MakeNewKeypair ( int32_t  nBits = 1024)

Definition at line 191 of file OTLowLevelKeyData.cpp.

192 {
193 
194  // OpenSSL_BIO bio_err = nullptr;
195  X509* x509 = nullptr;
196  EVP_PKEY* pNewKey = nullptr;
197 
198  // CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); // memory leak detection.
199  // Leaving this for now.
200  // bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
201 
202  // actually generate the things. // TODO THESE PARAMETERS...(mkcert)
203  mkcert(&x509, &pNewKey, nBits, 0, 3650); // 3650=10 years. Todo hardcoded.
204  // Note: 512 bit key CRASHES
205  // 1024 is apparently a minimum requirement, if not an only requirement.
206  // Will need to go over just what sorts of keys are involved here... todo.
207 
208  if (nullptr == x509) {
209  otErr << __FUNCTION__
210  << ": Failed attempting to generate new x509 cert.\n";
211 
212  if (nullptr != pNewKey) EVP_PKEY_free(pNewKey);
213  pNewKey = nullptr;
214 
215  return false;
216  }
217 
218  if (nullptr == pNewKey) {
219  otErr << __FUNCTION__
220  << ": Failed attempting to generate new private key.\n";
221 
222  if (nullptr != x509) X509_free(x509);
223  x509 = nullptr;
224 
225  return false;
226  }
227 
228  // Below this point, x509 and pNewKey will need to be cleaned up properly.
229 
230  if (m_bCleanup) Cleanup();
231 
232  m_bCleanup = true;
233  dp->m_pKey = pNewKey;
234  dp->m_pX509 = x509;
235 
236  // --------COMMENT THIS OUT FOR PRODUCTION -------- TODO security
237  // (Debug only.)
238  // RSA_print_fp(stdout, pNewKey->pkey.rsa, 0); // human readable
239  // X509_print_fp(stdout, x509); // human readable
240 
241  // --------COMMENT THIS OUT FOR PRODUCTION -------- TODO security
242  // (Debug only.)
243  // write the private key, then the x509, to stdout.
244 
245  // OTPasswordData thePWData2("OTPseudonym::GenerateNym is calling
246  // PEM_write_PrivateKey...");
247  //
248  // PEM_write_PrivateKey(stdout, pNewKey, EVP_des_ede3_cbc(), nullptr, 0,
249  // OTAsymmetricKey::GetPasswordCallback(), &thePWData2);
250  // PEM_write_X509(stdout, x509);
251 
252  return true;
253 }
OTLOG_IMPORT OTLogStream otErr
int32_t mkcert(X509 **x509p, EVP_PKEY **pkeyp, int32_t bits, int32_t serial, int32_t days)
Definition: mkcert.cpp:52
OTLowLevelKeyDataOpenSSLdp * dp
bool opentxs::OTLowLevelKeyData::SetOntoKeypair ( OTKeypair theKeypair)

Definition at line 255 of file OTLowLevelKeyData.cpp.

256 {
257  OT_ASSERT(nullptr != dp->m_pKey);
258  OT_ASSERT(nullptr != dp->m_pX509);
259 
260  OT_ASSERT(nullptr != theKeypair.m_pkeyPublic);
261  OT_ASSERT(nullptr != theKeypair.m_pkeyPrivate);
262 
263  // Since we are in OpenSSL-specific code, we have to make sure these are
264  // OpenSSL-specific keys.
265  //
266  OTAsymmetricKey_OpenSSL* pPublicKey =
267  dynamic_cast<OTAsymmetricKey_OpenSSL*>(theKeypair.m_pkeyPublic);
268  OTAsymmetricKey_OpenSSL* pPrivateKey =
269  dynamic_cast<OTAsymmetricKey_OpenSSL*>(theKeypair.m_pkeyPrivate);
270 
271  if (nullptr == pPublicKey) {
272  otErr << __FUNCTION__ << ": dynamic_cast to OTAsymmetricKey_OpenSSL "
273  "failed. (theKeypair.m_pkeyPublic)\n";
274  return false;
275  }
276  if (nullptr == pPrivateKey) {
277  otErr << __FUNCTION__ << ": dynamic_cast to OTAsymmetricKey_OpenSSL "
278  "failed. (theKeypair.m_pkeyPrivate)\n";
279  return false;
280  }
281 
282  // Now we can call OpenSSL-specific methods on these keys...
283  //
284  pPublicKey->SetAsPublic();
285  // EVP_PKEY * pEVP_PubKey = X509_get_pubkey(m_pX509);
286  // OT_ASSERT(nullptr != pEVP_PubKey);
287  // pPublicKey-> SetKeyAsCopyOf(*pEVP_PubKey); // bool bIsPrivateKey=false
288  // by default.
289  pPublicKey->dp->SetKeyAsCopyOf(
290  *dp->m_pKey); // bool bIsPrivateKey=false by default.
291  // EVP_PKEY_free(pEVP_PubKey);
292  // pEVP_PubKey = nullptr;
293 
294  pPublicKey->dp->SetX509(dp->m_pX509); // m_pX509 is now owned by pPublicKey.
295  // (No need to free it in our own
296  // destructor anymore.)
297  dp->m_pX509 =
298  nullptr; // pPublicKey took ownership, so we don't want to ALSO
299  // clean it up, since pPublicKey already will do so.
300 
301  pPrivateKey->SetAsPrivate();
302  pPrivateKey->dp->SetKeyAsCopyOf(
303  *dp->m_pKey, true); // bool bIsPrivateKey=true; (Default is false)
304  // Since pPrivateKey only takes a COPY of m_pKey, we are still responsible
305  // to clean up m_pKey in our own destructor.
306  // (Assuming m_bCleanup is set to true, which is the default.) That's why
307  // I'm NOT setting it to nullptr, as I did above
308  // with m_pX509.
309 
310  EVP_PKEY_free(dp->m_pKey);
311  dp->m_pKey = nullptr;
312 
313  // Success! At this point, theKeypair's public and private keys have been
314  // set.
315  // Keep in mind though, they still won't be "quite right" until saved and
316  // loaded
317  // again, at least according to existing logic. That saving/reloading is
318  // currently
319  // performed in OTPseudonym::GenerateNym().
320  //
321  return true;
322 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
OTLOG_IMPORT OTLogStream otErr
OTLowLevelKeyDataOpenSSLdp * dp

Member Data Documentation

OTLowLevelKeyDataOpenSSLdp* opentxs::OTLowLevelKeyData::dp

Definition at line 203 of file OTLowLevelKeyData.hpp.

bool opentxs::OTLowLevelKeyData::m_bCleanup

Definition at line 193 of file OTLowLevelKeyData.hpp.


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