forked from crabapples-h/navicat-keygen-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSACipher.hpp
274 lines (221 loc) · 10.4 KB
/
RSACipher.hpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#pragma once
#include <openssl/opensslv.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/bio.h>
#include <openssl/rsa.h>
#include <string>
#include "Exception.hpp"
#include "ExceptionOpenssl.hpp"
#include "ResourceWrapper.hpp"
#include "ResourceTraitsOpenssl.hpp"
namespace nkg {
enum class RSAKeyType {
PrivateKey,
PublicKey
};
enum class RSAKeyFormat {
PEM,
PKCS1
};
class RSACipher final : private ARL::ResourceWrapper<ARL::ResourceTraits::OpensslRSA> {
private:
template<RSAKeyType __Type, RSAKeyFormat __Format>
static void _WriteRSAToBIO(RSA* lpRSA, BIO* lpBIO) {
if constexpr (__Type == RSAKeyType::PrivateKey) {
if (PEM_write_bio_RSAPrivateKey(lpBIO, lpRSA, nullptr, nullptr, 0, nullptr, nullptr) == 0) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "PEM_write_bio_RSAPrivateKey failed.");
}
}
if constexpr (__Type == RSAKeyType::PublicKey) {
if constexpr (__Format == RSAKeyFormat::PEM) {
if (PEM_write_bio_RSA_PUBKEY(lpBIO, lpRSA) == 0) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "PEM_write_bio_RSA_PUBKEY failed.");
}
}
if constexpr (__Format == RSAKeyFormat::PKCS1) {
if (PEM_write_bio_RSAPublicKey(lpBIO, lpRSA) == 0) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "PEM_write_bio_RSAPublicKey failed.");
}
}
static_assert(__Format == RSAKeyFormat::PEM || __Format == RSAKeyFormat::PKCS1);
}
static_assert(__Type == RSAKeyType::PrivateKey || __Type == RSAKeyType::PublicKey);
}
template<RSAKeyType __Type, RSAKeyFormat __Format>
[[nodiscard]]
static RSA* _ReadRSAFromBIO(BIO* lpBIO) {
RSA* lpRSA;
if constexpr (__Type == RSAKeyType::PrivateKey) {
lpRSA = PEM_read_bio_RSAPrivateKey(lpBIO, nullptr, nullptr, nullptr);
if (lpRSA == nullptr) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "PEM_read_bio_RSAPrivateKey failed.")
.PushHint("Are you sure that you DO provide a valid RSA private key file?");
}
}
if constexpr (__Type == RSAKeyType::PublicKey) {
if constexpr (__Format == RSAKeyFormat::PEM) {
lpRSA = PEM_read_bio_RSA_PUBKEY(lpBIO, nullptr, nullptr, nullptr);
if (lpRSA == nullptr) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "PEM_read_bio_RSA_PUBKEY failed.")
.PushHint("Are you sure that you DO provide a valid RSA public key file with PEM format?");
}
}
if constexpr (__Format == RSAKeyFormat::PKCS1) {
lpRSA = PEM_read_bio_RSAPublicKey(lpBIO, nullptr, nullptr, nullptr);
if (lpRSA == nullptr) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "PEM_read_bio_RSAPublicKey failed.")
.PushHint("Are you sure that you DO provide a valid RSA public key file with PKCS1 format?");
}
}
static_assert(__Format == RSAKeyFormat::PEM || __Format == RSAKeyFormat::PKCS1);
}
static_assert(__Type == RSAKeyType::PrivateKey || __Type == RSAKeyType::PublicKey);
return lpRSA;
}
public:
RSACipher() : ARL::ResourceWrapper<ARL::ResourceTraits::OpensslRSA>(RSA_new()) {
if (IsValid() == false) {
throw ARL::OpensslError(__BASE_FILE__, __LINE__, ERR_get_error(), "RSA_new failed.");
}
}
[[nodiscard]]
size_t Bits() const {
#if (OPENSSL_VERSION_NUMBER & 0xffff0000) == 0x10000000 // openssl 1.0.x
if (Get()->n == nullptr) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "RSA modulus has not been set.");
} else {
return BN_num_bits(Get()->n);
}
#elif (OPENSSL_VERSION_NUMBER & 0xffff0000) == 0x10100000 // openssl 1.1.x
return RSA_bits(Get());
#else
#error "Unexpected openssl version!"
#endif
}
void GenerateKey(int bits, unsigned int e = RSA_F4) {
ARL::ResourceWrapper bn_e{ ARL::ResourceTraits::OpensslBIGNUM{} };
bn_e.TakeOver(BN_new());
if (bn_e.IsValid() == false) {
throw ARL::OpensslError(__BASE_FILE__, __LINE__, ERR_get_error(), "BN_new failed.");
}
if (!BN_set_word(bn_e, e)) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "BN_set_word failed.");
}
if (!RSA_generate_key_ex(Get(), bits, bn_e, nullptr)) {
throw ARL::OpensslError(__BASE_FILE__, __LINE__, ERR_get_error(), "RSA_generate_key_ex failed.");
}
}
template<RSAKeyType __Type, RSAKeyFormat __Format>
void ExportKeyToFile(std::string_view FileName) const {
ARL::ResourceWrapper KeyFile{ ARL::ResourceTraits::OpensslBIO{} };
KeyFile.TakeOver(BIO_new_file(FileName.data(), "w"));
if (KeyFile.IsValid() == false) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "BIO_new_file failed.");
}
_WriteRSAToBIO<__Type, __Format>(Get(), KeyFile);
}
template<RSAKeyType __Type, RSAKeyFormat __Format>
[[nodiscard]]
std::string ExportKeyString() const {
ARL::ResourceWrapper TempMemory{ ARL::ResourceTraits::OpensslBIO{} };
const char* lpsz = nullptr;
TempMemory.TakeOver(BIO_new(BIO_s_mem()));
if (TempMemory.IsValid() == false) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "BIO_new failed.");
}
_WriteRSAToBIO<__Type, __Format>(Get(), TempMemory);
auto l = BIO_get_mem_data(TempMemory.Get(), &lpsz);
std::string KeyString(lpsz, l);
while (KeyString.back() == '\n' || KeyString.back() == '\r') {
KeyString.pop_back();
}
return KeyString;
}
template<RSAKeyType __Type, RSAKeyFormat __Format>
void ImportKeyFromFile(std::string_view FileName) {
ARL::ResourceWrapper KeyFile{ ARL::ResourceTraits::OpensslBIO{} };
KeyFile.TakeOver(BIO_new_file(FileName.data(), "r"));
if (KeyFile.IsValid() == false) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "BIO_new_file failed.");
}
ReleaseAndTakeOver(_ReadRSAFromBIO<__Type, __Format>(KeyFile));
}
template<RSAKeyType __Type, RSAKeyFormat __Format>
void ImportKeyString(std::string_view KeyString) {
ARL::ResourceWrapper TempMemory{ ARL::ResourceTraits::OpensslBIO{} };
TempMemory.TakeOver(BIO_new(BIO_s_mem()));
if (TempMemory.IsValid() == false) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "BIO_new failed.");
}
if (BIO_puts(TempMemory.Get(), KeyString.data()) <= 0) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "BIO_puts failed.");
}
TakeOver(_ReadRSAFromBIO<__Type, __Format>(TempMemory));
}
template<RSAKeyType __Type = RSAKeyType::PublicKey>
size_t Encrypt(const void* lpFrom, size_t cbFrom, void* lpTo, int Padding) const {
int BytesWritten;
if (cbFrom > static_cast<size_t>(INT_MAX)) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "Length overflowed.");
}
if constexpr (__Type == RSAKeyType::PrivateKey) {
BytesWritten = RSA_private_encrypt(
static_cast<int>(cbFrom),
reinterpret_cast<const unsigned char*>(lpFrom),
reinterpret_cast<unsigned char*>(lpTo),
Get(),
Padding
);
if (BytesWritten == -1) {
throw ARL::OpensslError(__BASE_FILE__, __LINE__, ERR_get_error(), "RSA_private_encrypt failed.");
}
} else {
BytesWritten = RSA_public_encrypt(
static_cast<int>(cbFrom),
reinterpret_cast<const unsigned char*>(lpFrom),
reinterpret_cast<unsigned char*>(lpTo),
Get(),
Padding
);
if (BytesWritten == -1) {
throw ARL::OpensslError(__BASE_FILE__, __LINE__, ERR_get_error(), "RSA_public_encrypt failed.");
}
}
return BytesWritten;
}
template<RSAKeyType __Type = RSAKeyType::PrivateKey>
size_t Decrypt(const void* lpFrom, size_t cbFrom, void* lpTo, int Padding) const {
int BytesWritten;
if (cbFrom > static_cast<size_t>(INT_MAX)) {
throw ARL::Exception(__BASE_FILE__, __LINE__, "Length overflowed.");
}
if constexpr (__Type == RSAKeyType::PrivateKey) {
BytesWritten = RSA_private_decrypt(
static_cast<int>(cbFrom),
reinterpret_cast<const unsigned char*>(lpFrom),
reinterpret_cast<unsigned char*>(lpTo),
Get(),
Padding
);
if (BytesWritten == -1) {
throw ARL::OpensslError(__BASE_FILE__, __LINE__, ERR_get_error(), "RSA_private_decrypt failed.")
.PushHint("Are your sure you DO provide a correct private key?");
}
} else {
BytesWritten = RSA_public_decrypt(
static_cast<int>(cbFrom),
reinterpret_cast<const unsigned char*>(lpFrom),
reinterpret_cast<unsigned char*>(lpTo),
Get(),
Padding
);
if (BytesWritten == -1) {
throw ARL::OpensslError(__BASE_FILE__, __LINE__, ERR_get_error(), "RSA_public_decrypt failed.")
.PushHint("Are your sure you DO provide a correct public key?");
}
}
return BytesWritten;
}
};
}