37 #define PRINT_EXCEPTION \
38 catch(const BDocException& e) { printf("Caught BDocException: %s\n", parseException(e).c_str()); } \
39 catch(const IOException& e) { printf("Caught IOException: %s\n", parseException(e).c_str()); } \
40 catch(const SignException& e) { printf("Caught SignException: %s\n", parseException(e).c_str()); } \
41 catch(const SignatureException& e) { printf("Caught SignatureException: %s\n", parseException(e).c_str()); } \
42 catch(const Exception& e) { printf("Caught Exception: %s\n", parseException(e).c_str()); } \
43 catch(...) { printf("Caught unknown exception\n"); }
45 using namespace digidoc;
54 int len = MultiByteToWideChar(CP_ACP, 0, param.data(),
int(param.size()), 0, 0);
55 std::wstring out(len, 0);
56 len = MultiByteToWideChar(CP_ACP, 0, param.data(),
int(param.size()), &out[0], len);
77 const std::vector<PKCS11Signer::PKCS11Cert> &certificates)
const throw(
SignException);
90 printf(
"Usage: %s COMMAND [OPTIONS FILE]\n\n", executable);
91 printf(
" Command create:\n");
92 printf(
" Example: %s create --file=file1.txt --file=file2.txt --profile=TM demo-container.bdoc\n", executable);
93 printf(
" Available options:\n");
94 printf(
" --file - The option can occur multiple times. File(s) to be signed\n");
95 printf(
" --city - optional, city of production place\n");
96 printf(
" --state - optional, state of production place\n");
97 printf(
" --postalCode - optional, postalCode of production place\n");
98 printf(
" --country - optional, country of production place\n");
99 printf(
" --role - optional, option can occur multiple times. Signer role(s)\n");
101 printf(
" --cng - optional, Use CNG api for signing under windows.\n");
102 printf(
" --selectFirst - optional, Select first certificate in store.\n");
104 printf(
" --pkcs11Driver - optional, default is '%s'. Path of PKCS11 driver.\n",
Conf::getInstance()->getPKCS11DriverPath().c_str());
105 printf(
" --pin - optional, default asks pin from prompt\n");
106 printf(
" --profile - optional, default TM. Signing profile, available values: BES, TM\n");
107 printf(
" Command open:\n");
108 printf(
" Example: %s open --list --validateOnline container-file.bdoc\n", executable);
109 printf(
" Available options:\n");
110 printf(
" --extractAll - extracts documents (to path when provided)\n");
111 printf(
" --list - lists documents and signatures\n");
112 printf(
" --validateOnline - Whether online validate all signatures\n");
113 printf(
" Command sign:\n");
114 printf(
" Example: %s sign --profile=TM demo-container.bdoc\n", executable);
115 printf(
" Available options:\n");
116 printf(
" --city - optional, city of production place\n");
117 printf(
" --state - optional, state of production place\n");
118 printf(
" --postalCode - optional, postalCode of production place\n");
119 printf(
" --country - optional, country of production place\n");
120 printf(
" --role - optional, option can occur multiple times. Signer role(s)\n");
122 printf(
" --cng - optional, Use CNG api for signing under windows.\n");
123 printf(
" --selectFirst - optional, Select first certificate in store.\n");
125 printf(
" --pkcs11Driver - optional, default is '%s'. Path of PKCS11 driver.\n",
Conf::getInstance()->getPKCS11DriverPath().c_str());
126 printf(
" --pin - optional, default asks pin from prompt\n");
127 printf(
" --profile - optional, default TM. Signing profile, available values: BES, TM\n");
132 std::string result = e.
getMsg() +
"\n";
152 int open(
int argc,
char* argv[])
154 bool validateOnline =
false, listContent =
false, extractAll =
false;
155 std::string path, extractPath;
156 bool validStatus =
true, extractStatus =
true;
159 for(
int i = 2;
i < argc;
i++)
162 if(arg.find(
"--validateOnline") == 0)
164 validateOnline =
true;
166 else if(arg.find(
"--list") == 0)
170 else if(arg.find(
"--extractAll") == 0)
173 size_t pos = arg.find(
"=");
174 if(pos != std::string::npos)
175 extractPath = arg.substr(pos + 1);
194 printf(
"Extracting documents:\n");
201 printf(
" Document %u (%s) extracted to %s (%lu bytes)\n",
i, doc.
getMediaType().c_str(), dst.c_str(), doc.
getSize());
204 printf(
" Document %u extraction: FAILED\n",
i);
205 extractStatus =
false;
228 printf(
" Signature %u (%s):\n",
i, sig->
getMediaType().c_str());
235 printf(
" Offline validation: OK\n");
240 printf(
" Offline validation: FAILED\n");
254 printf(
" Online validation: FAILED\n");
264 printf(
" Signature production place:\n");
267 printf(
" City: %s\n", spp.
city.c_str());
268 printf(
" Postal code: %s\n", spp.
postalCode.c_str());
275 printf(
" Signer role(s):\n");
276 for(std::vector<std::string>::const_iterator iter = roles.
claimedRoles.begin(); iter != roles.
claimedRoles.end(); iter++)
277 printf(
" %s\n", iter->c_str());
300 std::string path, pkcs11Driver, pin, city, state, postalCode, country;
301 std::vector<std::string> files, roles;
305 bool cng =
false, selectFirst =
false;
310 for(
int i = 2;
i < argc;
i++)
314 if(arg.find(
"--file=") == 0)
316 files.push_back(arg.substr(7));
318 else if(arg.find(
"--profile=") == 0)
320 arg = arg.substr(10);
321 if(std::string(
"BES") == arg) { profile =
BDoc::BES; }
322 else if(std::string(
"TM") == arg) { profile =
BDoc::TM; }
330 else if(arg.find(
"--cng") == 0)
332 else if(arg.find(
"--selectFirst") == 0)
335 else if(arg.find(
"--pkcs11Driver=") == 0)
336 pkcs11Driver = arg.substr(15);
337 else if(arg.find(
"--pin=") == 0)
339 else if(arg.find(
"--city=") == 0)
340 city = arg.substr(7);
341 else if(arg.find(
"--state=") == 0)
342 state = arg.substr(8);
343 else if(arg.find(
"--postalCode=") == 0)
344 postalCode = arg.substr(13);
345 else if(arg.find(
"--country=") == 0)
346 country = arg.substr(10);
347 else if(arg.find(
"--role=") == 0)
348 roles.push_back(arg.substr(7));
354 if(path.empty() || files.empty())
362 if(pkcs11Driver.empty())
369 for(std::vector<std::string>::const_iterator iter = files.begin(); iter != files.end(); iter++)
375 signer =
new CNGSigner(pin, selectFirst);
381 if(!city.empty() || !state.empty() || !postalCode.empty() || !country.empty())
396 bdoc.
sign(signer, profile);
416 int sign(
int argc,
char* argv[])
418 std::string path, pkcs11Driver, pin, city, state, postalCode, country;
419 std::vector<std::string> roles;
423 bool cng =
false, selectFirst =
false;
428 for(
int i = 2;
i < argc;
i++)
432 if(arg.find(
"--profile=") == 0)
434 arg = arg.substr(10);
435 if(std::string(
"BES") == arg) { profile =
BDoc::BES; }
436 else if(std::string(
"TM") == arg) { profile =
BDoc::TM; }
444 else if(arg.find(
"--cng") == 0)
446 else if(arg.find(
"--selectFirst") == 0)
449 else if(arg.find(
"--pkcs11Driver=") == 0)
450 pkcs11Driver = arg.substr(15);
451 else if(arg.find(
"--pin=") == 0)
453 else if(arg.find(
"--city=") == 0)
454 city = arg.substr(7);
455 else if(arg.find(
"--state=") == 0)
456 state = arg.substr(8);
457 else if(arg.find(
"--postalCode=") == 0)
458 postalCode = arg.substr(13);
459 else if(arg.find(
"--country=") == 0)
460 country = arg.substr(10);
461 else if(arg.find(
"--role=") == 0)
462 roles.push_back(arg.substr(7));
476 if(pkcs11Driver.empty())
485 signer =
new CNGSigner(pin, selectFirst);
491 if(!city.empty() || !state.empty() || !postalCode.empty() || !country.empty())
506 bdoc.
sign(signer, profile);
545 const std::vector<PKCS11Signer::PKCS11Cert> &certificates)
const throw(
SignException)
547 printf(
"Available certificates:\n");
548 for(std::vector<PKCS11Signer::PKCS11Cert>::const_iterator iter = certificates.begin(); iter != certificates.end(); iter++)
549 printPKCS11Cert(*iter);
551 printf(
"Selected:\n");
552 printPKCS11Cert(cert);
563 printf(
" -------------------------------------------------------\n");
564 printf(
" token label: %s\n", cert.
token.
label.c_str());
566 printf(
" token model: %s\n", cert.
token.
model.c_str());
567 printf(
" token serial Nr: %s\n", cert.
token.
serialNr.c_str());
568 printf(
" label: %s\n", cert.
label.c_str());
578 int main(
int argc,
char* argv[])
585 printf(
" digidoc-tool version: %s\n",
VER_STR(MAJOR_VER.MINOR_VER.RELEASE_VER.BUILD_VER));
594 std::string command(argv[1]);
596 if(command.compare(
"open") == 0)
597 returnCode =
open(argc, argv);
598 else if(command.compare(
"create") == 0)
599 returnCode =
create(argc, argv);
600 else if(command.compare(
"sign") == 0)
601 returnCode =
sign(argc, argv);
602 else if(command.compare(
"version") == 0)