-
Notifications
You must be signed in to change notification settings - Fork 2
/
openssl_rsa.cc
254 lines (215 loc) · 6.87 KB
/
openssl_rsa.cc
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
#include "openssl_utils.hpp"
#include <utils/scopeguard.hpp>
#include <gtest/gtest.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <memory>
class OpensslRsa : noncopyable
{
public:
OpensslRsa() = default;
explicit OpensslRsa(int bits) { generate(bits); }
~OpensslRsa() { destroy(); }
void generate(int bits)
{
destroy();
m_pkey = EVP_RSA_gen(bits);
}
auto encrypt(int padding, const std::string &plain) -> std::string
{
assert(m_pkey != nullptr);
auto *ctx = EVP_PKEY_CTX_new(m_pkey, nullptr);
auto cleanup = scopeGuard([&] {
if (ctx != nullptr) {
EVP_PKEY_CTX_free(ctx);
}
});
if (ctx == nullptr) {
openssl_error();
return {};
}
if (EVP_PKEY_encrypt_init(ctx) <= 0) {
openssl_error();
return {};
}
if (EVP_PKEY_CTX_set_rsa_padding(ctx, padding) <= 0) {
openssl_error();
return {};
}
std::string cipher;
cipher.resize(EVP_PKEY_size(m_pkey));
size_t outLen = 0;
if (EVP_PKEY_encrypt(ctx,
reinterpret_cast<unsigned char *>(cipher.data()),
&outLen,
reinterpret_cast<const unsigned char *>(plain.c_str()),
static_cast<int>(plain.size()))
<= 0) {
openssl_error();
return {};
}
cipher.resize(outLen);
return cipher;
}
auto decrypt(int padding, const std::string &cipher) -> std::string
{
assert(m_pkey != nullptr);
auto *ctx = EVP_PKEY_CTX_new(m_pkey, nullptr);
auto cleanup = scopeGuard([&] {
if (ctx != nullptr) {
EVP_PKEY_CTX_free(ctx);
}
});
if (ctx == nullptr) {
openssl_error();
return {};
}
if (EVP_PKEY_decrypt_init(ctx) <= 0) {
openssl_error();
return {};
}
if (EVP_PKEY_CTX_set_rsa_padding(ctx, padding) <= 0) {
openssl_error();
return {};
}
std::string plain;
plain.resize(EVP_PKEY_size(m_pkey));
size_t len = plain.size();
if (EVP_PKEY_decrypt(ctx,
reinterpret_cast<unsigned char *>(plain.data()),
&len,
reinterpret_cast<const unsigned char *>(cipher.c_str()),
static_cast<int>(cipher.size()))
<= 0) {
openssl_error();
return {};
}
plain.resize(len);
return plain;
}
[[nodiscard]] auto toPublicPem() const -> std::string
{
BIO *bio = BIO_new(BIO_s_mem());
PEM_write_bio_PUBKEY(bio, m_pkey);
char *data;
int len = BIO_get_mem_data(bio, &data);
std::string ret(data, len);
BIO_free(bio);
return ret;
}
[[nodiscard]] auto toPrivatePem() const -> std::string
{
BIO *bio = BIO_new(BIO_s_mem());
PEM_write_bio_PrivateKey(bio, m_pkey, nullptr, nullptr, 0, nullptr, nullptr);
char *data;
int len = BIO_get_mem_data(bio, &data);
std::string ret(data, len);
BIO_free(bio);
return ret;
}
private:
void destroy()
{
if (m_pkey != nullptr) {
EVP_PKEY_free(m_pkey);
m_pkey = nullptr;
}
}
static auto fromPublicPem(const std::string &pem) -> OpensslRsa *
{
std::unique_ptr<OpensslRsa> retPtr(new OpensslRsa);
BIO *bio = BIO_new_mem_buf(pem.c_str(), -1);
if (bio == nullptr) {
return nullptr;
}
retPtr->m_pkey = PEM_read_bio_PUBKEY(bio, nullptr, nullptr, nullptr);
BIO_free(bio);
return retPtr.release();
}
static auto fromPrivatePem(const std::string &pem) -> OpensslRsa *
{
std::unique_ptr<OpensslRsa> retPtr(new OpensslRsa);
BIO *bio = BIO_new_mem_buf(pem.c_str(), -1);
if (bio == nullptr) {
return nullptr;
}
retPtr->m_pkey = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, nullptr);
BIO_free(bio);
return retPtr.release();
}
friend class OpensslRsaPublicKey;
friend class OpensslRsaPrivateKey;
EVP_PKEY *m_pkey = nullptr;
};
class OpensslRsaPublicKey : noncopyable
{
public:
explicit OpensslRsaPublicKey(const std::string &pem) { setPublicPem(pem); }
~OpensslRsaPublicKey() = default;
void setPublicPem(const std::string &pem)
{
m_opensslRsaPtr.reset(OpensslRsa::fromPublicPem(pem));
}
[[nodiscard]] auto toPem() const -> std::string
{
assert(m_opensslRsaPtr != nullptr);
return m_opensslRsaPtr->toPublicPem();
}
[[nodiscard]] auto encrypt(int padding, const std::string &plain) const -> std::string
{
assert(m_opensslRsaPtr != nullptr);
return m_opensslRsaPtr->encrypt(padding, plain);
}
private:
std::unique_ptr<OpensslRsa> m_opensslRsaPtr;
};
class OpensslRsaPrivateKey : noncopyable
{
public:
explicit OpensslRsaPrivateKey(const std::string &pem) { setPrivatePem(pem); }
~OpensslRsaPrivateKey() = default;
void setPrivatePem(const std::string &pem)
{
m_opensslRsaPtr.reset(OpensslRsa::fromPrivatePem(pem));
}
[[nodiscard]] auto toPem() const -> std::string
{
assert(m_opensslRsaPtr != nullptr);
return m_opensslRsaPtr->toPrivatePem();
}
[[nodiscard]] auto decrypt(int padding, const std::string &cipher) const -> std::string
{
assert(m_opensslRsaPtr != nullptr);
return m_opensslRsaPtr->decrypt(padding, cipher);
}
private:
std::unique_ptr<OpensslRsa> m_opensslRsaPtr;
};
TEST(OpensslRsa, test)
{
OpensslRsa opensslRsa(2048);
std::cout << "Rsa public key: \n" << opensslRsa.toPublicPem() << '\n';
std::cout << "Rsa private key: \n" << opensslRsa.toPrivatePem() << '\n';
const std::string plain = "hello world";
auto cipher = opensslRsa.encrypt(RSA_PKCS1_PADDING, plain);
auto plain2 = opensslRsa.decrypt(RSA_PKCS1_PADDING, cipher);
EXPECT_EQ(plain, plain2);
}
TEST(OpensslRsa, test2)
{
OpensslRsa opensslRsa(2048);
OpensslRsaPublicKey opensslRsaPublicKey(opensslRsa.toPublicPem());
OpensslRsaPrivateKey opensslRsaPrivateKey(opensslRsa.toPrivatePem());
std::cout << "Rsa public key: \n" << opensslRsaPublicKey.toPem() << '\n';
std::cout << "Rsa private key: \n" << opensslRsaPrivateKey.toPem() << '\n';
const std::string plain = "hello world";
auto cipher = opensslRsaPublicKey.encrypt(RSA_PKCS1_PADDING, plain);
auto plain2 = opensslRsaPrivateKey.decrypt(RSA_PKCS1_PADDING, cipher);
EXPECT_EQ(plain, plain2);
}
auto main() -> int
{
openssl_version();
testing::InitGoogleTest();
return RUN_ALL_TESTS();
}