forked from open-eid/libdigidocpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignatureA.cpp
193 lines (171 loc) · 6.28 KB
/
SignatureA.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
/*
* 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 "SignatureA.h"
#include "BDoc.h"
#include "Conf.h"
#include "DataFile_p.h"
#include "log.h"
#include "crypto/Digest.h"
#include "crypto/TS.h"
#include "crypto/X509Cert.h"
#include "util/DateTime.h"
#include "util/File.h"
#include "xml/XAdESv141.hxx"
#include <xsec/dsig/DSIGConstants.hpp>
using namespace digidoc;
using namespace digidoc::dsig;
using namespace digidoc::util;
using namespace digidoc::util::date;
using namespace digidoc::xades;
using namespace xml_schema;
using namespace std;
static Base64Binary toBase64(const vector<unsigned char> &v)
{
return v.empty() ? Base64Binary() : Base64Binary(v.data(), v.size());
}
SignatureA::SignatureA(unsigned int id, BDoc *bdoc, Signer *signer): SignatureTS(id, bdoc, signer) {}
SignatureA::SignatureA(std::istream &sigdata, BDoc *bdoc): SignatureTS(sigdata, bdoc) {}
SignatureA::~SignatureA() {}
void SignatureA::calcArchiveDigest(Digest *digest) const
{
string signedPropertiesId;
if(qualifyingProperties().signedProperties()->id().present())
signedPropertiesId = "#" + qualifyingProperties().signedProperties()->id().get();
for(const ReferenceType &ref: signature->signedInfo().reference())
{
if(ref.uRI().present() && ref.uRI().get() != signedPropertiesId)
{
for(const DataFile *file: bdoc->dataFiles())
if(file->fileName() == File::fromUriPath(ref.uRI().get()))
static_cast<const DataFilePrivate*>(file)->calcDigest(digest);
}
else
calcDigestOnNode(digest, XADES_NAMESPACE, "SignedProperties");
};
vector<string> list = {"SignedInfo", "SignatureValue", "KeyInfo"};
for(const string &name: list)
{
try {
calcDigestOnNode(digest, URI_ID_DSIG, name);
} catch(const Exception &) {
DEBUG("Element %s not found", name.c_str());
}
}
list = {
"SignatureTimeStamp",
"CounterSignature",
"CompleteCertificateRefs",
"CompleteRevocationRefs",
"AttributeCertificateRefs",
"AttributeRevocationRefs",
"CertificateValues",
"RevocationValues",
"SigAndRefsTimeStamp",
"RefsOnlyTimeStamp" };
for(const string &name: list)
{
try {
calcDigestOnNode(digest, XADES_NAMESPACE, name);
} catch(const Exception &) {
DEBUG("Element %s not found", name.c_str());
}
}
try {
calcDigestOnNode(digest, XADESv141_NAMESPACE, "TimeStampValidationData");
} catch(const Exception &) {
DEBUG("Element TimeStampValidationData not found");
}
//ds:Object
}
void SignatureA::extendSignatureProfile(const std::string &profile)
{
SignatureTS::extendSignatureProfile(profile);
if(profile != BDoc::ASIC_TSA_PROFILE && profile != BDoc::ASIC_TMA_PROFILE)
return;
Digest calc;
calcArchiveDigest(&calc);
TS tsa(CONF(TSUrl), calc, " Profile: " + profile);
xadesv141::ArchiveTimeStampType ts;
ts.id(id() + "-A0");
ts.encapsulatedTimeStamp().push_back(EncapsulatedPKIDataType(toBase64(tsa)));
unsignedSignatureProperties().archiveTimeStampV141().push_back(ts);
unsignedSignatureProperties().contentOrder().push_back(
UnsignedSignaturePropertiesType::ContentOrderType(
UnsignedSignaturePropertiesType::archiveTimeStampV141Id,
unsignedSignatureProperties().archiveTimeStampV141().size() - 1));
sigdata_.clear();
}
vector<unsigned char> SignatureA::tsaBase64() const
{
try {
if(unsignedSignatureProperties().archiveTimeStampV141().empty())
return vector<unsigned char>();
const xadesv141::ArchiveTimeStampType &ts = unsignedSignatureProperties().archiveTimeStampV141().front();
if(ts.encapsulatedTimeStamp().empty())
return vector<unsigned char>();
const GenericTimeStampType::EncapsulatedTimeStampType &bin =
ts.encapsulatedTimeStamp().front();
return vector<unsigned char>(bin.data(), bin.data() + bin.size());
} catch(const Exception &) {}
return vector<unsigned char>();
}
X509Cert SignatureA::ArchiveTimeStampCertificate() const
{
return TS(tsaBase64()).cert();
}
string SignatureA::ArchiveTimeStampTime() const
{
string time = TS(tsaBase64()).time();
if(time.empty())
return time;
tm datetime = ASN1TimeToTM(time);
return xsd2string(makeDateTime(datetime));
}
void SignatureA::validate() const
{
Exception exception(__FILE__, __LINE__, "Signature validation");
try {
SignatureTS::validate();
} catch(const Exception &e) {
for(const Exception &ex: e.causes())
exception.addCause(ex);
}
if(profile().find(BDoc::ASIC_TSA_PROFILE) == string::npos)
{
if(!exception.causes().empty())
throw exception;
return;
}
try {
if(unsignedSignatureProperties().archiveTimeStampV141().empty())
THROW("Missing ArchiveTimeStamp element");
const xadesv141::ArchiveTimeStampType &ts = unsignedSignatureProperties().archiveTimeStampV141().front();
if(ts.encapsulatedTimeStamp().empty())
THROW("Missing EncapsulatedTimeStamp");
const GenericTimeStampType::EncapsulatedTimeStampType &bin = ts.encapsulatedTimeStamp().front();
TS tsa(vector<unsigned char>(bin.data(), bin.data() + bin.size()));
Digest calc(tsa.digestMethod());
calcArchiveDigest(&calc);
tsa.verify(calc);
} catch(const Exception &e) {
exception.addCause(e);
}
if(!exception.causes().empty())
throw exception;
}