Giter VIP home page Giter VIP logo

sslpoke's Introduction

SSLPoke

Test of java SSL / keystore / cert setup. Came from https://confluence.atlassian.com/download/attachments/117455/SSLPoke.java

Use Gradle to build standalone JAR file:

./gradlew clean jar

Usage:

  1. Negative test SSL connection:

    java -jar build/libs/SSLPoke-1.0.jar <server> 443

    you should get exception like this:

    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    
  2. Create new empty keystore and add exported certificate from server:

    keytool -import -file certificate.cert -alias certificate -keystore trustStore.keystore
    
  3. Do the test again and specify trustStore with password:

    java -Djavax.net.ssl.trustStore=trustStore.keystore -Djavax.net.ssl.trustStorePassword=changeit -jar build/libs/SSLPoke-1.0.jar <server> 443

    you should get positive answer:

    Successfully connected

sslpoke's People

Contributors

fmachado091 avatar markhann avatar michalhecko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

sslpoke's Issues

not working in Intellij Idea

This is a very nice tool.

I run the SSLPoke in the IDE, the SSLHandshakeException with handshake_failure always happens. No logs appears with environment variable javax.net.debug=all. In the case that I run its jar file in command line of a terminal, it works fine. I can see the logs as well.

Any suggestion?

Does not work with higher java versions

Steps to reproduce

With Java 10+:

$ ./gradlew clean jar

Result

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine java version from '10.0.2'.

Cause

The gradle wrapper this repo contains is a bit old (version 3.4.1) and does not support Java 10+.

Possible fix

Upgrade gradle wrapper version.

feature request user friendly errors

Hello

Is it possible to send more user-friendly error in case of missing or wrong keystore password?

java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext)

OR

javax.net.ssl.SSLException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

SSLPoke should support Proxy - giving the solution in the comments

SSLPoke should support proxy

Here i am creating version of SSLPoke with proxy

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.util.Arrays;
import java.util.Optional;
import java.util.regex.Pattern;

/**
 * Establish a SSL connection to a host and port, writes a byte and
 * prints the response. See
 * http://confluence.atlassian.com/display/JIRA/Connecting+to+SSL+services
 */
public class SSLPoke {

    private static final Pattern PATTERN = Pattern.compile(
            "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");

    public static void main(String[] args) throws IOException {
        if (args.length < 2) {
            System.out.println("Usage: " + SSLPoke.class.getName() + " <host> <port>");
            System.out.println("Usage: Additionally Supply Proxy : " + SSLPoke.class.getName() + "" +
                    "<host> <port> --proxy=proxyHost:proxyPort");
            System.exit(1);
        }

        InetSocketAddress inetSocketAddress = getProxy(args);
        boolean proxyEnabled = inetSocketAddress != null;
        Socket socket = null;

        if (proxyEnabled) {
            Proxy proxy = new Proxy(Proxy.Type.HTTP,inetSocketAddress);
            socket = new Socket(proxy);
            InetSocketAddress address = new InetSocketAddress(args[0], 443);
            socket.connect(address);
        }

        try {
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = null;
            if (proxyEnabled) {
                sslsocket = (SSLSocket) sslsocketfactory.createSocket(socket, args[0], Integer.parseInt(args[1]),
                        true);
            } else {
                sslsocket = (SSLSocket) sslsocketfactory.createSocket(args[0], Integer.parseInt(args[1]));
            }
            InputStream in = sslsocket.getInputStream();
            OutputStream out = sslsocket.getOutputStream();
            out.write(1);
            while (in.available() > 0) {
                System.out.print(in.read());
            }
            System.out.println("Successfully connected");

        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public static InetSocketAddress getProxy(String[] args) {
        Optional<String> optProxy = Arrays.stream(args).filter(arg -> arg.startsWith("--proxy")).findFirst();
        if (optProxy.isPresent() && optProxy.get().split("=").length > 1 &&
                optProxy.get().split("=")[1].split(":").length > 1) {
            String[] proxy = optProxy.get().split("=")[1].split(":");
            try {
                if (!validate(proxy[0])) {
                    System.out.println("Not a valid IP");
                    System.exit(1);
                }
                return new InetSocketAddress(proxy[0], Integer.valueOf(proxy[1]));
            } catch (Throwable e) {
                System.out.println("PROXY FORMAT --proxy=IP:PORT");
                e.printStackTrace();
                System.exit(1);
            }
        }
        System.out.println("PROXY NOT PROVIDED");
        return null;
    }

    public static boolean hasProxyFlag(String[] args) {
        Optional<String> optProxy = Arrays.stream(args).filter(arg -> arg.startsWith("--proxy")).findFirst();
        return optProxy.isPresent();
    }

    public static boolean validate(final String ip) {
        return PATTERN.matcher(ip).matches();
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.