-
Notifications
You must be signed in to change notification settings - Fork 65
/
XAdESFormChecker.java
290 lines (248 loc) · 9.41 KB
/
XAdESFormChecker.java
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
/*
* XAdES4j - A Java library for generation and verification of XAdES signatures.
* Copyright (C) 2010 Luis Goncalves.
*
* XAdES4j 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 3 of the License, or any later version.
*
* XAdES4j 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 XAdES4j. If not, see <http://www.gnu.org/licenses/>.
*/
package xades4j.verification;
import xades4j.properties.ArchiveTimeStampProperty;
import xades4j.properties.CertificateValuesProperty;
import xades4j.properties.CompleteCertificateRefsProperty;
import xades4j.properties.CompleteRevocationRefsProperty;
import xades4j.properties.RevocationValuesProperty;
import xades4j.properties.SigAndRefsTimeStampProperty;
import xades4j.properties.SignaturePolicyBase;
import xades4j.properties.SignatureTimeStampProperty;
import xades4j.properties.SigningCertificateProperty;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
*
* @author Luís
*/
class XAdESFormChecker
{
private XAdESFormChecker()
{
}
static XAdESForm checkForm(Collection<PropertyInfo> props) throws InvalidXAdESFormException
{
Set<String> availablePropsNames = new HashSet<>();
for (PropertyInfo propInfo : props)
{
availablePropsNames.add(propInfo.getProperty().getName());
}
XAdESFormDesc formDesc = XADES_C_DESC;
do
{
if (formDesc.check(availablePropsNames))
return formDesc.getForm();
} while ((formDesc = formDesc.getPrevious()) != null);
// XAdES G.2.2.1: "The verification process should assess whether the signature
// is a XAdES signature and if so, identify the specific form, by inspecting
// the different qualifying properties present. The verification process
// should not accept as XAdES signature any combination not aligned with
// those established in the normative part of the present document or the
// extended forms defined in the informative annex B."
throw new InvalidXAdESFormException("Signature doesn't follow any of the XAdES forms");
}
static final XAdESFormDesc XADES_BES_DESC = new XAdES_BES_Desc();
static final XAdESFormDesc XADES_EPES_DESC = new XAdES_EPES_Desc();
static final XAdESFormDesc XADES_T_DESC = new XAdES_T_Desc();
static final XAdESFormDesc XADES_C_DESC = new XAdES_C_Desc();
static final XAdESFormDesc XADES_X_DESC = new XAdES_X_Desc();
static final XAdESFormDesc XADES_X_L_DESC = new XAdES_X_L_Desc();
/**************************************************************************/
/**/
private abstract static class XAdESFormDesc
{
private final XAdESFormDesc[] baseForms;
// Ordered from the top format to the lower format.
public XAdESFormDesc(XAdESFormDesc... baseForms)
{
this.baseForms = baseForms;
}
boolean check(Set<String> availablePropsNames) throws InvalidXAdESFormException
{
// Check the properties for the current form.
if (!checkProps(availablePropsNames))
return false;
// If the properties of the current form are available, at least one
// of the base forms has to be well-formed.
if (baseForms.length == 0)
return true;
for (XAdESFormDesc baseForm : baseForms) {
if (baseForm.check(availablePropsNames))
return true;
}
throw new InvalidXAdESFormException(String.format("Required base forms for %s are not present", this.getForm().toString()));
}
XAdESFormDesc getPrevious()
{
return baseForms.length > 0 ? baseForms[0] : null;
}
/**
* Checks the properties for the current form. Should throw an exception
* when the form is malformed.
* @return true if the format specific properties are available; false otherwise
*/
protected abstract boolean checkProps(Set<String> availablePropsNames) throws InvalidXAdESFormException;
abstract XAdESForm getForm();
}
/**/
static class XAdES_BES_Desc extends XAdESFormDesc
{
@Override
protected boolean checkProps(Set<String> availablePropsNames)
{
return availablePropsNames.contains(SigningCertificateProperty.PROP_NAME);
}
@Override
XAdESForm getForm()
{
return XAdESForm.BES;
}
}
/**/
static class XAdES_EPES_Desc extends XAdESFormDesc
{
public XAdES_EPES_Desc()
{
super(XADES_BES_DESC);
}
@Override
protected boolean checkProps(Set<String> availablePropsNames)
{
return availablePropsNames.contains(SignaturePolicyBase.PROP_NAME);
}
@Override
XAdESForm getForm()
{
return XAdESForm.EPES;
}
}
/**/
static class XAdES_T_Desc extends XAdESFormDesc
{
public XAdES_T_Desc()
{
super(XADES_EPES_DESC, XADES_BES_DESC);
}
@Override
protected boolean checkProps(Set<String> availablePropsNames)
{
return availablePropsNames.contains(SignatureTimeStampProperty.PROP_NAME);
}
@Override
XAdESForm getForm()
{
return XAdESForm.T;
}
}
/**/
static class XAdES_C_Desc extends XAdESFormDesc
{
public XAdES_C_Desc()
{
super(XADES_T_DESC);
}
@Override
protected boolean checkProps(Set<String> availablePropsNames) throws InvalidXAdESFormException
{
boolean hasCompCertRefs = availablePropsNames.contains(CompleteCertificateRefsProperty.PROP_NAME);
boolean hasCompRevocRefs = availablePropsNames.contains(CompleteRevocationRefsProperty.PROP_NAME);
boolean xor = hasCompCertRefs ^ hasCompRevocRefs;
if (xor)
throw new InvalidXAdESFormException(String.format(
"Both %s and %s have to be present in %s form",
CompleteCertificateRefsProperty.PROP_NAME,
CompleteRevocationRefsProperty.PROP_NAME,
this.getForm().toString()));
// If has both props, it's C form.
if (hasCompCertRefs)
return true;
// Can't have attr properties if the other 2 are not present.
if (availablePropsNames.contains("AttributeCertificateRefs") || availablePropsNames.contains("AttributeRevocationRefs"))
throw new InvalidXAdESFormException("Attr properties cannot be present without the base C form properties");
return false;
}
@Override
XAdESForm getForm()
{
return XAdESForm.C;
}
}
/**/
static class XAdES_X_Desc extends XAdESFormDesc
{
public XAdES_X_Desc()
{
super(XADES_C_DESC);
}
@Override
protected boolean checkProps(Set<String> availablePropsNames) {
return availablePropsNames.contains(SigAndRefsTimeStampProperty.PROP_NAME) ||
availablePropsNames.contains("RefsOnlyTimeStamp");
}
@Override
XAdESForm getForm()
{
return XAdESForm.X;
}
}
/**/
static class XAdES_X_L_Desc extends XAdESFormDesc
{
public XAdES_X_L_Desc()
{
super(XADES_X_DESC);
}
@Override
protected boolean checkProps(Set<String> availablePropsNames) throws InvalidXAdESFormException
{
boolean hasCompCert = availablePropsNames.contains(CertificateValuesProperty.PROP_NAME);
boolean hasCompRevoc = availablePropsNames.contains(RevocationValuesProperty.PROP_NAME);
boolean xor = hasCompCert ^ hasCompRevoc;
if (xor)
throw new InvalidXAdESFormException(String.format(
"Both %s and %s have to be present in %s form",
"CertificateValues",
"RevocationValues",
this.getForm().toString()));
return hasCompCert;
}
@Override
XAdESForm getForm()
{
return XAdESForm.X_L;
}
}
/**/
static class XAdES_A_Desc extends XAdESFormDesc
{
public XAdES_A_Desc()
{
super(XADES_X_L_DESC);
}
@Override
protected boolean checkProps(Set<String> availablePropsNames) {
return availablePropsNames.contains(ArchiveTimeStampProperty.PROP_NAME);
}
@Override
XAdESForm getForm()
{
return XAdESForm.A;
}
}
}