blob: e7cd7c59568013e2453ea41ad064a535a284680b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4634892
27 * @summary Ensure that supplying a list of realms in the
28 * "com.sun.security.sasl.digest.realm" property to the SASL server works.
29 */
30
31/*
32 * Server sends list of specified realms to client. Client selects one.
33 * Can set logging to FINEST to view exchange.
34 */
35import javax.security.sasl.*;
36import javax.security.auth.callback.*;
37import java.security.Security;
38import java.util.*;
39
40public class AuthRealms {
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.realm",
90 "IMC.ORG foo.bar machineX");
91
92 SaslClient clnt = Sasl.createSaslClient(
93 new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, null, clntCbh);
94
95 SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN, props,
96 srvCbh);
97
98 if (clnt == null) {
99 throw new IllegalStateException(
100 "Unable to find client impl for " + MECH);
101 }
102 if (srv == null) {
103 throw new IllegalStateException(
104 "Unable to find server impl for " + MECH);
105 }
106
107 byte[] response = (clnt.hasInitialResponse()?
108 clnt.evaluateChallenge(EMPTY) : EMPTY);
109 byte[] challenge;
110
111 while (!clnt.isComplete() || !srv.isComplete()) {
112 challenge = srv.evaluateResponse(response);
113
114 if (challenge != null) {
115 response = clnt.evaluateChallenge(challenge);
116 }
117 }
118
119 if (clnt.isComplete() && srv.isComplete()) {
120 if (verbose) {
121 System.out.println("SUCCESS");
122 System.out.println("authzid is " + srv.getAuthorizationID());
123 }
124 } else {
125 throw new IllegalStateException("FAILURE: mismatched state:" +
126 " client complete? " + clnt.isComplete() +
127 " server complete? " + srv.isComplete());
128 }
129
130 clnt.dispose();
131 srv.dispose();
132 }
133}