-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathdigidoc-tool.cpp
More file actions
1066 lines (989 loc) · 37.5 KB
/
Copy pathdigidoc-tool.cpp
File metadata and controls
1066 lines (989 loc) · 37.5 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libdigidocpp
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "Conf.h"
#include "Container.h"
#include "DataFile.h"
#include "Signature.h"
#include "XmlConf.h"
#include "crypto/Digest.h"
#include "crypto/PKCS11Signer.h"
#include "crypto/PKCS12Signer.h"
#include "crypto/TSL.h"
#include "crypto/WinSigner.h"
#include "crypto/X509Cert.h"
#include "util/File.h"
#include "util/log.h"
#include <algorithm>
#include <iostream>
#include <optional>
#include <sstream>
#include <unordered_map>
#ifdef _WIN32
#include <Windows.h>
#include <conio.h>
#else
#include <cstring>
#include <unistd.h>
#endif
using namespace digidoc;
using namespace digidoc::util;
using namespace std;
namespace fs = filesystem;
namespace std
{
static ostream &operator<<(ostream &os, const X509Cert &cert)
{
return os << cert.subjectName("CN");
}
static ostream &operator<<(ostream &os, const vector<unsigned char> &data)
{
os << hex << uppercase << setfill('0');
for(unsigned char i: data)
os << setw(2) << static_cast<int>(i) << ' ';
return os << dec << nouppercase << setfill(' ');
}
static ostream &operator<<(ostream &os, const Exception::ExceptionCode code)
{
switch(code)
{
case Exception::General: return os << "General";
case Exception::NetworkError: return os << "NetworkError";
case Exception::HostNotFound: return os << "HostNotFound";
case Exception::InvalidUrl: return os << "InvalidUrl";
case Exception::CertificateIssuerMissing: return os << "CertificateIssuerMissing";
case Exception::CertificateRevoked: return os << "CertificateRevoked";
case Exception::CertificateUnknown: return os << "CertificateUnknown";
case Exception::OCSPBeforeTimeStamp: return os << "OCSPBeforeTimeStamp";
case Exception::OCSPResponderMissing: return os << "OCSPResponderMissing";
case Exception::OCSPCertMissing: return os << "OCSPCertMissing";
case Exception::OCSPTimeSlot: return os << "OCSPTimeSlot";
case Exception::OCSPRequestUnauthorized: return os << "OCSPRequestUnauthorized";
case Exception::TSForbidden: return os << "TSForbidden";
case Exception::TSTooManyRequests: return os << "TSTooManyRequests";
case Exception::PINCanceled: return os << "PINCanceled";
case Exception::PINFailed: return os << "PINFailed";
case Exception::PINIncorrect: return os << "PINIncorrect";
case Exception::PINLocked: return os << "PINLocked";
case Exception::ReferenceDigestWeak: return os << "ReferenceDigestWeak";
case Exception::SignatureDigestWeak: return os << "SignatureDigestWeak";
case Exception::DataFileNameSpaceWarning: return os << "DataFileNameSpaceWarning";
case Exception::IssuerNameSpaceWarning: return os << "IssuerNameSpaceWarning";
case Exception::ProducedATLateWarning: return os << "ProducedATLateWarning";
case Exception::MimeTypeWarning: return os << "MimeTypeWarning";
case Exception::DDocError: return os << "DDocError";
}
return os;
}
static ostream &operator<<(ostream &os, const Exception &e)
{
os << e.file() << ":" << e.line() << " code(" << e.code() << ") " << e.msg() << endl;
for(const Exception &ex: e.causes())
os << ex;
return os;
}
static ostream &operator<<(ostream &os, Signature::Validator::Status status)
{
switch(status)
{
case Signature::Validator::Valid: return os;
case Signature::Validator::Warning: return os << "(Warning)";
case Signature::Validator::NonQSCD: return os << "(NonQSCD)";
case Signature::Validator::Test:
case Signature::Validator::Unknown: return os << "(Unknown)";
case Signature::Validator::Invalid: return os << "(Invalid)";
}
return os;
}
static ostream &endl(ostream &os)
{
return os.put('\n');
}
}
/**
* For demonstration purpose overwrites certificate selection to print out all
* the certificates available on ID-Card.
*/
class ConsolePinSigner final : public PKCS11Signer
{
public:
ConsolePinSigner(const string &driver, const string &pin)
: PKCS11Signer(driver)
{
setPin(pin);
}
private:
string pin(const X509Cert &certificate) const final;
X509Cert selectSigningCertificate(const vector<X509Cert> &certificates) const final
{
cout << "Available certificates:" << endl;
for(const X509Cert &cert: certificates)
cout << " label: " << cert << endl;
cout << "Selected:" << endl;
X509Cert cert = certificates.front();
cout << " label: " << cert << endl;
return cert;
}
};
string ConsolePinSigner::pin(const X509Cert &certificate) const
{
if(!PKCS11Signer::pin(certificate).empty())
return PKCS11Signer::pin(certificate);
char pin[16];
size_t pinMax = 16;
const char *prompt = "Please enter PIN for token '%s' or <enter> to cancel: ";
#if defined(_WIN32)
// something that acts wildly similarily with getpass()
{
printf(prompt, certificate.subjectName("CN").c_str());
size_t i = 0;
int c;
while ( (c = _getch()) != '\r' )
{
switch ( c )
{
default:
if ( i >= pinMax-1 || iscntrl( c ) )
{
// can't be a part of password
fputc( '\a', stdout );
break;
}
pin[i++] = static_cast<char>(c);
fputc( '*', stdout );
break;
case EOF:
{
fputs( "[EOF]\n", stdout );
Exception e(EXCEPTION_PARAMS("PIN acquisition canceled with [EOF]."));
e.setCode( Exception::PINCanceled );
throw e;
}
case 0:
case 0xE0: // FN Keys (0 or E0) start of two-character FN code
c = ( c << 4 ) | _getch();
if ( c != 0xE53 && c != 0xE4B && c != 0x053 && c != 0x04b )
{
// not {DELETE}, {<--}, Num{DEL} and Num{<--}
fputc( '\a', stdout );
break;
}
// NO BREAK, fall through to the one-character deletes
case '\b':
case 127:
if ( i == 0 )
{
// nothing to delete
fputc( '\a', stdout );
break;
}
pin[--i] = '\0';
fputs( "\b \b", stdout );
break;
case 3: // CTRL+C
{
fputs( "^C\n", stdout );
Exception e(EXCEPTION_PARAMS("PIN acquisition canceled with ^C."));
e.setCode( Exception::PINCanceled );
throw e;
}
case 26: // CTRL+Z
{
fputs( "^Z\n", stdout );
Exception e(EXCEPTION_PARAMS("PIN acquisition canceled with ^Z."));
e.setCode( Exception::PINCanceled );
throw e;
}
case 27: // ESC
fputc('\n', stdout );
printf(prompt, certificate.subjectName("CN").c_str());
i = 0;
break;
}
}
fputc( '\n', stdout );
pin[i] = '\0';
}
#else
char* pwd = getpass(Log::format(prompt, certificate.subjectName("CN").c_str()).c_str());
strncpy(pin, pwd, pinMax);
#endif
pin[pinMax-1] = '\0';
string result(pin);
if(result.empty())
{
Exception e(EXCEPTION_PARAMS("PIN acquisition canceled."));
e.setCode( Exception::PINCanceled );
throw e;
}
return result;
}
struct value: public string_view {
using string_view::string_view;
using string_view::operator=;
constexpr value(string_view arg, string_view param) noexcept
: string_view(arg.size() > param.size() && arg.starts_with(param) ?
arg.substr(param.size()) : string_view{})
{}
operator string() const
{
return {begin(), end()};
}
constexpr operator bool() const noexcept
{
return !empty();
}
};
class ToolConfig final: public XmlConfCurrent
{
public:
enum Warning : uint8_t {
WError,
WWarning,
WIgnore
};
ToolConfig(int argc, char *argv[]);
int logLevel() const final { return _logLevel.value_or(XmlConfCurrent::logLevel()); }
string logFile() const final { return _logFile.value_or(XmlConfCurrent::logFile()); }
string digestUri() const final { return uri.value_or(XmlConfCurrent::digestUri()); }
string signatureDigestUri() const final { return siguri.value_or(XmlConfCurrent::signatureDigestUri()); }
bool TSLAllowExpired() const final { return expired.value_or(XmlConfCurrent::TSLAllowExpired()); }
vector<X509Cert> TSLCerts() const final { return tslcerts.value_or(XmlConfCurrent::TSLCerts()); }
string TSUrl() const final { return tsurl.value_or(XmlConfCurrent::TSUrl()); }
string TSLUrl() const final { return tslurl.value_or(XmlConfCurrent::TSLUrl()); }
unique_ptr<Signer> getSigner(bool getwebsigner = false) const;
// Config
optional<int> _logLevel;
optional<bool> expired;
optional<vector<X509Cert>> tslcerts;
optional<string> _logFile;
optional<string> tsurl;
optional<string> tslurl;
optional<string> uri;
optional<string> siguri;
// Signer
value profile, city, street, state, postalCode, country, userAgent;
vector<string> roles;
bool XAdESEN = false;
// Token
optional<bool> rsaPss;
value pkcs11, pkcs12, pin, cert;
vector<unsigned char> thumbprint;
bool cng = true, selectFirst = false;
// Params
value path;
unordered_map<value,value,std::hash<string_view>> files;
bool doSign = true, dontValidate = false;
static string_view RED, GREEN, YELLOW, RESET;
};
/**
* Prints application usage.
*/
static int printUsage(const char *executable)
{
cout
<< "Usage: " << executable << " COMMAND [OPTIONS] FILE" << endl << endl
<< " Command create:" << endl
<< " Example: " << executable << " create --file=file1.txt --file=file2.txt demo-container.asice" << endl
<< " Available options:" << endl
<< " --file= - File(s) to be signed. The option can occur multiple times." << endl
<< " --mime= - Specifies the file's mime-type value. When used then must be written right " << endl
<< " after the \"-file\" parameter. Default value is application/octet-stream" << endl
<< " --dontsign - Don't sign the newly created container." << endl
<< " for additional options look sign command" << endl << endl
<< " Command createBatch:" << endl
<< " Example: " << executable << " createBatch folder/content/to/sign" << endl
<< " Available options:" << endl
<< " for additional options look sign command" << endl << endl
<< " Command open:" << endl
<< " Example: " << executable << " open container-file.asice" << endl
<< " Available options:" << endl
<< " --warnings=(ignore,warning,error) - warning handling (default warning)" << endl
<< " --extractAll[=path] - extracts documents without validating signatures (to path when provided)" << endl
<< " --validateOnExtract - validates container before extracting files" << endl << endl
<< " --offline - open container offline (eg. Don't send to SiVa)" << endl << endl
<< " Command add:" << endl
<< " Example: " << executable << " add --file=file1.txt container-file.asice" << endl
<< " Available options:" << endl
<< " --file and --mime look create command for info" << endl << endl
<< " Command remove:" << endl
<< " Example: " << executable << " remove --document=0 --document=1 --signature=1 container-file.asice" << endl
<< " Available options:" << endl
<< " --document= - documents to remove" << endl
<< " --signature= - signatures to remove" << endl << endl
<< " Command websign:" << endl
<< " Example: " << executable << " websign --cert=signer.crt demo-container.asice" << endl
<< " Available options:" << endl
<< " --cert= - signer token certificate" << endl
<< " for additional options look sign command" << endl << endl
<< " Command sign:" << endl
<< " Example: " << executable << " sign demo-container.asice" << endl
<< " Available options:" << endl
<< " --profile= - signature profile, TS, TSA, time-stamp, time-stamp-archive, TimeStampToken, time-stamp-token" << endl
<< " --XAdESEN - use XAdES EN profile" << endl
<< " --city= - city of production place" << endl
<< " --street= - streetAddress of production place in XAdES EN profile" << endl
<< " --state= - state of production place" << endl
<< " --postalCode= - postalCode of production place" << endl
<< " --country= - country of production place" << endl
<< " --role= - option can occur multiple times. Signer role(s)" << endl
#ifdef _WIN32
<< " --cng - Use CNG api for signing under windows." << endl
<< " --selectFirst - Select first certificate in store." << endl
<< " --thumbprint= - Select certificate in store with specified thumbprint (HEX)." << endl
#endif
<< " --pkcs11[=] - default is " << CONF(PKCS11Driver) << ". Path of PKCS11 driver." << endl
<< " --pkcs12= - pkcs12 signer certificate (use --pin for password)" << endl
<< " --pin= - default asks pin from prompt" << endl
<< " --sha(224,256,384,512) - set default digest method (default sha256)" << endl
<< " --sigsha(224,256,384,512) - set default digest method (default sha256)" << endl
<< " --sigpsssha(224,256,384,512) - set default digest method using RSA PSS (default sha256, same as --sigsha* with --rsapss)" << endl
<< " --rsapkcs15 - Use RSA PKCS1.5 padding" << endl
<< " --rsapss - Use RSA PSS padding" << endl
<< " --tsurl - option to change TS URL (default " << CONF(TSUrl) << ")" << endl
<< " --dontValidate - Don't validate container on signature creation" << endl << endl
<< " --userAgent - Additional info info that is sent to TSA or OCSP service" << endl << endl
<< " All commands:" << endl
<< " --nocolor - Disable terminal colors" << endl
<< " --loglevel=[0,1,2,3,4] - Log level 0 - none, 1 - error, 2 - warning, 3 - info, 4 - debug" << endl
<< " --logfile= - File to log, empty to console" << endl;
return EXIT_FAILURE;
}
string_view ToolConfig::RED = "\033[31m";
string_view ToolConfig::GREEN = "\033[32m";
string_view ToolConfig::YELLOW = "\033[33m";
string_view ToolConfig::RESET = "\033[0m";
ToolConfig::ToolConfig(int argc, char *argv[])
{
for(int i = 2; i < argc; i++)
{
string_view arg(argv[i]);
if(value v{arg, "--profile="}) profile = v;
else if(value v{arg, "--file="})
{
value mime(i+1 < argc ? argv[i+1] : string_view(), "--mime=");
files.emplace(v, mime ? mime : "application/octet-stream");
}
#ifdef _WIN32
else if(arg == "--cng") cng = true;
else if(arg == "--selectFirst") selectFirst = true;
else if(value v{arg, "--thumbprint="}) thumbprint = File::hexToBin(v);
#endif
else if(arg == "--pkcs11")
cng = false;
else if(value v{arg, "--pkcs11="})
{
cng = false;
pkcs11 = v;
}
else if(value v{arg, "--pkcs12="})
{
cng = false;
pkcs12 = v;
}
else if(arg == "--dontValidate") dontValidate = true;
else if(arg == "--XAdESEN") XAdESEN = true;
else if(value v{arg, "--pin="}) pin = v;
else if(value v{arg, "--cert="}) cert = v;
else if(value v{arg, "--city="}) city = v;
else if(value v{arg, "--street="}) street = v;
else if(value v{arg, "--state="}) state = v;
else if(value v{arg, "--postalCode="}) postalCode = v;
else if(value v{arg, "--country="}) country = v;
else if(value v{arg, "--role="}) roles.emplace_back(v);
else if(arg == "--sha224") uri = URI_SHA224;
else if(arg == "--sha256") uri = URI_SHA256;
else if(arg == "--sha384") uri = URI_SHA384;
else if(arg == "--sha512") uri = URI_SHA512;
else if(arg == "--sigsha224") siguri = URI_SHA224;
else if(arg == "--sigsha256") siguri = URI_SHA256;
else if(arg == "--sigsha384") siguri = URI_SHA384;
else if(arg == "--sigsha512") siguri = URI_SHA512;
else if(arg == "--sigpsssha224") { siguri = URI_SHA224; rsaPss = true; }
else if(arg == "--sigpsssha256") { siguri = URI_SHA256; rsaPss = true; }
else if(arg == "--sigpsssha384") { siguri = URI_SHA384; rsaPss = true; }
else if(arg == "--sigpsssha512") { siguri = URI_SHA512; rsaPss = true; }
else if(arg == "--rsapkcs15") rsaPss = false;
else if(arg == "--rsapss") rsaPss = true;
else if(value v{arg, "--tsurl="}) tsurl = v;
else if(value v{arg, "--tslurl="}) tslurl = v;
else if(value v{arg, "--tslcert="}) tslcerts = vector<X509Cert>{ X509Cert(v) };
else if(arg == "--TSLAllowExpired") expired = true;
else if(arg == "--dontsign") doSign = false;
else if(arg == "--nocolor") RED = GREEN = YELLOW = RESET = {};
else if(value v{arg, "--loglevel="}) _logLevel = atoi(v.data());
else if(value v{arg, "--logfile="}) _logFile = v;
else path = arg;
}
}
/**
* Create Signer object from Params.
*
* @param getwebsigner get WebSigner object
* @return Signer
*/
unique_ptr<Signer> ToolConfig::getSigner(bool getwebsigner) const
{
unique_ptr<Signer> signer;
if(getwebsigner)
{
class WebSigner final: public Signer
{
public:
explicit WebSigner(X509Cert cert): _cert(std::move(cert)) {}
X509Cert cert() const final { return _cert; }
vector<unsigned char> sign(const string & /*method*/, const vector<unsigned char> & /*digest*/) const final
{
THROW("Not implemented");
}
X509Cert _cert;
};
signer = make_unique<WebSigner>(X509Cert(cert, X509Cert::Pem));
}
#ifdef _WIN32
else if(cng)
{
auto win = make_unique<WinSigner>(pin, selectFirst);
win->setThumbprint(thumbprint);
signer = std::move(win);
}
#endif
else if(!pkcs12.empty())
signer = make_unique<PKCS12Signer>(pkcs12, pin);
else
signer = make_unique<ConsolePinSigner>(pkcs11, pin);
signer->setENProfile(XAdESEN);
signer->setSignatureProductionPlaceV2(city, street, state, postalCode, country);
signer->setSignerRoles(roles);
signer->setProfile(profile);
signer->setUserAgent(userAgent);
if(rsaPss.has_value())
signer->setMethod(rsaPss.value() ? Digest::toRsaPssUri(signatureDigestUri()) : Digest::toRsaUri(signatureDigestUri()));
return signer;
}
/**
* Validate signature.
*
* @param signature Signature to validated
* @return EXIT_FAILURE (1) - failure, EXIT_SUCCESS (0) - success
*/
static int validateSignature(const Signature *s, ToolConfig::Warning warning = ToolConfig::WWarning)
{
int returnCode = EXIT_SUCCESS;
Signature::Validator v(s);
cout << " Validation: ";
switch (v.status()) {
case Signature::Validator::Valid:
cout << ToolConfig::GREEN << "OK";
break;
case Signature::Validator::Warning:
case Signature::Validator::NonQSCD:
if(warning != ToolConfig::WError)
{
cout << ToolConfig::YELLOW << "OK " << v.status();
break;
}
[[fallthrough]];
case Signature::Validator::Test:
case Signature::Validator::Unknown:
case Signature::Validator::Invalid:
cout << ToolConfig::RED << "FAILED " << v.status();
returnCode = EXIT_FAILURE;
break;
}
cout << ToolConfig::RESET << endl;
if(!v.warnings().empty() && warning != ToolConfig::WIgnore)
{
cout << " Warnings: " << ToolConfig::YELLOW;
for(Exception::ExceptionCode code: v.warnings())
cout << code;
cout << ToolConfig::RESET << endl;
}
if(!v.diagnostics().empty())
cout << " Exception:" << endl << v.diagnostics() << endl;
return returnCode;
}
/**
* Open container
*
* @param argc number of command line arguments.
* @param argv command line arguments.
* @return EXIT_FAILURE (1) - failure, EXIT_SUCCESS (0) - success
*/
static int open(int argc, char* argv[])
{
ToolConfig::Warning reportwarnings = ToolConfig::WWarning;
value path;
fs::path extractPath;
bool validateOnExtract = false;
int returnCode = EXIT_SUCCESS;
struct OpenCB final: public ContainerOpenCB
{
bool online = true;
bool validateOnline() const final { return online; }
} cb;
// Parse command line arguments.
for(int i = 2; i < argc; i++)
{
string_view arg(argv[i]);
if(arg == "--list")
continue;
if(value v{arg, "--warnings="})
{
if(v == "ignore") reportwarnings = ToolConfig::WIgnore;
if(v == "error") reportwarnings = ToolConfig::WError;
}
else if(arg == "--extractAll")
extractPath = fs::current_path();
else if(value v{arg, "--extractAll="})
{
extractPath = fs::absolute(fs::path(v.begin(), v.end()));
if(!fs::is_directory(extractPath))
THROW("Path is not directory");
}
else if(arg == "--validateOnExtract")
validateOnExtract = true;
else if(arg == "--offline")
cb.online = false;
else
path = arg;
}
if(path.empty())
return printUsage(argv[0]);
unique_ptr<Container> doc;
try {
doc = Container::openPtr(path, &cb);
} catch(const Exception &e) {
cout << "Failed to parse container" << endl;
cout << " Exception:" << endl << e;
return EXIT_FAILURE;
}
auto extractFiles = [&doc,extractPath]() {
cout << "Extracting documents: " << endl;
for(const DataFile *file: doc->dataFiles())
{
try {
auto dst = (extractPath / fs::path(file->fileName()).filename());
file->saveAs(dst.string());
cout << " Document(" << file->mediaType() << ") extracted to " << dst << " (" << file->fileSize() << " bytes)" << endl;
} catch(const Exception &e) {
cout << " Document " << file->fileName() << " extraction: " << ToolConfig::RED << "FAILED" << ToolConfig::RESET << endl;
cout << " Exception:" << endl << e;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
};
if(!extractPath.empty() && !validateOnExtract)
return extractFiles();
cout << "Container file: " << path << endl;
cout << "Container type: " << doc->mediaType() << endl;
// Print container document list.
cout << "Documents (" << doc->dataFiles().size() << "):\n" << endl;
for(const DataFile *file: doc->dataFiles())
{
cout << " Document (" << file->mediaType() << "): " << file->fileName()
<< " (" << file->fileSize() << " bytes)" << endl;
}
// Print container signatures list.
cout << endl << "Signatures (" << doc->signatures().size() << "):" << endl;
unsigned int pos = 0;
for(const Signature *s: doc->signatures())
{
cout << " Signature " << pos++ << " (" << s->profile().c_str() << "):" << endl
<< " ID: " << s->id() << endl;
// Validate signature. Checks, whether signature format is correct
// and signed documents checksums are correct.
if(validateSignature(s, reportwarnings) == EXIT_FAILURE)
returnCode = EXIT_FAILURE;
// Get signature production place info.
if(!s->city().empty() || !s->stateOrProvince().empty() || !s->streetAddress().empty() || !s->postalCode().empty() || !s->countryName().empty())
{
cout << " Signature production place:" << endl
<< " City: " << s->city() << endl
<< " State or Province: " << s->stateOrProvince() << endl
<< " Street address: " << s->streetAddress() << endl
<< " Postal code: " << s->postalCode() << endl
<< " Country: " << s->countryName() << endl;
}
// Get signer role info.
vector<string> roles = s->signerRoles();
if(!roles.empty())
{
cout << " Signer role(s):" << endl;
for(const string &role : roles)
cout << " " << role << endl;
}
vector<unsigned char> msgImprint = s->messageImprint();
cout << " EPES policy: " << s->policy() << endl
<< " SPUri: " << s->SPUri() << endl
<< " Signature method: " << s->signatureMethod() << endl
<< " Signing time: " << s->claimedSigningTime() << endl
<< " Signing cert: " << s->signingCertificate() << endl
<< " Signed by: " << s->signedBy() << endl
<< " Produced At: " << s->OCSPProducedAt() << endl
<< " OCSP Responder: " << s->OCSPCertificate() << endl
<< " Message imprint (" << msgImprint.size() << "): " << msgImprint << endl
<< " TS: " << s->TimeStampCertificate() << endl
<< " TS time: " << s->TimeStampTime() << endl
<< " TSA: " << s->ArchiveTimeStampCertificate() << endl
<< " TSA time: " << s->ArchiveTimeStampTime() << endl;
}
if(returnCode == EXIT_SUCCESS && !extractPath.empty())
return extractFiles();
return returnCode;
}
/**
* Remove items from container.
*
* @param argc number of command line arguments.
* @param argv command line arguments.
* @return EXIT_FAILURE (1) - failure, EXIT_SUCCESS (0) - success
*/
static int remove(int argc, char *argv[])
{
vector<unsigned int> documents, signatures;
value path;
for(int i = 2; i < argc; i++)
{
string_view arg(argv[i]);
if(value v{arg, "--document="})
documents.push_back(unsigned(atoi(v.data())));
else if(value v{arg, "--signature="})
signatures.push_back(unsigned(atoi(v.data())));
else
path = arg;
}
if(path.empty())
return printUsage(argv[0]);
unique_ptr<Container> doc;
try {
doc = Container::openPtr(path);
} catch(const Exception &e) {
cout << "Failed to parse container" << endl;
cout << " Exception:" << endl << e;
return EXIT_FAILURE;
}
sort(signatures.begin(), signatures.end(), greater<unsigned int>());
for(unsigned int i : signatures)
{
cout << " Removing signature " << i << endl;
doc->removeSignature(i);
}
sort(documents.begin(), documents.end(), greater<unsigned int>());
for(unsigned int i : documents)
{
cout << " Removing document " << i << endl;
doc->removeDataFile(i);
}
doc->save();
return EXIT_SUCCESS;
}
/**
* Add items to the container.
*
* @param p ToolConfig object.
* @param program command line argument.
* @return EXIT_FAILURE (1) - failure, EXIT_SUCCESS (0) - success
*/
static int add(const ToolConfig &p, const char *program)
{
if(p.path.empty() || p.files.empty())
return printUsage(program);
unique_ptr<Container> doc;
try {
doc = Container::openPtr(p.path);
} catch(const Exception &e) {
cout << "Failed to parse container" << endl;
cout << " Exception:" << endl << e;
return EXIT_FAILURE;
}
for(const auto &[file, mime]: p.files)
doc->addDataFile(file, mime);
doc->save();
return EXIT_SUCCESS;
}
/**
* Sign the container.
*
* @param doc the container that is to be signed
* @param signer Signer to used for sign
* @param dontValidate Do not validate result
* @return EXIT_FAILURE (1) - failure, EXIT_SUCCESS (0) - success
*/
static int signContainer(Container *doc, const unique_ptr<Signer> &signer, bool dontValidate = false)
{
if(Signature *signature = doc->sign(signer.get()))
return dontValidate ? EXIT_SUCCESS : validateSignature(signature);
return EXIT_FAILURE;
}
/**
* Create new container and sign unless explicitly requested not to sign
*
* @param p ToolConfig object.
* @param program command line argument.
* @return EXIT_FAILURE (1) - failure, EXIT_SUCCESS (0) - success
*/
static int create(const ToolConfig &p, const char *program)
{
if(p.path.empty() || p.files.empty())
return printUsage(program);
unique_ptr<Container> doc;
try {
doc = Container::createPtr(p.path);
} catch(const Exception &e) {
cout << "Failed to parse container" << endl;
cout << " Exception:" << endl << e;
return EXIT_FAILURE;
}
for(const auto &[file, mime]: p.files)
doc->addDataFile(file, mime);
int returnCode = EXIT_SUCCESS;
if(p.doSign)
returnCode = signContainer(doc.get(), p.getSigner(), p.dontValidate);
doc->save();
return returnCode;
}
/**
* Create new container.
*
* @param p ToolConfig object
* @param program command line argument.
* @return EXIT_FAILURE (1) - failure, EXIT_SUCCESS (0) - success
*/
static int createBatch(const ToolConfig &p, const char *program)
{
if(p.path.empty())
return printUsage(program);
unique_ptr<Signer> signer = p.getSigner();
error_code ec;
int returnCode = EXIT_SUCCESS;
for(const auto &file: fs::directory_iterator(string_view(p.path), ec))
{
if(!fs::is_regular_file(file.status()) || file.path().extension() == ".asice")
continue;
const auto path = file.path().string();
cout << "Signing file: " << path << endl;
try {
unique_ptr<Container> doc = Container::createPtr(path + ".asice");
doc->addDataFile(path, "application/octet-stream");
if(signContainer(doc.get(), signer, p.dontValidate) == EXIT_FAILURE)
returnCode = EXIT_FAILURE;
doc->save();
} catch(const Exception &e) {
cout << " Exception:" << endl << e;
returnCode = EXIT_FAILURE;
}
}
if(ec)
{
cout << "Failed to open directory " << p.path << endl;
cout << " Exception: " << ec.message() << endl;
return EXIT_FAILURE;
}
return returnCode;
}
/**
* Sign container.
*
* @param p ToolConfig object.
* @param program command line argument.
* @return EXIT_FAILURE (1) - failure, EXIT_SUCCESS (0) - success
*/
static int sign(const ToolConfig &p, const char *program)
{
if(p.path.empty())
return printUsage(program);
unique_ptr<Container> doc;
try {
doc = Container::openPtr(p.path);
} catch(const Exception &e) {
cout << "Failed to parse container" << endl;
cout << " Exception:" << endl << e;
return EXIT_FAILURE;
}
int returnCode = signContainer(doc.get(), p.getSigner(), p.dontValidate);
doc->save();
return returnCode;
}
static int websign(const ToolConfig &p, const char *program)
{
if(p.path.empty())
return printUsage(program);
unique_ptr<Container> doc;
try {
doc = Container::createPtr(p.path);
} catch(const Exception &e) {
cout << "Failed to parse container" << endl;
cout << " Exception:" << endl << e;
return EXIT_FAILURE;
}
for(const auto &[file, mime]: p.files)
doc->addDataFile(file, mime);
int returnCode = EXIT_FAILURE;
if(auto signer = p.getSigner(true);
Signature *signature = doc->prepareSignature(signer.get()))
{
cout << "Signature method: " << signature->signatureMethod() << endl
<< "Digest to sign: " << signature->dataToSign() << endl
<< "Please enter signed digest in hex: " << endl;
string signedData;
cin >> signedData;
signature->setSignatureValue(File::hexToBin(signedData));
signature->extendSignatureProfile(signer.get());
returnCode = validateSignature(signature);
}
doc->save();
return returnCode;
}
static int tslcmd(int /*argc*/, char* /*argv*/[])
{
int returnCode = EXIT_SUCCESS;
string cache = CONF(TSLCache);
auto certs = CONF(TSLCerts);
for(const X509Cert &cert: certs)
cout << "Signer: " << cert << endl;
TSL t(File::path(cache, File::fileName(CONF(TSLUrl))));
cout << "TSL: " << t.url() << endl
<< " Type: " << t.type() << endl
<< " Territory: " << t.territory() << endl
<< " Operator: " << t.operatorName() << endl
<< " Sequence: " << t.sequenceNumber() << endl
<< " Issued: " << t.issueDate() << endl
<< " Next update: " << t.nextUpdate() << endl;
try {
cout << " Signature: ";
t.validate(certs);
cout << ToolConfig::GREEN << "VALID" << ToolConfig::RESET << endl;
} catch(const Exception &e) {
cout << ToolConfig::RED << "INVALID" << ToolConfig::RESET << endl;
cout << "Caught Exception:" << endl << e;
returnCode = EXIT_FAILURE;
}
for(const TSL::Service &s: t.services())
{
cout << " Service: " << s.name << endl;
for(const X509Cert &x: s.certs)
cout << " Cert: " << x << endl;
}
cout << "Pointers: " << endl;
for(const TSL::Pointer &p: t.pointers())
{
cout << " Pointer: " << p.territory << endl
<< " Url: " << p.location << endl;
for(const X509Cert &cert: p.certs)
cout << " Signer: " << cert << endl;
string path = cache + "/" + p.territory + ".xml";
if(error_code ec; !fs::exists(fs::path(path), ec))
{
cout << " TSL: missing" << endl;
continue;
}
TSL tp(std::move(path));
cout << " TSL: " << p.location << endl
<< " Type: " << tp.type() << endl
<< " Territory: " << tp.territory() << endl
<< " Operator: " << tp.operatorName() << endl
<< " Sequence: " << tp.sequenceNumber() << endl
<< " Issued: " << tp.issueDate() << endl
<< " Next update: " << tp.nextUpdate() << endl;
try {
cout << " Signature: ";
tp.validate(p.certs);
cout << ToolConfig::GREEN << "VALID" << ToolConfig::RESET << endl;
} catch(const Exception &e) {
cout << ToolConfig::RED << "INVALID" << ToolConfig::RESET << endl;
cout << "Caught Exception:" << endl << e;
returnCode = EXIT_FAILURE;
}
for(const TSL::Service &s: tp.services())
{
cout << " Service: " << s.name << endl
<< " Type: " << s.type << endl;
for(const X509Cert &x: s.certs)
cout << " Cert: " << x << endl;
}
}
return returnCode;
}
/**
* Executes digidoc demonstration application.
*