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