Skip to content

Instantly share code, notes, and snippets.

@tienthanh2509
Last active November 15, 2017 04:56
Show Gist options
  • Save tienthanh2509/7006a6ec8d404bccd887a90bbd14dcc4 to your computer and use it in GitHub Desktop.
Save tienthanh2509/7006a6ec8d404bccd887a90bbd14dcc4 to your computer and use it in GitHub Desktop.
Test RabbitMQ with validating server certificates
import com.google.gson.JsonObject;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
/**
* Snnd AMQP
*/
public class RequestDeviceCert {
private final static String QUEUE_NAME = "-----------------------";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setUri(new URI("amqps://admin:xxxxxx@localhost/%2F"));
factory.useSslProtocol(getSSLContextFromKeyStore());
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
JsonObject message = new JsonObject();
message.addProperty("name", "Hello World!");
channel.basicPublish("", QUEUE_NAME, null, message.toString().getBytes("UTF-8"));
System.out.println(" [o] Sent '" + message + "'");
channel.close();
connection.close();
}
/**
* Get SSLContext from CA bundle
*
* @return SSLContext
* @throws Exception FileNotFoundException...
*/
private static SSLContext getSSLContext() throws Exception {
InputStream is = new FileInputStream("ca-bundle.crt");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate caCert = (X509Certificate) cf.generateCertificate(is);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null);
ks.setCertificateEntry("caCert", caCert);
tmf.init(ks);
SSLContext c = SSLContext.getInstance("TLSv1.2");
c.init(null, tmf.getTrustManagers(), null);
return c;
}
/**
* Get SSLContext from CA bundle
*
* @return SSLContext
* @throws KeyStoreException
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws KeyManagementException
*/
private static SSLContext getSSLContextFromKeyStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException {
char[] trustPassphrase = "".toCharArray();
KeyStore tks = KeyStore.getInstance("JKS");
tks.load(new FileInputStream("ale-bundle.jks"), trustPassphrase);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(tks);
SSLContext c = SSLContext.getInstance("TLSv1.1");
c.init(null, tmf.getTrustManagers(), null);
return c;
}
}
@tienthanh2509
Copy link
Author

Exception if validating falied

Exception in thread "main" 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
	at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
	at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1959)
	at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
	at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
	at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1514)
	at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
	at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1026)
	at sun.security.ssl.Handshaker.process_record(Handshaker.java:961)
	at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
	at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
	at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:757)
	at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
	at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
	at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
	at java.io.DataOutputStream.flush(DataOutputStream.java:123)
	at com.rabbitmq.client.impl.SocketFrameHandler.sendHeader(SocketFrameHandler.java:129)
	at com.rabbitmq.client.impl.SocketFrameHandler.sendHeader(SocketFrameHandler.java:134)
	at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:276)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:590)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:612)
	at RequestDeviceCert.main(RequestDeviceCert.java:27)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:397)
	at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:302)
	at sun.security.validator.Validator.validate(Validator.java:260)
	at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
	at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
	at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
	at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1496)
	... 16 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
	at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
	at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
	at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:392)
	... 22 more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment