Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/security/sasl/Cram.java
38855 views
/*1* Copyright (c) 2003, 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* @summary Ensure that authentication via CRAM-MD5 works.27*/2829/*30* Can set logging to FINEST to view exchange.31*/32import javax.security.sasl.*;33import javax.security.auth.callback.*;34import java.security.Security;3536public class Cram {37private static final String MECH = "CRAM-MD5";38private static final String SERVER_FQDN = "machineX.imc.org";39private static final String PROTOCOL = "jmx";4041private static final byte[] EMPTY = new byte[0];42private static boolean auto;43private static boolean verbose = false;44private static String pwfile, namesfile;4546public static void main(String[] args) throws Exception {47if (args.length == 0) {48pwfile = "pw.properties";49namesfile = "names.properties";50auto = true;51} else {52int i = 0;53if (args[i].equals("-m")) {54i++;55auto = false;56}57if (args.length > i) {58pwfile = args[i++];5960if (args.length > i) {61namesfile = args[i++];62}63} else {64pwfile = "pw.properties";65namesfile = "names.properties";66}67}6869CallbackHandler clntCbh = new ClientCallbackHandler(auto);7071CallbackHandler srvCbh =72new PropertiesFileCallbackHandler(pwfile, namesfile, null);7374SaslClient clnt = Sasl.createSaslClient(75new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, null, clntCbh);7677SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN, null,78srvCbh);7980if (clnt == null) {81throw new IllegalStateException(82"Unable to find client impl for " + MECH);83}84if (srv == null) {85throw new IllegalStateException(86"Unable to find server impl for " + MECH);87}8889byte[] response = (clnt.hasInitialResponse()?90clnt.evaluateChallenge(EMPTY) : EMPTY);91byte[] challenge;9293while (!clnt.isComplete() || !srv.isComplete()) {94challenge = srv.evaluateResponse(response);9596if (challenge != null) {97response = clnt.evaluateChallenge(challenge);98}99}100101if (clnt.isComplete() && srv.isComplete()) {102if (verbose) {103System.out.println("SUCCESS");104System.out.println("authzid is " + srv.getAuthorizationID());105}106} else {107throw new IllegalStateException("FAILURE: mismatched state:" +108" client complete? " + clnt.isComplete() +109" server complete? " + srv.isComplete());110}111}112}113114115