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/base64.h>
24 #include <ripple/basics/Log.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  if (auto const token = ValidatorToken::make_ValidatorToken(
43  config.section(SECTION_VALIDATOR_TOKEN).lines()))
44  {
45  auto const pk = derivePublicKey(
46  KeyType::secp256k1, token->validationSecret);
47  auto const m = deserializeManifest(base64_decode(token->manifest));
48 
49  if (! m || pk != m->signingKey)
50  {
51  configInvalid_ = true;
52  JLOG(j.fatal())
53  << "Invalid token specified in [" SECTION_VALIDATOR_TOKEN "]";
54  }
55  else
56  {
57  secretKey = token->validationSecret;
58  publicKey = pk;
59  nodeID = calcNodeID(m->masterKey);
60  manifest = std::move(token->manifest);
61  }
62  }
63  else
64  {
65  configInvalid_ = true;
66  JLOG(j.fatal())
67  << "Invalid token specified in [" SECTION_VALIDATOR_TOKEN "]";
68  }
69  }
70  else if (config.exists(SECTION_VALIDATION_SEED))
71  {
72  auto const seed = parseBase58<Seed>(
73  config.section(SECTION_VALIDATION_SEED).lines().front());
74  if (!seed)
75  {
76  configInvalid_ = true;
77  JLOG(j.fatal()) <<
78  "Invalid seed specified in [" SECTION_VALIDATION_SEED "]";
79  }
80  else
81  {
85  }
86  }
87 }
88 } // namespace ripple
ripple::ValidatorKeys::publicKey
PublicKey publicKey
Definition: ValidatorKeys.h:39
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:312
ripple::calcNodeID
NodeID calcNodeID(PublicKey const &pk)
Calculate the 160-bit node ID from a node public key.
Definition: PublicKey.cpp:307
ripple::ValidatorKeys::nodeID
NodeID nodeID
Definition: ValidatorKeys.h:41
ripple::ValidatorToken::make_ValidatorToken
static boost::optional< ValidatorToken > make_ValidatorToken(std::vector< std::string > const &tokenBlob)
Definition: app/misc/impl/Manifest.cpp:243
std::vector::front
T front(T... args)
ripple::Config
Definition: Config.h:67
ripple::derivePublicKey
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
Definition: SecretKey.cpp:228
ripple::base64_decode
std::string base64_decode(std::string const &data)
Definition: base64.cpp:237
ripple::Section::lines
std::vector< std::string > const & lines() const
Returns all the lines in the section.
Definition: BasicConfig.h:69
ripple::generateSecretKey
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Definition: SecretKey.cpp:199
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
ripple::ValidatorKeys::secretKey
SecretKey secretKey
Definition: ValidatorKeys.h:40
ripple::KeyType::secp256k1
@ secp256k1
ripple::ValidatorKeys::configInvalid_
bool configInvalid_
Definition: ValidatorKeys.h:52
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::deserializeManifest
boost::optional< Manifest > deserializeManifest(Slice s)
Constructs Manifest from serialized string.
Definition: app/misc/impl/Manifest.cpp:37
ripple::ValidatorKeys::ValidatorKeys
ValidatorKeys(Config const &config, beast::Journal j)
Definition: ValidatorKeys.cpp:29
ripple::ValidatorKeys::manifest
std::string manifest
Definition: ValidatorKeys.h:42
ripple::BasicConfig::exists
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Definition: BasicConfig.cpp:134
ripple::BasicConfig::section
Section & section(std::string const &name)
Returns the section with the given name.
Definition: BasicConfig.cpp:140