Path: blob/master/test/jdk/com/sun/security/sasl/gsskerb/AuthOnly.java
41154 views
/*1* Copyright (c) 2003, 2014, 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 authentication via GSS-API/Kerberos v5 works.27* @run main/manual AuthOnly28*/2930/*31* Set logging to FINEST to view exchange.32* See runwjaas.csh for instructions for how to run this test.33*/3435import javax.security.sasl.*;36import javax.security.auth.callback.*;37import java.security.*;38import javax.security.auth.Subject;39import javax.security.auth.login.*;40import com.sun.security.auth.callback.*;41import java.util.HashMap;4243public class AuthOnly {44private static final String MECH = "GSSAPI";45private static final String SERVER_FQDN = "machineX.imc.org";46private static final String PROTOCOL = "sample";4748private static String namesfile, proxyfile;49private static final byte[] EMPTY = new byte[0];50private static boolean auto;51private static boolean verbose = false;5253public static void main(String[] args) throws Exception {54if (args.length == 0) {55namesfile = null;56auto = true;57} else {58int i = 0;59if (args[i].equals("-m")) {60i++;61auto = false;62}63if (args.length > i) {64namesfile = args[i++];65if (args.length > i) {66proxyfile = args[i];67}68} else {69namesfile = null;70}71}7273CallbackHandler clntCbh = null;74final CallbackHandler srvCbh = new PropertiesFileCallbackHandler(75null, namesfile, proxyfile);7677Subject clntSubj = doLogin("client");78Subject srvSubj = doLogin("server");79final HashMap clntprops = new HashMap();80final HashMap srvprops = new HashMap();8182clntprops.put(Sasl.QOP, "auth");83srvprops.put(Sasl.QOP, "auth,auth-int,auth-conf");8485final SaslClient clnt = (SaslClient)86Subject.doAs(clntSubj, new PrivilegedExceptionAction() {87public Object run() throws Exception {88return Sasl.createSaslClient(89new String[]{MECH}, null, PROTOCOL, SERVER_FQDN,90clntprops, null);91}92});9394if (verbose) {95System.out.println(clntSubj);96System.out.println(srvSubj);97}98final SaslServer srv = (SaslServer)99Subject.doAs(srvSubj, new PrivilegedExceptionAction() {100public Object run() throws Exception {101return Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN,102srvprops, srvCbh);103}104});105106107if (clnt == null) {108throw new IllegalStateException(109"Unable to find client impl for " + MECH);110}111if (srv == null) {112throw new IllegalStateException(113"Unable to find server impl for " + MECH);114}115116byte[] response;117byte[] challenge;118119response = (byte[]) Subject.doAs(clntSubj,120new PrivilegedExceptionAction() {121public Object run() throws Exception {122return (clnt.hasInitialResponse()? clnt.evaluateChallenge(EMPTY) : EMPTY);123}});124125while (!clnt.isComplete() || !srv.isComplete()) {126final byte[] responseCopy = response;127challenge = (byte[]) Subject.doAs(srvSubj,128new PrivilegedExceptionAction() {129public Object run() throws Exception {130return srv.evaluateResponse(responseCopy);131}});132133if (challenge != null) {134final byte[] challengeCopy = challenge;135response = (byte[]) Subject.doAs(clntSubj,136new PrivilegedExceptionAction() {137public Object run() throws Exception {138return clnt.evaluateChallenge(challengeCopy);139}});140}141}142143if (clnt.isComplete() && srv.isComplete()) {144if (verbose) {145System.out.println("SUCCESS");146System.out.println("authzid is " + srv.getAuthorizationID());147}148} else {149throw new IllegalStateException("FAILURE: mismatched state:" +150" client complete? " + clnt.isComplete() +151" server complete? " + srv.isComplete());152}153}154155private static Subject doLogin(String msg) throws LoginException {156LoginContext lc = null;157if (verbose) {158System.out.println(msg);159}160try {161lc = new LoginContext(msg, new TextCallbackHandler());162163// Attempt authentication164// You might want to do this in a "for" loop to give165// user more than one chance to enter correct username/password166lc.login();167168} catch (LoginException le) {169throw le;170}171return lc.getSubject();172}173}174175176