-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairKey.cpp
More file actions
177 lines (130 loc) · 3.88 KB
/
Copy pathPairKey.cpp
File metadata and controls
177 lines (130 loc) · 3.88 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
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
#include "pch.h"
#include "PairKey.h"
#include <openssl/pem.h>
PairKey::PairKey(Key_t type)
{
int ret = -1;
switch (type) {
case Key_t::EC_key:
{
auto eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
assert(1 == EC_KEY_generate_key(eckey));
assert(1 == EC_KEY_check_key(eckey));
assert(1 == EVP_PKEY_assign_EC_KEY(evp_ptr.get(), eckey));
break;
}
default:
{
auto rsa = RSA_new();
uniqeBignum bn_ptr(BN_new(), BIGNUMDeleter());
assert(1 == BN_set_word(bn_ptr.get(), RSA_F4));
assert(1 == RSA_generate_key_ex(rsa, 2048, bn_ptr.get(), nullptr));
assert(1 == EVP_PKEY_assign_RSA(evp_ptr.get(), rsa));
}
}
bPrivate = true;
}
bool PairKey::savePublicKey(const string& filename) const
{
auto bio_ptr = newBIO(filename.c_str(), "w+");
return PEM_write_bio_PUBKEY(bio_ptr.get(), evp_ptr.get());
}
bool PairKey::savePrivateKey(const string& filename) const
{
uniqeBIO bio_ptr = newBIO(filename.c_str(), "w+");
return PEM_write_bio_PrivateKey(bio_ptr.get(), evp_ptr.get(), nullptr, nullptr, 0, nullptr, nullptr);
}
bool PairKey::readPublicKey(const string& filename)
{
uniqeBIO bio_ptr = newBIO(filename, "r");
evp_ptr = uniqeEVP{ PEM_read_bio_PUBKEY(bio_ptr.get(), nullptr, nullptr, nullptr), EVPDeleter() };
bPrivate = false;
return evp_ptr != nullptr;
}
bool PairKey::readPrivate(const string& filename)
{
uniqeBIO bio_ptr = newBIO(filename, "r");
if (bio_ptr == nullptr) {
return false;
}
return readPrivate(move(bio_ptr));
}
bool PairKey::isPrivate() const
{
return bPrivate;
}
Key_t PairKey::getType() const
{
if (evp_ptr == nullptr) {
return Key_t::UNK;
}
switch (auto typeId = EVP_PKEY_base_id(evp_ptr.get())) {
case EVP_PKEY_RSA:
return Key_t::RSA_key;
case EVP_PKEY_EC:
return Key_t::EC_key;
default:
return Key_t::UNK;
}
}
vector<unsigned char> PairKey::sign(const vector<unsigned char>& msg) const
{
const int sha256Len = 32;
unsigned char sha[sha256Len];
SHA256(&(msg[0]), msg.size(), sha);
auto evpctx_ptr = newEVPCTX(evp_ptr);
assert(1 == EVP_PKEY_sign_init(evpctx_ptr.get()));
assert(1 == EVP_PKEY_CTX_set_signature_md(evpctx_ptr.get(), EVP_sha256()));
size_t signLen = 0;
assert(1 == EVP_PKEY_sign(evpctx_ptr.get(), NULL, &signLen, sha, sha256Len));
vector<unsigned char> sig(signLen);
auto ret = EVP_PKEY_sign(evpctx_ptr.get(), &(sig[0]), &signLen, sha, sha256Len);
if (ret != 1) {
return vector<unsigned char>(0);
}
if (signLen != sig.size()) {
sig.resize(signLen);
}
return sig;
}
bool PairKey::verifySign(const vector<unsigned char>& sign, const vector<unsigned char>& msg) const
{
const int sha256Len = 32;
unsigned char sha[sha256Len];
SHA256(&(msg[0]), msg.size(), sha);
auto evpctx_ptr = newEVPCTX(evp_ptr);
assert(1 == EVP_PKEY_verify_init(evpctx_ptr.get()));
assert(1 == EVP_PKEY_CTX_set_signature_md(evpctx_ptr.get(), EVP_sha256()));
auto ret = EVP_PKEY_verify(evpctx_ptr.get(), &sign[0], sign.size(), sha, sha256Len);
assert(ret != -2);
return ret == 1;
}
bool PairKey::readPrivate(uniqeBIO bioPtr)
{
evp_ptr = uniqeEVP{ PEM_read_bio_PrivateKey(bioPtr.get(), nullptr,nullptr, nullptr), EVPDeleter() };
bPrivate = evp_ptr != nullptr ? true : bPrivate;
return evp_ptr != nullptr;
}
bool PairKey::parsePrivate(const string& privateKey) {
auto bio_ptr = uniqeBIO{ BIO_new(BIO_s_mem()) , BIODeleter() };
if (bio_ptr == nullptr) {
return false;
}
assert(-1 != BIO_printf(bio_ptr.get(), "%s", privateKey.c_str()));
return readPrivate(move(bio_ptr));
}
const uniqeEVP& PairKey::getEVP() const
{
return evp_ptr;
}
bool PairKey::parsePublic(const string& publicKey)
{
auto bio_ptr = uniqeBIO{ BIO_new(BIO_s_mem()) , BIODeleter() };
if (bio_ptr == nullptr) {
return false;
}
assert(-1 != BIO_printf(bio_ptr.get(), "%s", publicKey.c_str()));
evp_ptr = uniqeEVP{ PEM_read_bio_PUBKEY(bio_ptr.get(), nullptr, nullptr, nullptr), EVPDeleter() };
bPrivate = false;
return evp_ptr != nullptr;
}