From be3c931f9b2db357c0b4306ad248c968d90254a3 Mon Sep 17 00:00:00 2001 From: Aleksi Lindeman Date: Sat, 10 Feb 2018 03:38:47 +0100 Subject: Add private/public key for users --- include/Database.hpp | 5 +-- include/DatabaseStorage.hpp | 10 ++++++ include/LocalUser.hpp | 20 ++++++++--- include/RemoteUser.hpp | 24 +++++++++++++ include/Signature.hpp | 83 +++++++++++++++++++++++++++++++++++++++++++++ include/User.hpp | 7 ++-- 6 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 include/DatabaseStorage.hpp create mode 100644 include/RemoteUser.hpp create mode 100644 include/Signature.hpp (limited to 'include') diff --git a/include/Database.hpp b/include/Database.hpp index 68fff62..bde4d5a 100644 --- a/include/Database.hpp +++ b/include/Database.hpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace odhtdb { @@ -15,7 +16,7 @@ namespace odhtdb class Database { public: - Database(const char *bootstrapNodeAddr, u16 port); + Database(const char *bootstrapNodeAddr, u16 port, boost::filesystem::path storageDir); ~Database(); void seed(); @@ -35,4 +36,4 @@ namespace odhtdb std::vector stagedCreateObjects; std::vector stagedAddObjects; }; -} \ No newline at end of file +} diff --git a/include/DatabaseStorage.hpp b/include/DatabaseStorage.hpp new file mode 100644 index 0000000..fee6b72 --- /dev/null +++ b/include/DatabaseStorage.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace odhtdb +{ + class DatabaseStorage + { + public: + + }; +} diff --git a/include/LocalUser.hpp b/include/LocalUser.hpp index 200f30f..04f483d 100644 --- a/include/LocalUser.hpp +++ b/include/LocalUser.hpp @@ -7,11 +7,23 @@ namespace odhtdb class LocalUser : public User { public: - static LocalUser* create(const std::string &name) + static LocalUser* create(const Signature::KeyPair &keyPair, const std::string &name) { - return new LocalUser(name); + return new LocalUser(keyPair, name); } + + const Signature::PublicKey& getPublicKey() const override + { + return keyPair.getPublicKey(); + } + + const Signature::PrivateKey& getPrivateKey() const + { + return keyPair.getPrivateKey(); + } + private: + LocalUser(const Signature::KeyPair &_keyPair, const std::string &name) : User(name), keyPair(_keyPair) {} private: - LocalUser(const std::string &name) : User(name){} + Signature::KeyPair keyPair; }; -} \ No newline at end of file +} diff --git a/include/RemoteUser.hpp b/include/RemoteUser.hpp new file mode 100644 index 0000000..770be61 --- /dev/null +++ b/include/RemoteUser.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "User.hpp" + +namespace odhtdb +{ + class RemoteUser : public User + { + public: + static RemoteUser* create(const Signature::PublicKey &publicKey, const std::string &name) + { + return new RemoteUser(publicKey, name); + } + + const Signature::PublicKey& getPublicKey() const override + { + return publicKey; + } + private: + RemoteUser(const Signature::PublicKey &_publicKey, const std::string &name) : User(name), publicKey(_publicKey){} + private: + Signature::PublicKey publicKey; + }; +} diff --git a/include/Signature.hpp b/include/Signature.hpp new file mode 100644 index 0000000..90d5278 --- /dev/null +++ b/include/Signature.hpp @@ -0,0 +1,83 @@ +#pragma once + +#include + +namespace odhtdb +{ + const int PUBLIC_KEY_NUM_BYTES = 32; + const int PRIVATE_KEY_NUM_BYTES = 64; + + class InvalidSignatureKeySize : public std::runtime_error + { + public: + InvalidSignatureKeySize(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class SignatureGenerationException : public std::runtime_error + { + public: + SignatureGenerationException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class DataSignException : public std::runtime_error + { + public: + DataSignException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + namespace Signature + { + class PublicKey + { + friend class KeyPair; + public: + // Throws InvalidSignatureKeySize if size is not PUBLIC_KEY_NUM_BYTES + PublicKey(char *data, size_t size); + PublicKey(const PublicKey &other); + PublicKey& operator=(const PublicKey &other); + + const char* getData() const { return data; } + size_t getSize() const { return PUBLIC_KEY_NUM_BYTES; } + + std::string toString() const; + private: + PublicKey(){} + private: + char data[PUBLIC_KEY_NUM_BYTES]; + }; + + class PrivateKey + { + friend class KeyPair; + public: + // Throws InvalidSignatureKeySize if size is not PRIVATE_KEY_NUM_BYTES + PrivateKey(char *data, size_t size); + PrivateKey(const PrivateKey &other); + PrivateKey& operator=(const PrivateKey &other); + + const char* getData() const { return data; } + size_t getSize() const { return PRIVATE_KEY_NUM_BYTES; } + + // Throws DataSignException if signing data failed for whatever reason. This wont happen unless there is an issue with the private key + std::string sign(const std::string &dataToSign) const; + std::string toString() const; + private: + PrivateKey(){} + private: + char data[PRIVATE_KEY_NUM_BYTES]; + }; + + class KeyPair + { + public: + // Throws SignatureGenerationException if generation of private/public key pair fails (should never happen) + KeyPair(); + + const PublicKey& getPublicKey() const { return publicKey; } + const PrivateKey& getPrivateKey() const { return privateKey; } + private: + PublicKey publicKey; + PrivateKey privateKey; + }; + } +} diff --git a/include/User.hpp b/include/User.hpp index e542434..ab5872a 100644 --- a/include/User.hpp +++ b/include/User.hpp @@ -1,5 +1,6 @@ #pragma once +#include "Signature.hpp" #include #include @@ -18,7 +19,10 @@ namespace odhtdb class User { public: + virtual ~User(){} + const std::string& getName() const { return name; } + virtual const Signature::PublicKey& getPublicKey() const = 0; protected: User(const std::string &_name) : name(_name) { @@ -26,7 +30,6 @@ namespace odhtdb throw UserNameTooLongException(name); } private: - // TODO: Add public key std::string name; }; -} \ No newline at end of file +} -- cgit v1.2.3