Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WebSocket SSL Client
private static Vertx vertx;
static {
// adds the Bouncy castle provider to java security
Security.addProvider(new BouncyCastleProvider());
}
public static void main(String [] args) throws Exception {
vertx = Vertx.vertx();
HttpClient client = vertx.createHttpClient(new HttpClientOptions()
.setDefaultHost("test.myserver.com")
.setDefaultPort(8080)
.setSsl(true)
.setTcpKeepAlive(true)
.setTryUseCompression(true)
.setTrustAll(true)
.setPemKeyCertOptions(getPemKeyOptions("myTestIdentity")));
client.websocket("/connect", new CaseInsensitiveHeaders(), ws -> {
System.out.println("connected");
ws.exceptionHandler(error -> {
System.out.println(error.getLocalizedMessage());
});
ws.endHandler(aVoid -> {
System.out.println("end handler");
});
ws.closeHandler(aVoid -> {
System.out.println("close handler");
});
});
System.in.read();
}
public static PemKeyCertOptions getPemKeyOptions(String principalName) throws NoSuchAlgorithmException, IOException, OperatorCreationException, CertificateException, NoSuchProviderException {
KeyPair pair = getKeyPair();
Date yesterday = DateTime.now().minusDays(1).toDate();
Date aMonthOut = DateTime.now().plusMonths(1).toDate();
X500Name name = new X500Name("CN=" + principalName);
X509v3CertificateBuilder builder = new X509v3CertificateBuilder(name,
BigInteger.valueOf(new SecureRandom().nextLong()), yesterday, aMonthOut, name,
SubjectPublicKeyInfo.getInstance(pair.getPublic().getEncoded()));
AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder()
.find("SHA1withRSA");
AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder()
.find(sigAlgId);
ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(PrivateKeyFactory.createKey(pair.getPrivate().getEncoded()));
X509CertificateHolder holder = builder.build(sigGen);
org.bouncycastle.asn1.x509.Certificate eeX509CertificateStructure = holder.toASN1Structure();
CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
// Read Certificate
InputStream is1 = new ByteArrayInputStream(eeX509CertificateStructure.getEncoded());
X509Certificate theCert = (X509Certificate) cf.generateCertificate(is1);
is1.close();
return new PemKeyCertOptions().setKeyValue(getPemString("PRIVATE KEY", pair.getPrivate().getEncoded()))
.setCertValue(getPemString("CERTIFICATE", theCert.getEncoded()));
}
public static Buffer getPemString(String type, byte [] data) throws IOException {
StringWriter stringWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(stringWriter);
pemWriter.writeObject(new PemObject(type, data));
pemWriter.close();
System.out.println(stringWriter.toString());
return Buffer.buffer(stringWriter.toString());
}
private static KeyPair getKeyPair() throws NoSuchAlgorithmException {
// GENERATE THE PUBLIC/PRIVATE RSA KEY PAIR
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024, new SecureRandom());
return keyPairGenerator.generateKeyPair();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.