blob: feabc27ea7f82874183d3fd9cf4c73b144ddd600 [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 authentication via CRAM-MD5 works.
28 */
29
30/*
31 * Can set logging to FINEST to view exchange.
32 */
33import javax.security.sasl.*;
34import javax.security.auth.callback.*;
35import java.security.Security;
36
37public class Cram {
38 private static final String MECH = "CRAM-MD5";
39 private static final String SERVER_FQDN = "machineX.imc.org";
40 private static final String PROTOCOL = "jmx";
41
42 private static final byte[] EMPTY = new byte[0];
43 private static boolean auto;
44 private static boolean verbose = false;
45 private static String pwfile, namesfile;
46
47 public static void main(String[] args) throws Exception {
48 if (args.length == 0) {
49 pwfile = "pw.properties";
50 namesfile = "names.properties";
51 auto = true;
52 } else {
53 int i = 0;
54 if (args[i].equals("-m")) {
55 i++;
56 auto = false;
57 }
58 if (args.length > i) {
59 pwfile = args[i++];
60
61 if (args.length > i) {
62 namesfile = args[i++];
63 }
64 } else {
65 pwfile = "pw.properties";
66 namesfile = "names.properties";
67 }
68 }
69
70 CallbackHandler clntCbh = new ClientCallbackHandler(auto);
71
72 CallbackHandler srvCbh =
73 new PropertiesFileCallbackHandler(pwfile, namesfile, null);
74
75 SaslClient clnt = Sasl.createSaslClient(
76 new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, null, clntCbh);
77
78 SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN, null,
79 srvCbh);
80
81 if (clnt == null) {
82 throw new IllegalStateException(
83 "Unable to find client impl for " + MECH);
84 }
85 if (srv == null) {
86 throw new IllegalStateException(
87 "Unable to find server impl for " + MECH);
88 }
89
90 byte[] response = (clnt.hasInitialResponse()?
91 clnt.evaluateChallenge(EMPTY) : EMPTY);
92 byte[] challenge;
93
94 while (!clnt.isComplete() || !srv.isComplete()) {
95 challenge = srv.evaluateResponse(response);
96
97 if (challenge != null) {
98 response = clnt.evaluateChallenge(challenge);
99 }
100 }
101
102 if (clnt.isComplete() && srv.isComplete()) {
103 if (verbose) {
104 System.out.println("SUCCESS");
105 System.out.println("authzid is " + srv.getAuthorizationID());
106 }
107 } else {
108 throw new IllegalStateException("FAILURE: mismatched state:" +
109 " client complete? " + clnt.isComplete() +
110 " server complete? " + srv.isComplete());
111 }
112 }
113}