Path: blob/master/test/jdk/com/sun/security/sasl/digest/NoQuoteParams.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 628717226* @summary SASL + Digest-MD5, charset quoted27*/2829import java.io.BufferedReader;30import java.io.InputStreamReader;31import java.io.IOException;32import java.util.Map;33import java.util.TreeMap;34import java.util.logging.Logger;35import javax.security.auth.callback.Callback;36import javax.security.auth.callback.NameCallback;37import javax.security.auth.callback.PasswordCallback;38import javax.security.auth.callback.UnsupportedCallbackException;39import javax.security.sasl.RealmCallback;40import javax.security.sasl.Sasl;41import javax.security.sasl.SaslClient;42import javax.security.sasl.SaslException;43import javax.security.sasl.SaslServer;44import javax.security.auth.callback.CallbackHandler;4546/*47* According to RFC 2831, DIGEST-MD5 servers must generate challenge strings48* whose charset and algorithm values are not enclosed within quotes.49* For example,50* challenge: realm="127.0.0.1",nonce="8GBOabRGeIqZB5BiaYJ1NDTuteV+D7n+qbSTH1fo",qop="auth",charset=utf-8,algorithm=md5-sess51*/52public class NoQuoteParams {5354private static Logger logger = Logger.getLogger("global");55private static final String DIGEST_MD5 = "DIGEST-MD5";56private static final byte[] EMPTY = new byte[0];5758private static CallbackHandler authCallbackHandler =59new SampleCallbackHandler();6061public static void main(String[] args) throws Exception {6263Map<String, String> props = new TreeMap<String, String>();64props.put(Sasl.QOP, "auth");6566// client67SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST_MD5 },68"user1", "xmpp", "127.0.0.1", props, authCallbackHandler);69if (client == null) {70throw new Exception("Unable to find client implementation for: " +71DIGEST_MD5);72}7374byte[] response = client.hasInitialResponse()75? client.evaluateChallenge(EMPTY) : EMPTY;76logger.info("initial: " + new String(response));7778// server79byte[] challenge = null;80SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp",81"127.0.0.1", props, authCallbackHandler);82if (server == null) {83throw new Exception("Unable to find server implementation for: " +84DIGEST_MD5);85}8687if (!client.isComplete() || !server.isComplete()) {88challenge = server.evaluateResponse(response);8990logger.info("challenge: " + new String(challenge));9192if (challenge != null) {93response = client.evaluateChallenge(challenge);94}95}9697String challengeString = new String(challenge, "UTF-8").toLowerCase();9899if (challengeString.indexOf("\"md5-sess\"") > 0 ||100challengeString.indexOf("\"utf-8\"") > 0) {101throw new Exception("The challenge string's charset and " +102"algorithm values must not be enclosed within quotes");103}104105client.dispose();106server.dispose();107}108}109110class SampleCallbackHandler implements CallbackHandler {111112public void handle(Callback[] callbacks)113throws java.io.IOException, UnsupportedCallbackException {114for (int i = 0; i < callbacks.length; i++) {115if (callbacks[i] instanceof NameCallback) {116NameCallback cb = (NameCallback)callbacks[i];117cb.setName(getInput(cb.getPrompt()));118119} else if (callbacks[i] instanceof PasswordCallback) {120PasswordCallback cb = (PasswordCallback)callbacks[i];121122String pw = getInput(cb.getPrompt());123char[] passwd = new char[pw.length()];124pw.getChars(0, passwd.length, passwd, 0);125126cb.setPassword(passwd);127128} else if (callbacks[i] instanceof RealmCallback) {129RealmCallback cb = (RealmCallback)callbacks[i];130cb.setText(getInput(cb.getPrompt()));131132} else {133throw new UnsupportedCallbackException(callbacks[i]);134}135}136}137138/**139* In real world apps, this would typically be a TextComponent or140* similar widget.141*/142private String getInput(String prompt) throws IOException {143return "dummy-value";144}145}146147148