Path: blob/master/test/jdk/com/sun/security/sasl/digest/HasInitialResponse.java
41154 views
/*1* Copyright (c) 2019, 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 668254026* @summary Incorrect SASL DIGEST-MD5 behavior27*/2829import javax.security.auth.callback.CallbackHandler;30import javax.security.sasl.Sasl;31import javax.security.sasl.SaslClient;32import javax.security.sasl.SaslException;33import javax.security.sasl.SaslServer;34import java.nio.charset.StandardCharsets;3536public class HasInitialResponse {3738private static final String MECH = "DIGEST-MD5";39private static final String SERVER_FQDN = "machineX.imc.org";40private static final String PROTOCOL = "jmx";4142private static final byte[] EMPTY = new byte[0];4344public static void main(String[] args) throws Exception {4546CallbackHandler clntCbh = new ClientCallbackHandler(true);47CallbackHandler srvCbh = new PropertiesFileCallbackHandler(48"pw.properties", "names.properties", null);4950// Get an existing SaslClient as base of our own client51SaslClient base = Sasl.createSaslClient(52new String[]{MECH}, null, PROTOCOL, SERVER_FQDN,53null, clntCbh);5455// Our own client that has initial response56SaslClient clnt = new MyDigestMD5Client(base);5758SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN,59null, srvCbh);6061// The usual loop62byte[] response = clnt.hasInitialResponse()63? clnt.evaluateChallenge(EMPTY) : EMPTY;64byte[] challenge;6566while (!clnt.isComplete() || !srv.isComplete()) {67challenge = srv.evaluateResponse(response);6869if (challenge != null) {70response = clnt.evaluateChallenge(challenge);71}72}7374if (clnt.isComplete() && srv.isComplete()) {75System.out.println("SUCCESS");76System.out.println("authzid is " + srv.getAuthorizationID());77} else {78throw new IllegalStateException(79"FAILURE: mismatched state:" +80" client complete? " + clnt.isComplete() +81" server complete? " + srv.isComplete());82}8384clnt.dispose();85srv.dispose();86}8788public static class MyDigestMD5Client implements SaslClient {8990final SaslClient base;91boolean first = true;9293public MyDigestMD5Client(SaslClient base) {94this.base = base;95}9697@Override98public String getMechanismName() {99return base.getMechanismName();100}101102@Override103public boolean hasInitialResponse() {104return true; // I have initial response105}106107@Override108public byte[] evaluateChallenge(byte[] challenge) throws SaslException {109if (first) {110first = false;111if (challenge.length == 0) {112return "hello".getBytes(StandardCharsets.UTF_8);113} else {114throw new SaslException("Non-empty challenge");115}116} else {117return base.evaluateChallenge(challenge);118}119}120121@Override122public boolean isComplete() {123return base.isComplete();124}125126@Override127public byte[] unwrap(byte[] incoming, int offset, int len)128throws SaslException {129return base.unwrap(incoming, offset, len);130}131132@Override133public byte[] wrap(byte[] outgoing, int offset, int len)134throws SaslException {135return base.wrap(outgoing, offset, len);136}137138@Override139public Object getNegotiatedProperty(String propName) {140return base.getNegotiatedProperty(propName);141}142143@Override144public void dispose() throws SaslException {145base.dispose();146}147}148}149150151