-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
312 lines (275 loc) · 9.13 KB
/
main.cpp
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <iostream>
#include <string>
#include <vector>
#include <botan/secmem.h>
#include <botan/p11.h>
#include <botan/p11_module.h>
#include <botan/p11_slot.h>
#include <botan/p11_session.h>
#include <botan/p11_rsa.h>
#include <botan/p11_object.h>
#include <botan/auto_rng.h>
#include <botan/pubkey.h>
#include <botan/pipe.h>
#include <botan/b64_filt.h>
#include <boost/program_options.hpp>
#include <boost/optional.hpp>
#include <boost/filesystem.hpp>
#include <optional>
//#include <filesystem> <-- should be in C++17
#include "session.h"
#include "deencryptor.h"
#include "signverifier.h"
#include "generatekey.h"
class ProgramOptions
{
public:
ProgramOptions(boost::program_options::options_description commandLineOptions) : commandLineOptions_(commandLineOptions)
{}
Botan::PKCS11::secure_string getPassword() const
{
std::string x = vm_["password"].as<std::string>();
Botan::PKCS11::secure_string ss(x.begin(), x.end());
return ss;
}
boost::filesystem::path getModule() const
{
std::string x = vm_["module"].as<std::string>();
boost::filesystem::path pt(x);
return pt;
}
Botan::PKCS11::SlotId getSlotId() const
{
Botan::PKCS11::SlotId s = vm_["slot"].as<Botan::PKCS11::SlotId>();
return s;
}
bool isGenerate() const
{
return vm_.count("generate");
}
bool isListAllSlotsObject() const
{
return vm_.count("list");
}
bool isEncrypt() const
{
return vm_.count("encrypt");
}
bool isDecrypt() const
{
return vm_.count("decrypt");
}
bool isSign() const
{
return vm_.count("sign");
}
bool isVerify() const
{
return vm_.count("verify");
}
boost::optional<boost::filesystem::path> getContent() const
{
if(vm_.count("input"))
{
std::string x = vm_["input"].as<std::string>();
boost::filesystem::path pt(x);
return std::move(pt);
}
else
{
return boost::optional<boost::filesystem::path>{};
}
}
boost::optional<boost::filesystem::path> getOutput() const
{
if(vm_.count("output"))
{
std::string x = vm_["output"].as<std::string>();
boost::filesystem::path pt(x);
return std::move(pt);
}
else
{
return boost::optional<boost::filesystem::path>{};
}
}
boost::optional<boost::filesystem::path> getSignatureFile() const
{
if(vm_.count("signature"))
{
std::string x = vm_["signature"].as<std::string>();
boost::filesystem::path pt(x);
return std::move(pt);
}
else
{
return boost::optional<boost::filesystem::path>{};
}
}
void printHelp() const
{
std::cout << commandLineOptions_ << '\n';
}
static std::optional<ProgramOptions> create(int argc, const char* const argv[], boost::program_options::options_description commandLineOptions)
{
ProgramOptions options(commandLineOptions);
try
{
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, commandLineOptions), options.vm_);
if(options.vm_.count("help"))
{
options.printHelp();
return std::optional<ProgramOptions>{};
}
if(!options.vm_.count("module") ||
!options.vm_.count("password") ||
(!options.vm_.count("slot") && !options.vm_.count("list")))
{
std::cerr << "Required parameter are missing:\n";
options.printHelp();
return std::optional<ProgramOptions>{};
}
if(!boost::filesystem::exists(options.getModule()))
{
std::cerr << "Module " << options.getModule().string() << " doesn't exists\n";
options.printHelp();
return std::optional<ProgramOptions>{};
}
}
catch(const boost::program_options::error &ex)
{
std::cerr << ex.what() << '\n';
std::cout << commandLineOptions << '\n';
return std::optional<ProgramOptions>{};
}
return std::move(options);
}
private:
boost::program_options::variables_map vm_;
boost::program_options::options_description commandLineOptions_;
};
std::optional<ProgramOptions> parseCommandLine(int argc, const char* const argv[])
{
boost::program_options::options_description commandLineOptions{"Options"};
commandLineOptions.add_options()
("help,h", "Help message")
("module,m", boost::program_options::value<std::string>(), "Path to pkcs11 module")
("slot,t", boost::program_options::value<Botan::PKCS11::SlotId>(), "Slot id")
("password,p", boost::program_options::value<std::string>(), "Password")
("list,l", "List Objects and slots")
("input,i", boost::program_options::value<std::string>(), "Content to encrypt,decrypt or sign")
("output,o", boost::program_options::value<std::string>(), "Encrypt, decrypt or sign output")
("decrypt,d", "Decrypt")
("encrypt,e", "Encrypt")
("sign,s", "Signing")
("signature,g", boost::program_options::value<std::string>(), "Signature")
("verify,r", "Verify")
("generate,g", "Generate key")
;
return ProgramOptions::create(argc, argv, commandLineOptions);
}
int main(int argc, const char* const argv[])
{
std::optional<ProgramOptions> programOptions = parseCommandLine(argc, argv);
if(programOptions)
{
boost::filesystem::path mp = programOptions->getModule();
std::cout << "Load module from " << mp.string() << '\n';
try {
if(programOptions->isListAllSlotsObject())
{
pkcs11::Session::showAllSlots(mp, programOptions->getPassword());
}
if(programOptions->isGenerate())
{
pkcs11::GenerateKey gen(mp, programOptions->getPassword(), programOptions->getSlotId());
gen.generate();
}
if(programOptions->isEncrypt() || programOptions->isDecrypt() ||
programOptions->isSign() || programOptions->isVerify())
{
boost::optional<boost::filesystem::path> contentFile = programOptions->getContent();
if(contentFile)
{
if(boost::filesystem::exists(*contentFile))
{
if(programOptions->isEncrypt() || programOptions->isDecrypt())
{
boost::optional<boost::filesystem::path> output = programOptions->getOutput();
if(!output)
{
std::cerr << "Required --output parameter is missing\n";
return -1;
}
pkcs11::DeEncryptor deencryptor(mp, programOptions->getPassword(), programOptions->getSlotId());
if(programOptions->isEncrypt())
{
std::cout << "Encrypt " << contentFile->string() << " to " << output->string() << '\n';
deencryptor.encrypt(contentFile.get(), output.get());
}
if(programOptions->isDecrypt())
{
std::cout << "Decrypt " << contentFile->string() << " to " << output->string() << '\n';
deencryptor.decrypt(contentFile.get(), output.get());
}
}
if(programOptions->isSign() || programOptions->isVerify())
{
pkcs11::SignVerifier verifier(mp, programOptions->getPassword(), programOptions->getSlotId());
if(programOptions->isSign())
{
boost::optional<boost::filesystem::path> output = programOptions->getOutput();
if(!output)
{
std::cerr << "Required --output parameter is missing\n";
return -1;
}
std::cout << "Sign " << contentFile->string() << " signature output: " << output->string() << '\n';
verifier.sign(contentFile.get(), output.get());
}
if(programOptions->isVerify())
{
boost::optional<boost::filesystem::path> signatureFile = programOptions->getSignatureFile();
if(!signatureFile)
{
std::cerr << "Required --signature parameter is missing\n";
return -1;
}
std::cout << "Verify " << contentFile->string() << " with " << signatureFile->string() << '\n';
if(verifier.verify(contentFile.get(), signatureFile.get()))
{
std::cout << "Signature is OK!\n";
}
else
{
std::cout << "Signature is WRONG!\n";
}
}
}
}
else
{
std::cerr << "Content file " << contentFile->string() << " doesn't exists.\n";
return -1;
}
}
else
{
std::cerr << "Required parameter --file is missing\n";
programOptions->printHelp();
return -1;
}
}
} catch(Botan::PKCS11::PKCS11_ReturnError &e)
{
std::cout << "PKCS11_ReturnError: " << e.what() << '\n';
Botan::PKCS11::ReturnValue value = e.get_return_value();
std::cout << pkcs11::pkcs11ErrorToStr(value) << '\n';
}
catch(Botan::PKCS11::PKCS11_Error &e)
{
std::cout << "PKCS11_Error: " << e.what() << '\n';
}
}
return 0;
}