0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4634892
0N/A * @summary Ensure that authentication via CRAM-MD5 works.
0N/A */
0N/A
0N/A/*
0N/A * Can set logging to FINEST to view exchange.
0N/A */
0N/Aimport javax.security.sasl.*;
0N/Aimport javax.security.auth.callback.*;
0N/Aimport java.security.Security;
0N/A
0N/Apublic class Cram {
0N/A private static final String MECH = "CRAM-MD5";
0N/A private static final String SERVER_FQDN = "machineX.imc.org";
0N/A private static final String PROTOCOL = "jmx";
0N/A
0N/A private static final byte[] EMPTY = new byte[0];
0N/A private static boolean auto;
0N/A private static boolean verbose = false;
0N/A private static String pwfile, namesfile;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A if (args.length == 0) {
0N/A pwfile = "pw.properties";
0N/A namesfile = "names.properties";
0N/A auto = true;
0N/A } else {
0N/A int i = 0;
0N/A if (args[i].equals("-m")) {
0N/A i++;
0N/A auto = false;
0N/A }
0N/A if (args.length > i) {
0N/A pwfile = args[i++];
0N/A
0N/A if (args.length > i) {
0N/A namesfile = args[i++];
0N/A }
0N/A } else {
0N/A pwfile = "pw.properties";
0N/A namesfile = "names.properties";
0N/A }
0N/A }
0N/A
0N/A CallbackHandler clntCbh = new ClientCallbackHandler(auto);
0N/A
0N/A CallbackHandler srvCbh =
0N/A new PropertiesFileCallbackHandler(pwfile, namesfile, null);
0N/A
0N/A SaslClient clnt = Sasl.createSaslClient(
0N/A new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, null, clntCbh);
0N/A
0N/A SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN, null,
0N/A srvCbh);
0N/A
0N/A if (clnt == null) {
0N/A throw new IllegalStateException(
0N/A "Unable to find client impl for " + MECH);
0N/A }
0N/A if (srv == null) {
0N/A throw new IllegalStateException(
0N/A "Unable to find server impl for " + MECH);
0N/A }
0N/A
0N/A byte[] response = (clnt.hasInitialResponse()?
0N/A clnt.evaluateChallenge(EMPTY) : EMPTY);
0N/A byte[] challenge;
0N/A
0N/A while (!clnt.isComplete() || !srv.isComplete()) {
0N/A challenge = srv.evaluateResponse(response);
0N/A
0N/A if (challenge != null) {
0N/A response = clnt.evaluateChallenge(challenge);
0N/A }
0N/A }
0N/A
0N/A if (clnt.isComplete() && srv.isComplete()) {
0N/A if (verbose) {
0N/A System.out.println("SUCCESS");
0N/A System.out.println("authzid is " + srv.getAuthorizationID());
0N/A }
0N/A } else {
0N/A throw new IllegalStateException("FAILURE: mismatched state:" +
0N/A " client complete? " + clnt.isComplete() +
0N/A " server complete? " + srv.isComplete());
0N/A }
0N/A }
0N/A}