-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcCmdInterp.cpp
More file actions
290 lines (266 loc) · 9.41 KB
/
cCmdInterp.cpp
File metadata and controls
290 lines (266 loc) · 9.41 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
282
283
284
285
286
287
288
289
290
#include "cCmdInterp.hpp"
#include <iostream>
#include <sstream>
#include <chrono>
#include <thread>
#include <boost/filesystem.hpp>
#define KEY_SIZE 2048
cCmdInterp::cCmdInterp(std::string pFifoName, std::string pInstance)
:inst(pInstance)
{
inputFIFO.open(pFifoName);
}
void cCmdInterp::cmdReadLoop()
{
std::string line;
keyStorage.GenerateRSAKey(KEY_SIZE, mOutDir + inst + "-key" + std::to_string(keyStorage.getCurrentKey()) + ".pub"); // generate 1st key
while (1)
{
//std::cout << "loop" << std::endl;
inputFIFO.open("fifo");
std::getline(inputFIFO, line);
//std::cout << "line " << line << std::endl;
inputFIFO.close();
if (line == "QUIT")
break;
else if(line == "SIGN-NEXTKEY")
{
std::cout << "SIGN-NEXTKEY" << std::endl;
std::cout << "current key: " << keyStorage.getCurrentKey() << std::endl;
std::cout << std::endl;
std::string pubFileName = inst + "-key" + std::to_string(keyStorage.getCurrentKey()) + ".pub";
keyStorage.GenerateRSAKey(KEY_SIZE, pubFileName); // XXX
//std::string nextPubFileName = inst + "-key" + std::to_string(keyStorage.getCurrentKey() + 1) + ".pub";
std::string path; // input dir
//system(std::string("touch " + pubFileName).c_str());
std::cout << "pubFileName " << pubFileName << std::endl;
inputFIFO.close();
line.clear();
while (line.size() == 0)
{
inputFIFO.open("fifo");
std::getline(inputFIFO, line);
inputFIFO.close();
}
std::cout << "File to sign " << line << std::endl;
std::cout << "line size " << line.size() << std::endl;
if (!boost::filesystem::exists(line))
{
std::cout << "No found file " << line << std::endl;
//system("rm *.pub");
continue;
}
std::cout << "sign file " << line << std::endl;
std::cout << "cp " << line << " to ." << std::endl;
system(std::string("cp " + line + " .").c_str());
auto it = line.end();
while (*it != '/')
it--;
auto it2 = line.begin();
while (it2 != it + 1)
{
path += *it2;
it2++;
}
std::cout << "path: " << path << std::endl;
line.erase(line.begin(), it + 1);
std::string outDir = line;
std::string file(line);
std::cout << "FILE: " << file << std::endl;
outDir.erase(outDir.end() - 4, outDir.end());
std::cout << "outDir " << outDir << std::endl;
setOutDir(outDir);
std::cout << "current file: " << line << std::endl;
system(std::string("cp " + line + " " + outDir).c_str()); // cp file to out dir(archive)
std::cout << "out path" << mOutDir + inst + "-" + line + ".sig" << std::endl;
std::cout << "current key: " << keyStorage.getCurrentKey() << std::endl;
system(std::string("cp " + line + " .").c_str()); // cp file co current dir
std::cout << "Sign last key" << std::endl;
std::cout << "Last key name: " << pubFileName << std::endl;
std::cout << "current key: " << keyStorage.getCurrentKey() << std::endl;
//keyStorage.GenerateRSAKey(KEY_SIZE, mOutDir + pubFileName); // XXX
keyStorage.RSASignFile(file, mOutDir + inst + "-" + file + ".sig", false); // sign file
//keyStorage.RSASignFile(mOutDir + pubFileName, mOutDir + pubFileName + ".sig", true); // sign key
keyStorage.RSASignFile(pubFileName, mOutDir + pubFileName + ".sig", true); // sign key XXX
//keyStorage.GenerateRSAKey(KEY_SIZE, mOutDir + pubFileName); // XXX
std::cout << "Generate pub key file: " << pubFileName << std::endl;
//keyStorage.GenerateRSAKey(KEY_SIZE, pubFileName); // XXX
keyStorage.RemoveRSAKey(); // XXX
//system(std::string("mv *.sig2 " + mOutDir).c_str());
system(std::string("cp *.sig2 " + mOutDir).c_str());
system(std::string("cp *.sig " + path).c_str());
system(std::string("cp *.pub " + path).c_str());
//system("rm *.pub");
std::cout << "outDir " << outDir << std::endl;
std::cout << "create archive" << std::endl;
std::cout << std::string("tar zcvf " + path + outDir + ".tar.gz " + outDir) << std::endl;
system(std::string("tar zcvf " + path + outDir + ".tar.gz " + outDir).c_str());
}
else if(line == "VERIFY-FILE")
{
bool ok;
std::cout << "VERIFY-FILE" << std::endl;
std::cout << std::endl;
inputFIFO.close();
inputFIFO.open("fifo");
std::getline(inputFIFO, line);
std::string instance;
auto it = line.begin();
while (*it != '-')
{
instance.push_back(*it);
it++;
}
std::cout << "line " << line << std::endl;
std::cout << "instance " << instance << std::endl;
std::string tmp;
unsigned int key; // file was signed this key
std::ifstream inFile(line);
inFile >> tmp;
inFile >> key;
inFile.close();
if (key >= verify(std::string(instance + "-key1.pub"))) // XXX key1.pub.sig
//if (key < verify(std::string(instance + "-key1.pub"))) // XXX
{
std::cout << " Keys OK" << std::endl;
ok = true;
}
else
{
ok = false;
std::cout << " Keys verification error" << std::endl;
}
bool fileOK = keyStorage.RSAVerifyFile(line, instance);
inputFIFO.close();
std::cout << "keys " << ok << std::endl;
std::cout << "file " << fileOK << std::endl;
if (ok && fileOK)
std::cout << "File OK" << std::endl;
}
else if(line == "SIGN-NEXTKEY-WAV-FILES")
{
std::cout << "SIGN-NEXTKEY-WAV-FILES" << std::endl;
boost::filesystem::directory_iterator dirIterator(".");
boost::filesystem::directory_iterator endIterator;
//generate new key
std::string pubFileName = inst + "-key" + std::to_string(keyStorage.getCurrentKey()) + ".pub";
system(std::string("touch " + pubFileName).c_str());
std::cout << "pubFileName " << pubFileName << std::endl;
while (dirIterator != endIterator)
{
if (boost::filesystem::is_regular_file(dirIterator->status()))
{
std::stringstream ss;
std::string fileName;
ss << *dirIterator;
ss >> fileName;
fileName.erase(fileName.begin(), fileName.begin() + 3);
fileName.pop_back();
//if (fileName.find("txt") == (fileName.size() - 3)) // XXX wav
if (fileName.find("wav") == (fileName.size() - 3))
{
std::cout << fileName << std::endl;
// sign
keyStorage.RSASignFile(fileName, mOutDir + inst + "-" + fileName + ".sig", false);
}
}
dirIterator++;
}
std::cout << "generate new key" << std::endl;
keyStorage.GenerateRSAKey(KEY_SIZE, mOutDir + pubFileName);
std::cout << "rm old key" << std::endl;
keyStorage.RemoveRSAKey();
keyStorage.RSASignFile(pubFileName, mOutDir + pubFileName + ".sig", true); // sign key
std::cout << "tar cf wav_files.tar " + inst + "*" << std::endl;
system(std::string("mv *.pub " + mOutDir).c_str());
system(std::string("mv *.sig2 " + mOutDir).c_str());
system(std::string("mv *.wav " + mOutDir).c_str());
//system(std::string("mv *.txt " + mOutDir).c_str());
system(std::string("tar czf " + inst + ".tar.gz " + mOutDir).c_str());
//system(std::string().c_str());
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
//std::cout << "loop end" << std::endl;
}
unsigned int cCmdInterp::verify(std::string firstKey) // verify keys, get name of 1st pub file, return last good key
{
std::cout << "start verify" << std::endl;
//std::ifstream pubFile;
//system(std::string("cp " + firstKey + " " + mOutDir + firstKey).c_str()); // copy 1st key to out dir
std::string instance;
std::string fileName = instance;
bool good = true;
int keyNumber = 2;
while (firstKey.front() != '-')
{
instance += firstKey.front();
firstKey.erase(firstKey.begin());
}
std::cout << "start loop" << std::endl;
unsigned int lastGoodKey = 1;
while (good)
{
std::ifstream pubFile;
fileName = instance + "-key" + std::to_string(keyNumber) + ".pub.sig";
std::cout << "file name " << fileName << std::endl;
pubFile.open(fileName);
if(!pubFile.good()) // no file
{
std::cout << "No found " << fileName << std::endl;
break;
}
std::cout << "start verify " << fileName << std::endl;
good = keyStorage.RSAVerifyFile(fileName, instance);
if (good)
{
lastGoodKey = keyNumber;
//std::cout << "mv cmd " << "mv " + fileName + " " + mOutDir + fileName << std::endl;
fileName.erase(fileName.end() - 4, fileName.end()); // rm ".sig"
system(std::string("cp " + fileName + " " + mOutDir + fileName).c_str());
}
keyNumber++;
}
std::cout << "Last good key: " << lastGoodKey << std::endl;
if (lastGoodKey > 1)
{
std::cout << "OK" << std::endl;
return lastGoodKey;
}
else
return -1;
}
void cCmdInterp::setOutDir(std::string outDir)
{
std::cout << "setOutDir()" << std::endl;
mOutDir = outDir;
system(std::string("mkdir " + mOutDir).c_str());
if (outDir.back() != '/')
mOutDir.push_back('/');
//std::cout << "out dir: " << mOutDir << std::endl;
}
unsigned int cCmdInterp::verifyOneFile(std::string fileName) //fileName = sig file
{
std::string instance;
auto it = fileName.begin();
while (*it != '-')
{
instance.push_back(*it);
it++;
}
//std::cout << "RSAVerifyFile " << keyStorage.RSAVerifyFile(fileName, instance) << std::endl;
std::string firstPubKey;
firstPubKey = instance + "-key1.pub";
std::cout << "Start keys verification" << std::endl;
std::cout << "first pub key " << firstPubKey << std::endl;
unsigned int ret = verify(std::string(fileName));
if (ret == -1)
return 2;
std::cout << "file name " << fileName << std::endl;
ret = keyStorage.RSAVerifyFile(fileName, instance);
//std::cout << ret << std::endl;
if (ret == 0)
return 3;
std::cout << "OK" << std::endl;
return 0;
}