rippled
ValidatorKeys.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/misc/ValidatorKeys.h>
21 
22 #include <ripple/app/misc/Manifest.h>
23 #include <ripple/basics/Log.h>
24 #include <ripple/basics/base64.h>
25 #include <ripple/core/Config.h>
26 #include <ripple/core/ConfigSections.h>
27 
28 namespace ripple {
30 {
31  if (config.exists(SECTION_VALIDATOR_TOKEN) &&
32  config.exists(SECTION_VALIDATION_SEED))
33  {
34  configInvalid_ = true;
35  JLOG(j.fatal()) << "Cannot specify both [" SECTION_VALIDATION_SEED
36  "] and [" SECTION_VALIDATOR_TOKEN "]";
37  return;
38  }
39 
40  if (config.exists(SECTION_VALIDATOR_TOKEN))
41  {
42  // token is non-const so it can be moved from
43  if (auto token = loadValidatorToken(
44  config.section(SECTION_VALIDATOR_TOKEN).lines()))
45  {
46  auto const pk =
47  derivePublicKey(KeyType::secp256k1, token->validationSecret);
48  auto const m = deserializeManifest(base64_decode(token->manifest));
49 
50  if (!m || pk != m->signingKey)
51  {
52  configInvalid_ = true;
53  JLOG(j.fatal())
54  << "Invalid token specified in [" SECTION_VALIDATOR_TOKEN
55  "]";
56  }
57  else
58  {
59  secretKey = token->validationSecret;
60  publicKey = pk;
61  masterPublicKey = m->masterKey;
62  nodeID = calcNodeID(m->masterKey);
63  sequence = m->sequence;
64  manifest = std::move(token->manifest);
65  }
66  }
67  else
68  {
69  configInvalid_ = true;
70  JLOG(j.fatal())
71  << "Invalid token specified in [" SECTION_VALIDATOR_TOKEN "]";
72  }
73  }
74  else if (config.exists(SECTION_VALIDATION_SEED))
75  {
76  auto const seed = parseBase58<Seed>(
77  config.section(SECTION_VALIDATION_SEED).lines().front());
78  if (!seed)
79  {
80  configInvalid_ = true;
81  JLOG(j.fatal())
82  << "Invalid seed specified in [" SECTION_VALIDATION_SEED "]";
83  }
84  else
85  {
90  sequence = 0;
91  }
92  }
93 }
94 } // namespace ripple
ripple::ValidatorKeys::publicKey
PublicKey publicKey
Definition: ValidatorKeys.h:40
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:339
ripple::calcNodeID
NodeID calcNodeID(PublicKey const &pk)
Calculate the 160-bit node ID from a node public key.
Definition: PublicKey.cpp:303
ripple::loadValidatorToken
std::optional< ValidatorToken > loadValidatorToken(std::vector< std::string > const &blob, beast::Journal journal)
Definition: app/misc/impl/Manifest.cpp:244
ripple::ValidatorKeys::nodeID
NodeID nodeID
Definition: ValidatorKeys.h:42
std::vector::front
T front(T... args)
ripple::Config
Definition: Config.h:89
ripple::derivePublicKey
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
Definition: SecretKey.cpp:313
ripple::base64_decode
std::string base64_decode(std::string const &data)
Definition: base64.cpp:245
ripple::Section::lines
std::vector< std::string > const & lines() const
Returns all the lines in the section.
Definition: BasicConfig.h:68
ripple::generateSecretKey
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Definition: SecretKey.cpp:291
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::ValidatorKeys::secretKey
SecretKey secretKey
Definition: ValidatorKeys.h:41
ripple::KeyType::secp256k1
@ secp256k1
ripple::ValidatorKeys::configInvalid_
bool configInvalid_
Definition: ValidatorKeys.h:55
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::deserializeManifest
std::optional< Manifest > deserializeManifest(Slice s, beast::Journal journal)
Constructs Manifest from serialized string.
Definition: app/misc/impl/Manifest.cpp:53
ripple::ValidatorKeys::sequence
std::uint32_t sequence
Definition: ValidatorKeys.h:44
ripple::ValidatorKeys::ValidatorKeys
ValidatorKeys(Config const &config, beast::Journal j)
Definition: ValidatorKeys.cpp:29
ripple::ValidatorKeys::manifest
std::string manifest
Definition: ValidatorKeys.h:43
ripple::BasicConfig::exists
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Definition: BasicConfig.cpp:121
ripple::ValidatorKeys::masterPublicKey
PublicKey masterPublicKey
Definition: ValidatorKeys.h:39
ripple::BasicConfig::section
Section & section(std::string const &name)
Returns the section with the given name.
Definition: BasicConfig.cpp:127