Skip to content

Commit

Permalink
Feature/rework key manager shm (hyperledger-iroha#1185)
Browse files Browse the repository at this point in the history
* move keys_manager to shared_model

Signed-off-by: Alexey Chernyshov <[email protected]>
  • Loading branch information
Alexey-N-Chernyshov authored Apr 13, 2018
1 parent d491793 commit 9ebde5a
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 159 deletions.
25 changes: 10 additions & 15 deletions iroha-cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@
#include "validators.hpp"

// Account information
DEFINE_bool(
new_account,
false,
"Generate and save locally new public/private keys");
DEFINE_bool(new_account,
false,
"Generate and save locally new public/private keys");
DEFINE_string(account_name,
"",
"Name of the account. Must be unique in iroha network");
Expand All @@ -64,7 +63,6 @@ DEFINE_string(peers_address,
// Run iroha-cli in interactive mode
DEFINE_bool(interactive, true, "Run iroha-cli in interactive mode");


using namespace iroha::protocol;
using namespace iroha::model::generators;
using namespace iroha::model::converters;
Expand Down Expand Up @@ -160,20 +158,17 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
iroha::KeysManagerImpl manager((path / FLAGS_account_name).string());
boost::optional<iroha::keypair_t> keypair;
if (FLAGS_pass_phrase.size() != 0) {
keypair = manager.loadKeys(FLAGS_pass_phrase);
} else {
keypair = manager.loadKeys();
}
auto keypair = FLAGS_pass_phrase.size() != 0
? manager.loadKeys(FLAGS_pass_phrase)
: manager.loadKeys();
if (not keypair) {
logger->error(
"Cannot load specified keypair, or keypair is invalid. Path: {}, "
"keypair name: {}. Use --key_path to path to your keypair. \nMaybe wrong pass phrase (\"{}\")?",
"keypair name: {}. Use --key_path with path of your keypair. \n"
"Maybe wrong pass phrase (\"{}\")?",
path.string(),
FLAGS_account_name,
FLAGS_pass_phrase
);
FLAGS_pass_phrase);
return EXIT_FAILURE;
}
// TODO 13/09/17 grimadas: Init counters from Iroha, or read from disk?
Expand All @@ -185,7 +180,7 @@ int main(int argc, char *argv[]) {
0,
0,
std::make_shared<iroha::model::ModelCryptoProviderImpl>(
*keypair));
*std::unique_ptr<iroha::keypair_t>(keypair->makeOldModel())));
interactiveCli.run();
} else {
logger->error("Invalid flags");
Expand Down
9 changes: 5 additions & 4 deletions irohad/consensus/yac/impl/yac_crypto_provider_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace iroha {
namespace consensus {
namespace yac {
CryptoProviderImpl::CryptoProviderImpl(const keypair_t &keypair)
CryptoProviderImpl::CryptoProviderImpl(const shared_model::crypto::Keypair &keypair)
: keypair_(keypair) {}

bool CryptoProviderImpl::verify(CommitMessage msg) {
Expand Down Expand Up @@ -52,14 +52,15 @@ namespace iroha {
VoteMessage CryptoProviderImpl::getVote(YacHash hash) {
VoteMessage vote;
vote.hash = hash;
keypair_t keypair = *std::unique_ptr<keypair_t>(keypair_.makeOldModel());
auto signature = iroha::sign(
iroha::sha3_256(
PbConverters::serializeVote(vote).hash().SerializeAsString())
.to_string(),
keypair_.pubkey,
keypair_.privkey);
keypair.pubkey,
keypair.privkey);
vote.signature.signature = signature;
vote.signature.pubkey = keypair_.pubkey;
vote.signature.pubkey = keypair.pubkey;
return vote;
}

Expand Down
6 changes: 4 additions & 2 deletions irohad/consensus/yac/impl/yac_crypto_provider_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
#define IROHA_YAC_CRYPTO_PROVIDER_IMPL_HPP

#include "consensus/yac/yac_crypto_provider.hpp"
#include "cryptography/keypair.hpp"

namespace iroha {
namespace consensus {
namespace yac {
class CryptoProviderImpl : public YacCryptoProvider {
public:
explicit CryptoProviderImpl(const keypair_t &keypair);
explicit CryptoProviderImpl(
const shared_model::crypto::Keypair &keypair);

bool verify(CommitMessage msg) override;

Expand All @@ -36,7 +38,7 @@ namespace iroha {
VoteMessage getVote(YacHash hash) override;

private:
keypair_t keypair_;
shared_model::crypto::Keypair keypair_;
};
} // namespace yac
} // namespace consensus
Expand Down
17 changes: 9 additions & 8 deletions irohad/main/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Irohad::Irohad(const std::string &block_store_dir,
std::chrono::milliseconds proposal_delay,
std::chrono::milliseconds vote_delay,
std::chrono::milliseconds load_delay,
const keypair_t &keypair)
const shared_model::crypto::Keypair &keypair)
: block_store_dir_(block_store_dir),
pg_conn_(pg_conn),
torii_port_(torii_port),
Expand Down Expand Up @@ -136,11 +136,8 @@ void Irohad::initPeerQuery() {
* Initializing crypto provider
*/
void Irohad::initCryptoProvider() {
shared_model::crypto::Keypair keypair_(
shared_model::crypto::PublicKey(keypair.pubkey.to_string()),
shared_model::crypto::PrivateKey(keypair.privkey.to_string()));
crypto_signer_ =
std::make_shared<shared_model::crypto::CryptoModelSigner<>>(keypair_);
std::make_shared<shared_model::crypto::CryptoModelSigner<>>(keypair);

log_->info("[Init] => crypto provider");
}
Expand Down Expand Up @@ -183,8 +180,7 @@ void Irohad::initSimulator() {
* Initializing block loader
*/
void Irohad::initBlockLoader() {
block_loader = loader_init.initBlockLoader(
wsv, storage->getBlockQuery());
block_loader = loader_init.initBlockLoader(wsv, storage->getBlockQuery());

log_->info("[Init] => block loader");
}
Expand All @@ -194,7 +190,12 @@ void Irohad::initBlockLoader() {
*/
void Irohad::initConsensusGate() {
consensus_gate = yac_init.initConsensusGate(
wsv, simulator, block_loader, keypair, vote_delay_, load_delay_);
wsv,
simulator,
block_loader,
keypair,
vote_delay_,
load_delay_);

log_->info("[Init] => consensus gate");
}
Expand Down
5 changes: 3 additions & 2 deletions irohad/main/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ametsuchi/impl/storage_impl.hpp"
#include "ametsuchi/ordering_service_persistent_state.hpp"
#include "cryptography/crypto_provider/crypto_model_signer.hpp"
#include "cryptography/keypair.hpp"
#include "logger/logger.hpp"
#include "main/impl/block_loader_init.hpp"
#include "main/impl/consensus_init.hpp"
Expand Down Expand Up @@ -76,7 +77,7 @@ class Irohad {
std::chrono::milliseconds proposal_delay,
std::chrono::milliseconds vote_delay,
std::chrono::milliseconds load_delay,
const iroha::keypair_t &keypair);
const shared_model::crypto::Keypair &keypair);

/**
* Initialization of whole objects in system
Expand Down Expand Up @@ -204,7 +205,7 @@ class Irohad {
public:
std::shared_ptr<iroha::ametsuchi::Storage> storage;

iroha::keypair_t keypair;
shared_model::crypto::Keypair keypair;
grpc::ServerBuilder builder;
};

Expand Down
6 changes: 3 additions & 3 deletions irohad/main/impl/consensus_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace iroha {
return consensus_network;
}

auto YacInit::createCryptoProvider(const keypair_t &keypair) {
auto YacInit::createCryptoProvider(const shared_model::crypto::Keypair &keypair) {
auto crypto = std::make_shared<CryptoProviderImpl>(keypair);

return crypto;
Expand All @@ -54,7 +54,7 @@ namespace iroha {

std::shared_ptr<consensus::yac::Yac> YacInit::createYac(
ClusterOrdering initial_order,
const keypair_t &keypair,
const shared_model::crypto::Keypair &keypair,
std::chrono::milliseconds delay_milliseconds) {
return Yac::create(YacVoteStorage(),
createNetwork(),
Expand All @@ -68,7 +68,7 @@ namespace iroha {
std::shared_ptr<ametsuchi::PeerQuery> wsv,
std::shared_ptr<simulator::BlockCreator> block_creator,
std::shared_ptr<network::BlockLoader> block_loader,
const keypair_t &keypair,
const shared_model::crypto::Keypair &keypair,
std::chrono::milliseconds vote_delay_milliseconds,
std::chrono::milliseconds load_delay_milliseconds) {
auto peer_orderer = createPeerOrderer(wsv);
Expand Down
7 changes: 4 additions & 3 deletions irohad/main/impl/consensus_init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "consensus/yac/yac_gate.hpp"
#include "consensus/yac/yac_hash_provider.hpp"
#include "consensus/yac/yac_peer_orderer.hpp"
#include "cryptography/keypair.hpp"
#include "network/block_loader.hpp"
#include "simulator/block_creator.hpp"

Expand All @@ -45,23 +46,23 @@ namespace iroha {

auto createNetwork();

auto createCryptoProvider(const keypair_t &keypair);
auto createCryptoProvider(const shared_model::crypto::Keypair &keypair);

auto createTimer();

auto createHashProvider();

std::shared_ptr<consensus::yac::Yac> createYac(
ClusterOrdering initial_order,
const keypair_t &keypair,
const shared_model::crypto::Keypair &keypair,
std::chrono::milliseconds delay_milliseconds);

public:
std::shared_ptr<YacGate> initConsensusGate(
std::shared_ptr<ametsuchi::PeerQuery> wsv,
std::shared_ptr<simulator::BlockCreator> block_creator,
std::shared_ptr<network::BlockLoader> block_loader,
const keypair_t &keypair,
const shared_model::crypto::Keypair &keypair,
std::chrono::milliseconds vote_delay_milliseconds,
std::chrono::milliseconds load_delay_milliseconds);

Expand Down
8 changes: 3 additions & 5 deletions irohad/main/irohad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,9 @@ int main(int argc, char *argv[]) {

// Reading public and private key files
iroha::KeysManagerImpl keysManager(FLAGS_keypair_name);
iroha::keypair_t keypair{};
auto keypair = keysManager.loadKeys();
// Check if both keys are read properly
if (auto loadedKeypair = keysManager.loadKeys()) {
keypair = *loadedKeypair;
} else {
if (not keypair) {
// Abort execution if not
log->error("Failed to load keypair");
return EXIT_FAILURE;
Expand All @@ -119,7 +117,7 @@ int main(int argc, char *argv[]) {
std::chrono::milliseconds(config[mbr::ProposalDelay].GetUint()),
std::chrono::milliseconds(config[mbr::VoteDelay].GetUint()),
std::chrono::milliseconds(config[mbr::LoadDelay].GetUint()),
keypair);
*keypair);

// Check if iroha daemon storage was successfully initialized
if (not irohad.storage) {
Expand Down
6 changes: 3 additions & 3 deletions irohad/model/generators/impl/transaction_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace iroha {
for (size_t i = 0; i < peers_address.size(); ++i) {
KeysManagerImpl manager("node" + std::to_string(i));
manager.createKeys();
auto keypair = *manager.loadKeys();
auto keypair = *std::unique_ptr<iroha::keypair_t>(manager.loadKeys()->makeOldModel());
tx.commands.push_back(command_generator.generateAddPeer(
Peer(peers_address[i], keypair.pubkey)));
}
Expand All @@ -57,12 +57,12 @@ namespace iroha {
// Create accounts
KeysManagerImpl manager("admin@test");
manager.createKeys();
auto keypair = *manager.loadKeys();
auto keypair = *std::unique_ptr<iroha::keypair_t>(manager.loadKeys()->makeOldModel());
tx.commands.push_back(command_generator.generateCreateAccount(
"admin", "test", keypair.pubkey));
manager = KeysManagerImpl("test@test");
manager.createKeys();
keypair = *manager.loadKeys();
keypair = *std::unique_ptr<iroha::keypair_t>(manager.loadKeys()->makeOldModel());
tx.commands.push_back(command_generator.generateCreateAccount(
"test", "test", keypair.pubkey));

Expand Down
2 changes: 1 addition & 1 deletion libs/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ add_library(keys_manager
)

target_link_libraries(keys_manager
ed25519_crypto
shared_model_cryptography
logger
)
36 changes: 22 additions & 14 deletions libs/crypto/keys_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
* limitations under the License.
*/

#ifndef IROHA_CLI_KEYS_MANAGER_HPP
#define IROHA_CLI_KEYS_MANAGER_HPP
#ifndef IROHA_KEYS_MANAGER_HPP
#define IROHA_KEYS_MANAGER_HPP

#include <string>

#include <boost/optional.hpp>

namespace iroha {
struct keypair_t;
namespace shared_model {
namespace crypto {
class Keypair;
}
}

namespace iroha {
/**
* Interface provides facilities to create and store keypair on disk.
*/
class KeysManager {
public:
virtual ~KeysManager() = default;
Expand All @@ -34,31 +42,31 @@ namespace iroha {
*/
virtual bool createKeys() = 0;

/**
* Load plain-text keys associated with the manager, then validate loaded
* keypair by signing and verifying signature of test message
* @return nullopt if no keypair found locally, or verification failure;
* related keypair otherwise
*/
virtual boost::optional<iroha::keypair_t> loadKeys() = 0;

/**
* Create keys a new keypair and store it encrypted on disk
* @param pass_phrase is a password for the keys
* @return false if create account failed
*/
virtual bool createKeys(const std::string &pass_phrase) = 0;

/**
* Load plain-text keys associated with the manager, then validate loaded
* keypair by signing and verifying signature of test message
* @return nullopt if no keypair found locally, or verification failure;
* related keypair otherwise
*/
virtual boost::optional<shared_model::crypto::Keypair> loadKeys() = 0;

/**
* Load encrypted keys associated with the manager, then validate loaded
* keypair by signing and verifying signature of test message
* @param pass_phrase is a password for decryption
* @return nullopt if no keypair found locally, or verification failure;
* related keypair otherwise
*/
virtual boost::optional<iroha::keypair_t> loadKeys(
virtual boost::optional<shared_model::crypto::Keypair> loadKeys(
const std::string &pass_phrase) = 0;
};

} // namespace iroha
#endif // IROHA_CLI_KEYS_MANAGER_HPP
#endif // IROHA_KEYS_MANAGER_HPP
Loading

0 comments on commit 9ebde5a

Please sign in to comment.