Commit a17da995 authored by Dave Cridland's avatar Dave Cridland

OF-405 Addressing review comments from Simon White

parent 78e4eff7
...@@ -182,37 +182,8 @@ public class SASLAuthentication { ...@@ -182,37 +182,8 @@ public class SASLAuthentication {
if (!(session instanceof ClientSession) && !(session instanceof IncomingServerSession)) { if (!(session instanceof ClientSession) && !(session instanceof IncomingServerSession)) {
return ""; return "";
} }
StringBuilder sb = new StringBuilder(195); Element mechs = getSASLMechanismsElement(session);
sb.append("<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">"); return mechs.asXML();
if (session instanceof IncomingServerSession) {
// Server connections don't follow the same rules as clients
if (session.isSecure()) {
boolean usingSelfSigned = true;
try {
X509Certificate trusted = CertificateManager.getEndEntityCertificate(session.getConnection().getPeerCertificates(), SSLConfig.getKeyStore(), SSLConfig.gets2sTrustStore());
usingSelfSigned = trusted == null;
} catch (IOException ex) {
Log.warn("Exception occurred while trying to determine whether remote certificate is trusted. Proceeding as if it is.", ex);
usingSelfSigned = true;
}
if (!usingSelfSigned) {
// Offer SASL EXTERNAL only if TLS has already been negotiated and the peer is not
// using a self-signed certificate
sb.append("<mechanism>EXTERNAL</mechanism>");
}
}
}
else {
for (String mech : getSupportedMechanisms()) {
sb.append("<mechanism>");
sb.append(mech);
sb.append("</mechanism>");
}
}
sb.append("</mechanisms>");
return sb.toString();
} }
public static Element getSASLMechanismsElement(Session session) { public static Element getSASLMechanismsElement(Session session) {
...@@ -230,7 +201,7 @@ public class SASLAuthentication { ...@@ -230,7 +201,7 @@ public class SASLAuthentication {
X509Certificate trusted = CertificateManager.getEndEntityCertificate(((LocalSession)session).getConnection().getPeerCertificates(), SSLConfig.getKeyStore(), SSLConfig.gets2sTrustStore()); X509Certificate trusted = CertificateManager.getEndEntityCertificate(((LocalSession)session).getConnection().getPeerCertificates(), SSLConfig.getKeyStore(), SSLConfig.gets2sTrustStore());
usingSelfSigned = trusted == null; usingSelfSigned = trusted == null;
} catch (IOException ex) { } catch (IOException ex) {
Log.warn("Exception occurred while trying to determine whether remote certificate is trusted. Proceeding as if it is.", ex); Log.warn("Exception occurred while trying to determine whether remote certificate is trusted. Treating as untrusted.", ex);
usingSelfSigned = true; usingSelfSigned = true;
} }
if (!usingSelfSigned) { if (!usingSelfSigned) {
......
...@@ -223,10 +223,11 @@ public class CertificateManager { ...@@ -223,10 +223,11 @@ public class CertificateManager {
* Decide whether or not to trust the given supplied certificate chain, returning the * Decide whether or not to trust the given supplied certificate chain, returning the
* End Entity Certificate in this case where it can, and null otherwise. * End Entity Certificate in this case where it can, and null otherwise.
* A self-signed certificate will, for example, return null. * A self-signed certificate will, for example, return null.
* For certain failures, we SHOULD generate an exception - revocations and the like. * For certain failures, we SHOULD generate an exception - revocations and the like,
* but we currently do not.
* *
* @param certChain an array of X509Certificate where the first one is the endEntityCertificate. * @param certChain an array of X509Certificate where the first one is the endEntityCertificate.
* @return a boolean indicating whether the endEntityCertificate should be trusted. * @return trusted end-entity certificate, or null.
*/ */
public static X509Certificate getEndEntityCertificate(Certificate chain[], public static X509Certificate getEndEntityCertificate(Certificate chain[],
KeyStore certStore, KeyStore trustStore) { KeyStore certStore, KeyStore trustStore) {
...@@ -235,7 +236,7 @@ public class CertificateManager { ...@@ -235,7 +236,7 @@ public class CertificateManager {
} }
X509Certificate first = (X509Certificate) chain[0]; X509Certificate first = (X509Certificate) chain[0];
if (chain.length == 1 if (chain.length == 1
&& first.getSubjectDN().equals(first.getIssuerDN())) { && first.getSubjectX500Principal().equals(first.getIssuerX500Principal())) {
// Chain is single cert, and self-signed. // Chain is single cert, and self-signed.
try { try {
if (trustStore.getCertificateAlias(first) != null) { if (trustStore.getCertificateAlias(first) != null) {
...@@ -243,7 +244,7 @@ public class CertificateManager { ...@@ -243,7 +244,7 @@ public class CertificateManager {
return first; return first;
} }
} catch (KeyStoreException e) { } catch (KeyStoreException e) {
// Ignore. Log.warn("Keystore error while looking for self-signed cert; assuming untrusted.");
} }
return null; return null;
} }
...@@ -299,7 +300,7 @@ public class CertificateManager { ...@@ -299,7 +300,7 @@ public class CertificateManager {
ls.add((X509Certificate) chain[i]); ls.add((X509Certificate) chain[i]);
} }
for (X509Certificate last = ls.get(ls.size() - 1); !last for (X509Certificate last = ls.get(ls.size() - 1); !last
.getIssuerDN().equals(last.getSubjectDN()); last = ls .getIssuerX500Principal().equals(last.getSubjectX500Principal()); last = ls
.get(ls.size() - 1)) { .get(ls.size() - 1)) {
X509CertSelector sel = new X509CertSelector(); X509CertSelector sel = new X509CertSelector();
sel.setSubject(last.getIssuerX500Principal()); sel.setSubject(last.getIssuerX500Principal());
...@@ -319,6 +320,7 @@ public class CertificateManager { ...@@ -319,6 +320,7 @@ public class CertificateManager {
} catch (CertPathValidatorException e) { } catch (CertPathValidatorException e) {
Log.warn("Path validator: " + e.getMessage()); Log.warn("Path validator: " + e.getMessage());
} catch (Exception e) { } catch (Exception e) {
Log.warn("Unkown exception while validating certificate chain: " + e.getMessage());
} }
return null; return null;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment