0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test 1.2 07/03/29
0N/A * @bug 4634892
0N/A * @summary Ensure that client requesting integrity causes resulting channel to
0N/A * be integrity-protected.
0N/A */
0N/A
0N/A/*
0N/A * Can set logging to FINEST to view exchange.
0N/A */
0N/Aimport javax.security.sasl.*;
0N/Aimport javax.security.auth.callback.*;
0N/Aimport java.security.Security;
0N/Aimport java.util.*;
0N/A
0N/Apublic class Integrity {
0N/A private static final String MECH = "DIGEST-MD5";
0N/A private static final String SERVER_FQDN = "machineX.imc.org";
0N/A private static final String PROTOCOL = "jmx";
0N/A
0N/A private static final byte[] EMPTY = new byte[0];
0N/A
0N/A private static String pwfile, namesfile, proxyfile;
0N/A private static boolean auto;
0N/A private static boolean verbose = false;
0N/A
0N/A private static byte[][] clntdata, srvdata;
0N/A
0N/A private static void init(String[] args) throws Exception {
0N/A if (args.length == 0) {
0N/A pwfile = "pw.properties";
0N/A namesfile = "names.properties";
0N/A auto = true;
0N/A } else {
0N/A int i = 0;
0N/A if (args[i].equals("-m")) {
0N/A i++;
0N/A auto = false;
0N/A }
0N/A if (args.length > i) {
0N/A pwfile = args[i++];
0N/A
0N/A if (args.length > i) {
0N/A namesfile = args[i++];
0N/A
0N/A if (args.length > i) {
0N/A proxyfile = args[i];
0N/A }
0N/A }
0N/A } else {
0N/A pwfile = "pw.properties";
0N/A namesfile = "names.properties";
0N/A }
0N/A }
0N/A
0N/A initData();
0N/A }
0N/A
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A init(args);
0N/A
0N/A CallbackHandler clntCbh = new ClientCallbackHandler(auto);
0N/A
0N/A CallbackHandler srvCbh =
0N/A new PropertiesFileCallbackHandler(pwfile, namesfile, proxyfile);
0N/A
0N/A Map srvProps = new HashMap();
0N/A srvProps.put(Sasl.QOP, "auth-int");
0N/A
0N/A Map clntProps = new HashMap();
0N/A clntProps.put(Sasl.QOP, "auth-int");
0N/A
0N/A SaslClient clnt = Sasl.createSaslClient(
0N/A new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, clntProps, clntCbh);
0N/A
0N/A SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN,
0N/A srvProps, srvCbh);
0N/A
0N/A if (clnt == null) {
0N/A throw new IllegalStateException(
0N/A "Unable to find client impl for " + MECH);
0N/A }
0N/A if (srv == null) {
0N/A throw new IllegalStateException(
0N/A "Unable to find server impl for " + MECH);
0N/A }
0N/A
0N/A byte[] response = (clnt.hasInitialResponse()?
0N/A clnt.evaluateChallenge(EMPTY) : EMPTY);
0N/A byte[] challenge;
0N/A
0N/A while (!clnt.isComplete() || !srv.isComplete()) {
0N/A challenge = srv.evaluateResponse(response);
0N/A
0N/A if (challenge != null) {
0N/A response = clnt.evaluateChallenge(challenge);
0N/A }
0N/A }
0N/A
0N/A if (clnt.isComplete() && srv.isComplete()) {
0N/A if (verbose) {
0N/A System.out.println("SUCCESS");
0N/A System.out.println("authzid is " + srv.getAuthorizationID());
0N/A }
0N/A } else {
0N/A throw new IllegalStateException("FAILURE: mismatched state:" +
0N/A " client complete? " + clnt.isComplete() +
0N/A " server complete? " + srv.isComplete());
0N/A }
0N/A
0N/A /* Use security layer */
0N/A int count = 0;
0N/A for (int i = 0; i < clntStrs.length; i++) {
0N/A byte[] orig = clntdata[i];
0N/A byte[] wrapped = clnt.wrap(clntdata[i], 0, clntdata[i].length);
0N/A byte[] unwrapped = srv.unwrap(wrapped, 0, wrapped.length);
0N/A
0N/A if (!Arrays.equals(orig, unwrapped)) {
0N/A throw new SaslException("Server cannot unwrap client data");
0N/A }
0N/A
0N/A byte[] sorig = srvdata[i];
0N/A byte[] swrapped = srv.wrap(srvdata[i], 0, srvdata[i].length);
0N/A byte[] sunwrapped = clnt.unwrap(swrapped, 0, swrapped.length);
0N/A
0N/A if (!Arrays.equals(sorig, sunwrapped)) {
0N/A throw new SaslException("Client cannot unwrap server data");
0N/A }
0N/A ++count;
0N/A }
0N/A
0N/A if (verbose)
0N/A System.out.println(count + " sets of wrap/unwrap between client/server");
0N/A
0N/A clnt.dispose();
0N/A srv.dispose();
0N/A }
0N/A
0N/A private static final String[] srvStrs = new String[] {
0N/A"A is the 1st letter",
0N/A"B is the 2nd letter",
0N/A"C is the 3rd letter",
0N/A"D is the 4th letter",
0N/A"E is the 5th letter",
0N/A"F is the 6th letter",
0N/A"G is the 7th letter",
0N/A"H is the 8th letter",
0N/A"I is the 9th letter",
0N/A"J is the 10th letter",
0N/A"K is the 11th letter",
0N/A"L is the 12th letter",
0N/A"M is the 13th letter",
0N/A };
0N/A
0N/A private static final String[] clntStrs = new String[] {
0N/A"0",
0N/A"1",
0N/A"2",
0N/A"3",
0N/A"4",
0N/A"5",
0N/A"6",
0N/A"7",
0N/A"8",
0N/A"9",
0N/A"10",
0N/A"11",
0N/A"12",
0N/A };
0N/A
0N/A private static void initData() {
0N/A clntdata = new byte[clntStrs.length][];
0N/A for (int i = 0; i < clntStrs.length; i++) {
0N/A clntdata[i] = clntStrs[i].getBytes();
0N/A }
0N/A
0N/A srvdata = new byte[srvStrs.length][];
0N/A for (int i = 0; i < srvStrs.length; i++) {
0N/A srvdata[i] = srvStrs[i].getBytes();
0N/A }
0N/A }
0N/A}