Improve manifest public key loading

This commit is contained in:
Nik Bougalis
2016-01-21 21:41:19 -08:00
parent 78e59191ed
commit 767d253593
6 changed files with 10 additions and 461 deletions

View File

@@ -1962,8 +1962,6 @@
</ClCompile>
<ClInclude Include="..\..\src\ripple\core\TimeKeeper.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\crypto\Base58.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\crypto\CAutoBN_CTX.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\crypto\CBigNum.h">
@@ -1974,10 +1972,6 @@
</ClInclude>
<ClInclude Include="..\..\src\ripple\crypto\GenerateDeterministicKey.h">
</ClInclude>
<ClCompile Include="..\..\src\ripple\crypto\impl\Base58.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\crypto\impl\CBigNum.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>

View File

@@ -2589,9 +2589,6 @@
<ClInclude Include="..\..\src\ripple\core\TimeKeeper.h">
<Filter>ripple\core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\crypto\Base58.h">
<Filter>ripple\crypto</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\crypto\CAutoBN_CTX.h">
<Filter>ripple\crypto</Filter>
</ClInclude>
@@ -2607,9 +2604,6 @@
<ClInclude Include="..\..\src\ripple\crypto\GenerateDeterministicKey.h">
<Filter>ripple\crypto</Filter>
</ClInclude>
<ClCompile Include="..\..\src\ripple\crypto\impl\Base58.cpp">
<Filter>ripple\crypto\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\crypto\impl\CBigNum.cpp">
<Filter>ripple\crypto\impl</Filter>
</ClCompile>

View File

@@ -1,161 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2011 The Bitcoin Developers
// Distributed under the MIT/X11 software license, see the accompanying
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
//
// Why base-58 instead of standard base-64 encoding?
// - Don't want 0OIl characters that look the same in some fonts and
// could be used to create visually identical looking account numbers.
// - A string with non-alphanumeric characters is not as easily accepted as an account number.
// - E-mail usually won't line-break if there's no punctuation to break at.
// - Doubleclicking selects the whole number as one word if it's all alphanumeric.
//
#ifndef RIPPLE_CRYPTO_BASE58_H_INCLUDED
#define RIPPLE_CRYPTO_BASE58_H_INCLUDED
#include <ripple/basics/Blob.h>
#include <array>
#include <cassert>
#include <iterator>
#include <string>
#include <type_traits>
#include <vector>
namespace ripple {
/** Performs Base 58 encoding and decoding. */
class Base58
{
public:
class Alphabet
{
public:
// chars may not contain high-ASCII values
explicit Alphabet (char const* chars)
: m_chars (chars)
{
assert (m_inverse.size () >= m_chars.length ());
m_inverse.fill (-1);
int i = 0;
for (auto c : m_chars)
{
assert ((c >= 0) && (c < m_inverse.size ()));
assert (m_inverse[c] == -1);
m_inverse[c] = i++;
}
}
char const* chars () const
{ return m_chars.c_str (); }
char to_char (int digit) const
{ return m_chars [digit]; }
char operator[] (int digit) const
{ return to_char (digit); }
int from_char (char c) const
{ return m_inverse [c]; }
private:
std::string const m_chars;
std::array <int, 256> m_inverse;
};
static Alphabet const& getBitcoinAlphabet ();
static Alphabet const& getRippleAlphabet ();
static std::string raw_encode (unsigned char const* begin,
unsigned char const* end, Alphabet const& alphabet);
static void fourbyte_hash256 (void* out, void const* in, std::size_t bytes);
template <class InputIt>
static std::string encode (InputIt first, InputIt last,
Alphabet const& alphabet, bool withCheck)
{
using value_type = typename std::iterator_traits<InputIt>::value_type;
std::vector <typename std::remove_const <value_type>::type> v;
std::size_t const size (std::distance (first, last));
if (withCheck)
{
v.reserve (size + 1 + 4);
v.insert (v.begin(), first, last);
unsigned char hash [4];
fourbyte_hash256 (hash, &v.front(), v.size());
v.resize (0);
// Place the hash reversed in the front
std::copy (std::reverse_iterator <unsigned char const*> (hash+4),
std::reverse_iterator <unsigned char const*> (hash),
std::back_inserter (v));
}
else
{
v.reserve (size + 1);
}
// Append input little endian
std::copy (std::reverse_iterator <InputIt> (last),
std::reverse_iterator <InputIt> (first),
std::back_inserter (v));
// Pad zero to make the BIGNUM positive
v.push_back (0);
return raw_encode (&v.front(), &v.back()+1, alphabet);
}
template <class Container>
static std::string encode (Container const& container)
{
return encode (container.container.begin(), container.end(),
getRippleAlphabet(), false);
}
template <class Container>
static std::string encodeWithCheck (Container const& container)
{
return encode (&container.front(), &container.back()+1,
getRippleAlphabet(), true);
}
static std::string encode (const unsigned char* pbegin, const unsigned char* pend)
{
return encode (pbegin, pend, getRippleAlphabet(), false);
}
//--------------------------------------------------------------------------
// Raw decoder leaves the check bytes in place if present
static bool raw_decode (char const* first, char const* last,
void* dest, std::size_t size, bool checked, Alphabet const& alphabet);
static bool decode (const char* psz, Blob& vchRet, Alphabet const& alphabet = getRippleAlphabet ());
static bool decode (std::string const& str, Blob& vchRet);
static bool decodeWithCheck (const char* psz, Blob& vchRet, Alphabet const& alphabet = getRippleAlphabet());
static bool decodeWithCheck (std::string const& str, Blob& vchRet, Alphabet const& alphabet = getRippleAlphabet());
};
}
#endif

View File

@@ -1,263 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/contract.h>
#include <ripple/crypto/Base58.h>
#include <ripple/crypto/CAutoBN_CTX.h>
#include <ripple/crypto/CBigNum.h>
#include <openssl/sha.h>
#include <algorithm>
#include <stdexcept>
#include <string>
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2011 The Bitcoin Developers
// Distributed under the MIT/X11 software license, see the accompanying
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
namespace ripple {
uint256 SHA256Hash (unsigned char const* pbegin, unsigned char const* pend)
{
uint256 hash1;
SHA256 (pbegin, pend - pbegin, hash1.begin ());
uint256 hash2;
SHA256 (hash1.begin (), hash1.size (), hash2.begin());
return hash2;
}
void Base58::fourbyte_hash256 (void* out, void const* in, std::size_t bytes)
{
auto p = static_cast <unsigned char const*>(in);
uint256 hash (SHA256Hash (p, p + bytes));
memcpy (out, hash.begin(), 4);
}
Base58::Alphabet const& Base58::getBitcoinAlphabet ()
{
static Alphabet alphabet (
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
);
return alphabet;
}
Base58::Alphabet const& Base58::getRippleAlphabet ()
{
static Alphabet alphabet (
"rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"
);
return alphabet;
}
std::string Base58::raw_encode (unsigned char const* begin,
unsigned char const* end, Alphabet const& alphabet)
{
CAutoBN_CTX pctx;
CBigNum bn58 = 58;
CBigNum bn0 = 0;
// Convert little endian data to bignum
CBigNum bn (begin, end);
std::size_t const size (std::distance (begin, end));
// Convert bignum to std::string
std::string str;
// Expected size increase from base58 conversion is approximately 137%
// use 138% to be safe
str.reserve (size * 138 / 100 + 1);
CBigNum dv;
CBigNum rem;
while (bn > bn0)
{
if (!BN_div (&dv, &rem, &bn, &bn58, pctx))
Throw<std::runtime_error> ("EncodeBase58 : BN_div failed");
bn = dv;
unsigned int c = rem.getuint ();
str += alphabet [c];
}
for (const unsigned char* p = end-2; p >= begin && *p == 0; p--)
str += alphabet [0];
// Convert little endian std::string to big endian
std::reverse (str.begin (), str.end ());
return str;
}
//------------------------------------------------------------------------------
bool Base58::raw_decode (char const* first, char const* last, void* dest,
std::size_t size, bool checked, Alphabet const& alphabet)
{
CAutoBN_CTX pctx;
CBigNum bn58 = 58;
CBigNum bn = 0;
CBigNum bnChar;
// Convert big endian string to bignum
for (char const* p = first; p != last; ++p)
{
int i (alphabet.from_char (*p));
if (i == -1)
return false;
bnChar.setuint ((unsigned int) i);
int const success (BN_mul (&bn, &bn, &bn58, pctx));
assert (success);
(void) success;
bn += bnChar;
}
// Get bignum as little endian data
Blob vchTmp = bn.getvch ();
// Trim off sign byte if present
if (vchTmp.size () >= 2 && vchTmp.end ()[-1] == 0 && vchTmp.end ()[-2] >= 0x80)
vchTmp.erase (vchTmp.end () - 1);
char* const out (static_cast <char*> (dest));
// Count leading zeros
int nLeadingZeros = 0;
for (char const* p = first; p!=last && *p==alphabet[0]; p++)
nLeadingZeros++;
// Verify that the size is correct
if (vchTmp.size() + nLeadingZeros != size)
return false;
// Fill the leading zeros
memset (out, 0, nLeadingZeros);
// Copy little endian data to big endian
std::reverse_copy (vchTmp.begin (), vchTmp.end (),
out + nLeadingZeros);
if (checked)
{
char hash4 [4];
fourbyte_hash256 (hash4, out, size - 4);
if (memcmp (hash4, out + size - 4, 4) != 0)
return false;
}
return true;
}
bool Base58::decode (const char* psz, Blob& vchRet, Alphabet const& alphabet)
{
CAutoBN_CTX pctx;
vchRet.clear ();
CBigNum bn58 = 58;
CBigNum bn = 0;
CBigNum bnChar;
while (isspace (*psz))
psz++;
// Convert big endian string to bignum
for (const char* p = psz; *p; p++)
{
// VFALCO TODO Make this use the inverse table!
// Or better yet ditch this and call raw_decode
//
const char* p1 = strchr (alphabet.chars(), *p);
if (p1 == nullptr)
{
while (isspace (*p))
p++;
if (*p != '\0')
return false;
break;
}
bnChar.setuint (p1 - alphabet.chars());
if (!BN_mul (&bn, &bn, &bn58, pctx))
Throw<std::runtime_error> ("DecodeBase58 : BN_mul failed");
bn += bnChar;
}
// Get bignum as big endian data
Blob vchTmp = bn.getvch ();
// Trim off sign byte if present
if (vchTmp.size () >= 2 && vchTmp.end ()[-1] == 0 && vchTmp.end ()[-2] >= 0x80)
vchTmp.erase (vchTmp.end () - 1);
// Restore leading zeros
int nLeadingZeros = 0;
for (const char* p = psz; *p == alphabet.chars()[0]; p++)
nLeadingZeros++;
vchRet.assign (nLeadingZeros + vchTmp.size (), 0);
// Convert big endian data to little endian
std::reverse_copy (vchTmp.begin (), vchTmp.end (), vchRet.end () - vchTmp.size ());
return true;
}
bool Base58::decode (std::string const& str, Blob& vchRet)
{
return decode (str.c_str (), vchRet);
}
bool Base58::decodeWithCheck (const char* psz, Blob& vchRet, Alphabet const& alphabet)
{
if (!decode (psz, vchRet, alphabet))
return false;
auto size = vchRet.size ();
if (size < 4)
{
vchRet.clear ();
return false;
}
uint256 hash = SHA256Hash (vchRet.data (), vchRet.data () + size - 4);
if (memcmp (&hash, &vchRet.end ()[-4], 4) != 0)
{
vchRet.clear ();
return false;
}
vchRet.resize (size - 4);
return true;
}
bool Base58::decodeWithCheck (std::string const& str, Blob& vchRet, Alphabet const& alphabet)
{
return decodeWithCheck (str.c_str (), vchRet, alphabet);
}
}

View File

@@ -19,9 +19,9 @@
#include <ripple/app/main/Application.h>
#include <ripple/basics/contract.h>
#include <ripple/basics/Log.h>
#include <ripple/app/misc/ValidatorList.h>
#include <ripple/core/DatabaseCon.h>
#include <ripple/crypto/Base58.h>
#include <ripple/overlay/impl/Manifest.h>
#include <ripple/protocol/PublicKey.h>
#include <ripple/protocol/Sign.h>
@@ -134,35 +134,21 @@ ManifestCache::configValidatorKey(
auto const words = beast::rfc2616::split(line.begin(), line.end(), ' ');
if (words.size () != 2)
{
Throw<std::runtime_error> ("[validator_keys] format is `<key> <comment>");
}
Blob key;
if (! Base58::decodeWithCheck (words[0], key))
{
auto const masterKey = parseBase58<PublicKey>(
TokenType::TOKEN_NODE_PUBLIC, words[0]);
if (!masterKey)
Throw<std::runtime_error> ("Error decoding validator key");
}
if (key.size() != 34)
{
Throw<std::runtime_error> ("Expected 34-byte validator key");
}
if (key[0] != TOKEN_NODE_PUBLIC)
{
Throw<std::runtime_error> ("Expected TOKEN_NODE_PUBLIC (28)");
}
if (key[1] != 0xED)
{
Throw<std::runtime_error> ("Expected Ed25519 key (0xED)");
}
auto const masterKey = PublicKey (Slice(key.data() + 1, key.size() - 1));
std::string comment = std::move(words[1]);
if (publicKeyType(*masterKey) != KeyType::ed25519)
Throw<std::runtime_error> ("Validator key must use Ed25519");
if (journal.debug) journal.debug
<< toBase58(TokenType::TOKEN_NODE_PUBLIC, masterKey) << " " << comment;
JLOG (journal.debug) << "Loaded key: " <<
toBase58(TokenType::TOKEN_NODE_PUBLIC, *masterKey);
addTrustedKey (masterKey, std::move(comment));
addTrustedKey (*masterKey, std::move(words[1]));
}
void

View File

@@ -19,7 +19,6 @@
#include <BeastConfig.h>
#include <ripple/crypto/impl/Base58.cpp>
#include <ripple/crypto/impl/CBigNum.cpp>
#include <ripple/crypto/impl/ec_key.cpp>
#include <ripple/crypto/impl/ECDSAKey.cpp>