Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/security/sasl/digest/NoQuoteParams.java
38867 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;45import com.sun.security.auth.callback.DialogCallbackHandler;46import com.sun.security.auth.callback.TextCallbackHandler;4748/*49* According to RFC 2831, DIGEST-MD5 servers must generate challenge strings50* whose charset and algorithm values are not enclosed within quotes.51* For example,52* challenge: realm="127.0.0.1",nonce="8GBOabRGeIqZB5BiaYJ1NDTuteV+D7n+qbSTH1fo",qop="auth",charset=utf-8,algorithm=md5-sess53*/54public class NoQuoteParams {5556private static Logger logger = Logger.getLogger("global");57private static final String DIGEST_MD5 = "DIGEST-MD5";58private static final byte[] EMPTY = new byte[0];5960private static CallbackHandler authCallbackHandler =61new SampleCallbackHandler();6263public static void main(String[] args) throws Exception {6465Map<String, String> props = new TreeMap<String, String>();66props.put(Sasl.QOP, "auth");6768// client69SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST_MD5 },70"user1", "xmpp", "127.0.0.1", props, authCallbackHandler);71if (client == null) {72throw new Exception("Unable to find client implementation for: " +73DIGEST_MD5);74}7576byte[] response = client.hasInitialResponse()77? client.evaluateChallenge(EMPTY) : EMPTY;78logger.info("initial: " + new String(response));7980// server81byte[] challenge = null;82SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp",83"127.0.0.1", props, authCallbackHandler);84if (server == null) {85throw new Exception("Unable to find server implementation for: " +86DIGEST_MD5);87}8889if (!client.isComplete() || !server.isComplete()) {90challenge = server.evaluateResponse(response);9192logger.info("challenge: " + new String(challenge));9394if (challenge != null) {95response = client.evaluateChallenge(challenge);96}97}9899String challengeString = new String(challenge, "UTF-8").toLowerCase();100101if (challengeString.indexOf("\"md5-sess\"") > 0 ||102challengeString.indexOf("\"utf-8\"") > 0) {103throw new Exception("The challenge string's charset and " +104"algorithm values must not be enclosed within quotes");105}106107client.dispose();108server.dispose();109}110}111112class SampleCallbackHandler implements CallbackHandler {113114public void handle(Callback[] callbacks)115throws java.io.IOException, UnsupportedCallbackException {116for (int i = 0; i < callbacks.length; i++) {117if (callbacks[i] instanceof NameCallback) {118NameCallback cb = (NameCallback)callbacks[i];119cb.setName(getInput(cb.getPrompt()));120121} else if (callbacks[i] instanceof PasswordCallback) {122PasswordCallback cb = (PasswordCallback)callbacks[i];123124String pw = getInput(cb.getPrompt());125char[] passwd = new char[pw.length()];126pw.getChars(0, passwd.length, passwd, 0);127128cb.setPassword(passwd);129130} else if (callbacks[i] instanceof RealmCallback) {131RealmCallback cb = (RealmCallback)callbacks[i];132cb.setText(getInput(cb.getPrompt()));133134} else {135throw new UnsupportedCallbackException(callbacks[i]);136}137}138}139140/**141* In real world apps, this would typically be a TextComponent or142* similar widget.143*/144private String getInput(String prompt) throws IOException {145return "dummy-value";146}147}148149150