本文整理汇总了C++中OTPayload::GetSize方法的典型用法代码示例。如果您正苦于以下问题:C++ OTPayload::GetSize方法的具体用法?C++ OTPayload::GetSize怎么用?C++ OTPayload::GetSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OTPayload
的用法示例。
在下文中一共展示了OTPayload::GetSize方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: thePWData
// Low-level
//
// (Internal) ASCII-Armored key ====> (Internal) Actual loaded OpenSSL key.
//
//
EVP_PKEY * OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::InstantiatePublicKey(OTPasswordData * pPWData/*=NULL*/)
{
OT_ASSERT(m_pKey == NULL);
OT_ASSERT(backlink->m_p_ascKey != NULL);
OT_ASSERT(backlink->IsPublic());
const char * szFunc = "OTAsymmetricKey_OpenSSL::InstantiatePublicKey";
// ------------------------------
EVP_PKEY * pReturnKey = NULL;
OTPayload theData;
// -----------------------------------------------
// This base64 decodes the string m_p_ascKey into the
// binary payload object "theData"
//
backlink->m_p_ascKey->GetData(theData);
if (theData.GetSize() > 0)
{
// -------------------------------------------
// Next, copy theData's contents into a new BIO_mem_buf,
// so OpenSSL can load the key out of it.
//
OpenSSL_BIO keyBio = BIO_new_mem_buf(static_cast<char*>(const_cast<void*>(theData.GetPayloadPointer())),
theData.GetSize());
OT_ASSERT_MSG(NULL != keyBio, "OTAsymmetricKey_OpenSSL::InstantiatePublicKey: Assert: NULL != keyBio \n");
// -------------------------------------------
// Next we load up the key from the BIO string into an instantiated key object.
//
OTPasswordData thePWData("OTAsymmetricKey_OpenSSL::InstantiatePublicKey is calling PEM_read_bio_PUBKEY...");
if (NULL == pPWData)
pPWData = &thePWData;
pReturnKey = PEM_read_bio_PUBKEY(keyBio, NULL, OTAsymmetricKey::GetPasswordCallback(), pPWData);
// -------------------------------------------
// -------------------------------------------
backlink->ReleaseKeyLowLevel(); // Release whatever loaded key I might have already had.
if (NULL != pReturnKey)
{
m_pKey = pReturnKey;
OTLog::vOutput(4, "%s: Success reading public key from ASCII-armored data:\n\n%s\n\n",
szFunc, backlink->m_p_ascKey->Get());
return m_pKey;
}
}
OTLog::vError("%s: Failed reading public key from ASCII-armored data:\n\n%s\n\n",
szFunc, backlink->m_p_ascKey->Get());
return NULL;
}
示例2: SetupHeader
void SetupHeader( union u_header * pCMD, int nTypeID, int nCmdID, OTPayload & thePayload)
{
pCMD->fields.type_id = nTypeID;
pCMD->fields.command_id = nCmdID;
// pCMD->fields.size = thePayload.GetSize();
pCMD->fields.size = htonl(thePayload.GetSize()); // think this is causing problems
pCMD->fields.checksum = CalcChecksum(pCMD->buf, OT_CMD_HEADER_SIZE-1);
BYTE byChecksum = (BYTE)pCMD->fields.checksum;
int nChecksum = byChecksum;
uint32_t nTemp = thePayload.GetSize();
fprintf(stderr, "(Payload size %d, TYPE %d command, checksum: %d...)\n", nTemp, nTypeID, nChecksum);
}
示例3: SetupHeader
void SetupHeader( union u_header * pCMD, int nTypeID, int nCmdID, OTPayload & thePayload)
{
OT_ASSERT(NULL != pCMD);
pCMD->fields.type_id = (nTypeID > 0) ? static_cast<BYTE>(nTypeID) : '\0';
pCMD->fields.command_id = (nCmdID > 0) ? static_cast<BYTE>(nCmdID) : '\0';
// pCMD->fields.size = thePayload.GetSize();
pCMD->fields.size = htonl(thePayload.GetSize()); // think this is causing problems
pCMD->fields.checksum = CalcChecksum(pCMD->buf, OT_CMD_HEADER_SIZE-1);
BYTE byChecksum = (BYTE)pCMD->fields.checksum;
int nChecksum = byChecksum;
uint32_t nTemp = thePayload.GetSize();
OTLog::vOutput(4, "(Payload size %d, TYPE %d command, checksum: %d...)\n", nTemp, nTypeID, nChecksum);
}
示例4: ProcessReply
// Process my reply back out to the client. @something.
// For TCP / SSL mode.
void OTClientConnection::ProcessReply(OTMessage &theReply)
{
OT_ASSERT(NULL != m_pPublicKey);
int err = 0;
uint32_t nwritten = 0;
bool bSendCommand = false;
bool bSendPayload = false;
u_header theCMD;
OTPayload thePayload;
memset((void *)theCMD.buf, 0, OT_CMD_HEADER_SIZE); // todo cast
// For now let's send ALL replies in Envelopes (encrypted to public key of client)
// IF we have a public key, that is. Otherwise we send as a normal message.
//
// All messages already require either a public key, or a nymID used to look up a
// public key. So given that I have that information when I reply, I might as well
// ENCRYPT my reply to that same public key. More secure that way.
//
// The wallet (and server) are both ready to open and process these encrypted envelopes.
// If GetKey() returns something, that means the key was set in there, it's
// not just a null pointer. This means we can use it! So let's encrypt to it.
if (m_pPublicKey->IsPublic())
{
OTString strEnvelopeContents(theReply);
// Save the ready-to-go message into a string.
OTEnvelope theEnvelope;
// Seal the string up into an encrypted Envelope
theEnvelope.Seal(*m_pPublicKey, strEnvelopeContents);
// From here on out, theMessage is disposable. OTPayload takes over.
// OTMessage doesn't care about checksums and headers.
thePayload.SetEnvelope(theEnvelope);
// Now that the payload is ready, we'll set up the header.
SetupHeader(&theCMD, CMD_TYPE_1, TYPE_1_CMD_2, thePayload);
}
else
{
thePayload.SetMessage(theReply);
// Now that the payload is ready, we'll set up the header.
SetupHeader(&theCMD, CMD_TYPE_1, TYPE_1_CMD_1, thePayload);
}
bSendCommand = true;
bSendPayload = true;
OTLog::vOutput(2, "\n****************************************************************\n"
"===> Finished setting up header for response.\nFirst 9 bytes are: %d %d %d %d %d %d %d %d %d...\n",
theCMD.buf[0], theCMD.buf[1], theCMD.buf[2], theCMD.buf[3], theCMD.buf[4],
theCMD.buf[5], theCMD.buf[6], theCMD.buf[7], theCMD.buf[8]);
// ------------------------------------------------------------------------------
/*
// Write to Client
strcpy(buffer, "Hello Client!");
SFSocketWrite(clientSocket, buffer, strlen(buffer));
*/
if (bSendCommand)
{
const unsigned int nHeaderSize = OT_CMD_HEADER_SIZE;
for (nwritten = 0; nwritten < nHeaderSize; nwritten += err)
{
// err = SFSocketWrite(m_pSocket, theCMD.buf + nwritten, nHeaderSize - nwritten);
#ifdef _WIN32
if (0 == err || SOCKET_ERROR == err) // 0 means disconnect. error means error. >0 means bytes read.
#else
if (err <= 0)
#endif
break;
}
}
// At this point, we have sent the header across the pipe.
if (bSendPayload)
{
uint32_t nPayloadSize = thePayload.GetSize();
for (nwritten = 0; nwritten < nPayloadSize; nwritten += err)
{
// err = SFSocketWrite(m_pSocket, (unsigned char *)thePayload.GetPayloadPointer() + nwritten, nPayloadSize - nwritten);
#ifdef _WIN32
if (0 == err || SOCKET_ERROR == err) // 0 means disconnect. error means error. >0 means bytes read.
//.........这里部分代码省略.........
示例5: Decrypt
//.........这里部分代码省略.........
if (2 != env_type)
{
const uint32_t l_env_type = static_cast<uint32_t>(env_type);
OTLog::vError("%s: Error: Expected Envelope for Symmetric key (type 2) but instead found type: %ld.\n",
szFunc, l_env_type);
return false;
}
// ****************************************************************************
//
// Read network-order IV size (and convert to host version)
//
const uint32_t max_iv_length = OTCryptoConfig::SymmetricIvSize(); // I believe this is a max length, so it may not match the actual length of the IV.
// Read the IV SIZE (network order version -- convert to host version.)
//
uint32_t iv_size_n = 0;
if (0 == (nRead = m_dataContents.OTfread(reinterpret_cast<uint8_t*>(&iv_size_n),
static_cast<uint32_t>(sizeof(iv_size_n)))))
{
OTLog::vError("%s: Error reading IV Size.\n", szFunc);
return false;
}
nRunningTotal += nRead;
OT_ASSERT(nRead == static_cast<uint32_t>(sizeof(iv_size_n)));
// ----------------------------------------------------------------------------
// convert that iv size from network to HOST endian.
//
const uint32_t iv_size_host_order = ntohl(iv_size_n);
if (iv_size_host_order > max_iv_length)
{
OTLog::vError("%s: Error: iv_size (%ld) is larger than max_iv_length (%ld).\n",
szFunc, static_cast<long>(iv_size_host_order), static_cast<long>(max_iv_length));
return false;
}
// nRunningTotal += iv_size_host_order; // Nope!
// ****************************************************************************
//
// Then read the IV (initialization vector) itself.
//
OTPayload theIV;
theIV.SetPayloadSize(iv_size_host_order);
if (0 == (nRead = m_dataContents.OTfread(static_cast<uint8_t*>(const_cast<void *>(theIV.GetPayloadPointer())),
static_cast<uint32_t>(iv_size_host_order))))
{
OTLog::vError("%s: Error reading initialization vector.\n", szFunc);
return false;
}
nRunningTotal += nRead;
OT_ASSERT(nRead == static_cast<uint32_t>(iv_size_host_order));
OT_ASSERT(nRead <= max_iv_length);
// ----------------------------------------------------------------------------
// We create an OTPayload object to store the ciphertext itself, which begins AFTER the end of the IV.
// So we see pointer + nRunningTotal as the starting point for the ciphertext.
// the size of the ciphertext, meanwhile, is the size of the entire thing, MINUS nRunningTotal.
//
OTPayload theCipherText(static_cast<const void*>(
static_cast<const uint8_t *>(m_dataContents.GetPointer()) + nRunningTotal
),
m_dataContents.GetSize() - nRunningTotal);
// ----------------------------------------------------------------------------
// Now we've got all the pieces together, let's try to decrypt it...
//
OTPayload thePlaintext; // for output.
const bool bDecrypted = OTCrypto::It()->Decrypt(theRawSymmetricKey, // The symmetric key, in clear form.
// -------------------------------
static_cast<const char *>(theCipherText.GetPayloadPointer()), // This is the Ciphertext.
theCipherText.GetSize(),
// -------------------------------
theIV,
// -------------------------------
thePlaintext); // OUTPUT. (Recovered plaintext.) You can pass OTPassword& OR OTPayload& here (either will work.)
// -----------------------------------------------
// theOutput is where we'll put the decrypted data.
//
theOutput.Release();
if (bDecrypted)
{
// -----------------------------------------------------
// Make sure it's null-terminated...
//
uint32_t nIndex = thePlaintext.GetSize()-1;
(static_cast<uint8_t*>(const_cast<void *>(thePlaintext.GetPointer())))[nIndex] = '\0';
// -----------------------------------------------------
// Set it into theOutput (to return the plaintext to the caller)
//
theOutput.Set(static_cast<const char *>(thePlaintext.GetPointer()));
// ----------------
}
return bDecrypted;
}
示例6: Encrypt
bool OTEnvelope::Encrypt(const OTString & theInput, OTSymmetricKey & theKey, const OTPassword & thePassword)
{
OT_ASSERT((thePassword.isPassword() && (thePassword.getPasswordSize() > 0)) || (thePassword.isMemory() && (thePassword.getMemorySize() > 0)));
OT_ASSERT(theInput.Exists());
// -----------------------------------------------
// Generate a random initialization vector.
//
OTPayload theIV;
if (false == theIV.Randomize(OTCryptoConfig::SymmetricIvSize()))
{
OTLog::vError("%s: Failed trying to randomly generate IV.\n", __FUNCTION__);
return false;
}
// -----------------------------------------------
// If the symmetric key hasn't already been generated, we'll just do that now...
// (The passphrase is used to derive another key that is used to encrypt the
// actual symmetric key, and to access it later.)
//
if ((false == theKey.IsGenerated()) && (false == theKey.GenerateKey(thePassword)))
{
OTLog::vError("%s: Failed trying to generate symmetric key using password.\n", __FUNCTION__);
return false;
}
// -----------------------------------------------
if (!theKey.HasHashCheck())
{
if(!theKey.GenerateHashCheck(thePassword))
{
OTLog::vError("%s: Failed trying to generate hash check using password.\n", __FUNCTION__);
return false;
}
}
OT_ASSERT(theKey.HasHashCheck());
OTPassword theRawSymmetricKey;
if (false == theKey.GetRawKeyFromPassphrase(thePassword, theRawSymmetricKey))
{
OTLog::vError("%s: Failed trying to retrieve raw symmetric key using password.\n", __FUNCTION__);
return false;
}
// -----------------------------------------------
//
OTPayload theCipherText;
const bool bEncrypted = OTCrypto::It()->Encrypt(theRawSymmetricKey, // The symmetric key, in clear form.
// -------------------------------
theInput.Get(), // This is the Plaintext.
theInput.GetLength() + 1, // for null terminator
// -------------------------------
theIV, // Initialization vector.
// -------------------------------
theCipherText); // OUTPUT. (Ciphertext.)
// -----------------------------------------------
//
// Success?
//
if (!bEncrypted)
{
OTLog::vError("%s: (static) call failed to encrypt. Wrong key? (Returning false.)\n", __FUNCTION__);
return false;
}
// -----------------------------------------------
//
// This is where the envelope final contents will be placed,
// including the envelope type, the size of the IV, the IV
// itself, and the ciphertext.
//
m_dataContents.Release();
// -----------------------------------------------
// Write the ENVELOPE TYPE (network order version.)
//
// 0 == Error
// 1 == Asymmetric Key (other functions -- Seal / Open.)
// 2 == Symmetric Key (this function -- Encrypt / Decrypt.)
// Anything else: error.
uint16_t env_type_n = static_cast<uint16_t>(htons(static_cast<uint16_t>(2))); // Calculate "network-order" version of envelope type 2.
m_dataContents.Concatenate(reinterpret_cast<void *>(&env_type_n),
// (uint32_t here is the 2nd parameter to Concatenate, and has nothing to do with env_type_n being uint16_t)
static_cast<uint32_t>(sizeof(env_type_n)));
// ------------------------------------------------------------
//
// Write IV size (in network-order)
//
uint32_t ivlen = OTCryptoConfig::SymmetricIvSize(); // Length of IV for this cipher...
OT_ASSERT(ivlen >= theIV.GetSize());
uint32_t ivlen_n = htonl(theIV.GetSize()); // Calculate "network-order" version of iv length.
m_dataContents.Concatenate(reinterpret_cast<void *>(&ivlen_n),
static_cast<uint32_t>(sizeof(ivlen_n)));
// Write the IV itself.
//
m_dataContents.Concatenate(theIV.GetPayloadPointer(),
//.........这里部分代码省略.........
示例7:
// Take a public key, theKey (input), and create an armored version of
// it into ascKey (output.)
//
// OpenSSL loaded key ===> ASCII-Armored export of same key.
//
//static
//
bool OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::ArmorPublicKey(EVP_PKEY & theKey, OTASCIIArmor & ascKey)
{
bool bReturnVal = false;
const char * szFunc = "OTAsymmetricKey_OpenSSL::ArmorPublicKey";
ascKey.Release();
// ----------------------------------------
// Create a new memory buffer on the OpenSSL side
OpenSSL_BIO bmem = BIO_new(BIO_s_mem());
OT_ASSERT_MSG(NULL != bmem, "OTAsymmetricKey_OpenSSL::ArmorPublicKey: ASSERT: NULL != bmem");
int64_t lSize = 0;
// ----------------------------------------
// write a public key to that buffer, from theKey (parameter.)
//
int32_t nWriteBio = PEM_write_bio_PUBKEY(bmem, &theKey);
if (0 == nWriteBio)
{
OTLog::vError("%s: Error: Failed writing EVP_PKEY to memory buffer.\n", szFunc);
}
else
{
OTLog::vOutput(5, "%s: Success writing EVP_PKEY to memory buffer.\n", szFunc);
OTPayload theData;
char * pChar = NULL;
// After the below call, pChar will point to the memory buffer where the public key
// supposedly is, and lSize will contain the size of that memory.
//
lSize = BIO_get_mem_data(bmem, &pChar);
uint32_t nSize = static_cast<uint32_t>(lSize); // todo security, etc. Fix this assumed type conversion.
if (nSize > 0)
{
// Set the buffer size in our own memory.
theData.SetPayloadSize(nSize);
// void * pv =
OTPassword::safe_memcpy((static_cast<char*>(const_cast<void*>(theData.GetPayloadPointer()))), // destination
theData.GetSize(), // size of destination buffer.
pChar, // source
nSize); // length of source.
// bool bZeroSource=false); // if true, sets the source buffer to zero after copying is done.
// ------------------------------------------------
// This base64 encodes the public key data
//
ascKey.SetData(theData);
OTLog::vOutput(5, "%s: Success copying public key into memory.\n", szFunc);
bReturnVal = true;
}
else
{
OTLog::vError("%s: Failed copying public key into memory.\n", szFunc);
}
}
return bReturnVal;
}
示例8: thePWDataWrite
// NOTE: OpenSSL will store the EVP_PKEY inside the X509, and when I get it,
// I'm not supposed to destroy the x509 until I destroy the EVP_PKEY FIRST!
// (AND it reference-counts.)
// Since I want ability to destroy the two, independent of each other, I made
// static functions here for copying public and private keys, so I am ALWAYS
// working with MY OWN copy of any given key, and not OpenSSL's reference-counted
// one.
//
// Furthermore, BIO_mem_buf doesn't allocate its own memory, but uses the memory
// you pass to it. You CANNOT free that memory until you destroy the BIO.
//
// That's why you see me copying one bio into a payload, before copying it into
// the next bio. Todo security: copy it into an OTPassword here, instead of an
// OTPayload, which is safer, and more appropriate for a private key. Make sure
// OTPassword can accommodate a bit larger size than what it does now.
//
//static // CALLER must EVP_pkey_free!
EVP_PKEY * OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::CopyPrivateKey(EVP_PKEY & theKey, OTPasswordData * pPWData/*=NULL*/, OTPassword * pImportPassword/*=NULL*/)
{
const EVP_CIPHER * pCipher = EVP_des_ede3_cbc(); // todo should this algorithm be hardcoded?
// ----------------------------------------
// Create a new memory buffer on the OpenSSL side
OpenSSL_BIO bmem = BIO_new(BIO_s_mem());
OT_ASSERT(NULL != bmem);
EVP_PKEY * pReturnKey = NULL;
// ----------------------------------------
// write a private key to that buffer, from theKey
//
OTPasswordData thePWDataWrite("OTAsymmetricKey_OpenSSL::CopyPrivateKey is calling PEM_write_bio_PrivateKey...");
// todo optimization: might just remove the password callback here, and just write the private key in the clear,
// and then load it up again, saving the encrypt/decrypt step that otherwise occurs, and then as long as we OpenSSL_cleanse
// the BIO, then it SHOULD stil be safe, right?
//
int32_t nWriteBio = false;
if (NULL == pImportPassword)
nWriteBio = PEM_write_bio_PrivateKey(bmem, &theKey, pCipher,
NULL, 0, OTAsymmetricKey::GetPasswordCallback(), NULL == pPWData ? &thePWDataWrite : pPWData);
else
nWriteBio = PEM_write_bio_PrivateKey(bmem, &theKey, pCipher,
NULL, 0, 0, const_cast<void*>(reinterpret_cast<const void*>(pImportPassword->getPassword())));
// ------------------------------------------------------------------------
if (0 == nWriteBio)
{
OTLog::vError("%s: Failed writing EVP_PKEY to memory buffer.\n", __FUNCTION__);
}
else
{
OTLog::vOutput(5, "%s: Success writing EVP_PKEY to memory buffer.\n", __FUNCTION__);
char * pChar = NULL;
// After the below call, pChar will point to the memory buffer where the private key supposedly is,
// and lSize will contain the size of that memory.
//
const int64_t lSize = BIO_get_mem_data(bmem, &pChar);
const uint32_t nSize = static_cast<uint32_t>(lSize);
if (nSize > 0)
{
OTPayload theData;
// Set the buffer size in our own memory.
theData.SetPayloadSize(nSize);
void * pv =
OTPassword::safe_memcpy((static_cast<char*>(const_cast<void*>(theData.GetPayloadPointer()))), // destination
theData.GetSize(), // size of destination buffer.
pChar, // source
nSize); // length of source.
// bool bZeroSource=false); // if true, sets the source buffer to zero after copying is done.
if (NULL != pv)
{
// -----------------------------------------------
// Next, copy theData's contents into a new BIO_mem_buf,
// so OpenSSL can load the key out of it.
//
OpenSSL_BIO keyBio = BIO_new_mem_buf(static_cast<char*>(const_cast<void*>(theData.GetPayloadPointer())),
theData.GetSize());
OT_ASSERT_MSG(NULL != keyBio, "OTAsymmetricKey_OpenSSL::CopyPrivateKey: Assert: NULL != keyBio \n");
// -------------------------------------------
// Next we load up the key from the BIO string into an instantiated key object.
//
OTPasswordData thePWData("OTAsymmetricKey_OpenSSL::CopyPrivateKey is calling PEM_read_bio_PUBKEY...");
if (NULL == pImportPassword)
pReturnKey = PEM_read_bio_PrivateKey( keyBio, NULL, OTAsymmetricKey::GetPasswordCallback(), NULL == pPWData ? &thePWData : pPWData);
else
pReturnKey = PEM_read_bio_PrivateKey( keyBio, NULL, 0, const_cast<void*>(reinterpret_cast<const void*>(pImportPassword->getPassword())));
// -------------------------------------------
}
else
OTLog::vError("%s: Error: Failed copying memory from BIO into OTPayload.\n");
// -------------------------------------------
}
else
{
//.........这里部分代码省略.........