|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 8160818 |
| 27 | + * @summary GssKrb5Client violates RFC 4752 |
| 28 | + * @library /test/lib |
| 29 | + * @compile -XDignore.symbol.file SaslMutual.java |
| 30 | + * @run main jdk.test.lib.FileInstaller TestHosts TestHosts |
| 31 | + * @run main/othervm -Djdk.net.hosts.file=TestHosts SaslMutual |
| 32 | + */ |
| 33 | +import jdk.test.lib.Asserts; |
| 34 | + |
| 35 | +import java.util.Map; |
| 36 | +import javax.security.auth.callback.Callback; |
| 37 | +import javax.security.sasl.*; |
| 38 | + |
| 39 | +public class SaslMutual { |
| 40 | + |
| 41 | + public static void main(String[] args) throws Exception { |
| 42 | + |
| 43 | + String name = "host." + OneKDC.REALM_LOWER_CASE; |
| 44 | + |
| 45 | + new OneKDC(null).writeJAASConf(); |
| 46 | + System.setProperty("javax.security.auth.useSubjectCredsOnly", "false"); |
| 47 | + |
| 48 | + SaslClient sc; |
| 49 | + |
| 50 | + sc = Sasl.createSaslClient( |
| 51 | + new String[]{"GSSAPI"}, null, "server", |
| 52 | + name, |
| 53 | + Map.of(), |
| 54 | + null); |
| 55 | + Asserts.assertEQ(round(sc, server()), 2); |
| 56 | + |
| 57 | + sc = Sasl.createSaslClient( |
| 58 | + new String[]{"GSSAPI"}, null, "server", |
| 59 | + name, |
| 60 | + Map.of(Sasl.SERVER_AUTH, "true"), |
| 61 | + null); |
| 62 | + Asserts.assertEQ(round(sc, server()), 3); |
| 63 | + |
| 64 | + sc = Sasl.createSaslClient( |
| 65 | + new String[]{"GSSAPI"}, null, "server", |
| 66 | + name, |
| 67 | + Map.of(Sasl.QOP, "auth-int"), |
| 68 | + null); |
| 69 | + Asserts.assertEQ(round(sc, server()), 3); |
| 70 | + |
| 71 | + sc = Sasl.createSaslClient( |
| 72 | + new String[]{"GSSAPI"}, null, "server", |
| 73 | + name, |
| 74 | + Map.of(Sasl.QOP, "auth-conf"), |
| 75 | + null); |
| 76 | + Asserts.assertEQ(round(sc, server()), 3); |
| 77 | + } |
| 78 | + |
| 79 | + static SaslServer server() throws Exception { |
| 80 | + return Sasl.createSaslServer("GSSAPI", "server", |
| 81 | + null, |
| 82 | + Map.of(Sasl.QOP, "auth,auth-int,auth-conf"), |
| 83 | + callbacks -> { |
| 84 | + for (Callback cb : callbacks) { |
| 85 | + if (cb instanceof RealmCallback) { |
| 86 | + ((RealmCallback) cb).setText(OneKDC.REALM); |
| 87 | + } else if (cb instanceof AuthorizeCallback) { |
| 88 | + ((AuthorizeCallback) cb).setAuthorized(true); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + static int round(SaslClient sc, SaslServer ss) throws Exception { |
| 95 | + int round = 0; |
| 96 | + byte[] token = new byte[0]; |
| 97 | + while (!sc.isComplete() || !ss.isComplete()) { |
| 98 | + if (!sc.isComplete()) { |
| 99 | + token = sc.evaluateChallenge(token); |
| 100 | + } |
| 101 | + if (!ss.isComplete()) { |
| 102 | + token = ss.evaluateResponse(token); |
| 103 | + } |
| 104 | + round++; |
| 105 | + } |
| 106 | + return round; |
| 107 | + } |
| 108 | +} |
0 commit comments