Skip to content

Commit 40893b7

Browse files
authored
Merge pull request #394 from RUB-NDS/configDisplayFilter
DisplayFilter for Config Objects
2 parents 612ab52 + eca3269 commit 40893b7

File tree

4 files changed

+96
-26
lines changed

4 files changed

+96
-26
lines changed

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/config/Config.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,6 @@ public static Config createConfig(InputStream stream) {
8888
return ConfigIO.read(stream);
8989
}
9090

91-
public static Config createEmptyConfig() {
92-
Config c = new Config();
93-
for (Field field : c.getClass().getDeclaredFields()) {
94-
if (!field.getName().equals("LOGGER") && !field.getType().isPrimitive()) {
95-
field.setAccessible(true);
96-
try {
97-
field.set(c, null);
98-
} catch (IllegalAccessException e) {
99-
LOGGER.warn("Could not set field in Config!", e);
100-
}
101-
}
102-
}
103-
return c;
104-
}
105-
106-
public static Config mergeWithDefaultValues(Config c) {
107-
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
108-
ConfigIO.write(c, byteArrayOutputStream);
109-
c = ConfigIO.read(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
110-
return c;
111-
}
112-
11391
/**
11492
* List of filters to apply on workflow traces before serialization.
11593
*/
@@ -954,7 +932,7 @@ public static Config mergeWithDefaultValues(Config c) {
954932
* Extension defined in RFC5077, followed by additional TLS 1.3 draft 21
955933
* NewSessionTicket parameters.
956934
*/
957-
private long sessionTicketLifetimeHint = 0;
935+
private Long sessionTicketLifetimeHint = 0l;
958936

959937
@XmlJavaTypeAdapter(ByteArrayAdapter.class)
960938
private byte[] sessionTicketKeyAES = ArrayConverter.hexStringToByteArray("536563757265535469636b65744b6579"); // SecureSTicketKey

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/config/ConfigIO.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
*/
99
package de.rub.nds.tlsattacker.core.config;
1010

11-
import java.io.File;
12-
import java.io.InputStream;
13-
import java.io.OutputStream;
11+
import de.rub.nds.tlsattacker.core.config.filter.ConfigDisplayFilter;
12+
13+
import java.io.*;
1414
import javax.xml.bind.JAXB;
1515

1616
public class ConfigIO {
@@ -22,6 +22,18 @@ public static void write(Config config, OutputStream os) {
2222
JAXB.marshal(config, os);
2323
}
2424

25+
public static void write(Config config, File f, ConfigDisplayFilter filter) {
26+
Config filteredConfig = copy(config);
27+
filter.applyFilter(filteredConfig);
28+
write(filteredConfig, f);
29+
}
30+
31+
public static void write(Config config, OutputStream os, ConfigDisplayFilter filter) {
32+
Config filteredConfig = copy(config);
33+
filter.applyFilter(filteredConfig);
34+
write(filteredConfig, os);
35+
}
36+
2537
public static Config read(File f) {
2638
Config config = JAXB.unmarshal(f, Config.class);
2739
return config;
@@ -32,6 +44,12 @@ public static Config read(InputStream stream) {
3244
return config;
3345
}
3446

47+
public static Config copy(Config config) {
48+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
49+
ConfigIO.write(config, byteArrayOutputStream);
50+
return ConfigIO.read(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
51+
}
52+
3553
private ConfigIO() {
3654
}
3755
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* TLS-Attacker - A Modular Penetration Testing Framework for TLS
3+
*
4+
* Copyright 2014-2017 Ruhr University Bochum / Hackmanit GmbH
5+
*
6+
* Licensed under Apache License 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*/
9+
package de.rub.nds.tlsattacker.core.config.filter;
10+
11+
import de.rub.nds.tlsattacker.core.config.Config;
12+
13+
public interface ConfigDisplayFilter {
14+
void applyFilter(Config config);
15+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* TLS-Attacker - A Modular Penetration Testing Framework for TLS
3+
*
4+
* Copyright 2014-2017 Ruhr University Bochum / Hackmanit GmbH
5+
*
6+
* Licensed under Apache License 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*/
9+
package de.rub.nds.tlsattacker.core.config.filter;
10+
11+
import de.rub.nds.tlsattacker.core.config.Config;
12+
import org.apache.logging.log4j.LogManager;
13+
import org.apache.logging.log4j.Logger;
14+
15+
import java.lang.reflect.Array;
16+
import java.lang.reflect.Field;
17+
import java.lang.reflect.Modifier;
18+
19+
public class RemoveDefaultValues implements ConfigDisplayFilter {
20+
21+
static final Logger LOGGER = LogManager.getLogger(RemoveDefaultValues.class);
22+
23+
@Override
24+
public void applyFilter(Config config) {
25+
Config defaultConfig = Config.createConfig();
26+
for (Field field : Config.class.getDeclaredFields()) {
27+
if (!(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()))) {
28+
if (field.getType().isArray() || !field.getType().isPrimitive()) {
29+
field.setAccessible(true);
30+
try {
31+
Object defaultValue = field.get(defaultConfig);
32+
Object configValue = field.get(config);
33+
if (configValue != null)
34+
if (field.getType().isArray()) {
35+
if (Array.getLength(defaultValue) == Array.getLength(configValue)) {
36+
boolean equal = true;
37+
for (int i = 0; i < Array.getLength(defaultValue); i++) {
38+
if (!Array.get(defaultValue, i).equals(Array.get(configValue, i))) {
39+
equal = false;
40+
break;
41+
}
42+
}
43+
if (equal)
44+
field.set(config, null);
45+
}
46+
} else {
47+
if (defaultValue.equals(configValue))
48+
field.set(config, null);
49+
}
50+
} catch (IllegalAccessException e) {
51+
LOGGER.warn("Could not remove field in Config!", e);
52+
}
53+
field.setAccessible(false);
54+
}
55+
}
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)