-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeys.h
More file actions
137 lines (104 loc) · 3.35 KB
/
Copy pathkeys.h
File metadata and controls
137 lines (104 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Copyright (c) 2023 Taha
* this program is free software: you can redistribute it and/or modify
* it under the terms of the gnu general public license as published by
* the free software foundation, either version 3 of the license, or
* (at your option) any later version.
* this program is distributed in the hope that it will be useful,
* but without any warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose. see the
* gnu general public license for more details.
* you should have received a copy of the gnu general public license
* along with this program. if not, see <https://www.gnu.org/licenses/>.
*
* Author: Taha
* Date: 2023, Dec 9
* Description: For secure local-key management. This is for encrypting local data in configure.json files
*/
#ifndef KEYS_H
#define KEYS_H
#include <string>
#include <stdint.h>
#include <iomanip>
#include <optional>
#include <json/json.h>
// read keys from file
struct LocalKeys
{
uint8_t* keys;
uint16_t key_len;
uint16_t pepper_len;
uint16_t ports_key_len;
uint16_t keys_len; // total length of keys, length of keys pointer
LocalKeys(std::string keys_path);
~LocalKeys()
{
delete[] keys;
}
uint8_t *get_pepper();
uint8_t *get_key();
uint8_t *get_port_key();
};
std::string to_hex_str(uint8_t *ptr, uint16_t ptr_len);
// convert any unsigned num to hex-string
std::string to_hex_str(auto num)
{
std::stringstream ss;
if constexpr(sizeof(num) == 1)
ss << std::hex << std::setfill('0') << std::setw(2) << num+0;
else
ss << std::hex << std::setfill('0') << std::setw(sizeof(num)<<1) << num;
return ss.str();
}
// convert hex-string to any unsigned num
void hex_str_to(std::string num, auto &out)
{
std::stringstream ss;
ss << num;
ss >> out;
}
uint16_t hex_str_to(std::string str, uint8_t *ptr);
void parse_ipv4(uint8_t *out, std::string ip);
void parse_ipv6(uint8_t *out, std::string ip);
std::string get_ipv6();
void init_configure_json(std::string config_path);
class Configure
{
public:
uint16_t port;
uint16_t tor_port;
// not relating to specific protocols. This is just for communications
std::string public_ip;
std::string *other_public_ips; // other ips
uint32_t node_count;
Json::Value config;
std::string config_path;
uint8_t **IVs; // IVs for all ips and ports in configuration.json
enum IV {PORT, TOR_PORT, PUBLIC, PUBLIC_1}; // indexing of iv
Configure(std::string configpath);
// Configure to json value
Configure(std::string configpath, Json::Value _config);
uint32_t get_node_count();
// destructor
~Configure();
// assign variables to config
void get_values();
// reset config to file
void reset();
// write member values back
void write_values();
// call write_values() to assign object members to config then call this function to write to file
void write_to_file();
// generate 6 new IVs for encrypting config members
// IVs are a nonce so save them in config and only use them once
void new_ivs();
// decrypt all
void decrypt(std::string keys_path);
void encrypt(std::string keys_path);
protected:
// encrypt/decrypt configure.json values in every session
// config_path: the path of configure.json
// keys_path: path of keys.txt
// return config, call config.write_to_file() to save values
void process(std::string keys_path);
};
#endif /* KEYS_H */