Example usage for org.bouncycastle.util.encoders Base64 encode

List of usage examples for org.bouncycastle.util.encoders Base64 encode

Introduction

In this page you can find the example usage for org.bouncycastle.util.encoders Base64 encode.

Prototype

public static byte[] encode(byte[] data) 

Source Link

Document

encode the input data producing a base 64 encoded byte array.

Usage

From source file:org.freenetproject.freemail.smtp.SMTPAuthTest.java

License:Open Source License

@Test
public void correctAuthPlainInitial() throws IOException {
    List<String> commands = new LinkedList<String>();

    final String authData = new String(
            Base64.encode((BASE64_USERNAME + "\0" + BASE64_USERNAME + "\0password").getBytes("ASCII")),
            "ASCII");
    commands.add("AUTH PLAIN " + authData);

    List<String> expectedResponse = new LinkedList<String>();
    expectedResponse.add("220 localhost ready");
    expectedResponse.add("235 Authenticated");

    runSimpleTest(commands, expectedResponse);
}

From source file:org.freenetproject.freemail.smtp.SMTPAuthTest.java

License:Open Source License

@Test
public void plainAuthWithoutAuthzid() throws IOException {
    List<String> commands = new LinkedList<String>();

    final String authData = new String(Base64.encode(("\0" + BASE64_USERNAME + "\0password").getBytes("ASCII")),
            "ASCII");
    commands.add("AUTH PLAIN " + authData);

    List<String> expectedResponse = new LinkedList<String>();
    expectedResponse.add("220 localhost ready");
    expectedResponse.add("235 Authenticated");

    runSimpleTest(commands, expectedResponse);
}

From source file:org.freenetproject.freemail.smtp.SMTPAuthTest.java

License:Open Source License

/**
 * Tests case where:/*ww w. j a v  a2s  .  co m*/
 *   * authzid is invalid
 *   * authcid is valid
 *   * passwd is valid (for authcid)
 *
 * This should fail since Freemail doesn't support authenticating as one user while authorizing (acting) as another.
 */
@Test
public void plainAuthWithInvalidAuthzidValidAuthcid() throws IOException {
    String authData = new String(
            Base64.encode(("nosuchuser\0" + BASE64_USERNAME + "\0password").getBytes("ASCII")), "ASCII");

    List<String> commands = new LinkedList<String>();
    commands.add("AUTH PLAIN " + authData);

    List<String> expectedResponse = new LinkedList<String>();
    expectedResponse.add("220 localhost ready");
    expectedResponse.add("535 Authentication failed");

    runSimpleTest(commands, expectedResponse);
}

From source file:org.freenetproject.freemail.smtp.SMTPAuthTest.java

License:Open Source License

/**
 * Tests case where:/*from   w w w .j  av a2s.  co  m*/
 *   * authzid is valid
 *   * authcid is invalid
 *   * passwd is valid (for authzid)
 *
 * This should fail since
 *   1. Freemail doesn't support authenticating as one user while authorizing (acting) as another
 *   2. Authenticating user is invalid
 */
@Test
public void plainAuthWithValidAuthzidInValidAuthcid() throws IOException {
    String authData = new String(Base64.encode((BASE64_USERNAME + "\0nosuchuser\0password").getBytes("ASCII")),
            "ASCII");

    List<String> commands = new LinkedList<String>();
    commands.add("AUTH PLAIN " + authData);

    List<String> expectedResponse = new LinkedList<String>();
    expectedResponse.add("220 localhost ready");
    expectedResponse.add("535 Authentication failed");

    runSimpleTest(commands, expectedResponse);
}

From source file:org.freenetproject.freemail.smtp.SMTPAuthTest.java

License:Open Source License

/**
 * Tests case where:/* w  ww. jav  a 2s  .c  o  m*/
 *   * authzid is valid
 *   * authcid is valid (but != authzid)
 *   * passwd is valid (for both user)
 *
 * This should fail since
 *   1. Freemail doesn't support authenticating as one user while authorizing (acting) as another
 *   2. Password is invalid
 */
@Test
public void plainAuthTwoUsersValidPassword() throws IOException {
    String authData = new String(
            Base64.encode((BASE64_USERNAMES[0] + "\0" + BASE64_USERNAMES[1] + "\0password").getBytes("ASCII")),
            "ASCII");

    List<String> commands = new LinkedList<String>();
    commands.add("AUTH PLAIN " + authData);

    List<String> expectedResponse = new LinkedList<String>();
    expectedResponse.add("220 localhost ready");
    expectedResponse.add("535 Authentication failed");

    runSimpleTest(commands, expectedResponse);
}

From source file:org.freenetproject.freemail.smtp.SMTPAuthTest.java

License:Open Source License

/**
 * Checks that the server handles receiving AUTH PLAIN data with only the username
 *//*from w  w  w .ja  va2  s  .co  m*/
@Test
public void plainAuthOnlyUsername() throws IOException {
    List<String> commands = new LinkedList<String>();
    commands.add("AUTH PLAIN " + new String(Base64.encode((BASE64_USERNAME).getBytes("ASCII")), "ASCII"));

    List<String> expectedResponse = new LinkedList<String>();
    expectedResponse.add("220 localhost ready");
    expectedResponse.add("501 Invalid arguments to plain auth");

    runSimpleTest(commands, expectedResponse);
}

From source file:org.freenetproject.freemail.smtp.SMTPAuthTest.java

License:Open Source License

/**
 * Tests authentication of a valid username:password combination when using the full email for the username
 *//*  www. jav a2 s  .co  m*/
@Test
public void plainAuthWithEmailAsUsername() throws IOException {
    String authData = new String(
            Base64.encode(("\0zidel@" + BASE64_USERNAME + ".freemail\0password").getBytes("ASCII")), "ASCII");

    List<String> commands = new LinkedList<String>();
    commands.add("AUTH PLAIN " + authData);

    List<String> expectedResponse = new LinkedList<String>();
    expectedResponse.add("220 localhost ready");
    expectedResponse.add("535 Authentication failed");

    runSimpleTest(commands, expectedResponse);
}

From source file:org.freenetproject.freemail.smtp.SMTPAuthTest.java

License:Open Source License

@Test
public void correctAuthLogin() throws IOException {
    List<String> commands = new LinkedList<String>();
    commands.add("AUTH LOGIN");
    commands.add(new String(Base64.encode(BASE64_USERNAME.getBytes("ASCII")), "ASCII"));
    commands.add(new String(Base64.encode("password".getBytes("ASCII")), "ASCII"));

    List<String> expectedResponse = new LinkedList<String>();
    expectedResponse.add("220 localhost ready");
    expectedResponse.add("334 " + new String(Base64.encode("Username:".getBytes("ASCII")), "ASCII"));
    expectedResponse.add("334 " + new String(Base64.encode("Password:".getBytes("ASCII")), "ASCII"));
    expectedResponse.add("235 Authenticated");

    runSimpleTest(commands, expectedResponse);
}

From source file:org.freenetproject.freemail.smtp.SMTPHandler.java

License:Open Source License

private void handle_auth(SMTPCommand cmd) {
    String uname;/*from ww  w .  java2s  .  c  om*/
    String password;

    if (cmd.args.length == 0) {
        this.ps.print("504 No auth type given\r\n");
        return;
    }

    if (this.account != null) {
        this.ps.print("503 Already authenticated\r\n");
        return;
    }

    if (cmd.args[0].equalsIgnoreCase("login")) {
        try {
            this.ps.print("334 " + new String(Base64.encode("Username:".getBytes("UTF-8"))) + "\r\n");
        } catch (UnsupportedEncodingException e) {
            //JVMs are required to support UTF-8, so we can assume it is always available
            throw new AssertionError("JVM doesn't support UTF-8 charset");
        }

        String b64username;
        String b64password;
        try {
            b64username = this.bufrdr.readLine();
        } catch (IOException ioe) {
            return;
        }
        if (b64username == null)
            return;

        try {
            this.ps.print("334 " + new String(Base64.encode("Password:".getBytes("UTF-8"))) + "\r\n");
        } catch (UnsupportedEncodingException e) {
            //JVMs are required to support UTF-8, so we can assume it is always available
            throw new AssertionError("JVM doesn't support UTF-8 charset");
        }
        try {
            b64password = this.bufrdr.readLine();
        } catch (IOException ioe) {
            return;
        }
        if (b64password == null)
            return;

        try {
            uname = new String(Base64.decode(b64username.getBytes("UTF-8")));
            password = new String(Base64.decode(b64password.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
            //JVMs are required to support UTF-8, so we can assume it is always available
            throw new AssertionError("JVM doesn't support UTF-8 charset");
        }
    } else if (cmd.args[0].equalsIgnoreCase("plain")) {
        String b64creds;

        if (cmd.args.length > 1) {
            b64creds = cmd.args[1];
        } else {
            this.ps.print("334 \r\n");
            try {
                b64creds = this.bufrdr.readLine();
                if (b64creds == null)
                    return;
            } catch (IOException ioe) {
                return;
            }
        }

        if (b64creds.equals("*")) {
            this.ps.print("501 Authentication canceled\r\n");
            return;
        }

        String creds_plain;
        try {
            creds_plain = new String(Base64.decode(b64creds.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
            //JVMs are required to support UTF-8, so we can assume it is always available
            throw new AssertionError("JVM doesn't support UTF-8 charset");
        }
        String[] creds = creds_plain.split("\0");
        if (creds.length != 3) {
            this.ps.print("501 Invalid arguments to plain auth\r\n");
            return;
        }

        String authzid = creds[0];
        uname = creds[1];
        password = creds[2];

        if (!authzid.isEmpty()) {
            if (!authzid.equals(uname)) {
                this.ps.print("535 Authentication failed\r\n");
                return;
            }
        }
    } else {
        this.ps.print("504 Auth type unimplemented - weren't you listening?\r\n");
        return;
    }

    if (uname.contains("@") && uname.endsWith(".freemail")) {
        //Extract the base32 identity string and convert it to base64
        uname = uname.substring(uname.indexOf("@") + 1, uname.length() - ".freemail".length());

        //We need to use the Freenet Base64 encoder here since it uses a slightly different set
        //of characters
        uname = freenet.support.Base64.encode(Base32.decode(uname));

        Logger.debug(this, "Extracted Identity string: " + uname);
    }

    account = accountmanager.authenticate(uname, password);
    if (account != null) {
        this.ps.print("235 Authenticated\r\n");
    } else {
        this.ps.print("535 Authentication failed\r\n");
    }
}

From source file:org.freenetproject.freemail.smtp.SMTPHandlerTest.java

License:Open Source License

@Test
public void mailAfterAuth() throws IOException {
    List<String> commands = new LinkedList<String>();
    final String authData = new String(Base64.encode(("\0" + BASE64_USERNAME + "\0password").getBytes("ASCII")),
            "ASCII");
    commands.add("AUTH PLAIN " + authData);
    commands.add("MAIL");

    List<String> expectedResponse = new LinkedList<String>();
    expectedResponse.add("220 localhost ready");
    expectedResponse.add("235 Authenticated");
    expectedResponse.add("250 OK");

    runSimpleTest(commands, expectedResponse);
}