From 767d2535934f9390b12e458cd245fb36a3cf85cb Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Thu, 21 Jan 2016 21:41:19 -0800 Subject: [PATCH] Improve manifest public key loading --- Builds/VisualStudio2015/RippleD.vcxproj | 6 - .../VisualStudio2015/RippleD.vcxproj.filters | 6 - src/ripple/crypto/Base58.h | 161 ----------- src/ripple/crypto/impl/Base58.cpp | 263 ------------------ src/ripple/overlay/impl/Manifest.cpp | 34 +-- src/ripple/unity/crypto.cpp | 1 - 6 files changed, 10 insertions(+), 461 deletions(-) delete mode 100644 src/ripple/crypto/Base58.h delete mode 100644 src/ripple/crypto/impl/Base58.cpp diff --git a/Builds/VisualStudio2015/RippleD.vcxproj b/Builds/VisualStudio2015/RippleD.vcxproj index 3f73c31e90..a6e38c2553 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj +++ b/Builds/VisualStudio2015/RippleD.vcxproj @@ -1962,8 +1962,6 @@ - - @@ -1974,10 +1972,6 @@ - - True - True - True True diff --git a/Builds/VisualStudio2015/RippleD.vcxproj.filters b/Builds/VisualStudio2015/RippleD.vcxproj.filters index cf36b5249c..aef648164f 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2015/RippleD.vcxproj.filters @@ -2589,9 +2589,6 @@ ripple\core - - ripple\crypto - ripple\crypto @@ -2607,9 +2604,6 @@ ripple\crypto - - ripple\crypto\impl - ripple\crypto\impl diff --git a/src/ripple/crypto/Base58.h b/src/ripple/crypto/Base58.h deleted file mode 100644 index fb5640bc83..0000000000 --- a/src/ripple/crypto/Base58.h +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include - -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 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 - static std::string encode (InputIt first, InputIt last, - Alphabet const& alphabet, bool withCheck) - { - using value_type = typename std::iterator_traits::value_type; - std::vector ::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 (hash+4), - std::reverse_iterator (hash), - std::back_inserter (v)); - } - else - { - v.reserve (size + 1); - } - // Append input little endian - std::copy (std::reverse_iterator (last), - std::reverse_iterator (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 - static std::string encode (Container const& container) - { - return encode (container.container.begin(), container.end(), - getRippleAlphabet(), false); - } - - template - 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 diff --git a/src/ripple/crypto/impl/Base58.cpp b/src/ripple/crypto/impl/Base58.cpp deleted file mode 100644 index 0a636837da..0000000000 --- a/src/ripple/crypto/impl/Base58.cpp +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include -#include -#include - -// 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 (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 ("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 (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 ("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); -} - -} diff --git a/src/ripple/overlay/impl/Manifest.cpp b/src/ripple/overlay/impl/Manifest.cpp index 3d22bf73ad..1536cbe531 100644 --- a/src/ripple/overlay/impl/Manifest.cpp +++ b/src/ripple/overlay/impl/Manifest.cpp @@ -19,9 +19,9 @@ #include #include +#include #include #include -#include #include #include #include @@ -134,35 +134,21 @@ ManifestCache::configValidatorKey( auto const words = beast::rfc2616::split(line.begin(), line.end(), ' '); if (words.size () != 2) - { Throw ("[validator_keys] format is ` "); - } - Blob key; - if (! Base58::decodeWithCheck (words[0], key)) - { + auto const masterKey = parseBase58( + TokenType::TOKEN_NODE_PUBLIC, words[0]); + + if (!masterKey) Throw ("Error decoding validator key"); - } - if (key.size() != 34) - { - Throw ("Expected 34-byte validator key"); - } - if (key[0] != TOKEN_NODE_PUBLIC) - { - Throw ("Expected TOKEN_NODE_PUBLIC (28)"); - } - if (key[1] != 0xED) - { - Throw ("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 ("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 diff --git a/src/ripple/unity/crypto.cpp b/src/ripple/unity/crypto.cpp index 7281b7cc10..02355b1433 100644 --- a/src/ripple/unity/crypto.cpp +++ b/src/ripple/unity/crypto.cpp @@ -19,7 +19,6 @@ #include -#include #include #include #include