-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathASiC_E.cpp
More file actions
371 lines (335 loc) · 13 KB
/
ASiC_E.cpp
File metadata and controls
371 lines (335 loc) · 13 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
/*
* 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 "ASiC_E.h"
#include "Conf.h"
#include "DataFile_p.h"
#include "SignatureXAdES_LTA.h"
#include "crypto/Digest.h"
#include "crypto/Signer.h"
#include "util/File.h"
#include "util/log.h"
#include "util/ZipSerialize.h"
#include "xml/OpenDocument_manifest.hxx"
#include "xml/OpenDocument_manifest_v1_2.hxx"
#include "xml/SecureDOMParser.h"
#include <xercesc/util/OutOfMemoryException.hpp>
#include <set>
using namespace digidoc;
using namespace digidoc::util;
using namespace std;
const string_view ASiC_E::ASIC_TM_PROFILE = "time-mark";
const string_view ASiC_E::ASIC_TS_PROFILE = "time-stamp";
const string_view ASiC_E::ASIC_TSA_PROFILE = "time-stamp-archive";
const string_view ASiC_E::ASIC_TMA_PROFILE = "time-mark-archive";
const string ASiC_E::MANIFEST_NAMESPACE = "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0";
class ASiC_E::Private
{
public:
vector<DataFile*> metadata;
};
/**
* Initialize BDOC container.
*/
ASiC_E::ASiC_E()
: ASiContainer(MIMETYPE_ASIC_E)
, d(make_unique<Private>())
{
}
/**
* Opens BDOC container from a file
*/
ASiC_E::ASiC_E(const string &path)
: ASiContainer(MIMETYPE_ASIC_E)
, d(make_unique<Private>())
{
auto *zip = load(path, true, {MIMETYPE_ASIC_E, MIMETYPE_ADOC});
parseManifestAndLoadFiles(*zip);
}
ASiC_E::~ASiC_E()
{
for_each(d->metadata.cbegin(), d->metadata.cend(), default_delete<DataFile>());
}
vector<DataFile*> ASiC_E::metaFiles() const
{
return d->metadata;
}
/**
* Saves the container using the <code>serializer</code> implementation provided in
* <code>readFrom()</code> method.
*
* @throws Exception is thrown if there was a failure saving BDOC container. For example added
* document does not exist.
* @throws Exception is thrown if ASiC_E class is not correctly initialized.
*/
void ASiC_E::save(const string &path)
{
if(dataFiles().empty())
THROW("Can not save, container is empty.");
if(mediaType() != MIMETYPE_ASIC_E)
THROW("'%s' format is not supported", mediaType().c_str());
if(!path.empty())
zpath(path);
ZipSerialize s(zpath(), true);
stringstream mimetype;
mimetype << mediaType();
s.addFile("mimetype", mimetype, zproperty("mimetype"), ZipSerialize::DontCompress);
stringstream manifest;
createManifest(manifest);
s.addFile("META-INF/manifest.xml", manifest, zproperty("META-INF/manifest.xml"));
for(const DataFile *file: dataFiles())
s.addFile(file->fileName(), *(static_cast<const DataFilePrivate*>(file)->m_is), zproperty(file->fileName()));
std::set<Signatures*> saved;
unsigned int i = 0;
for(Signature *iter: signatures())
{
string file = Log::format("META-INF/signatures%u.xml", i++);
auto *signature = static_cast<SignatureXAdES_B*>(iter);
if(!saved.insert(signature->signatures.get()).second)
continue;
stringstream ofs;
signature->signatures->save(ofs);
s.addFile(file, ofs, zproperty(file));
}
}
unique_ptr<Container> ASiC_E::createInternal(const string &path)
{
DEBUG("ASiC_E::createInternal(%s)", path.c_str());
unique_ptr<ASiC_E> doc = unique_ptr<ASiC_E>(new ASiC_E);
doc->zpath(path);
return doc;
}
/**
* Adds signature to the container. Default profile is TM
*
* @param sigdata signature, which is added to the container.
* @throws Exception throws exception if there are no documents in container.
*/
void ASiC_E::addAdESSignature(istream &data)
{
if(dataFiles().empty())
THROW("No documents in container, can not add signature.");
if(mediaType() != MIMETYPE_ASIC_E)
THROW("'%s' format is not supported", mediaType().c_str());
try
{
auto signatures = make_shared<Signatures>(data, this);
for(size_t i = 0, count = signatures->count(); i < count; ++i)
addSignature(make_unique<SignatureXAdES_LTA>(signatures, i, this));
}
catch(const Exception &e)
{
THROW_CAUSE(e, "Failed to add signature.");
}
}
unique_ptr<Container> ASiC_E::openInternal(const string &path)
{
DEBUG("ASiC_E::openInternal(%s)", path.c_str());
return unique_ptr<Container>(new ASiC_E(path));
}
/**
* Creates BDoc container manifest file and returns its path.
*
* Note: If non-ascii characters are present in XML data, we depend on the LANG variable to be set properly
* (see iconv --list for the list of supported encoding values for libiconv).
*
*
* @return returns created manifest file path.
* @throws Exception exception is thrown if manifest file creation failed.
*/
void ASiC_E::createManifest(ostream &os)
{
DEBUG("ASiC_E::createManifest()");
try
{
manifest_1_2::Manifest manifest(manifest_1_2::Manifest::VersionType::cxx_1_2);
manifest.file_entry().push_back(make_unique<manifest_1_2::File_entry>("/", mediaType()));
for(const DataFile *file: dataFiles())
manifest.file_entry().push_back(make_unique<manifest_1_2::File_entry>(file->fileName(), file->mediaType()));
xml_schema::NamespaceInfomap map;
map["manifest"].name = ASiC_E::MANIFEST_NAMESPACE;
manifest_1_2::manifest(os, manifest, map, {}, xml_schema::Flags::dont_initialize);
if(os.fail())
THROW("Failed to create manifest XML");
}
catch(const xercesc::DOMException &e)
{
try {
string result = xsd::cxx::xml::transcode<char>(e.getMessage());
THROW("Failed to create manifest XML file. Error: %s", result.c_str());
} catch(const xsd::cxx::xml::invalid_utf16_string & /* ex */) {
THROW("Failed to create manifest XML file.");
}
}
catch(const xml_schema::Exception &e)
{
THROW("Failed to create manifest XML file. Error: %s", e.what());
}
}
/**
* Parses manifest file and checks that files described in manifest exist, also
* checks that no extra file do exist that are not described in manifest.xml.
*
* Note: If non-ascii characters are present in XML data, we depend on the LANG variable to be set properly
* (see iconv --list for the list of supported encoding values for libiconv).
*
* @param path directory on disk of the BDOC container.
* @throws Exception exception is thrown if the manifest.xml file parsing failed.
*/
void ASiC_E::parseManifestAndLoadFiles(const ZipSerialize &z)
{
DEBUG("ASiC_E::readManifest()");
const vector<string> &list = z.list();
auto mcount = size_t(count(list.cbegin(), list.cend(), "META-INF/manifest.xml"));
if(mcount < 1)
THROW("Manifest file is missing");
if(mcount > 1)
THROW("Found multiple manifest files");
try
{
unique_ptr<istream> manifestdata = z.stream("META-INF/manifest.xml");
xml_schema::Properties p;
p.schema_location(ASiC_E::MANIFEST_NAMESPACE,
File::fullPathUrl(Conf::instance()->xsdPath() + "/OpenDocument_manifest.xsd"));
unique_ptr<xercesc::DOMDocument> doc = SecureDOMParser(p.schema_location(), true).parseIStream(*manifestdata);
unique_ptr<manifest::Manifest> manifest = manifest::manifest(*doc, {}, p);
set<string> manifestFiles;
bool mimeFound = false;
for(const manifest::File_entry &file: manifest->file_entry())
{
DEBUG("full_path = '%s', media_type = '%s'", file.full_path().c_str(), file.media_type().c_str());
if(manifestFiles.find(file.full_path()) != manifestFiles.end())
THROW("Manifest multiple entries defined for file '%s'.", file.full_path().c_str());
// ODF does not specify that mimetype should be first in manifest
if(file.full_path() == "/")
{
if(mediaType() != file.media_type())
THROW("Manifest has incorrect container media type defined '%s', expecting '%s'.", file.media_type().c_str(), mediaType().c_str());
mimeFound = true;
continue;
}
if(file.full_path().back() == '/') // Skip Directory entries
continue;
auto fcount = size_t(count(list.cbegin(), list.cend(), file.full_path()));
if(fcount < 1)
THROW("File described in manifest '%s' does not exist in container.", file.full_path().c_str());
if(fcount > 1)
THROW("Found multiple references of file '%s' in zip container.", file.full_path().c_str());
manifestFiles.insert(file.full_path());
if(mediaType() == MIMETYPE_ADOC &&
(file.full_path().compare(0, 9, "META-INF/") == 0 ||
file.full_path().compare(0, 9, "metadata/") == 0))
d->metadata.push_back(dataFile(file.full_path(), file.media_type()));
else
addDataFilePrivate(file.full_path(), file.media_type());
}
if(!mimeFound)
THROW("Manifest is missing mediatype file entry.");
for(const string &file: list)
{
/**
* http://www.etsi.org/deliver/etsi_ts/102900_102999/102918/01.03.01_60/ts_102918v010301p.pdf
* 6.2.2 Contents of Container
* 3) The root element of each "*signatures*.xml" content shall be either:
*/
if(file.compare(0, 9, "META-INF/") == 0 &&
file.find("signatures") != string::npos)
{
if(count(list.begin(), list.end(), file) > 1)
THROW("Multiple signature files with same name found '%s'", file.c_str());
try
{
auto signatures = make_shared<Signatures>(*z.stream(file), this);
for(size_t i = 0, count = signatures->count(); i < count; ++i)
addSignature(make_unique<SignatureXAdES_LTA>(signatures, i, this));
}
catch(const Exception &e)
{
THROW_CAUSE(e, "Failed to parse signature '%s'.", file.c_str());
}
continue;
}
if(file == "mimetype" || file.compare(0, 8,"META-INF") == 0)
continue;
if(manifestFiles.find(file) == manifestFiles.end())
THROW("File '%s' found in container is not described in manifest.", file.c_str());
}
}
catch(const xercesc::DOMException &e)
{
try {
string result = xsd::cxx::xml::transcode<char>(e.getMessage());
THROW("Failed to create manifest XML file. Error: %s", result.c_str());
} catch(const xsd::cxx::xml::invalid_utf16_string & /* ex */) {
THROW("Failed to create manifest XML file.");
}
}
catch(const xsd::cxx::xml::invalid_utf16_string &)
{
THROW("Failed to parse manifest XML: %s", Conf::instance()->xsdPath().c_str());
}
catch(const xsd::cxx::xml::properties<char>::argument & /* e */)
{
THROW("Failed to parse manifest XML: %s", Conf::instance()->xsdPath().c_str());
}
catch(const xsd::cxx::tree::unexpected_element<char> &e)
{
THROW("Failed to parse manifest XML: %s %s %s", Conf::instance()->xsdPath().c_str(), e.expected_name().c_str(), e.encountered_name().c_str());
}
catch(const xml_schema::Exception& e)
{
THROW("Failed to parse manifest XML: %s (xsd path: %s)", e.what(), Conf::instance()->xsdPath().c_str());
}
catch(const xercesc::OutOfMemoryException &)
{
THROW("Failed to parse manifest XML: out of memory");
}
catch(const Exception &e)
{
THROW_CAUSE(e, "Failed to parse manifest");
}
catch(...)
{
THROW("Failed to parse manifest XML: Unknown exception");
}
}
Signature* ASiC_E::prepareSignature(Signer *signer)
{
if(mediaType() != MIMETYPE_ASIC_E)
THROW("'%s' format is not supported", mediaType().c_str());
if(dataFiles().empty())
THROW("No documents in container, can not sign container.");
if(!signer)
THROW("Null pointer in ASiC_E::sign");
return addSignature(make_unique<SignatureXAdES_LTA>(newSignatureId(), this, signer));
}
Signature *ASiC_E::sign(Signer* signer)
{
auto *s = static_cast<SignatureXAdES_LTA*>(prepareSignature(signer));
try
{
s->setSignatureValue(signer->sign(s->signatureMethod(), s->dataToSign()));
s->extendSignatureProfile(signer->profile());
}
catch(const Exception& e)
{
deleteSignature(s);
THROW_CAUSE(e, "Failed to sign container.");
}
return s;
}