forked from rfree/chainsign
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathckeysstorage.cpp
More file actions
281 lines (235 loc) · 7.44 KB
/
ckeysstorage.cpp
File metadata and controls
281 lines (235 loc) · 7.44 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
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
274
275
276
277
278
279
280
281
#include "ckeysstorage.h"
#include <memory>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <crypto++/rsa.h>
#include <crypto++/osrng.h>
#include <crypto++/base64.h>
#include <crypto++/files.h>
#include <crypto++/hex.h>
using namespace CryptoPP;
cKeysStorage::cKeysStorage()
: mCurrentKey(1)
{
}
void cKeysStorage::GenerateRSAKey(unsigned int keyLength, std::string fileName)
{
using namespace CryptoPP;
AutoSeededRandomPool rng;
// Generate Parameters
InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(rng, keyLength);
// Create Keys
CryptoPP::RSA::PrivateKey privateKey(params);
CryptoPP::RSA::PublicKey publicKey(params);
//mPrvKeys.push_back(privateKey);
mPrvKeys[mCurrentKey] = privateKey;
std::cout << "start saving pub file" << std::endl;
std::cout << "generated file: " << fileName << std::endl;
savePubFile(mCurrentKey, publicKey, fileName);
mCurrentKey++;
std::cout << "end of GenerateRSAKey" << std::endl;
}
bool cKeysStorage::RSAVerifyFile(const std::string &fileName, const std::string &instance) // load .sig file
{
using namespace CryptoPP;
std::cout << "Start RSAVerifyFile" << std::endl;
std::cout << "File name: " << fileName << std::endl;
std::cout << "instance: " << instance << std::endl;
std::string line;
std::string clearTextFileName;
int pubicKeyNumber;
// read sig file
std::ifstream input(fileName);
input >> line;
//parse data
input >> pubicKeyNumber;
//std::cout << line << " " << pubicKeyNumber << std::endl;
std::cout << "====sig===" << std::endl;
for (int i = 0; i < 4; ++i) // 3 lines
{
input >> line;
std::cout << line << " ";
input >> line;
std::cout << line << " " << std::endl;
}
std::cout << "====sig===" << std::endl;
clearTextFileName = line;
input >> line;
input >> line;
std::string sig2file = line;
std::cout << std::endl;
std::cout << "clear file: " << clearTextFileName << std::endl;
std::cout << "sig2 file: " << sig2file << std::endl;
//load signature
std::string signedTxt;
FileSource(clearTextFileName.c_str(), true, new StringSink(signedTxt));
std::string signature;
FileSource(sig2file.c_str(), true, new StringSink(signature));
//std::cout << std::endl << "signature " << std::noskipws << signature << std::endl;
//std::cout << std::endl << "pubicKeyNumber " << pubicKeyNumber << std::endl;
std::string pubFile;
pubFile = instance + "-key" + std::to_string(pubicKeyNumber) + ".pub";
std::cout << "pub file: " << pubFile << std::endl;
CryptoPP::RSA::PublicKey currentPubKey = loadPubFile(pubFile);
AutoSeededRandomPool rng;
std::cout << "pub key validate " << currentPubKey.Validate(rng, 1);
std::cout << std::endl << "start verify" << std::endl;
RSASSA_PKCS1v15_SHA_Verifier verifier(currentPubKey);
std::string combined(signedTxt);
combined.append(signature);
try
{
StringSource(combined, true,
new SignatureVerificationFilter(verifier, NULL, SignatureVerificationFilter::THROW_EXCEPTION) );
std::cout << "Signature OK" << std::endl;
return true;
}
catch(SignatureVerificationFilter::SignatureVerificationFailed &err)
{
std::cout << "verify error " << err.what() << std::endl;
return false;
}
//std::cout << "end of RsaVerifyFile" << std::endl;
}
void cKeysStorage::savePubFile(unsigned int numberOfKey, const CryptoPP::RSA::PublicKey& pPubKey, std::string fileName)
{
std::ofstream mOutFile;
std::string mOutName; //(std::to_string(numberOfKey));
//mOutName += ".pub";
mOutName = fileName;
std::cout << "Pub file: " << fileName << std::endl;
//mOutFile.open(mOutName);
//save header
//mOutFile << "version 1" << std::endl;
//mOutFile << "crypto rsa" << std::endl;
//mOutFile << "size 4096" << std::endl;
//mOutFile << "END" << std::endl;
//generate pub key in txt file
//Base64Encoder pubkeysink(new FileSink("tmp"));
Base64Encoder pubkeysink(new FileSink(mOutName.c_str()));
pPubKey.DEREncode(pubkeysink);
pubkeysink.MessageEnd();
std::cout << "Pub key:" << std::endl;
//append from tmp to pub file
std::ifstream outFile(fileName);
char s;
while (!outFile.eof())
{
outFile >> std::noskipws >> s;
std::cout << s;
}
mOutFile.close();
std::cout << std::endl;
std::cout << "end of savePubFile" << std::endl;
}
CryptoPP::RSA::PublicKey cKeysStorage::loadPubFile(std::string pPubKey)
{
std::string fileName(pPubKey);
//fileName += ".pub";
std::cout << "Public key file: " << fileName << std::endl;
/*std::string line;
std::ifstream input(fileName);
for (int i = 0; i < 3; i++)
{
input >> line;
//std::cout << line << " ";
input >> line;
//std::cout << line << std::endl;
}
std::cout << "Load rsa data" << std::endl;
input >> line; // END
*/
// load rsa data
//char byte;
//from .pub to tmp
/*std::ofstream tmpFile("tmp", std::ios::trunc);
while (!input.eof())
{
input >> std::noskipws >> byte;
std::cout << byte;
tmpFile << byte;
}
tmpFile.close();*/
//Read public key
CryptoPP::ByteQueue bytes;
FileSource file(fileName.c_str(), true, new Base64Decoder);
file.TransferTo(bytes);
bytes.MessageEnd();
RSA::PublicKey pubKey;
pubKey.Load(bytes);
std::cout << "end of loadPubFile" << std::endl;
return pubKey;
}
//https://gist.github.com/TimSC/5251670
void cKeysStorage::RSASignFile(const std::string& messageFilename, const std::string& signatureFilename, bool signKey)
{
if (signKey)
--mCurrentKey;
AutoSeededRandomPool rng;
std::string strContents;
FileSource(messageFilename.c_str(), true, new StringSink(strContents));
std::string sig2File = messageFilename + ".sig2";
//sign file
std::cout << std::endl << std::endl << "start sign file " << messageFilename << std::endl;
std::cout << "size of map " << mPrvKeys.size() << std::endl;
std::cout << "current key " << mCurrentKey << std::endl;
RSASSA_PKCS1v15_SHA_Signer privkey(mPrvKeys.at(mCurrentKey - 1));
std::cout << "sign file using key nr " << mCurrentKey - 1 << std::endl;
SecByteBlock sbbSignature(privkey.SignatureLength());
std::cout << "sign message" << std::endl;
privkey.SignMessage(
rng,
(byte const*) strContents.data(),
strContents.size(),
sbbSignature);
//std::cout << "Size of signature: " << sbbSignature.size() << std::endl;
//std::cout<<std::endl;
//std::cout.write( reinterpret_cast<const char*> (sbbSignature.BytePtr()) , sbbSignature.size() ); // XXX FIXME remove the bad cast!!! test
//std::cout<<std::endl;
//Save result
FileSink sinksig(sig2File.c_str());
sinksig.Put(sbbSignature, sbbSignature.size());
sinksig.MessageSeriesEnd();
std::ofstream output(signatureFilename);
output << "id-nr " << mCurrentKey - 1 << std::endl;
output << "key-ver 1" << std::endl;
output << "key-crypto rsa" << std::endl;
output << "key-size 2048" << std::endl;
output << "cleartext-file "<< messageFilename << std::endl;
output << "sig2-file " << sig2File << std::endl;
output << "END" << std::endl;
output.close();
if (signKey)
++mCurrentKey;
std::cout << "end of RSASignFile" << std::endl;
}
void cKeysStorage::RemoveRSAKey()
{
if (mCurrentKey == 1)
return;
mPrvKeys.erase(mPrvKeys.begin());
}
/*
.pub format:
id-instance inst20141120_1242
id-nr 1
key-ver 1
key-crypto rsa
key-size 4096
END
<after this line "END" we paste here the RSA data: of the public key>
.sig format:
id-instance inst20141120_1242
id-nr 1
key-ver 1
key-crypto rsa
key-size 4096
cleartext-file foo1.log
END
<after this line "END" we paste here the RSA data: of signature>
*/