Работа над расшифровкой TLS
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package ru.serega6531.packmate.service.optimization;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import ru.serega6531.packmate.model.Packet;
|
||||
import ru.serega6531.packmate.utils.PacketUtils;
|
||||
import ru.serega6531.packmate.utils.SSLUtils;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLEngine;
|
||||
import javax.net.ssl.SSLEngineResult;
|
||||
import java.io.File;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class TlsDecryptor {
|
||||
|
||||
private final List<Packet> packets;
|
||||
|
||||
@SneakyThrows
|
||||
public void decryptTls() {
|
||||
List<List<Packet>> sides = PacketUtils.sliceToSides(packets);
|
||||
|
||||
File pemFile = new File(getClass().getClassLoader().getResource("tls.pem").getFile());
|
||||
File keyFile = new File(getClass().getClassLoader().getResource("tls.key").getFile());
|
||||
SSLContext context = SSLUtils.createContext(pemFile, keyFile);
|
||||
SSLEngine serverEngine = context.createSSLEngine();
|
||||
serverEngine.setUseClientMode(false);
|
||||
serverEngine.setNeedClientAuth(true);
|
||||
|
||||
ByteBuffer decodedServerBuf = ByteBuffer.allocate(1000);
|
||||
|
||||
SSLEngineResult unwrap = serverEngine.unwrap(ByteBuffer.wrap(packets.get(0).getContent()), decodedServerBuf);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -104,7 +104,7 @@ public class WebSocketsParser {
|
||||
}
|
||||
|
||||
private void parse(final List<Packet> wsPackets, final List<Packet> handshakes, Draft_6455 draft) {
|
||||
List<List<Packet>> sides = sliceToSides(wsPackets);
|
||||
List<List<Packet>> sides = PacketUtils.sliceToSides(wsPackets);
|
||||
parsedPackets = new ArrayList<>(handshakes);
|
||||
|
||||
for (List<Packet> side : sides) {
|
||||
@@ -149,31 +149,6 @@ public class WebSocketsParser {
|
||||
return parsedPackets;
|
||||
}
|
||||
|
||||
private List<List<Packet>> sliceToSides(List<Packet> packets) {
|
||||
List<List<Packet>> result = new ArrayList<>();
|
||||
List<Packet> side = new ArrayList<>();
|
||||
boolean incoming = true;
|
||||
|
||||
for (Packet packet : packets) {
|
||||
if(packet.isIncoming() != incoming) {
|
||||
incoming = packet.isIncoming();
|
||||
|
||||
if(!side.isEmpty()) {
|
||||
result.add(side);
|
||||
side = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
side.add(packet);
|
||||
}
|
||||
|
||||
if(!side.isEmpty()) {
|
||||
result.add(side);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private String getHandshake(final List<Packet> packets) {
|
||||
final String handshake = PacketUtils.mergePackets(packets)
|
||||
.map(String::new)
|
||||
|
||||
@@ -4,6 +4,7 @@ import lombok.experimental.UtilityClass;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import ru.serega6531.packmate.model.Packet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -16,4 +17,29 @@ public class PacketUtils {
|
||||
.reduce(ArrayUtils::addAll);
|
||||
}
|
||||
|
||||
public List<List<Packet>> sliceToSides(List<Packet> packets) {
|
||||
List<List<Packet>> result = new ArrayList<>();
|
||||
List<Packet> side = new ArrayList<>();
|
||||
boolean incoming = true;
|
||||
|
||||
for (Packet packet : packets) {
|
||||
if(packet.isIncoming() != incoming) {
|
||||
incoming = packet.isIncoming();
|
||||
|
||||
if(!side.isEmpty()) {
|
||||
result.add(side);
|
||||
side = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
side.add(packet);
|
||||
}
|
||||
|
||||
if(!side.isEmpty()) {
|
||||
result.add(side);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
54
src/main/java/ru/serega6531/packmate/utils/SSLUtils.java
Normal file
54
src/main/java/ru/serega6531/packmate/utils/SSLUtils.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package ru.serega6531.packmate.utils;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import javax.net.ssl.KeyManagerFactory;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.security.KeyStore;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
|
||||
public class SSLUtils {
|
||||
|
||||
@SneakyThrows
|
||||
public static SSLContext createContext(File pemFile, File keyFile) {
|
||||
final String pass = "abcdef";
|
||||
|
||||
File jksKeystoreFile = File.createTempFile("packmate_", ".jks");
|
||||
File pkcsKeystoreFile = File.createTempFile("packmate_", ".pkcs12");
|
||||
Splitter splitter = Splitter.on(' ');
|
||||
|
||||
jksKeystoreFile.delete();
|
||||
|
||||
String command = "openssl pkcs12 -export -out " + pkcsKeystoreFile.getAbsolutePath() + " -in " + pemFile.getAbsolutePath() +
|
||||
" -inkey " + keyFile.getAbsolutePath() + " -passout pass:" + pass;
|
||||
|
||||
Process process = new ProcessBuilder(splitter.splitToList(command)).inheritIO().start();
|
||||
checkState(process.waitFor() == 0);
|
||||
|
||||
command = "keytool -importkeystore -srckeystore " + pkcsKeystoreFile.getAbsolutePath() + " -srcstoretype PKCS12 -destkeystore " +
|
||||
jksKeystoreFile.getAbsolutePath() + " -srcstorepass " + pass + " -deststorepass " + pass;
|
||||
|
||||
process = new ProcessBuilder(splitter.splitToList(command)).inheritIO().start();
|
||||
checkState(process.waitFor() == 0);
|
||||
|
||||
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||
keystore.load(new FileInputStream(jksKeystoreFile), pass.toCharArray());
|
||||
|
||||
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
||||
keyManagerFactory.init(keystore, pass.toCharArray());
|
||||
|
||||
SSLContext ret = SSLContext.getInstance("TLSv1.2");
|
||||
TrustManagerFactory factory = TrustManagerFactory.getInstance(
|
||||
TrustManagerFactory.getDefaultAlgorithm());
|
||||
factory.init(keystore);
|
||||
ret.init(keyManagerFactory.getKeyManagers(), factory.getTrustManagers(), null);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user