// Decodes a private key from ASCII armor into an actual key pointer // and sets that as the m_pPrivateKey on this object. // This is the version that will handle the bookends ( -----BEGIN ENCRYPTED PRIVATE KEY-----) bool OTKeypair::SetPrivateKey(const OTString & strKey, bool bEscaped/*=false*/) { OT_ASSERT(NULL != m_pkeyPrivate); // --------------------------------------------------------------- const char * szOverride = "PGP PRIVATE KEY"; if (strKey.Contains(szOverride)) { OTASCIIArmor theArmor; if (theArmor.LoadFromString(const_cast<OTString &>(strKey), bEscaped, szOverride)) // szOverride == "PGP PRIVATE KEY" { // This function expects that the bookends are already removed. // The ascii-armor loading code removes them and handles the escapes also. // return m_pkeyPrivate->LoadPrivateKeyFromPGPKey(theArmor); // OTLog::vOutput(0, "OTKeypair::SetPrivateKey 1: Failure: PGP private keys are NOT YET SUPPORTED.\n\n"); // OTLog::vOutput(0, "OTKeypair::SetPrivateKey 1: Failure: PGP private keys are NOT YET SUPPORTED:\n\n%s\n\n", // strKey.Get()); return false; } else { OTLog::vOutput(0, "OTKeypair::SetPrivateKey 2: Failure: PGP private keys are NOT YET SUPPORTED.\n\n"); // OTLog::vOutput(0, "OTKeypair::SetPrivateKey 2: Failure: PGP private keys are NOT YET SUPPORTED:\n\n%s\n\n", // strKey.Get()); return false; } } else // the below function SetPrivateKey (in the return call) expects the // bookends to still be there, and it will handle removing them. (Unlike PGP code above.) // return m_pkeyPrivate->SetPrivateKey(strKey, bEscaped); }
// Decodes a public key from bookended key string into an actual key // pointer, and sets that as the m_pkeyPublic on this object. // This is the version that will handle the bookends ( -----BEGIN PUBLIC KEY-----) // bool OTKeypair::SetPublicKey(const OTString & strKey, bool bEscaped/*=false*/) { OT_ASSERT(NULL != m_pkeyPublic); // --------------------------------------------------------------- if (strKey.Contains("PGP PUBLIC KEY")) { OTASCIIArmor theArmor; if (theArmor.LoadFromString(const_cast<OTString &>(strKey), bEscaped)) { // This function expects that the bookends are already removed. // The ascii-armor loading code removes them and handles the escapes also. return m_pkeyPublic->LoadPublicKeyFromPGPKey(theArmor); } else { OTLog::Output(2, "OTKeypair::SetPublicKey: Failed extracting PGP public key from ascii-armored text.\n"); return false; } } else // the below function SetPublicKey (in the return call) expects the // bookends to still be there, and it will handle removing them. (Unlike PGP code above.) return m_pkeyPublic->SetPublicKey(strKey, bEscaped); }
// Let's say you don't know if the input string is raw base64, or if it has bookends // on it like -----BEGIN BLAH BLAH ... // And if it DOES have Bookends, you don't know if they are escaped: - -----BEGIN ... // Let's say you just want an easy function that will figure that crap out, and load the // contents up properly into an OTASCIIArmor object. (That's what this function will do.) // // str_bookend is a default. // So you could make it more specific like, -----BEGIN ENCRYPTED KEY (or whatever.) // //static bool OTASCIIArmor::LoadFromString(OTASCIIArmor & ascArmor, const OTString & strInput, const std::string str_bookend/*="-----BEGIN"*/) { // ----------------------------------------------------- if (strInput.Contains(str_bookend)) // YES there are bookends around this. { const std::string str_escaped("- " + str_bookend); // ----------------------------------- const bool bEscaped = strInput.Contains(str_escaped); // ----------------------------------- OTString strLoadFrom(strInput); if (!ascArmor.LoadFromString(strLoadFrom, bEscaped)) // removes the bookends so we have JUST the coded part. { // OTLog::vError("%s: Failure loading string into OTASCIIArmor object:\n\n%s\n\n", // __FUNCTION__, strInput.Get()); return false; } } else ascArmor.Set(strInput.Get()); // ------------------------------------------------- return true; }
bool OTPayment::SetPayment(const OTString & strPayment) { if (!strPayment.Exists()) return false; // -------------------------------------------------------------------- // // To support legacy data, we check here to see if it's armored or not. // If it's not, we support it. But if it IS, we ALSO support it (we de-armor it here.) // bool bArmoredAndALSOescaped = false; // "- -----BEGIN OT ARMORED" bool bArmoredButNOTescaped = false; // "-----BEGIN OT ARMORED" if (strPayment.Contains(OT_BEGIN_ARMORED_escaped)) // check this one first... { bArmoredAndALSOescaped = true; OTLog::Error("OTPayment::SetPayment: Armored and escaped value passed in, but escaped are forbidden here. (Returning false.)\n"); return false; } else if (strPayment.Contains(OT_BEGIN_ARMORED)) { bArmoredButNOTescaped = true; } // ---------------------------------------- const bool bArmored = (bArmoredAndALSOescaped || bArmoredButNOTescaped); // ---------------------------------------- // Whether the string is armored or not, (-----BEGIN OT ARMORED) // either way, we'll end up with the decoded version in this variable: // std::string str_Trim; // ------------------------------------------------ if (bArmored) // it's armored, we have to decode it first. { OTASCIIArmor ascTemp; OTString strPaymentTemp(strPayment); if (false == (ascTemp.LoadFromString(strPaymentTemp, bArmoredAndALSOescaped, // if it IS escaped or not, this variable will be true or false to show it. // The below szOverride sub-string determines where the content starts, when loading. OT_BEGIN_ARMORED))) // Default is: "-----BEGIN" // We're doing this: "-----BEGIN OT ARMORED" (Should worked for escaped as well, here.) { OTLog::vError("OTPayment::SetPayment: Error loading string contents from ascii-armored encoding. Contents: \n%s\n", strPayment.Get()); return false; } else // success loading the actual contents out of the ascii-armored version. { OTString strTemp(ascTemp); // <=== ascii-decoded here. std::string str_temp(strTemp.Get(), strTemp.GetLength()); str_Trim = OTString::trim(str_temp); // This is the std::string for the trim process. } } else { std::string str_temp(strPayment.Get(), strPayment.GetLength()); str_Trim = OTString::trim(str_temp); // This is the std::string for the trim process. (Wasn't armored, so here we use it as passed in.) } // ------------------------------------------------ // At this point, str_Trim contains the actual contents, whether they // were originally ascii-armored OR NOT. (And they are also now trimmed, either way.) // ------------------------------------------ OTString strContract(str_Trim.c_str()); m_strPayment.Release(); // ---------------------- // todo: should be "starts with" and perhaps with a trim first // if (strContract.Contains("-----BEGIN SIGNED CHEQUE-----")) m_Type = OTPayment::CHEQUE; else if (strContract.Contains("-----BEGIN SIGNED VOUCHER-----")) m_Type = OTPayment::VOUCHER; else if (strContract.Contains("-----BEGIN SIGNED INVOICE-----")) m_Type = OTPayment::INVOICE; // ------------------- else if (strContract.Contains("-----BEGIN SIGNED PAYMENT PLAN-----")) m_Type = OTPayment::PAYMENT_PLAN; else if (strContract.Contains("-----BEGIN SIGNED SMART CONTRACT-----")) m_Type = OTPayment::SMART_CONTRACT; // ------------------- else if (strContract.Contains("-----BEGIN SIGNED PURSE-----")) m_Type = OTPayment::PURSE; else m_Type = OTPayment::ERROR_STATE; // ---------------------- if (OTPayment::ERROR_STATE == m_Type) return false; // ********************************* m_strPayment.Set(strContract); // ********************************* return true; }