Skip to content

Commit 4dff8da

Browse files
authored
Merge pull request #744 from tls-attacker/performanceFix
Reworked the Transport module & XSD Validation
2 parents 31017ca + 16f440f commit 4dff8da

File tree

32 files changed

+318
-109
lines changed

32 files changed

+318
-109
lines changed

Attacks/src/main/java/de/rub/nds/tlsattacker/attacks/task/InvalidCurveTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public boolean execute() {
5959
}
6060

6161
if (!state.getWorkflowTrace().executedAsPlanned()) {
62+
LOGGER.debug("Not executed as planned!");
6263
return false;
6364
}
6465
fingerprint = ResponseExtractor.getFingerprint(getState());

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/state/State.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private WorkflowTrace loadWorkflowTrace() {
122122

123123
if (config.getWorkflowInput() != null) {
124124
try {
125-
trace = WorkflowTraceSerializer.read(new FileInputStream(new File(config.getWorkflowInput())));
125+
trace = WorkflowTraceSerializer.secureRead(new FileInputStream(new File(config.getWorkflowInput())));
126126
LOGGER.debug("Loaded workflow trace from " + config.getWorkflowInput());
127127
} catch (FileNotFoundException ex) {
128128
LOGGER.warn("Could not read workflow trace. File not found: " + config.getWorkflowInput());

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/workflow/WorkflowTrace.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static WorkflowTrace copy(WorkflowTrace orig) {
6868
try {
6969
String origTraceStr = WorkflowTraceSerializer.write(orig);
7070
InputStream is = new ByteArrayInputStream(origTraceStr.getBytes(StandardCharsets.UTF_8.name()));
71-
copy = WorkflowTraceSerializer.read(is);
71+
copy = WorkflowTraceSerializer.insecureRead(is);
7272
} catch (JAXBException | IOException | XMLStreamException ex) {
7373
throw new ConfigurationException("Could not copy workflow trace: " + ex);
7474
}

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/workflow/WorkflowTraceSerializer.java

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static String write(WorkflowTrace trace) throws JAXBException, IOExceptio
9898

9999
/**
100100
* @param outputStream
101-
* The OutputStream to which the Trace should be written to
101+
* The OutputStream to which the Trace should be written to.
102102
* @param workflowTrace
103103
* The WorkflowTrace that should be written
104104
* @throws JAXBException
@@ -135,7 +135,8 @@ public static void write(OutputStream outputStream, WorkflowTrace workflowTrace)
135135

136136
/**
137137
* @param inputStream
138-
* The InputStream from which the Parameter should be read
138+
* The InputStream from which the Parameter should be read. Does NOT perform schema
139+
* validation
139140
* @return The deserialized WorkflowTrace
140141
* @throws JAXBException
141142
* JAXBException if the JAXB reports a problem
@@ -144,7 +145,71 @@ public static void write(OutputStream outputStream, WorkflowTrace workflowTrace)
144145
* @throws XMLStreamException
145146
* If there is a Problem with the XML Stream
146147
*/
147-
public static WorkflowTrace read(InputStream inputStream) throws JAXBException, IOException, XMLStreamException {
148+
public static WorkflowTrace insecureRead(InputStream inputStream)
149+
throws JAXBException, IOException, XMLStreamException {
150+
context = getJAXBContext();
151+
Unmarshaller unmarshaller = context.createUnmarshaller();
152+
unmarshaller.setEventHandler(new ValidationEventHandler() {
153+
@Override
154+
public boolean handleEvent(ValidationEvent event) {
155+
// raise an Exception also on Warnings
156+
return false;
157+
}
158+
});
159+
XMLInputFactory xif = XMLInputFactory.newFactory();
160+
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
161+
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
162+
XMLStreamReader xsr = xif.createXMLStreamReader(inputStream);
163+
WorkflowTrace wt = (WorkflowTrace) unmarshaller.unmarshal(xsr);
164+
inputStream.close();
165+
return wt;
166+
}
167+
168+
/**
169+
* Reads a file and does not perform schema validation
170+
*
171+
* @param f
172+
* @return
173+
*/
174+
public static List<WorkflowTrace> insecureReadFolder(File f) {
175+
if (f.isDirectory()) {
176+
ArrayList<WorkflowTrace> list = new ArrayList<>();
177+
for (File file : f.listFiles()) {
178+
if (file.getName().startsWith(".")) {
179+
// We ignore the .gitignore File
180+
continue;
181+
}
182+
WorkflowTrace trace;
183+
try {
184+
trace = WorkflowTraceSerializer.insecureRead(new FileInputStream(file));
185+
trace.setName(file.getAbsolutePath());
186+
list.add(trace);
187+
} catch (JAXBException | IOException | XMLStreamException ex) {
188+
LOGGER.warn("Could not read " + file.getAbsolutePath() + " from Folder.");
189+
LOGGER.debug(ex.getLocalizedMessage(), ex);
190+
}
191+
}
192+
return list;
193+
} else {
194+
throw new IllegalArgumentException("Cannot read Folder, because its not a Folder");
195+
}
196+
197+
}
198+
199+
/**
200+
* @param inputStream
201+
* The InputStream from which the Parameter should be read. Does perform schema
202+
* validation
203+
* @return The deserialized WorkflowTrace
204+
* @throws JAXBException
205+
* JAXBException if the JAXB reports a problem
206+
* @throws IOException
207+
* If something goes wrong while writing to the stream
208+
* @throws XMLStreamException
209+
* If there is a Problem with the XML Stream
210+
*/
211+
public static WorkflowTrace secureRead(InputStream inputStream)
212+
throws JAXBException, IOException, XMLStreamException {
148213
try {
149214
context = getJAXBContext();
150215
Unmarshaller unmarshaller = context.createUnmarshaller();
@@ -175,7 +240,13 @@ public boolean handleEvent(ValidationEvent event) {
175240
}
176241
}
177242

178-
public static List<WorkflowTrace> readFolder(File f) {
243+
/**
244+
* Reads a folder. Does perform schema validation.
245+
*
246+
* @param f
247+
* @return
248+
*/
249+
public static List<WorkflowTrace> secureReadFolder(File f) {
179250
if (f.isDirectory()) {
180251
ArrayList<WorkflowTrace> list = new ArrayList<>();
181252
for (File file : f.listFiles()) {
@@ -185,7 +256,7 @@ public static List<WorkflowTrace> readFolder(File f) {
185256
}
186257
WorkflowTrace trace;
187258
try {
188-
trace = WorkflowTraceSerializer.read(new FileInputStream(file));
259+
trace = WorkflowTraceSerializer.secureRead(new FileInputStream(file));
189260
trace.setName(file.getAbsolutePath());
190261
list.add(trace);
191262
} catch (JAXBException | IOException | XMLStreamException ex) {

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/workflow/task/TlsTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public ITask call() {
5757
hasError = false;
5858
break;
5959
} else {
60+
LOGGER.debug("Could not execute task correctly. Increasing Timeout and reexecuting");
6061
if (increasingSleepTimes) {
6162
sleepTime += additionalSleepTime;
6263
}

TLS-Core/src/main/resources/workflowTrace.xsd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2988,7 +2988,6 @@
29882988

29892989
<xs:complexType name="transportHandler" abstract="true">
29902990
<xs:sequence>
2991-
<xs:element name="isInStreamTerminating" type="xs:boolean"/>
29922991
<xs:element name="timeout" type="xs:long"/>
29932992
</xs:sequence>
29942993
</xs:complexType>

TLS-Core/src/test/java/de/rub/nds/tlsattacker/core/protocol/XmlSerialisationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testProtocolMessages() throws Exception {
4040
trace.addTlsAction(new ReceiveAction(message));
4141
File f = folder.newFile();
4242
WorkflowTraceSerializer.write(f, trace);
43-
WorkflowTrace newWorkflowTrace = WorkflowTraceSerializer.read(new FileInputStream(f));
43+
WorkflowTrace newWorkflowTrace = WorkflowTraceSerializer.secureRead(new FileInputStream(f));
4444
assertTrue(newWorkflowTrace.getTlsActions().size() == 2);
4545

4646
assertTrue("Message failed: " + message.getClass().getName(),
@@ -67,7 +67,7 @@ public void testExtensionMessages() throws Exception {
6767
trace.addTlsAction(new ReceiveAction(message));
6868
File f = folder.newFile();
6969
WorkflowTraceSerializer.write(f, trace);
70-
WorkflowTrace newWorkflowTrace = WorkflowTraceSerializer.read(new FileInputStream(f));
70+
WorkflowTrace newWorkflowTrace = WorkflowTraceSerializer.secureRead(new FileInputStream(f));
7171
assertTrue(newWorkflowTrace.getTlsActions().size() == 2);
7272

7373
assertTrue("Extension failed: " + extension.getClass().getName(),

TLS-Core/src/test/java/de/rub/nds/tlsattacker/core/unittest/helper/FakeTransportHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,8 @@ public void closeClientConnection() throws IOException {
7171
opened = false;
7272
}
7373

74+
@Override
75+
public void setTimeout(long timeout) {
76+
}
77+
7478
}

TLS-Core/src/test/java/de/rub/nds/tlsattacker/core/workflow/SerializationFullTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public void test() throws JAXBException, IOException {
158158
}
159159
LOGGER.info(builder.toString());
160160
try {
161-
trace = WorkflowTraceSerializer.read(new FileInputStream(f));
161+
trace = WorkflowTraceSerializer.secureRead(new FileInputStream(f));
162162
} catch (XMLStreamException ex) {
163163
fail();
164164
}

TLS-Core/src/test/java/de/rub/nds/tlsattacker/core/workflow/WorkflowTraceNormalizerTestBadInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private void loadTestVector(File testVectorPath) {
135135

136136
try {
137137
trace = WorkflowTraceSerializer
138-
.read(new ByteArrayInputStream(traceInputXml.getBytes(StandardCharsets.UTF_8.name())));
138+
.secureRead(new ByteArrayInputStream(traceInputXml.getBytes(StandardCharsets.UTF_8.name())));
139139
} catch (JAXBException | IOException | XMLStreamException | DataBindingException ex) {
140140
LOGGER.error("Could not load workflow trace from test file " + testVectorPath + ": " + ex);
141141
}

0 commit comments

Comments
 (0)