1/*
2 * Copyright (c) 2003, 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 4634892
27 * @summary Ensure that setting com.sun.security.sasl.digest.utf8 to "false"
28 *      for the SASL server causes server to not issue a charset=utf-8 directive.
29 */
30/**
31 * Default is to use UTF-8 (server will by default issue charset directive).
32 * Can set logging to FINEST to view exchange.
33 */
34
35import javax.security.sasl.*;
36import javax.security.auth.callback.*;
37import java.security.Security;
38import java.util.*;
39
40public class AuthNoUtf8 {
41    private static final String MECH = "DIGEST-MD5";
42    private static final String SERVER_FQDN = "machineX.imc.org";
43    private static final String PROTOCOL = "jmx";
44
45    private static final byte[] EMPTY = new byte[0];
46
47    private static String pwfile, namesfile, proxyfile;
48    private static boolean auto;
49    private static boolean verbose = false;
50
51    private static void init(String[] args) throws Exception {
52        if (args.length == 0) {
53            pwfile = "pw.properties";
54            namesfile = "names.properties";
55            auto = true;
56        } else {
57            int i = 0;
58            if (args[i].equals("-m")) {
59                i++;
60                auto = false;
61            }
62            if (args.length > i) {
63                pwfile = args[i++];
64
65                if (args.length > i) {
66                    namesfile = args[i++];
67
68                    if (args.length > i) {
69                        proxyfile = args[i];
70                    }
71                }
72            } else {
73                pwfile = "pw.properties";
74                namesfile = "names.properties";
75            }
76        }
77    }
78
79    public static void main(String[] args) throws Exception {
80
81        init(args);
82
83        CallbackHandler clntCbh = new ClientCallbackHandler(auto);
84
85        CallbackHandler srvCbh =
86            new PropertiesFileCallbackHandler(pwfile, namesfile, proxyfile);
87
88        Map props = new HashMap();
89        props.put("com.sun.security.sasl.digest.utf8", "false");
90
91        SaslClient clnt = Sasl.createSaslClient(
92            new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, null, clntCbh);
93
94        SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN, props,
95            srvCbh);
96
97        if (clnt == null) {
98            throw new IllegalStateException(
99                "Unable to find client impl for " + MECH);
100        }
101        if (srv == null) {
102            throw new IllegalStateException(
103                "Unable to find server impl for " + MECH);
104        }
105
106        byte[] response = (clnt.hasInitialResponse()?
107            clnt.evaluateChallenge(EMPTY) : EMPTY);
108        byte[] challenge;
109
110        while (!clnt.isComplete() || !srv.isComplete()) {
111            challenge = srv.evaluateResponse(response);
112
113            if (challenge != null) {
114                response = clnt.evaluateChallenge(challenge);
115            }
116        }
117
118        if (clnt.isComplete() && srv.isComplete()) {
119            if (verbose) {
120                System.out.println("SUCCESS");
121                System.out.println("authzid is " + srv.getAuthorizationID());
122            }
123        } else {
124            throw new IllegalStateException("FAILURE: mismatched state:" +
125                " client complete? " + clnt.isComplete() +
126                " server complete? " + srv.isComplete());
127        }
128
129        clnt.dispose();
130        srv.dispose();
131    }
132}
133