From 085c3469e95c6ad6c465767f0fdefa216ea6cd0c Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 19 Jan 2016 17:04:11 +0100 Subject: [PATCH] Fix XMPPTCPConnection.proceedTLSReceived() The method was changed in c6594aec2f5a07618738e3e838b88238abbd6139, but this change causes issues if Smack is used on Android *without* a custom SSLContext: Caused by: java.security.KeyStoreException: java.security.NoSuchAlgorithmException: KeyStore jks implementation not found at java.security.KeyStore.getInstance(KeyStore.java:119) at org.jivesoftware.smack.tcp.XMPPTCPConnection.proceedTLSReceived(XMPPTCPConnection.java:697) Caused by: java.security.NoSuchAlgorithmException: KeyManagerFactory SunX509 implementation not found at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:177) at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151) at javax.net.ssl.KeyManagerFactory.getInstance(KeyManagerFactory.java:77) at org.jivesoftware.smack.tcp.XMPPTCPConnection.proceedTLSReceived(XMPPTCPConnection.java:708) --- .../smack/tcp/XMPPTCPConnection.java | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 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 4883dcf64..cbd6a54cb 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 @@ -674,7 +674,8 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { PasswordCallback pcb = null; if (context == null) { - if(config.getKeystoreType().equals("PKCS11")) { + final String keyStoreType = config.getKeystoreType(); + if ("PKCS11".equals(keyStoreType)) { try { Constructor c = Class.forName("sun.security.pkcs11.SunPKCS11").getConstructor(InputStream.class); String pkcs11Config = "name = SmartCard\nlibrary = "+config.getPKCS11Library(); @@ -687,47 +688,53 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { ks.load(null,pcb.getPassword()); } catch (Exception e) { + LOGGER.log(Level.WARNING, "Exception", e); ks = null; pcb = null; } } - else if(config.getKeystoreType().equals("Apple")) { + else if ("Apple".equals(keyStoreType)) { ks = KeyStore.getInstance("KeychainStore","Apple"); ks.load(null,null); //pcb = new PasswordCallback("Apple Keychain",false); //pcb.setPassword(null); } - else { - ks = KeyStore.getInstance(config.getKeystoreType()); + 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; pcb = null; } } - KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); - try { - if(pcb == null) { - kmf.init(ks,null); - } else { - kmf.init(ks,pcb.getPassword()); - pcb.clearPassword(); - } - kms = kmf.getKeyManagers(); - } catch (NullPointerException npe) { - kms = null; - } - } - // If the user didn't specify a SSLContext, use the default one - if (context == null) { + if (ks != null) { + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + try { + if (pcb == null) { + kmf.init(ks, null); + } + else { + kmf.init(ks, pcb.getPassword()); + pcb.clearPassword(); + } + kms = kmf.getKeyManagers(); + } + catch (NullPointerException npe) { + LOGGER.log(Level.WARNING, "NullPointerException", npe); + } + } + + // If the user didn't specify a SSLContext, use the default one context = SSLContext.getInstance("TLS"); context.init(kms, null, new java.security.SecureRandom()); } + Socket plain = socket; // Secure the plain connection socket = context.getSocketFactory().createSocket(plain,