Path: blob/master/test/jdk/com/sun/security/sasl/digest/CheckNegotiatedQOPs.java
41154 views
/*1* Copyright (c) 2005, 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 509117426* @summary DigestMD5Server does not return correct value for getNegotiatedProperty(Sasl.QOP)27*/2829import java.io.IOException;30import java.util.Map;31import java.util.HashMap;3233import javax.security.auth.callback.Callback;34import javax.security.auth.callback.NameCallback;35import javax.security.auth.callback.PasswordCallback;36import javax.security.auth.callback.UnsupportedCallbackException;37import javax.security.sasl.AuthorizeCallback;38import javax.security.sasl.RealmCallback;39import javax.security.sasl.Sasl;40import javax.security.sasl.SaslClient;41import javax.security.sasl.SaslException;42import javax.security.sasl.SaslServer;43import javax.security.auth.callback.CallbackHandler;4445public final class CheckNegotiatedQOPs {4647static final String DIGEST_MD5 = "DIGEST-MD5";48private final int caseNumber;49private final String requestedQOPs;50private final String supportedQOPs;51private final SampleClient client;52private final SampleServer server;5354public static void main(String[] args) throws Exception {5556new CheckNegotiatedQOPs(1, "auth", "auth-conf,auth-int,auth")57.execute(false);58new CheckNegotiatedQOPs(2, "auth-int", "auth-conf,auth-int,auth")59.execute(false);60new CheckNegotiatedQOPs(3, "auth-conf", "auth-conf,auth-int,auth")61.execute(false);62new CheckNegotiatedQOPs(4, "auth", "auth-int,auth")63.execute(false);64new CheckNegotiatedQOPs(5, "auth-int", "auth-int,auth")65.execute(false);66new CheckNegotiatedQOPs(6, "auth-conf", "auth-int,auth")67.execute(true);68new CheckNegotiatedQOPs(7, "auth", "auth")69.execute(false);70new CheckNegotiatedQOPs(8, "auth-int", "auth")71.execute(true);72new CheckNegotiatedQOPs(9, "auth-conf", "auth")73.execute(true);74new CheckNegotiatedQOPs(10, "auth", null)75.execute(false);76new CheckNegotiatedQOPs(11, "auth-int", null)77.execute(true);78new CheckNegotiatedQOPs(12, "auth-conf", null)79.execute(true);80}8182private CheckNegotiatedQOPs(int caseNumber, String requestedQOPs,83String supportedQOPs) throws SaslException {8485this.caseNumber = caseNumber;86this.requestedQOPs = requestedQOPs;87this.supportedQOPs = supportedQOPs;88this.client = new SampleClient(requestedQOPs);89this.server = new SampleServer(supportedQOPs);90}9192private void execute(boolean expectException) throws Exception {9394System.err.println ("Case #" + caseNumber);95System.err.println ("client requested QOPs=" + requestedQOPs);96System.err.println ("server supported QOPs=" + supportedQOPs);9798try {99client.negotiate(server);100101if (expectException) {102throw new103Exception("An exception was expected but none was thrown");104}105106} catch (SaslException e) {107108if (expectException) {109System.err.println(e);110return;111112} else {113throw e;114}115}116117System.err.println("client negotiated QOP=" +118client.getSaslClient ().getNegotiatedProperty (Sasl.QOP));119120System.err.println("server negotiated QOP=" +121server.getSaslServer ().getNegotiatedProperty (Sasl.QOP));122123System.err.println();124}125126private final class SampleCallbackHandler implements CallbackHandler {127128public void handle(Callback[] callbacks)129throws java.io.IOException, UnsupportedCallbackException {130for (int i = 0; i < callbacks.length; i++) {131if (callbacks[i] instanceof NameCallback) {132NameCallback cb = (NameCallback)callbacks[i];133cb.setName(getInput(cb.getPrompt()));134135} else if (callbacks[i] instanceof PasswordCallback) {136PasswordCallback cb = (PasswordCallback)callbacks[i];137138String pw = getInput(cb.getPrompt());139char[] passwd = new char[pw.length()];140pw.getChars(0, passwd.length, passwd, 0);141142cb.setPassword(passwd);143144} else if (callbacks[i] instanceof RealmCallback) {145RealmCallback cb = (RealmCallback)callbacks[i];146//cb.setText(getInput(cb.getPrompt()));147cb.setText("127.0.0.1");148149} else if (callbacks[i] instanceof AuthorizeCallback) {150AuthorizeCallback cb = (AuthorizeCallback)callbacks[i];151cb.setAuthorized(true);152153} else {154throw new UnsupportedCallbackException(callbacks[i]);155}156}157}158159/**160* In real world apps, this would typically be a TextComponent or161* similar widget.162*/163private String getInput(String prompt) throws IOException {164return "dummy-value";165}166}167168private final class SampleClient {169170private final SaslClient saslClient;171172public SampleClient(String requestedQOPs) throws SaslException {173174Map<String,String> properties = new HashMap<String,String>();175176if (requestedQOPs != null) {177properties.put(Sasl.QOP, requestedQOPs);178}179saslClient = Sasl.createSaslClient(new String[]{ DIGEST_MD5 }, null,180"local", "127.0.0.1", properties, new SampleCallbackHandler());181}182183public SaslClient getSaslClient() {184return saslClient;185}186187public void negotiate(SampleServer server) throws SaslException {188189byte[] challenge;190byte[] response;191192response = (saslClient.hasInitialResponse () ?193saslClient.evaluateChallenge (new byte [0]) : new byte [0]);194195while (! saslClient.isComplete()) {196challenge = server.evaluate(response);197response = saslClient.evaluateChallenge(challenge);198}199}200}201202private final class SampleServer {203204private final SaslServer saslServer;205206public SampleServer(String supportedQOPs) throws SaslException {207208Map<String,String> properties = new HashMap<String,String>();209210if (supportedQOPs != null) {211properties.put(Sasl.QOP, supportedQOPs);212}213saslServer = Sasl.createSaslServer(DIGEST_MD5, "local", "127.0.0.1",214properties, new SampleCallbackHandler());215}216217public SaslServer getSaslServer() {218return saslServer;219}220221public byte[] evaluate(byte[] response) throws SaslException {222223if (saslServer.isComplete()) {224throw new SaslException ("Server is already complete");225}226227return saslServer.evaluateResponse(response);228}229}230}231232233