Path: blob/master/test/jdk/com/sun/security/sasl/digest/Unbound.java
41154 views
/*1* Copyright (c) 2012, 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 711080326* @summary SASL service for multiple hostnames27* @run main Unbound jmx28* @run main/fail Unbound j29*/30import javax.security.sasl.*;31import javax.security.auth.callback.*;32import java.util.*;3334public class Unbound {35private static final String MECH = "DIGEST-MD5";36private static final String SERVER_FQDN = "machineX.imc.org";37private static final String PROTOCOL = "jmx";3839private static final byte[] EMPTY = new byte[0];4041private static String pwfile, namesfile, proxyfile;42private static boolean auto;43private static boolean verbose = false;4445private static void init(String[] args) throws Exception {46if (args.length == 1) {47pwfile = "pw.properties";48namesfile = "names.properties";49auto = true;50} else {51int i = 1;52if (args[i].equals("-m")) {53i++;54auto = false;55}56if (args.length > i) {57pwfile = args[i++];5859if (args.length > i) {60namesfile = args[i++];6162if (args.length > i) {63proxyfile = args[i];64}65}66} else {67pwfile = "pw.properties";68namesfile = "names.properties";69}70}71}7273public static void main(String[] args) throws Exception {7475init(args);7677CallbackHandler clntCbh = new ClientCallbackHandler(auto);7879CallbackHandler srvCbh =80new PropertiesFileCallbackHandler(pwfile, namesfile, proxyfile);8182SaslClient clnt = Sasl.createSaslClient(83new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, null, clntCbh);8485Map props = System.getProperties();86props.put("com.sun.security.sasl.digest.realm", SERVER_FQDN);8788SaslServer srv = Sasl.createSaslServer(MECH, args[0], null, props,89srvCbh);9091if (clnt == null) {92throw new IllegalStateException(93"Unable to find client impl for " + MECH);94}95if (srv == null) {96throw new IllegalStateException(97"Unable to find server impl for " + MECH);98}99100byte[] response = (clnt.hasInitialResponse()?101clnt.evaluateChallenge(EMPTY) : EMPTY);102byte[] challenge;103104while (!clnt.isComplete() || !srv.isComplete()) {105challenge = srv.evaluateResponse(response);106107if (challenge != null) {108response = clnt.evaluateChallenge(challenge);109}110}111112if (clnt.isComplete() && srv.isComplete()) {113if (verbose) {114System.out.println("SUCCESS");115System.out.println("authzid is " + srv.getAuthorizationID());116}117} else {118throw new IllegalStateException(119"FAILURE: mismatched state:" +120" client complete? " + clnt.isComplete() +121" server complete? " + srv.isComplete());122}123124if (!SERVER_FQDN.equalsIgnoreCase((String)125srv.getNegotiatedProperty(Sasl.BOUND_SERVER_NAME))) {126throw new Exception("Server side gets wrong requested server name");127}128clnt.dispose();129srv.dispose();130}131}132133134