Created
October 6, 2020 04:06
-
-
Save frasertweedale/dfac386579cb2d1fe892f39d22c2975d to your computer and use it in GitHub Desktop.
JSS ALPN test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.PrintWriter; | |
import java.net.InetAddress; | |
import java.nio.charset.StandardCharsets; | |
import org.mozilla.jss.CryptoManager; | |
import org.mozilla.jss.InitializationValues; | |
import org.mozilla.jss.crypto.X509Certificate; | |
import org.mozilla.jss.netscape.security.util.Utils; | |
import org.mozilla.jss.ssl.SSLSocket; | |
import org.mozilla.jss.ssl.SSLCertificateApprovalCallback; | |
class Main { | |
static X509Certificate cert = null; | |
static SSLCertificateApprovalCallback certCallback = new CertCallback(); | |
public static void main(String[] args) { | |
String dbdir = "/home/ftweedal/dev/jss/nssdb"; | |
InitializationValues iv = new InitializationValues(dbdir); | |
String host = "google.com"; | |
byte[] http11 = "http/1.1".getBytes(StandardCharsets.UTF_8); | |
byte[] h2 = "h2".getBytes(StandardCharsets.UTF_8); | |
byte[] wat1 = "wat1".getBytes(StandardCharsets.UTF_8); | |
byte[][] alpn = { http11, wat1 }; | |
try { | |
CryptoManager.initialize(iv); | |
InetAddress addr = InetAddress.getByName(host); | |
SSLSocket sock = new SSLSocket( | |
addr, 443, | |
host, alpn, | |
certCallback, null /* client cert callback */); | |
sock.forceHandshake(); | |
byte[] proto = sock.getNegotiatedProtocol(); | |
if (proto == null) { | |
System.out.println("NO PROTO"); | |
} else { | |
System.out.println("PROTO: " + new String(h2, StandardCharsets.UTF_8)); | |
} | |
if (cert == null) { | |
System.out.println("no cert!"); | |
} else { | |
//System.out.println(Utils.base64encodeMultiLine(cert.getEncoded())); | |
} | |
OutputStream out = sock.getOutputStream(); | |
PrintWriter sink = new PrintWriter(out); | |
sink.write("GET / HTTP/1.0\r\nHost: " + host + "\r\n\r\n"); | |
sink.flush(); | |
out.flush(); | |
try (InputStream in = sock.getInputStream()) { | |
ByteArrayOutputStream result = new ByteArrayOutputStream(); | |
byte[] buffer = new byte[1024]; | |
int length; | |
while ((length = in.read(buffer)) != -1) { | |
result.write(buffer, 0, length); | |
} | |
System.out.println(result.toString("UTF-8")); | |
} | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
} | |
System.out.println("done"); | |
} | |
static class CertCallback implements SSLCertificateApprovalCallback { | |
public boolean approve( | |
org.mozilla.jss.crypto.X509Certificate cert, | |
SSLCertificateApprovalCallback.ValidityStatus status) { | |
Main.cert = cert; | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment