Доделан KeyShareExtension

This commit is contained in:
serega6531
2020-04-20 16:09:42 +03:00
parent 686ebc10b5
commit c72e7b1da9
8 changed files with 72 additions and 22 deletions

View File

@@ -9,13 +9,14 @@ public abstract class TlsExtension {
protected ExtensionType type; protected ExtensionType type;
protected short extensionLength; protected short extensionLength;
public static TlsExtension newInstance(ExtensionType type, byte[] rawData, int offset, short extensionLength) { public static TlsExtension newInstance(ExtensionType type, byte[] rawData, int offset,
short extensionLength, boolean client) {
if (extensionLength > 0) { if (extensionLength > 0) {
ByteArrays.validateBounds(rawData, offset, extensionLength); ByteArrays.validateBounds(rawData, offset, extensionLength);
} }
if (type == ExtensionType.KEY_SHARE) { if (type == ExtensionType.KEY_SHARE) {
return new KeyShareExtension(type, rawData, offset, extensionLength); return KeyShareExtension.newInstance(type, rawData, offset, extensionLength, client);
} else { } else {
return new UnimplementedTlsExtension(type, rawData, offset, extensionLength); return new UnimplementedTlsExtension(type, rawData, offset, extensionLength);
} }

View File

@@ -15,6 +15,10 @@ public class UnimplementedTlsExtension extends TlsExtension {
@Override @Override
public String toString() { public String toString() {
if(extensionLength > 0) {
return type.name() + " [" + extensionLength + " bytes]"; return type.name() + " [" + extensionLength + " bytes]";
} else {
return type.name();
}
} }
} }

View File

@@ -0,0 +1,23 @@
package ru.serega6531.packmate.service.optimization.tls.extensions.keyshare;
import org.pcap4j.util.ByteArrays;
import ru.serega6531.packmate.service.optimization.tls.numbers.ExtensionType;
import static org.pcap4j.util.ByteArrays.SHORT_SIZE_IN_BYTES;
public class ClientKeyShareExtension extends KeyShareExtension {
private static final int KEY_SHARE_LENGTH_OFFSET = 0;
private static final int KEY_SHARE_ENTRY_OFFSET = KEY_SHARE_LENGTH_OFFSET + SHORT_SIZE_IN_BYTES;
private short keyShareLength;
public ClientKeyShareExtension(ExtensionType type, byte[] rawData, int offset, short extensionLength) {
super(type, extensionLength);
this.keyShareLength = ByteArrays.getShort(rawData, KEY_SHARE_LENGTH_OFFSET + offset); // the field is not always there
int cursor = KEY_SHARE_ENTRY_OFFSET + offset;
ByteArrays.validateBounds(rawData, cursor + offset, keyShareLength);
readEntries(rawData, KEY_SHARE_ENTRY_OFFSET + offset, offset + keyShareLength);
}
}

View File

@@ -7,31 +7,38 @@ import ru.serega6531.packmate.service.optimization.tls.numbers.ExtensionType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.pcap4j.util.ByteArrays.SHORT_SIZE_IN_BYTES; public abstract class KeyShareExtension extends TlsExtension {
public class KeyShareExtension extends TlsExtension { private final List<KeyShareEntry> entries = new ArrayList<>();
private static final int KEY_SHARE_LENGTH_OFFSET = 0; public static KeyShareExtension newInstance(ExtensionType type, byte[] rawData, int offset,
private static final int KEY_SHARE_ENTRY_OFFSET = KEY_SHARE_LENGTH_OFFSET + SHORT_SIZE_IN_BYTES; short extensionLength, boolean client) {
ByteArrays.validateBounds(rawData, offset, extensionLength);
private short keyShareLength; if(client) {
private List<KeyShareEntry> entries = new ArrayList<>(); return new ClientKeyShareExtension(type, rawData, offset, extensionLength);
} else {
return new ServerKeyShareExtension(type, rawData, offset, extensionLength);
}
}
public KeyShareExtension(ExtensionType type, byte[] rawData, int offset, short extensionLength) { protected KeyShareExtension(ExtensionType type, short extensionLength) {
super(type, extensionLength); super(type, extensionLength);
}
this.keyShareLength = ByteArrays.getShort(rawData, KEY_SHARE_LENGTH_OFFSET + offset); // the field is not always there protected void readEntries(byte[] rawData, int cursor, int end) {
ByteArrays.validateBounds(rawData, KEY_SHARE_ENTRY_OFFSET + offset, keyShareLength); while (cursor < end) {
KeyShareEntry entry = readEntry(rawData, cursor);
int cursor = KEY_SHARE_ENTRY_OFFSET + offset;
while (cursor < offset + this.keyShareLength) {
KeyShareEntry entry = new KeyShareEntry(rawData, cursor);
entries.add(entry);
cursor += entry.size(); cursor += entry.size();
} }
} }
protected KeyShareEntry readEntry(byte[] rawData, int cursor) {
KeyShareEntry entry = new KeyShareEntry(rawData, cursor);
entries.add(entry);
return entry;
}
@Override @Override
public String toString() { public String toString() {
return type.name() + " " + entries.toString(); return type.name() + " " + entries.toString();

View File

@@ -0,0 +1,14 @@
package ru.serega6531.packmate.service.optimization.tls.extensions.keyshare;
import ru.serega6531.packmate.service.optimization.tls.numbers.ExtensionType;
public class ServerKeyShareExtension extends KeyShareExtension {
private static final int KEY_SHARE_ENTRY_OFFSET = 0;
public ServerKeyShareExtension(ExtensionType type, byte[] rawData, int offset, short extensionLength) {
super(type, extensionLength);
readEntry(rawData, KEY_SHARE_ENTRY_OFFSET + offset);
}
}

View File

@@ -56,7 +56,8 @@ public class ClientHelloHandshakeRecordContent extends HelloHandshakeRecordConte
this.extensionsLength = ByteArrays.getShort(rawData, this.extensionsLength = ByteArrays.getShort(rawData,
EXTENSIONS_LENGTH_OFFSET + compressionMethodsLength + sessionIdLength + cipherSuitesLength + offset); EXTENSIONS_LENGTH_OFFSET + compressionMethodsLength + sessionIdLength + cipherSuitesLength + offset);
readExtensions(rawData, EXTENSIONS_OFFSET + compressionMethodsLength + sessionIdLength + cipherSuitesLength + offset); readExtensions(rawData, EXTENSIONS_OFFSET + compressionMethodsLength +
sessionIdLength + cipherSuitesLength + offset, true);
} }
@Override @Override

View File

@@ -37,7 +37,7 @@ public abstract class HelloHandshakeRecordContent implements HandshakeRecordCont
} }
} }
protected void readExtensions(byte[] rawData, int offset) { protected void readExtensions(byte[] rawData, int offset, boolean client) {
extensions = new ArrayList<>(extensionsLength); extensions = new ArrayList<>(extensionsLength);
int cursor = offset; int cursor = offset;
@@ -49,7 +49,7 @@ public abstract class HelloHandshakeRecordContent implements HandshakeRecordCont
short extensionLength = ByteArrays.getShort(rawData, cursor); short extensionLength = ByteArrays.getShort(rawData, cursor);
cursor += SHORT_SIZE_IN_BYTES; cursor += SHORT_SIZE_IN_BYTES;
extensions.add(TlsExtension.newInstance(extensionType, rawData, cursor, extensionLength)); extensions.add(TlsExtension.newInstance(extensionType, rawData, cursor, extensionLength, client));
cursor += extensionLength; cursor += extensionLength;
} }

View File

@@ -32,7 +32,7 @@ public class ServerHelloHandshakeRecordContent extends HelloHandshakeRecordConte
this.extensionsLength = ByteArrays.getShort(rawData, this.extensionsLength = ByteArrays.getShort(rawData,
EXTENSIONS_LENGTH_OFFSET + sessionIdLength + offset); EXTENSIONS_LENGTH_OFFSET + sessionIdLength + offset);
readExtensions(rawData, EXTENSIONS_OFFSET + sessionIdLength + offset); readExtensions(rawData, EXTENSIONS_OFFSET + sessionIdLength + offset, false);
} }
@Override @Override