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