Path: blob/master/test/jdk/com/sun/security/sasl/digest/PrivacyRc4.java
41154 views
/*1* Copyright (c) 2003, 2005, 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.3 07/03/2925* @bug 463489226* @summary Ensure that client requesting privacy via RC4 cipher27* causes resulting channel to be encrypted using RC4.28*/2930/*31* Can set logging to FINEST to view exchange.32*/3334import javax.security.sasl.*;35import javax.security.auth.callback.*;36import java.security.Security;37import java.util.*;3839public class PrivacyRc4 {40private static final String MECH = "DIGEST-MD5";41private static final String SERVER_FQDN = "machineX.imc.org";42private static final String PROTOCOL = "jmx";4344private static final byte[] EMPTY = new byte[0];4546private static String pwfile, namesfile, proxyfile;47private static boolean auto;48private static boolean verbose = false;4950private static byte[][] clntdata, srvdata;5152private static void init(String[] args) throws Exception {53if (args.length == 0) {54pwfile = "pw.properties";55namesfile = "names.properties";56auto = true;57} else {58int i = 0;59if (args[i].equals("-m")) {60i++;61auto = false;62}63if (args.length > i) {64pwfile = args[i++];6566if (args.length > i) {67namesfile = args[i++];6869if (args.length > i) {70proxyfile = args[i];71}72}73} else {74pwfile = "pw.properties";75namesfile = "names.properties";76}77}7879initData();80}818283public static void main(String[] args) throws Exception {8485init(args);8687CallbackHandler clntCbh = new ClientCallbackHandler(auto);8889CallbackHandler srvCbh =90new PropertiesFileCallbackHandler(pwfile, namesfile, proxyfile);9192Map srvProps = new HashMap();93srvProps.put(Sasl.QOP, "auth-conf");9495Map clntProps = new HashMap();96clntProps.put(Sasl.QOP, "auth-conf");97clntProps.put("com.sun.security.sasl.digest.cipher", "rc4-56");9899SaslClient clnt = Sasl.createSaslClient(100new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, clntProps, clntCbh);101102SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN,103srvProps, srvCbh);104105if (clnt == null) {106throw new IllegalStateException(107"Unable to find client impl for " + MECH);108}109if (srv == null) {110throw new IllegalStateException(111"Unable to find server impl for " + MECH);112}113114byte[] response = (clnt.hasInitialResponse()?115clnt.evaluateChallenge(EMPTY) : EMPTY);116byte[] challenge;117118while (!clnt.isComplete() || !srv.isComplete()) {119challenge = srv.evaluateResponse(response);120121if (challenge != null) {122response = clnt.evaluateChallenge(challenge);123}124}125126if (clnt.isComplete() && srv.isComplete()) {127if (verbose) {128System.out.println("SUCCESS");129System.out.println("authzid is " + srv.getAuthorizationID());130}131} else {132throw new IllegalStateException("FAILURE: mismatched state:" +133" client complete? " + clnt.isComplete() +134" server complete? " + srv.isComplete());135}136137/* Use security layer */138int count = 0;139for (int i = 0; i < clntStrs.length; i++) {140byte[] orig = clntdata[i];141byte[] wrapped = clnt.wrap(clntdata[i], 0, clntdata[i].length);142byte[] unwrapped = srv.unwrap(wrapped, 0, wrapped.length);143144if (!Arrays.equals(orig, unwrapped)) {145throw new SaslException("Server cannot unwrap client data");146}147148byte[] sorig = srvdata[i];149byte[] swrapped = srv.wrap(srvdata[i], 0, srvdata[i].length);150byte[] sunwrapped = clnt.unwrap(swrapped, 0, swrapped.length);151152if (!Arrays.equals(sorig, sunwrapped)) {153throw new SaslException("Client cannot unwrap server data");154}155++count;156}157158if (verbose) {159System.out.println(count + " sets of wrap/unwrap between client/server");160}161162clnt.dispose();163srv.dispose();164}165166private static final String[] srvStrs = new String[] {167"A is the 1st letter",168"B is the 2nd letter",169"C is the 3rd letter",170"D is the 4th letter",171"E is the 5th letter",172"F is the 6th letter",173"G is the 7th letter",174"H is the 8th letter",175"I is the 9th letter",176"J is the 10th letter",177"K is the 11th letter",178"L is the 12th letter",179"M is the 13th letter",180};181182private static final String[] clntStrs = new String[] {183"0",184"1",185"2",186"3",187"4",188"5",189"6",190"7",191"8",192"9",193"10",194"11",195"12",196};197198private static void initData() {199clntdata = new byte[clntStrs.length][];200for (int i = 0; i < clntStrs.length; i++) {201clntdata[i] = clntStrs[i].getBytes();202}203204srvdata = new byte[srvStrs.length][];205for (int i = 0; i < srvStrs.length; i++) {206srvdata[i] = srvStrs[i].getBytes();207}208}209}210211212