Guest User

Untitled

a guest
Jun 11th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. public class App {
  2. public static void main(String[] args) {
  3. String serverUrl = "ssl://129.6.60.24:8883";
  4. String caFilePath = "./src/main/resources/ca.crt";
  5. String clientCrtFilePath = "./src/main/resources/brokerCertificate.crt";
  6. String clientKeyFilePath = "./src/main/resources/ca.key";
  7. String mqttUserName = "cpcc";
  8. String mqttPassword = "1qaz!QAZ1qaz";
  9. MqttClient client;
  10.  
  11. try {
  12. client = new MqttClient(serverUrl, "TestBedBroker");
  13. client.setCallback(new SimpleMqttCallBack());
  14. MqttConnectOptions options = new MqttConnectOptions();
  15. options.setUserName(mqttUserName);
  16. options.setPassword(mqttPassword.toCharArray());
  17. SSLSocketFactory socketFactory = SslUtil.getSocketFactory(caFilePath, clientCrtFilePath, clientKeyFilePath, "1qaz!QAZ1qaz");
  18. options.setSocketFactory(socketFactory);
  19. System.out.println("starting connect the server...");
  20. client.connect(options);
  21. System.out.println("connected!");
  22. Thread.sleep(1000);
  23. client.disconnect();
  24. System.out.println("disconnected!");
  25.  
  26. }
  27. catch (MqttException e) {
  28. e.printStackTrace();
  29. }
  30. catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  34.  
  35. }
  36.  
  37. public class SslUtil {
  38. public static SSLSocketFactory getSocketFactory(final String caCrtFile, final String crtFile, final String keyFile, final String password) throws Exception {
  39. Security.addProvider(new BouncyCastleProvider());
  40.  
  41. // load CA certificate
  42. X509Certificate caCert = null;
  43.  
  44. FileInputStream fis = new FileInputStream(caCrtFile);
  45. BufferedInputStream bis = new BufferedInputStream(fis);
  46. CertificateFactory cf = CertificateFactory.getInstance("X.509");
  47.  
  48. while (bis.available() > 0) {
  49. caCert = (X509Certificate) cf.generateCertificate(bis);
  50. // System.out.println(caCert.toString());
  51. }
  52.  
  53. // load client certificate
  54. bis = new BufferedInputStream(new FileInputStream(crtFile));
  55. X509Certificate cert = null;
  56. while (bis.available() > 0) {
  57. cert = (X509Certificate) cf.generateCertificate(bis);
  58. // System.out.println(caCert.toString());
  59. }
  60.  
  61. // load client private key
  62. PEMParser pemParser = new PEMParser(new FileReader(keyFile));
  63. Object object = pemParser.readObject();
  64. PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(password.toCharArray());
  65. JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
  66. KeyPair key;
  67. if (object instanceof PEMEncryptedKeyPair) {
  68. System.out.println("Encrypted key - we will use provided password");
  69. key = converter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decProv));
  70. } else {
  71. System.out.println("Unencrypted key - no password needed");
  72. key = converter.getKeyPair((PEMKeyPair) object);
  73. }
  74. pemParser.close();
  75.  
  76. // CA certificate is used to authenticate server
  77. KeyStore caKs = KeyStore.getInstance(KeyStore.getDefaultType());
  78. caKs.load(null, null);
  79. caKs.setCertificateEntry("ca-certificate", caCert);
  80. TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
  81. tmf.init(caKs);
  82.  
  83. // client key and certificates are sent to server so it can authenticate
  84. // us
  85. KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
  86. ks.load(null, null);
  87. ks.setCertificateEntry("certificate", cert);
  88. ks.setKeyEntry("private-key", key.getPrivate(), password.toCharArray(), new java.security.cert.Certificate[] { cert });
  89. KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  90. kmf.init(ks, password.toCharArray());
  91.  
  92. // finally, create SSL socket factory
  93. SSLContext context = SSLContext.getInstance("TLSv1.2");
  94. context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
  95.  
  96. return context.getSocketFactory();
  97. }
  98. }
  99.  
  100. PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Add Comment
Please, Sign In to add comment