Path: blob/master/test/jdk/com/sun/security/sasl/digest/Integrity.java
41154 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test 1.2 07/03/2925* @bug 463489226* @summary Ensure that client requesting integrity causes resulting channel to27* be integrity-protected.28*/2930/*31* Can set logging to FINEST to view exchange.32*/33import javax.security.sasl.*;34import javax.security.auth.callback.*;35import java.security.Security;36import java.util.*;3738public class Integrity {39private static final String MECH = "DIGEST-MD5";40private static final String SERVER_FQDN = "machineX.imc.org";41private static final String PROTOCOL = "jmx";4243private static final byte[] EMPTY = new byte[0];4445private static String pwfile, namesfile, proxyfile;46private static boolean auto;47private static boolean verbose = false;4849private static byte[][] clntdata, srvdata;5051private static void init(String[] args) throws Exception {52if (args.length == 0) {53pwfile = "pw.properties";54namesfile = "names.properties";55auto = true;56} else {57int i = 0;58if (args[i].equals("-m")) {59i++;60auto = false;61}62if (args.length > i) {63pwfile = args[i++];6465if (args.length > i) {66namesfile = args[i++];6768if (args.length > i) {69proxyfile = args[i];70}71}72} else {73pwfile = "pw.properties";74namesfile = "names.properties";75}76}7778initData();79}808182public static void main(String[] args) throws Exception {8384init(args);8586CallbackHandler clntCbh = new ClientCallbackHandler(auto);8788CallbackHandler srvCbh =89new PropertiesFileCallbackHandler(pwfile, namesfile, proxyfile);9091Map srvProps = new HashMap();92srvProps.put(Sasl.QOP, "auth-int");9394Map clntProps = new HashMap();95clntProps.put(Sasl.QOP, "auth-int");9697SaslClient clnt = Sasl.createSaslClient(98new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, clntProps, clntCbh);99100SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN,101srvProps, srvCbh);102103if (clnt == null) {104throw new IllegalStateException(105"Unable to find client impl for " + MECH);106}107if (srv == null) {108throw new IllegalStateException(109"Unable to find server impl for " + MECH);110}111112byte[] response = (clnt.hasInitialResponse()?113clnt.evaluateChallenge(EMPTY) : EMPTY);114byte[] challenge;115116while (!clnt.isComplete() || !srv.isComplete()) {117challenge = srv.evaluateResponse(response);118119if (challenge != null) {120response = clnt.evaluateChallenge(challenge);121}122}123124if (clnt.isComplete() && srv.isComplete()) {125if (verbose) {126System.out.println("SUCCESS");127System.out.println("authzid is " + srv.getAuthorizationID());128}129} else {130throw new IllegalStateException("FAILURE: mismatched state:" +131" client complete? " + clnt.isComplete() +132" server complete? " + srv.isComplete());133}134135/* Use security layer */136int count = 0;137for (int i = 0; i < clntStrs.length; i++) {138byte[] orig = clntdata[i];139byte[] wrapped = clnt.wrap(clntdata[i], 0, clntdata[i].length);140byte[] unwrapped = srv.unwrap(wrapped, 0, wrapped.length);141142if (!Arrays.equals(orig, unwrapped)) {143throw new SaslException("Server cannot unwrap client data");144}145146byte[] sorig = srvdata[i];147byte[] swrapped = srv.wrap(srvdata[i], 0, srvdata[i].length);148byte[] sunwrapped = clnt.unwrap(swrapped, 0, swrapped.length);149150if (!Arrays.equals(sorig, sunwrapped)) {151throw new SaslException("Client cannot unwrap server data");152}153++count;154}155156if (verbose)157System.out.println(count + " sets of wrap/unwrap between client/server");158159clnt.dispose();160srv.dispose();161}162163private static final String[] srvStrs = new String[] {164"A is the 1st letter",165"B is the 2nd letter",166"C is the 3rd letter",167"D is the 4th letter",168"E is the 5th letter",169"F is the 6th letter",170"G is the 7th letter",171"H is the 8th letter",172"I is the 9th letter",173"J is the 10th letter",174"K is the 11th letter",175"L is the 12th letter",176"M is the 13th letter",177};178179private static final String[] clntStrs = new String[] {180"0",181"1",182"2",183"3",184"4",185"5",186"6",187"7",188"8",189"9",190"10",191"11",192"12",193};194195private static void initData() {196clntdata = new byte[clntStrs.length][];197for (int i = 0; i < clntStrs.length; i++) {198clntdata[i] = clntStrs[i].getBytes();199}200201srvdata = new byte[srvStrs.length][];202for (int i = 0; i < srvStrs.length; i++) {203srvdata[i] = srvStrs[i].getBytes();204}205}206}207208209