aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-02-10 03:38:47 +0100
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:12 +0200
commita19e68b9b029d5374604e4b81dcff161d4b465ba (patch)
treef1401a0278bd82deff117279376beec761e60a55 /include
parent5c1a20c4dacfe03db90b70c2665e66a76574196c (diff)
Add private/public key for users
Diffstat (limited to 'include')
-rw-r--r--include/Database.hpp5
-rw-r--r--include/DatabaseStorage.hpp10
-rw-r--r--include/LocalUser.hpp20
-rw-r--r--include/RemoteUser.hpp24
-rw-r--r--include/Signature.hpp83
-rw-r--r--include/User.hpp7
6 files changed, 141 insertions, 8 deletions
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 <opendht/dhtrunner.h>
#include <vector>
#include <ntp/NtpClient.hpp>
+#include <boost/filesystem/path.hpp>
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<StagedCreateObject> stagedCreateObjects;
std::vector<StagedAddObject> 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 <stdexcept>
+
+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 <string>
#include <stdexcept>
@@ -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
+}