0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 5091174
0N/A * @summary DigestMD5Server does not return correct value for getNegotiatedProperty(Sasl.QOP)
0N/A */
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.util.Map;
0N/Aimport java.util.HashMap;
0N/A
0N/Aimport javax.security.auth.callback.Callback;
0N/Aimport javax.security.auth.callback.NameCallback;
0N/Aimport javax.security.auth.callback.PasswordCallback;
0N/Aimport javax.security.auth.callback.UnsupportedCallbackException;
0N/Aimport javax.security.sasl.AuthorizeCallback;
0N/Aimport javax.security.sasl.RealmCallback;
0N/Aimport javax.security.sasl.Sasl;
0N/Aimport javax.security.sasl.SaslClient;
0N/Aimport javax.security.sasl.SaslException;
0N/Aimport javax.security.sasl.SaslServer;
0N/Aimport javax.security.auth.callback.CallbackHandler;
0N/A
0N/Apublic final class CheckNegotiatedQOPs {
0N/A
0N/A static final String DIGEST_MD5 = "DIGEST-MD5";
0N/A private final int caseNumber;
0N/A private final String requestedQOPs;
0N/A private final String supportedQOPs;
0N/A private final SampleClient client;
0N/A private final SampleServer server;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A new CheckNegotiatedQOPs(1, "auth", "auth-conf,auth-int,auth")
0N/A .execute(false);
0N/A new CheckNegotiatedQOPs(2, "auth-int", "auth-conf,auth-int,auth")
0N/A .execute(false);
0N/A new CheckNegotiatedQOPs(3, "auth-conf", "auth-conf,auth-int,auth")
0N/A .execute(false);
0N/A new CheckNegotiatedQOPs(4, "auth", "auth-int,auth")
0N/A .execute(false);
0N/A new CheckNegotiatedQOPs(5, "auth-int", "auth-int,auth")
0N/A .execute(false);
0N/A new CheckNegotiatedQOPs(6, "auth-conf", "auth-int,auth")
0N/A .execute(true);
0N/A new CheckNegotiatedQOPs(7, "auth", "auth")
0N/A .execute(false);
0N/A new CheckNegotiatedQOPs(8, "auth-int", "auth")
0N/A .execute(true);
0N/A new CheckNegotiatedQOPs(9, "auth-conf", "auth")
0N/A .execute(true);
0N/A new CheckNegotiatedQOPs(10, "auth", null)
0N/A .execute(false);
0N/A new CheckNegotiatedQOPs(11, "auth-int", null)
0N/A .execute(true);
0N/A new CheckNegotiatedQOPs(12, "auth-conf", null)
0N/A .execute(true);
0N/A }
0N/A
0N/A private CheckNegotiatedQOPs(int caseNumber, String requestedQOPs,
0N/A String supportedQOPs) throws SaslException {
0N/A
0N/A this.caseNumber = caseNumber;
0N/A this.requestedQOPs = requestedQOPs;
0N/A this.supportedQOPs = supportedQOPs;
0N/A this.client = new SampleClient(requestedQOPs);
0N/A this.server = new SampleServer(supportedQOPs);
0N/A }
0N/A
0N/A private void execute(boolean expectException) throws Exception {
0N/A
0N/A System.err.println ("Case #" + caseNumber);
0N/A System.err.println ("client requested QOPs=" + requestedQOPs);
0N/A System.err.println ("server supported QOPs=" + supportedQOPs);
0N/A
0N/A try {
0N/A client.negotiate(server);
0N/A
0N/A if (expectException) {
0N/A throw new
0N/A Exception("An exception was expected but none was thrown");
0N/A }
0N/A
0N/A } catch (SaslException e) {
0N/A
0N/A if (expectException) {
0N/A System.err.println(e);
0N/A return;
0N/A
0N/A } else {
0N/A throw e;
0N/A }
0N/A }
0N/A
0N/A System.err.println("client negotiated QOP=" +
0N/A client.getSaslClient ().getNegotiatedProperty (Sasl.QOP));
0N/A
0N/A System.err.println("server negotiated QOP=" +
0N/A server.getSaslServer ().getNegotiatedProperty (Sasl.QOP));
0N/A
0N/A System.err.println();
0N/A }
0N/A
0N/Aprivate final class SampleCallbackHandler implements CallbackHandler {
0N/A
0N/A public void handle(Callback[] callbacks)
0N/A throws java.io.IOException, UnsupportedCallbackException {
0N/A for (int i = 0; i < callbacks.length; i++) {
0N/A if (callbacks[i] instanceof NameCallback) {
0N/A NameCallback cb = (NameCallback)callbacks[i];
0N/A cb.setName(getInput(cb.getPrompt()));
0N/A
0N/A } else if (callbacks[i] instanceof PasswordCallback) {
0N/A PasswordCallback cb = (PasswordCallback)callbacks[i];
0N/A
0N/A String pw = getInput(cb.getPrompt());
0N/A char[] passwd = new char[pw.length()];
0N/A pw.getChars(0, passwd.length, passwd, 0);
0N/A
0N/A cb.setPassword(passwd);
0N/A
0N/A } else if (callbacks[i] instanceof RealmCallback) {
0N/A RealmCallback cb = (RealmCallback)callbacks[i];
0N/A //cb.setText(getInput(cb.getPrompt()));
0N/A cb.setText("127.0.0.1");
0N/A
0N/A } else if (callbacks[i] instanceof AuthorizeCallback) {
0N/A AuthorizeCallback cb = (AuthorizeCallback)callbacks[i];
0N/A cb.setAuthorized(true);
0N/A
0N/A } else {
0N/A throw new UnsupportedCallbackException(callbacks[i]);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * In real world apps, this would typically be a TextComponent or
0N/A * similar widget.
0N/A */
0N/A private String getInput(String prompt) throws IOException {
0N/A return "dummy-value";
0N/A }
0N/A}
0N/A
0N/Aprivate final class SampleClient {
0N/A
0N/A private final SaslClient saslClient;
0N/A
0N/A public SampleClient(String requestedQOPs) throws SaslException {
0N/A
0N/A Map<String,String> properties = new HashMap<String,String>();
0N/A
0N/A if (requestedQOPs != null) {
0N/A properties.put(Sasl.QOP, requestedQOPs);
0N/A }
0N/A saslClient = Sasl.createSaslClient(new String[]{ DIGEST_MD5 }, null,
0N/A "local", "127.0.0.1", properties, new SampleCallbackHandler());
0N/A }
0N/A
0N/A public SaslClient getSaslClient() {
0N/A return saslClient;
0N/A }
0N/A
0N/A public void negotiate(SampleServer server) throws SaslException {
0N/A
0N/A byte[] challenge;
0N/A byte[] response;
0N/A
0N/A response = (saslClient.hasInitialResponse () ?
0N/A saslClient.evaluateChallenge (new byte [0]) : new byte [0]);
0N/A
0N/A while (! saslClient.isComplete()) {
0N/A challenge = server.evaluate(response);
0N/A response = saslClient.evaluateChallenge(challenge);
0N/A }
0N/A }
0N/A}
0N/A
0N/Aprivate final class SampleServer {
0N/A
0N/A private final SaslServer saslServer;
0N/A
0N/A public SampleServer(String supportedQOPs) throws SaslException {
0N/A
0N/A Map<String,String> properties = new HashMap<String,String>();
0N/A
0N/A if (supportedQOPs != null) {
0N/A properties.put(Sasl.QOP, supportedQOPs);
0N/A }
0N/A saslServer = Sasl.createSaslServer(DIGEST_MD5, "local", "127.0.0.1",
0N/A properties, new SampleCallbackHandler());
0N/A }
0N/A
0N/A public SaslServer getSaslServer() {
0N/A return saslServer;
0N/A }
0N/A
0N/A public byte[] evaluate(byte[] response) throws SaslException {
0N/A
0N/A if (saslServer.isComplete()) {
0N/A throw new SaslException ("Server is already complete");
0N/A }
0N/A
0N/A return saslServer.evaluateResponse(response);
0N/A }
0N/A}
0N/A}