Skip to content

Commit dbc4f6a

Browse files
committed
Merge branch 'releaseVersion' into devVersion
2 parents 184a52f + 7a1d3a8 commit dbc4f6a

File tree

11 files changed

+973
-0
lines changed

11 files changed

+973
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* TLS-Attacker - A Modular Penetration Testing Framework for TLS
3+
*
4+
* Copyright 2014-2022 Ruhr University Bochum, Paderborn University, Hackmanit GmbH
5+
*
6+
* Licensed under Apache License, Version 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0.txt
8+
*/
9+
10+
package de.rub.nds.tlsattacker.core.socket;
11+
12+
import de.rub.nds.tlsattacker.core.protocol.ProtocolMessage;
13+
import de.rub.nds.tlsattacker.core.protocol.message.ApplicationMessage;
14+
import de.rub.nds.tlsattacker.core.state.State;
15+
import de.rub.nds.tlsattacker.core.workflow.action.ReceiveAction;
16+
import java.io.ByteArrayInputStream;
17+
import java.io.ByteArrayOutputStream;
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.util.LinkedList;
21+
import java.util.List;
22+
23+
/**
24+
* An input stream that is reading from a tls attacker state
25+
*
26+
*/
27+
public class EncapsulatingInputStream extends InputStream {
28+
29+
private final State state;
30+
31+
private ByteArrayInputStream inputStream;
32+
33+
public EncapsulatingInputStream(State state) {
34+
this.inputStream = new ByteArrayInputStream(new byte[0]);
35+
this.state = state;
36+
}
37+
38+
@Override
39+
public int read() throws IOException {
40+
if (available() == 0) {
41+
checkForNewData();
42+
}
43+
return inputStream.read();
44+
}
45+
46+
private void checkForNewData() throws IOException {
47+
ReceiveAction action = new ReceiveAction(new ApplicationMessage());
48+
action.setConnectionAlias(state.getTlsContext().getConnection().getAlias());
49+
action.execute(state);
50+
List<ProtocolMessage> receivedMessages = action.getReceivedMessages();
51+
52+
List<ApplicationMessage> receivedAppMessages = new LinkedList<>();
53+
for (ProtocolMessage message : receivedMessages) {
54+
if (message instanceof ApplicationMessage) {
55+
receivedAppMessages.add((ApplicationMessage) message);
56+
}
57+
}
58+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
59+
for (ApplicationMessage message : receivedAppMessages) {
60+
stream.write(message.getData().getValue());
61+
}
62+
inputStream = new ByteArrayInputStream(stream.toByteArray());
63+
}
64+
65+
@Override
66+
public int available() throws IOException {
67+
if (inputStream.available() == 0) {
68+
checkForNewData();
69+
return inputStream.available();
70+
} else {
71+
return inputStream.available();
72+
}
73+
}
74+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* TLS-Attacker - A Modular Penetration Testing Framework for TLS
3+
*
4+
* Copyright 2014-2022 Ruhr University Bochum, Paderborn University, Hackmanit GmbH
5+
*
6+
* Licensed under Apache License, Version 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0.txt
8+
*/
9+
10+
package de.rub.nds.tlsattacker.core.socket;
11+
12+
import de.rub.nds.tlsattacker.core.protocol.ProtocolMessage;
13+
import de.rub.nds.tlsattacker.core.protocol.message.ApplicationMessage;
14+
import de.rub.nds.tlsattacker.core.state.State;
15+
import de.rub.nds.tlsattacker.core.workflow.action.SendAction;
16+
import java.io.ByteArrayInputStream;
17+
import java.io.ByteArrayOutputStream;
18+
import java.io.IOException;
19+
import java.io.OutputStream;
20+
import java.util.Arrays;
21+
import org.apache.logging.log4j.LogManager;
22+
import org.apache.logging.log4j.Logger;
23+
24+
/**
25+
* An input stream that is reading from a tls attacker state
26+
*
27+
*/
28+
public class EncapsulatingOutputStream extends OutputStream {
29+
30+
private Logger LOGGER = LogManager.getLogger();
31+
32+
private final State state;
33+
34+
private ByteArrayOutputStream outputStream;
35+
36+
public EncapsulatingOutputStream(State state) {
37+
this.outputStream = new ByteArrayOutputStream();
38+
this.state = state;
39+
}
40+
41+
@Override
42+
public void write(int i) throws IOException {
43+
outputStream.write(i);
44+
}
45+
46+
@Override
47+
public void flush() throws IOException {
48+
ApplicationMessage message = new ApplicationMessage();
49+
ByteArrayInputStream stream = new ByteArrayInputStream(outputStream.toByteArray());
50+
byte[] sendingBytes = new byte[16384];
51+
int actuallyRead;
52+
do {
53+
actuallyRead = 0;
54+
try {
55+
actuallyRead = stream.read(sendingBytes);
56+
if (actuallyRead > 0) {
57+
message.setDataConfig(Arrays.copyOf(sendingBytes, actuallyRead));
58+
send(message);
59+
}
60+
} catch (IOException ex) {
61+
LOGGER.warn(ex);
62+
}
63+
} while (actuallyRead > 0);
64+
outputStream = new ByteArrayOutputStream();
65+
}
66+
67+
private void send(ProtocolMessage message) {
68+
SendAction action = new SendAction(message);
69+
action.setConnectionAlias(state.getTlsContext().getConnection().getAlias());
70+
action.execute(state);
71+
}
72+
}

0 commit comments

Comments
 (0)