1/*
2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6287172
27 * @summary SASL + Digest-MD5, charset quoted
28 */
29
30import java.io.BufferedReader;
31import java.io.InputStreamReader;
32import java.io.IOException;
33import java.util.Map;
34import java.util.TreeMap;
35import java.util.logging.Logger;
36import javax.security.auth.callback.Callback;
37import javax.security.auth.callback.NameCallback;
38import javax.security.auth.callback.PasswordCallback;
39import javax.security.auth.callback.UnsupportedCallbackException;
40import javax.security.sasl.RealmCallback;
41import javax.security.sasl.Sasl;
42import javax.security.sasl.SaslClient;
43import javax.security.sasl.SaslException;
44import javax.security.sasl.SaslServer;
45import javax.security.auth.callback.CallbackHandler;
46
47/*
48 * According to RFC 2831, DIGEST-MD5 servers must generate challenge strings
49 * whose charset and algorithm values are not enclosed within quotes.
50 * For example,
51 *     challenge: realm="127.0.0.1",nonce="8GBOabRGeIqZB5BiaYJ1NDTuteV+D7n+qbSTH1fo",qop="auth",charset=utf-8,algorithm=md5-sess
52 */
53public class NoQuoteParams {
54
55    private static Logger logger = Logger.getLogger("global");
56    private static final String DIGEST_MD5 = "DIGEST-MD5";
57    private static final byte[] EMPTY = new byte[0];
58
59    private static CallbackHandler authCallbackHandler =
60        new SampleCallbackHandler();
61
62    public static void main(String[] args) throws Exception {
63
64        Map<String, String> props = new TreeMap<String, String>();
65        props.put(Sasl.QOP, "auth");
66
67        // client
68        SaslClient client = Sasl.createSaslClient(new String[]{ DIGEST_MD5 },
69            "user1", "xmpp", "127.0.0.1", props, authCallbackHandler);
70        if (client == null) {
71            throw new Exception("Unable to find client implementation for: " +
72                DIGEST_MD5);
73        }
74
75        byte[] response = client.hasInitialResponse()
76            ? client.evaluateChallenge(EMPTY) : EMPTY;
77        logger.info("initial: " + new String(response));
78
79        // server
80        byte[] challenge = null;
81        SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp",
82          "127.0.0.1", props, authCallbackHandler);
83        if (server == null) {
84            throw new Exception("Unable to find server implementation for: " +
85                DIGEST_MD5);
86        }
87
88        if (!client.isComplete() || !server.isComplete()) {
89            challenge = server.evaluateResponse(response);
90
91            logger.info("challenge: " + new String(challenge));
92
93            if (challenge != null) {
94                response = client.evaluateChallenge(challenge);
95            }
96        }
97
98        String challengeString = new String(challenge, "UTF-8").toLowerCase();
99
100        if (challengeString.indexOf("\"md5-sess\"") > 0 ||
101            challengeString.indexOf("\"utf-8\"") > 0) {
102            throw new Exception("The challenge string's charset and " +
103                "algorithm values must not be enclosed within quotes");
104        }
105
106        client.dispose();
107        server.dispose();
108    }
109}
110
111class SampleCallbackHandler implements CallbackHandler {
112
113    public void handle(Callback[] callbacks)
114        throws java.io.IOException, UnsupportedCallbackException {
115            for (int i = 0; i < callbacks.length; i++) {
116                if (callbacks[i] instanceof NameCallback) {
117                    NameCallback cb = (NameCallback)callbacks[i];
118                    cb.setName(getInput(cb.getPrompt()));
119
120                } else if (callbacks[i] instanceof PasswordCallback) {
121                    PasswordCallback cb = (PasswordCallback)callbacks[i];
122
123                    String pw = getInput(cb.getPrompt());
124                    char[] passwd = new char[pw.length()];
125                    pw.getChars(0, passwd.length, passwd, 0);
126
127                    cb.setPassword(passwd);
128
129                } else if (callbacks[i] instanceof RealmCallback) {
130                    RealmCallback cb = (RealmCallback)callbacks[i];
131                    cb.setText(getInput(cb.getPrompt()));
132
133                } else {
134                    throw new UnsupportedCallbackException(callbacks[i]);
135                }
136            }
137    }
138
139    /**
140     * In real world apps, this would typically be a TextComponent or
141     * similar widget.
142     */
143    private String getInput(String prompt) throws IOException {
144        return "dummy-value";
145    }
146}
147