Search in sources :

Example 1 with SaslCramMD5

use of gnu.inet.util.SaslCramMD5 in project ats-framework by Axway.

the class InetSmtpConnection method authenticate.

// -- Authentication --
/**
     * Authenticates the connection using the specified SASL mechanism,
     * username, and password.
     * @param mechanism a SASL authentication mechanism, e.g. LOGIN, PLAIN,
     * CRAM-MD5, GSSAPI
     * @param username the authentication principal
     * @param password the authentication credentials
     * @return true if authentication was successful, false otherwise
     */
public boolean authenticate(String mechanism, String username, String password) throws IOException {
    try {
        String[] m = new String[] { mechanism };
        CallbackHandler ch = new SaslCallbackHandler(username, password);
        // Avoid lengthy callback procedure for GNU Crypto
        HashMap<String, String> p = new HashMap<String, String>();
        p.put("gnu.crypto.sasl.username", username);
        p.put("gnu.crypto.sasl.password", password);
        SaslClient sasl = Sasl.createSaslClient(m, null, "smtp", socket.getInetAddress().getHostName(), p, ch);
        if (sasl == null) {
            // Fall back to home-grown SASL clients
            if ("LOGIN".equalsIgnoreCase(mechanism)) {
                sasl = new SaslLogin(username, password);
            } else if ("PLAIN".equalsIgnoreCase(mechanism)) {
                sasl = new SaslPlain(username, password);
            } else if ("CRAM-MD5".equalsIgnoreCase(mechanism)) {
                sasl = new SaslCramMD5(username, password);
            } else {
                return false;
            }
        }
        StringBuffer cmd = new StringBuffer(AUTH);
        cmd.append(' ');
        cmd.append(mechanism);
        if (sasl.hasInitialResponse()) {
            cmd.append(' ');
            byte[] init = sasl.evaluateChallenge(new byte[0]);
            if (init.length == 0) {
                cmd.append('=');
            } else {
                cmd.append(new String(BASE64.encode(init), "US-ASCII"));
            }
        }
        send(cmd.toString());
        while (true) {
            switch(getAllResponses()) {
                case 334:
                    try {
                        byte[] c0 = response.getBytes("US-ASCII");
                        // challenge
                        byte[] c1 = BASE64.decode(c0);
                        byte[] r0 = sasl.evaluateChallenge(c1);
                        // response
                        byte[] r1 = BASE64.encode(r0);
                        out.write(r1);
                        out.write(0x0d);
                        out.flush();
                        log.trace("> " + new String(r1, "US-ASCII"));
                    } catch (SaslException e) {
                        // Error in SASL challenge evaluation - cancel exchange
                        out.write(0x2a);
                        out.write(0x0d);
                        out.flush();
                        log.trace("> *");
                    }
                    break;
                case 235:
                    String qop = (String) sasl.getNegotiatedProperty(Sasl.QOP);
                    if ("auth-int".equalsIgnoreCase(qop) || "auth-conf".equalsIgnoreCase(qop)) {
                        InputStream is = socket.getInputStream();
                        is = new BufferedInputStream(is);
                        is = new SaslInputStream(sasl, is);
                        is = new CRLFInputStream(is);
                        in = new LineInputStream(is);
                        OutputStream os = socket.getOutputStream();
                        os = new BufferedOutputStream(os);
                        os = new SaslOutputStream(sasl, os);
                        out = new CRLFOutputStream(os);
                    }
                    return true;
                default:
                    return false;
            }
        }
    } catch (SaslException e) {
        log.error(e.getMessage(), e);
        // No provider for mechanism
        return false;
    } catch (RuntimeException e) {
        log.error(e.getMessage(), e);
        // No javax.security.sasl classes
        return false;
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) SaslCallbackHandler(gnu.inet.util.SaslCallbackHandler) SaslInputStream(gnu.inet.util.SaslInputStream) HashMap(java.util.HashMap) CRLFInputStream(gnu.inet.util.CRLFInputStream) BufferedInputStream(java.io.BufferedInputStream) LineInputStream(gnu.inet.util.LineInputStream) SaslInputStream(gnu.inet.util.SaslInputStream) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) MessageOutputStream(gnu.inet.util.MessageOutputStream) OutputStream(java.io.OutputStream) SaslOutputStream(gnu.inet.util.SaslOutputStream) CRLFOutputStream(gnu.inet.util.CRLFOutputStream) SaslException(javax.security.sasl.SaslException) CRLFOutputStream(gnu.inet.util.CRLFOutputStream) SaslClient(javax.security.sasl.SaslClient) SaslPlain(gnu.inet.util.SaslPlain) SaslOutputStream(gnu.inet.util.SaslOutputStream) BufferedInputStream(java.io.BufferedInputStream) SaslCramMD5(gnu.inet.util.SaslCramMD5) SaslCallbackHandler(gnu.inet.util.SaslCallbackHandler) CRLFInputStream(gnu.inet.util.CRLFInputStream) LineInputStream(gnu.inet.util.LineInputStream) BufferedOutputStream(java.io.BufferedOutputStream) SaslLogin(gnu.inet.util.SaslLogin)

Aggregations

CRLFInputStream (gnu.inet.util.CRLFInputStream)1 CRLFOutputStream (gnu.inet.util.CRLFOutputStream)1 LineInputStream (gnu.inet.util.LineInputStream)1 MessageOutputStream (gnu.inet.util.MessageOutputStream)1 SaslCallbackHandler (gnu.inet.util.SaslCallbackHandler)1 SaslCramMD5 (gnu.inet.util.SaslCramMD5)1 SaslInputStream (gnu.inet.util.SaslInputStream)1 SaslLogin (gnu.inet.util.SaslLogin)1 SaslOutputStream (gnu.inet.util.SaslOutputStream)1 SaslPlain (gnu.inet.util.SaslPlain)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 SaslClient (javax.security.sasl.SaslClient)1 SaslException (javax.security.sasl.SaslException)1