@@ -34,47 +34,51 @@ using namespace std;
34
34
/* *
35
35
* Initialize ASiCS container.
36
36
*/
37
- ASiC_S::ASiC_S (): ASiContainer(MIMETYPE_ASIC_S)
37
+ ASiC_S::ASiC_S ()
38
+ : ASiContainer(MIMETYPE_ASIC_S)
38
39
{}
39
40
40
41
/* *
41
42
* Opens ASiC-S container from a file
42
43
*/
43
- ASiC_S::ASiC_S (const string &path): ASiContainer(MIMETYPE_ASIC_S)
44
+ ASiC_S::ASiC_S (const string &path)
45
+ : ASiContainer(MIMETYPE_ASIC_S)
44
46
{
45
47
auto z = load (path, false , {mediaType ()});
46
- static const string_view metaInf = " META-INF/" ;
48
+ auto starts_with = [](string_view str, string_view needle) constexpr {
49
+ return str.size () >= needle.size () && str.compare (0 , needle.size (), needle) == 0 ;
50
+ };
47
51
48
52
for (const string &file: z.list ())
49
53
{
50
- if (file == " mimetype" ||
51
- (metaInf.size () < file.size () && file.compare (0 , metaInf.size (), metaInf) == 0 ))
52
- {
53
- if (file == " META-INF/timestamp.tst" )
54
- {
55
- if (!signatures ().empty ())
56
- THROW (" Can not add signature to ASiC-S container which already contains a signature." );
57
- addSignature (make_unique<SignatureTST>(z.extract <stringstream>(file).str (), this ));
58
- }
59
- if (file == " META-INF/signatures.xml" )
60
- {
61
- if (!signatures ().empty ())
62
- THROW (" Can not add signature to ASiC-S container which already contains a signature." );
63
- auto data = z.extract <stringstream>(file);
64
- auto signatures = make_shared<Signatures>(data, mediaType ());
65
- for (auto s = signatures->signature (); s; s++)
66
- addSignature (make_unique<SignatureXAdES_LTA>(signatures, s, this ));
67
- }
54
+ if (file == " mimetype" )
68
55
continue ;
56
+ if (file == " META-INF/timestamp.tst" )
57
+ {
58
+ if (!signatures ().empty ())
59
+ THROW (" Can not add signature to ASiC-S container which already contains a signature." );
60
+ addSignature (make_unique<SignatureTST>(z.extract <stringstream>(file).str (), this ));
69
61
}
70
-
71
- const auto directory = File::directory (file);
72
- if (directory.empty () || directory == " /" || directory == " ./" )
62
+ else if (file == " META-INF/signatures.xml" )
73
63
{
74
- if (!dataFiles ().empty ())
75
- THROW (" Can not add document to ASiC-S container which already contains a document." );
76
- addDataFile (dataStream (file, z), file, " application/octet-stream" );
64
+ if (!signatures ().empty ())
65
+ THROW (" Can not add signature to ASiC-S container which already contains a signature." );
66
+ auto data = z.extract <stringstream>(file);
67
+ auto signatures = make_shared<Signatures>(data, mediaType ());
68
+ for (auto s = signatures->signature (); s; s++)
69
+ addSignature (make_unique<SignatureXAdES_LTA>(signatures, s, this ));
77
70
}
71
+ else if (file == " META-INF/ASiCArchiveManifest.xml" )
72
+ THROW (" ASiCArchiveManifest are not supported." );
73
+ else if (starts_with (file, " META-INF/" ))
74
+ continue ;
75
+ else if (const auto directory = File::directory (file);
76
+ !directory.empty () && directory != " /" && directory != " ./" )
77
+ THROW (" Subfolders are not supported %s" , directory.c_str ());
78
+ else if (!dataFiles ().empty ())
79
+ THROW (" Can not add document to ASiC-S container which already contains a document." );
80
+ else
81
+ addDataFile (dataStream (file, z), file, " application/octet-stream" );
78
82
}
79
83
80
84
if (dataFiles ().empty ())
@@ -83,11 +87,6 @@ ASiC_S::ASiC_S(const string &path): ASiContainer(MIMETYPE_ASIC_S)
83
87
THROW (" ASiC-S container does not contain any signatures." );
84
88
}
85
89
86
- void ASiC_S::save (const string & /* path*/ )
87
- {
88
- THROW (" Not implemented." );
89
- }
90
-
91
90
unique_ptr<Container> ASiC_S::createInternal (const string & /* path*/ )
92
91
{
93
92
return {};
@@ -98,6 +97,12 @@ void ASiC_S::addAdESSignature(istream & /*signature*/)
98
97
THROW (" Not implemented." );
99
98
}
100
99
100
+ void ASiC_S::canSave ()
101
+ {
102
+ if (auto list = signatures (); !list.empty () && list.front ()->profile () != ASIC_TST_PROFILE)
103
+ THROW (" ASiC-S container supports only saving TimeStampToken signatures." );
104
+ }
105
+
101
106
unique_ptr<Container> ASiC_S::openInternal (const string &path, ContainerOpenCB * /* cb*/ )
102
107
{
103
108
if (!isContainerSimpleFormat (path))
@@ -111,6 +116,14 @@ Signature* ASiC_S::prepareSignature(Signer * /*signer*/)
111
116
THROW (" Not implemented." );
112
117
}
113
118
119
+ void ASiC_S::save (const ZipSerialize &s)
120
+ {
121
+ if (zproperty (" META-INF/manifest.xml" ).size && !createManifest ().save (s.addFile (" META-INF/manifest.xml" , zproperty (" META-INF/manifest.xml" )), true ))
122
+ THROW (" Failed to create manifest XML" );
123
+ if (auto list = signatures (); !list.empty ())
124
+ s.addFile (" META-INF/timestamp.tst" , zproperty (" META-INF/timestamp.tst" ))(static_cast <SignatureTST*>(list.front ())->save ());
125
+ }
126
+
114
127
Signature *ASiC_S::sign (Signer * /* signer*/ )
115
128
{
116
129
THROW (" Not implemented." );
0 commit comments