From e0cbb95b5de9ad452e6944fc189dd4485403f22d Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 21 Jan 2016 20:20:42 +0100 Subject: [PATCH] Check callbackHandler/keystorePath for null before using it. --- .../smack/tcp/XMPPTCPConnection.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java index 14da76ff4..51269401a 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java @@ -91,6 +91,7 @@ import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; import javax.security.auth.callback.PasswordCallback; import java.io.BufferedReader; @@ -675,6 +676,8 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { if (context == null) { final String keyStoreType = config.getKeystoreType(); + final CallbackHandler callbackHandler = config.getCallbackHandler(); + final String keystorePath = config.getKeystorePath(); if ("PKCS11".equals(keyStoreType)) { try { Constructor c = Class.forName("sun.security.pkcs11.SunPKCS11").getConstructor(InputStream.class); @@ -684,7 +687,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { Security.addProvider(p); ks = KeyStore.getInstance("PKCS11",p); pcb = new PasswordCallback("PKCS11 Password: ",false); - this.config.getCallbackHandler().handle(new Callback[]{pcb}); + callbackHandler.handle(new Callback[]{pcb}); ks.load(null,pcb.getPassword()); } catch (Exception e) { @@ -700,14 +703,16 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { } else if (keyStoreType != null){ ks = KeyStore.getInstance(keyStoreType); - try { - pcb = new PasswordCallback("Keystore Password: ",false); - config.getCallbackHandler().handle(new Callback[]{pcb}); - ks.load(new FileInputStream(config.getKeystorePath()), pcb.getPassword()); - } - catch(Exception e) { - LOGGER.log(Level.WARNING, "Exception", e); - ks = null; + if (callbackHandler != null && StringUtils.isNotEmpty(keystorePath)) { + try { + pcb = new PasswordCallback("Keystore Password: ", false); + callbackHandler.handle(new Callback[] { pcb }); + ks.load(new FileInputStream(keystorePath), pcb.getPassword()); + } + catch (Exception e) { + LOGGER.log(Level.WARNING, "Exception", e); + ks = null; + } } }