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
|
package cz.crcs.ectester.data;
import cz.crcs.ectester.applet.EC_Consts;
import cz.crcs.ectester.common.ec.*;
import javacard.security.KeyPair;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ext.EntityResolver2;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
/**
* @author Jan Jancar johny@neuromancer.sk
*/
public class EC_Store {
private DocumentBuilder db;
private Map<String, EC_Category> categories;
public EC_Store() throws IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
SchemaFactory scf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema sch = scf.newSchema(this.getClass().getResource("/cz/crcs/ectester/data/schema.xsd"));
dbf.setSchema(sch);
dbf.setNamespaceAware(true);
dbf.setIgnoringComments(true);
dbf.setIgnoringElementContentWhitespace(true);
db = dbf.newDocumentBuilder();
db.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) throws SAXException {
System.err.println("EC_Store | Warning : " + exception);
}
@Override
public void error(SAXParseException exception) throws SAXException {
System.err.println("EC_Store | Error : " + exception);
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
System.err.println("EC_Store | Fatal : " + exception);
throw new SAXException(exception);
}
});
db.setEntityResolver(new EntityResolver2() {
@Override
public InputSource getExternalSubset(String name, String baseURI) throws SAXException, IOException {
return null;
}
@Override
public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId) throws SAXException, IOException {
InputSource is = new InputSource();
is.setSystemId(systemId);
is.setByteStream(getClass().getClass().getResourceAsStream("/cz/crcs/ectester/data/" + systemId));
return is;
}
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return null;
}
});
parse();
} catch (ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
}
private void parse() throws SAXException, ParserConfigurationException, IOException {
InputStream categories = this.getClass().getResourceAsStream("/cz/crcs/ectester/data/categories.xml");
if (categories == null) {
throw new IOException();
}
Document categoriesDoc = db.parse(categories);
categories.close();
categoriesDoc.normalize();
NodeList catList = categoriesDoc.getElementsByTagName("category");
this.categories = new TreeMap<>();
for (int i = 0; i < catList.getLength(); ++i) {
Node catNode = catList.item(i);
if (catNode instanceof Element) {
Element catElem = (Element) catNode;
Node name = catElem.getElementsByTagName("name").item(0);
Node dir = catElem.getElementsByTagName("directory").item(0);
Node desc = catElem.getElementsByTagName("desc").item(0);
EC_Category category = parseCategory(name.getTextContent(), dir.getTextContent(), desc.getTextContent());
this.categories.put(name.getTextContent(), category);
} else {
throw new SAXException("?");
}
}
}
private EC_Category parseCategory(String name, String dir, String desc) throws ParserConfigurationException, IOException, SAXException {
Map<String, EC_Data> objMap = new TreeMap<>();
InputStream curves = this.getClass().getResourceAsStream("/cz/crcs/ectester/data/" + dir + "/curves.xml");
if (curves != null) {
Document curvesDoc = db.parse(curves);
curvesDoc.normalize();
NodeList curveList = curvesDoc.getElementsByTagName("curve");
for (int i = 0; i < curveList.getLength(); ++i) {
Node curveNode = curveList.item(i);
if (curveNode instanceof Element) {
Element curveElem = (Element) curveNode;
Node id = curveElem.getElementsByTagName("id").item(0);
Node bits = curveElem.getElementsByTagName("bits").item(0);
Node field = curveElem.getElementsByTagName("field").item(0);
NodeList descc = curveElem.getElementsByTagName("desc");
String descs = null;
if (descc.getLength() != 0) {
descs = descc.item(0).getTextContent();
}
byte alg;
if (field.getTextContent().equalsIgnoreCase("prime")) {
alg = KeyPair.ALG_EC_FP;
} else {
alg = KeyPair.ALG_EC_F2M;
}
short bitsize = Short.parseShort(bits.getTextContent());
EC_Curve curve = new EC_Curve(id.getTextContent(), bitsize, alg, descs);
InputStream csv = parseDataElement(dir, curveElem);
if (!curve.readCSV(csv)) {
throw new IOException("Invalid csv data.");
}
csv.close();
objMap.put(id.getTextContent(), curve);
} else {
throw new SAXException("?");
}
}
curves.close();
}
InputStream keys = this.getClass().getResourceAsStream("/cz/crcs/ectester/data/" + dir + "/keys.xml");
if (keys != null) {
Document keysDoc = db.parse(keys);
keysDoc.normalize();
NodeList directs = keysDoc.getDocumentElement().getChildNodes();
for (int i = 0; i < directs.getLength(); ++i) {
Node direct = directs.item(i);
if (direct instanceof Element) {
Element elem = (Element) direct;
NodeList ids = elem.getElementsByTagName("id");
if (ids.getLength() != 1) {
throw new SAXException("key no id?");
}
String id = ids.item(0).getTextContent();
EC_Params result = parseKeylike(dir, elem);
objMap.put(id, result);
} else {
throw new SAXException("?");
}
}
keys.close();
}
InputStream results = this.getClass().getResourceAsStream("/cz/crcs/ectester/data/" + dir + "/results.xml");
if (results != null) {
Document resultsDoc = db.parse(results);
resultsDoc.normalize();
NodeList directs = resultsDoc.getDocumentElement().getChildNodes();
for (int i = 0; i < directs.getLength(); ++i) {
Node direct = directs.item(i);
if (direct instanceof Element) {
Element elem = (Element) direct;
Node id = elem.getElementsByTagName("id").item(0);
Node ka = elem.getElementsByTagName("ka").item(0);
Node curve = elem.getElementsByTagName("curve").item(0);
Node onekey = elem.getElementsByTagName("onekey").item(0);
Node otherkey = elem.getElementsByTagName("otherkey").item(0);
NodeList descc = elem.getElementsByTagName("desc");
String descs = null;
if (descc.getLength() != 0) {
descs = descc.item(0).getTextContent();
}
byte kab = EC_Consts.KA_ANY;
switch (ka.getTextContent()) {
case "DH":
case "ECDH":
kab = EC_Consts.KA_ECDH;
break;
case "DHC":
case "ECDHC":
kab = EC_Consts.KA_ECDHC;
break;
case "ANY":
kab = EC_Consts.KA_ANY;
break;
case "BOTH":
kab = EC_Consts.KA_BOTH;
break;
}
EC_KAResult kaResult = new EC_KAResult(id.getTextContent(), kab, curve.getTextContent(), onekey.getTextContent(), otherkey.getTextContent(), descs);
InputStream csv = parseDataElement(dir, elem);
if (!kaResult.readCSV(csv)) {
throw new IOException("Invalid csv data.");
}
csv.close();
objMap.put(id.getTextContent(), kaResult);
} else {
throw new SAXException("?");
}
}
results.close();
}
return new EC_Category(name, dir, desc, objMap);
}
private EC_Params parseKeylike(String dir, Element elem) throws SAXException, IOException {
Node id = elem.getElementsByTagName("id").item(0);
Node curve = elem.getElementsByTagName("curve").item(0);
NodeList desc = elem.getElementsByTagName("desc");
String descs = null;
if (desc.getLength() != 0) {
descs = desc.item(0).getTextContent();
}
EC_Params result;
if (elem.getTagName().equals("pubkey")) {
result = new EC_Key.Public(id.getTextContent(), curve.getTextContent(), descs);
} else if (elem.getTagName().equals("privkey")) {
result = new EC_Key.Private(id.getTextContent(), curve.getTextContent(), descs);
} else if (elem.getTagName().equals("keypair")) {
result = new EC_Keypair(id.getTextContent(), curve.getTextContent(), descs);
} else {
throw new SAXException("?");
}
InputStream csv = parseDataElement(dir, elem);
if (!result.readCSV(csv)) {
throw new IOException("Invalid CSV data.");
}
csv.close();
return result;
}
private InputStream parseDataElement(String dir, Element elem) throws SAXException {
NodeList file = elem.getElementsByTagName("file");
NodeList inline = elem.getElementsByTagName("inline");
InputStream csv;
if (file.getLength() == 1) {
csv = this.getClass().getResourceAsStream("/cz/crcs/ectester/data/" + dir + "/" + file.item(0).getTextContent());
} else if (inline.getLength() == 1) {
csv = new ByteArrayInputStream(inline.item(0).getTextContent().getBytes());
} else {
throw new SAXException("?");
}
return csv;
}
public Map<String, EC_Category> getCategories() {
return Collections.unmodifiableMap(categories);
}
public EC_Category getCategory(String category) {
return categories.get(category);
}
public Map<String, EC_Data> getObjects(String category) {
EC_Category cat = categories.get(category);
if (cat != null) {
return cat.getObjects();
}
return null;
}
public <T extends EC_Data> Map<String, T> getObjects(Class<T> objClass, String category) {
EC_Category cat = categories.get(category);
if (cat != null) {
return cat.getObjects(objClass);
}
return null;
}
public <T extends EC_Data> T getObject(Class<T> objClass, String category, String id) {
EC_Category cat = categories.get(category);
if (cat != null) {
return cat.getObject(objClass, id);
}
return null;
}
public <T extends EC_Data> T getObject(Class<T> objClass, String query) {
int split = query.indexOf("/");
if (split < 0) {
return null;
}
return getObject(objClass, query.substring(0, split), query.substring(split + 1));
}
}
|