Path: blob/master/test/jdk/com/sun/security/sasl/Cram.java
41152 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 463489226* @modules jdk.security.auth27* @summary Ensure that authentication via CRAM-MD5 works.28*/2930/*31* Can set logging to FINEST to view exchange.32*/33import javax.security.auth.callback.CallbackHandler;34import javax.security.sasl.Sasl;35import javax.security.sasl.SaslClient;36import javax.security.sasl.SaslServer;3738public class Cram {39private static final String MECH = "CRAM-MD5";40private static final String SERVER_FQDN = "machineX.imc.org";41private static final String PROTOCOL = "jmx";4243private static final byte[] EMPTY = new byte[0];44private static boolean auto;45private static boolean verbose = false;46private static String pwfile, namesfile;4748public static void main(String[] args) throws Exception {49if (args.length == 0) {50pwfile = "pw.properties";51namesfile = "names.properties";52auto = true;53} else {54int i = 0;55if (args[i].equals("-m")) {56i++;57auto = false;58}59if (args.length > i) {60pwfile = args[i++];6162if (args.length > i) {63namesfile = args[i++];64}65} else {66pwfile = "pw.properties";67namesfile = "names.properties";68}69}7071CallbackHandler clntCbh = new ClientCallbackHandler(auto);7273CallbackHandler srvCbh =74new PropertiesFileCallbackHandler(pwfile, namesfile, null);7576SaslClient clnt = Sasl.createSaslClient(77new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, null, clntCbh);7879SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN, null,80srvCbh);8182if (clnt == null) {83throw new IllegalStateException(84"Unable to find client impl for " + MECH);85}86if (srv == null) {87throw new IllegalStateException(88"Unable to find server impl for " + MECH);89}9091byte[] response = (clnt.hasInitialResponse()?92clnt.evaluateChallenge(EMPTY) : EMPTY);93byte[] challenge;9495while (!clnt.isComplete() || !srv.isComplete()) {96challenge = srv.evaluateResponse(response);9798if (challenge != null) {99response = clnt.evaluateChallenge(challenge);100}101}102103if (clnt.isComplete() && srv.isComplete()) {104if (verbose) {105System.out.println("SUCCESS");106System.out.println("authzid is " + srv.getAuthorizationID());107}108} else {109throw new IllegalStateException("FAILURE: mismatched state:" +110" client complete? " + clnt.isComplete() +111" server complete? " + srv.isComplete());112}113}114}115116117