Path: blob/master/test/jdk/com/sun/security/sasl/gsskerb/ConfSecurityLayer.java
41154 views
/*1* Copyright (c) 2004, 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 501449326* @summary SaslServer.wrap throws NullPointerException when security27* layer negotiated.28* @run main/manual ConfSecurityLayer29*/3031/*32* Set logging to FINEST to view exchange.33* See run-conf-wjaas.csh for instructions for how to run this test.34*/3536import javax.security.sasl.*;37import javax.security.auth.callback.*;38import java.security.*;39import javax.security.auth.Subject;40import javax.security.auth.login.*;41import com.sun.security.auth.callback.*;42import java.util.HashMap;4344public class ConfSecurityLayer {45private static final String MECH = "GSSAPI";46private static final String SERVER_FQDN = "machineX.imc.org";47private static final String PROTOCOL = "sample";4849private static String namesfile, proxyfile;50private static final byte[] EMPTY = new byte[0];51private static boolean auto;52private static boolean verbose = false;5354public static void main(String[] args) throws Exception {55if (args.length == 0) {56namesfile = null;57auto = true;58} else {59int i = 0;60if (args[i].equals("-m")) {61i++;62auto = false;63}64if (args.length > i) {65namesfile = args[i++];66if (args.length > i) {67proxyfile = args[i];68}69} else {70namesfile = null;71}72}7374CallbackHandler clntCbh = null;75final CallbackHandler srvCbh = new PropertiesFileCallbackHandler(76null, namesfile, proxyfile);7778Subject clntSubj = doLogin("client");79Subject srvSubj = doLogin("server");80final HashMap clntprops = new HashMap();81final HashMap srvprops = new HashMap();8283clntprops.put(Sasl.QOP, "auth-conf");84srvprops.put(Sasl.QOP, "auth,auth-int,auth-conf");8586final SaslClient clnt = (SaslClient)87Subject.doAs(clntSubj, new PrivilegedExceptionAction() {88public Object run() throws Exception {89return Sasl.createSaslClient(90new String[]{MECH}, null, PROTOCOL, SERVER_FQDN,91clntprops, null);92}93});9495if (verbose) {96System.out.println(clntSubj);97System.out.println(srvSubj);98}99final SaslServer srv = (SaslServer)100Subject.doAs(srvSubj, new PrivilegedExceptionAction() {101public Object run() throws Exception {102return Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN,103srvprops, srvCbh);104}105});106107108if (clnt == null) {109throw new IllegalStateException(110"Unable to find client impl for " + MECH);111}112if (srv == null) {113throw new IllegalStateException(114"Unable to find server impl for " + MECH);115}116117byte[] response;118byte[] challenge;119120response = (byte[]) Subject.doAs(clntSubj,121new PrivilegedExceptionAction() {122public Object run() throws Exception {123return (clnt.hasInitialResponse()? clnt.evaluateChallenge(EMPTY) : EMPTY);124}});125126while (!clnt.isComplete() || !srv.isComplete()) {127final byte[] responseCopy = response;128challenge = (byte[]) Subject.doAs(srvSubj,129new PrivilegedExceptionAction() {130public Object run() throws Exception {131return srv.evaluateResponse(responseCopy);132}});133134if (challenge != null) {135final byte[] challengeCopy = challenge;136response = (byte[]) Subject.doAs(clntSubj,137new PrivilegedExceptionAction() {138public Object run() throws Exception {139return clnt.evaluateChallenge(challengeCopy);140}});141}142}143144if (clnt.isComplete() && srv.isComplete()) {145if (verbose) {146System.out.println("SUCCESS");147System.out.println("authzid is " + srv.getAuthorizationID());148}149} else {150throw new IllegalStateException("FAILURE: mismatched state:" +151" client complete? " + clnt.isComplete() +152" server complete? " + srv.isComplete());153}154155if (verbose) {156System.out.println(clnt.getNegotiatedProperty(Sasl.QOP));157}158159// Now try to use security layer160161byte[] clntBuf = new byte[]{0, 1, 2, 3};162byte[] wrappedClnt = clnt.wrap(clntBuf, 0, clntBuf.length);163System.out.println("plaintext2: " + bytesToString(clntBuf));164System.out.println("wrapped2: " + bytesToString(wrappedClnt));165166byte[] srvBuf = new byte[]{10, 11, 12, 13};167byte[] wrappedSrv = srv.wrap(srvBuf, 0, srvBuf.length);168System.out.println("plaintext1: " + bytesToString(srvBuf));169System.out.println("wrapped1: " + bytesToString(wrappedSrv));170171byte[] unwrapped1 = clnt.unwrap(wrappedSrv, 0, wrappedSrv.length);172System.out.println("unwrapped1: " + bytesToString(unwrapped1));173174byte[] unwrapped2 = srv.unwrap(wrappedClnt, 0, wrappedClnt.length);175System.out.println("unwrapped2: " + bytesToString(unwrapped2));176}177178private static Subject doLogin(String msg) throws LoginException {179LoginContext lc = null;180if (verbose) {181System.out.println(msg);182}183try {184lc = new LoginContext(msg, new TextCallbackHandler());185186// Attempt authentication187// You might want to do this in a "for" loop to give188// user more than one chance to enter correct username/password189lc.login();190191} catch (LoginException le) {192throw le;193}194return lc.getSubject();195}196197private static String bytesToString(byte[] digest) {198// Get character representation of digest199StringBuffer digestString = new StringBuffer();200201for (int i = 0; i < digest.length; i++) {202if ((digest[i] & 0x000000ff) < 0x10) {203digestString.append("0" +204Integer.toHexString(digest[i] & 0x000000ff));205} else {206digestString.append(207Integer.toHexString(digest[i] & 0x000000ff));208}209}210return digestString.toString();211}212}213214215