Skip to content

Commit

Permalink
Add the feature to use an URL instead of a server and port
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielpadilh4 committed Dec 27, 2023
1 parent a91504f commit 4207c33
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.gabrielpadilh4</groupId>
<artifactId>ssl-handshake-debugger</artifactId>
<version>1.5-SNAPSHOT</version>
<version>1.6-SNAPSHOT</version>

<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package io.github.gabrielpadilh4.commands;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.util.concurrent.Callable;

import io.github.gabrielpadilh4.models.SslCliParams;
import io.github.gabrielpadilh4.services.SSLService;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

/**
* @author gabrielpadilhasantos@gmail.com
*/
@Command(name = "ssl-handshake-debugger",
mixinStandardHelpOptions = true,
description = "Command line application that tests SSL/TLS handshake as client or server and prints the javax.net.debug output.",
version = { "SSL Handshake Debugger 1.5",
"JVM: ${java.version} (${java.vendor} ${java.vm.name} ${java.vm.version})",
"OS: ${os.name} ${os.version} ${os.arch}"
@Command(name = "ssl-handshake-debugger",
mixinStandardHelpOptions = true,
description = "Command line application that tests SSL/TLS handshake as client or server and prints the javax.net.debug output.",
version = { "SSL Handshake Debugger 1.6",
"JVM: ${java.version} (${java.vendor} ${java.vm.name} ${java.vm.version})",
"OS: ${os.name} ${os.version} ${os.arch}"
},
sortOptions = false,
sortOptions = false,
usageHelpAutoWidth = true,
descriptionHeading = "%nDescription:%n%n",
optionListHeading = "%nParameters:%n",
Expand All @@ -29,10 +28,13 @@ public class SSLDebugCommand implements Callable<Integer> {
@Parameters(description = "mode to run, client or server", defaultValue = "client")
private String mode;

@Option(names = { "-server", "-s" }, required = true, description = "ip or host to bind or call")
@Option(names = { "-server", "-s" }, description = "ip or host to bind or call")
private String server;

@Option(names = { "--port", "-p" }, required = true, description = "port to listen or be hit")
@Option(names = { "-url", "-u" }, description = "url to be called")
private String url;

@Option(names = { "--port", "-p" }, description = "port to listen or be hit")
private int port;

@Option(names = { "--ciphers", "-c" }, description = "enabled cipher suites(e.g TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384)", defaultValue = "")
Expand Down Expand Up @@ -72,6 +74,7 @@ public Integer call() throws Exception {

sslCliParams.setMode(mode);
sslCliParams.setServer(server);
sslCliParams.setUrl(url);
sslCliParams.setPort(port);
sslCliParams.setCiphers(ciphers);
sslCliParams.setFileName(fileName);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/github/gabrielpadilh4/models/SslCliParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class SslCliParams {

private String mode;
private String server;
private String url;
private int port;
private String ciphers;
private String fileName;
Expand All @@ -22,6 +23,7 @@ public class SslCliParams {
public SslCliParams() {
this.mode = "";
this.server = "";
this.url = "";
this.port = 0;
this.ciphers = "";
this.fileName = "";
Expand Down Expand Up @@ -55,6 +57,14 @@ public void setServer(String server) {
this.server = server;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public int getPort() {
return port;
}
Expand Down
35 changes: 24 additions & 11 deletions src/main/java/io/github/gabrielpadilh4/services/SSLService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package io.github.gabrielpadilh4.services;

import io.github.gabrielpadilh4.commands.SSLDebugCommand;
import io.github.gabrielpadilh4.models.Server;
import io.github.gabrielpadilh4.models.SslCliParams;

import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -19,10 +9,21 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import io.github.gabrielpadilh4.commands.SSLDebugCommand;
import io.github.gabrielpadilh4.models.Server;
import io.github.gabrielpadilh4.models.SslCliParams;

/**
* @author gabrielpadilhasantos@gmail.com
*/
Expand Down Expand Up @@ -63,7 +64,7 @@ private static Server parseSslCliParams(SslCliParams sslCliParams) throws Except

if (!sslCliParams.getTruststore().isBlank()) {
System.setProperty("javax.net.ssl.trustStore", sslCliParams.getTruststore());
System.setProperty("javax.net.ssl.trustStorePassword", sslCliParams.getTruststorePassword());
System.setProperty("javax.net.ssl.trustStorePassword", sslCliParams.getTruststorePassword());
}

if (!sslCliParams.getFileName().isBlank()) {
Expand All @@ -79,6 +80,14 @@ private static Server parseSslCliParams(SslCliParams sslCliParams) throws Except
return new Server(serverName, serverPort);
}

private static void openUrlSocket(String url) throws Exception {
URL urlTest = new URL(url);
SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(urlTest.getHost(), urlTest.getDefaultPort());
socket.startHandshake();
socket.close();
}

private static void openClientSocket(Server serverToBeCalled) throws IOException {
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
socket.setSoTimeout(DEFAULT_SOCKET_TIMEOUT_MILLIS);
Expand Down Expand Up @@ -118,6 +127,10 @@ public static void logSSLHandshake(SslCliParams sslCliParams) {
Server server = parseSslCliParams(sslCliParams);

if (sslCliParams.getMode().equals("client")) {
if (!sslCliParams.getUrl().isBlank()) {
openUrlSocket(sslCliParams.getUrl());
return;
}
openClientSocket(server);
}

Expand Down

0 comments on commit 4207c33

Please sign in to comment.