diff --git a/app/src/main/java/com.sun.mail.imap.protocol/IMAPSaslAuthenticator.java b/app/src/main/java/com.sun.mail.imap.protocol/IMAPSaslAuthenticator.java index 954a83f918..9ce30797af 100644 --- a/app/src/main/java/com.sun.mail.imap.protocol/IMAPSaslAuthenticator.java +++ b/app/src/main/java/com.sun.mail.imap.protocol/IMAPSaslAuthenticator.java @@ -58,7 +58,7 @@ public class IMAPSaslAuthenticator implements SaslAuthenticator { final String p) throws ProtocolException { if (!pr.hasCapability("AUTH=CRAM-MD5")) - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("SASL not supported"); List v = new ArrayList<>(); String tag = null; @@ -66,11 +66,11 @@ public class IMAPSaslAuthenticator implements SaslAuthenticator { boolean done = false; try { - Log.i("IMAP SASL command=AUTHENTICATE"); + Log.i("SASL IMAP command=AUTHENTICATE"); Argument args = new Argument(); args.writeAtom("CRAM-MD5"); tag = pr.writeCommand("AUTHENTICATE", args); - Log.i("IMAP SASL tag=" + tag); + Log.i("SASL IMAP tag=" + tag); } catch (Exception ex) { r = Response.byeResponse(ex); done = true; @@ -79,13 +79,13 @@ public class IMAPSaslAuthenticator implements SaslAuthenticator { while (!done) { try { r = pr.readResponse(); - Log.i("IMAP SASL response=" + r); + Log.i("SASL IMAP response=" + r); if (r.isContinuation()) { byte[] nonce = Base64.decode(r.getRest(), Base64.NO_WRAP); - Log.i("IMAP SASL nonce=" + new String(nonce)); + Log.i("SASL IMAP nonce=" + new String(nonce)); String hmac = Helper.HMAC("MD5", 64, p.getBytes(), nonce); String hash = Base64.encodeToString((u + " " + hmac).getBytes(), Base64.NO_WRAP) + "\r\n"; - Log.i("IMAP SASL hash=" + hash); + Log.i("SASL IMAP hash=" + hash); pr.getIMAPOutputStream().write(hash.getBytes()); pr.getIMAPOutputStream().flush(); } else if (r.isTagged() && r.getTag().equals(tag)) @@ -99,11 +99,18 @@ public class IMAPSaslAuthenticator implements SaslAuthenticator { v.add(r); } - Response[] responses = v.toArray(new Response[0]); - pr.handleCapabilityResponse(responses); - pr.notifyResponseHandlers(responses); - pr.handleLoginResult(r); - pr.setCapabilities(r); + try { + Response[] responses = v.toArray(new Response[0]); + pr.handleCapabilityResponse(responses); + pr.notifyResponseHandlers(responses); + pr.handleLoginResult(r); + pr.setCapabilities(r); + } catch (ProtocolException ex) { + Log.w(ex); + throw new UnsupportedOperationException("SASL not authenticated"); + } + + Log.i("SASL IMAP authenticated"); return true; } } diff --git a/app/src/main/java/com.sun.mail.smtp/SMTPSaslAuthenticator.java b/app/src/main/java/com.sun.mail.smtp/SMTPSaslAuthenticator.java index a76ce2a2db..19a32a0f92 100644 --- a/app/src/main/java/com.sun.mail.smtp/SMTPSaslAuthenticator.java +++ b/app/src/main/java/com.sun.mail.smtp/SMTPSaslAuthenticator.java @@ -56,7 +56,7 @@ public class SMTPSaslAuthenticator implements SaslAuthenticator { final String p) throws MessagingException { if (!pr.supportsAuthentication("CRAM-MD5")) - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException("SASL not supported"); // https://tools.ietf.org/html/rfc4954 int resp = simpleCommand(pr, "AUTH CRAM-MD5"); @@ -65,11 +65,15 @@ public class SMTPSaslAuthenticator implements SaslAuthenticator { resp = simpleCommand(pr, "AUTH CRAM-MD5"); } - if (resp == 235) // Authentication Succeeded + if (resp == 235) { // Authentication Succeeded + Log.i("SASL SMTP already authenticated"); return true; + } - if (resp != 334) // server challenge - return false; + if (resp != 334) { // server challenge + Log.i("SASL SMTP response=" + resp); + throw new UnsupportedOperationException("SASL not supported"); + } try { String t = responseText(pr); @@ -81,13 +85,19 @@ public class SMTPSaslAuthenticator implements SaslAuthenticator { throw new MessagingException("CRAM-MD5", ex); } - return (resp == 235); + if (resp != 235) { + Log.i("SASL SMTP not authenticated response=" + resp); + throw new UnsupportedOperationException("SASL not authenticated"); + } + + Log.i("SASL SMTP authenticated"); + return true; } private static int simpleCommand(SMTPTransport pr, String command) throws MessagingException { - Log.i("SMTP SASL command=" + command); + Log.i("SASL SMTP command=" + command); int resp = pr.simpleCommand(command); - Log.i("SMTP SASL response=" + pr.getLastServerResponse()); + Log.i("SASL SMTP response=" + pr.getLastServerResponse()); return resp; }