8078439: SPNEGO auth fails if client proposes MS krb5 OID
authorweijun
Tue, 05 May 2015 21:14:12 +0800
changeset 30324 6f3109ded2b8
parent 30323 1b121c8569a2
child 30325 1472b22475f2
8078439: SPNEGO auth fails if client proposes MS krb5 OID Reviewed-by: valeriep
jdk/src/java.security.jgss/share/classes/sun/security/jgss/GSSUtil.java
jdk/src/java.security.jgss/share/classes/sun/security/jgss/spnego/SpNegoContext.java
jdk/test/sun/security/jgss/spnego/MSOID.java
jdk/test/sun/security/jgss/spnego/msoid.txt
jdk/test/sun/security/krb5/auto/MSOID2.java
--- a/jdk/src/java.security.jgss/share/classes/sun/security/jgss/GSSUtil.java	Tue May 05 12:49:11 2015 +0200
+++ b/jdk/src/java.security.jgss/share/classes/sun/security/jgss/GSSUtil.java	Tue May 05 21:14:12 2015 +0800
@@ -59,6 +59,8 @@
                 GSSUtil.createOid("1.2.840.113554.1.2.2");
     public static final Oid GSS_KRB5_MECH_OID2 =
                 GSSUtil.createOid("1.3.5.1.5.2");
+    public static final Oid GSS_KRB5_MECH_OID_MS =
+                GSSUtil.createOid("1.2.840.48018.1.2.2");
 
     public static final Oid GSS_SPNEGO_MECH_OID =
                 GSSUtil.createOid("1.3.6.1.5.5.2");
@@ -101,7 +103,8 @@
 
     public static boolean isKerberosMech(Oid oid) {
         return (GSS_KRB5_MECH_OID.equals(oid) ||
-                GSS_KRB5_MECH_OID2.equals(oid));
+                GSS_KRB5_MECH_OID2.equals(oid) ||
+                GSS_KRB5_MECH_OID_MS.equals(oid));
 
     }
 
--- a/jdk/src/java.security.jgss/share/classes/sun/security/jgss/spnego/SpNegoContext.java	Tue May 05 12:49:11 2015 +0200
+++ b/jdk/src/java.security.jgss/share/classes/sun/security/jgss/spnego/SpNegoContext.java	Tue May 05 21:14:12 2015 +0800
@@ -538,14 +538,21 @@
                 // get the token for mechanism
                 byte[] accept_token;
 
-                if (mechList[0].equals(mech_wanted)) {
+                if (mechList[0].equals(mech_wanted) ||
+                        (GSSUtil.isKerberosMech(mechList[0]) &&
+                         GSSUtil.isKerberosMech(mech_wanted))) {
                     // get the mechanism token
+                    if (DEBUG && !mech_wanted.equals(mechList[0])) {
+                        System.out.println("SpNegoContext.acceptSecContext: " +
+                                "negotiated mech adjusted to " + mechList[0]);
+                    }
                     byte[] mechToken = initToken.getMechToken();
                     if (mechToken == null) {
                         throw new GSSException(GSSException.FAILURE, -1,
                                 "mechToken is missing");
                     }
                     accept_token = GSS_acceptSecContext(mechToken);
+                    mech_wanted = mechList[0];
                 } else {
                     accept_token = null;
                 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/security/jgss/spnego/MSOID.java	Tue May 05 21:14:12 2015 +0800
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8078439
+ * @summary SPNEGO auth fails if client proposes MS krb5 OID
+ */
+
+import org.ietf.jgss.GSSContext;
+import org.ietf.jgss.GSSCredential;
+import org.ietf.jgss.GSSException;
+import org.ietf.jgss.GSSManager;
+
+import java.lang.Exception;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.Base64;
+
+public class MSOID {
+    public static void main(String[] args) throws Exception {
+
+        // msoid.txt is a NegTokenInit packet sent from Internet Explorer to
+        // IIS server on a test machine. No sensitive info included.
+        byte[] header = Files.readAllBytes(
+                Paths.get(System.getProperty("test.src"), "msoid.txt"));
+        byte[] token = Base64.getMimeDecoder().decode(
+                Arrays.copyOfRange(header, 10, header.length));
+
+        GSSCredential cred = null;
+        GSSContext ctx = GSSManager.getInstance().createContext(cred);
+
+        try {
+            ctx.acceptSecContext(token, 0, token.length);
+            // Before the fix, GSS_KRB5_MECH_OID_MS is not recognized
+            // and acceptor chooses another mech and goes on
+            throw new Exception("Should fail");
+        } catch (GSSException gsse) {
+            // After the fix, GSS_KRB5_MECH_OID_MS is recognized but the token
+            // cannot be accepted because we don't have any krb5 credential.
+            gsse.printStackTrace();
+            if (gsse.getMajor() != GSSException.NO_CRED) {
+                throw gsse;
+            }
+            for (StackTraceElement st: gsse.getStackTrace()) {
+                if (st.getClassName().startsWith("sun.security.jgss.krb5.")) {
+                    // Good, it is already in krb5 mech's hand.
+                    return;
+                }
+            }
+            throw gsse;
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/security/jgss/spnego/msoid.txt	Tue May 05 21:14:12 2015 +0800
@@ -0,0 +1,27 @@
+Negotiate YIIGPAYGKwYBBQUCoIIGMDCCBiygMDAuBgkqhkiC9xIBAgIGCSqGSIb3EgECAgYKKwYBBA
+GCNwICHgYKKwYBBAGCNwICCqKCBfYEggXyYIIF7gYJKoZIhvcSAQICAQBuggXdMIIF2aADAgEFoQMCAQ
+6iBwMFACAAAACjggTIYYIExDCCBMCgAwIBBaEOGwxUV0VMVkUuVEhJTkuiIzAhoAMCAQKhGjAYGwRIVF
+RQGxBrZGMuVFdFTFZFLlRISU5Lo4IEgjCCBH6gAwIBEqEDAgEJooIEcASCBGyoIL0zQevk57pY6D25+1
+SQbAeldYXkpdn8JlKgSyz1cdiTpwqD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yQF3KDs5cpvROXcapT1+KlFU4lEO8lnKM/Ipq81ED
+s+/DygXCvlskeKV57URx+XcMWnURu4hdGHbCPY/X7eOmox0mw5/V0rJMIjSjQNPyi4UM4dDTso6mt0XE
+h+YyCGmV67D8/nihO/NaRFEFxHlaGwh3Lqu/Tero88iuDb9U1uEWz8cF8wr+2azyOTmhi/ID/jfiEC8i
+b/hjYEcBI99x/CNmuuM7uCwTPIsJtBD3AnUdPa/yo41rCtm/K5HZCTzw2W93vaHqyttEC7c70rdAUB49
+CfSAVtH4gwxCDKMSJMlELfHGrIloEppEoUEc7LOdmzinvzcuajj0moBn5WUZHiVmopLjGjW7wunmMPQS
+H9FmCQf2I1N4E6nZfH+cUzBbHkIF5XHY4KXwmJQ3UdbUDp8z3npIH3MIH0oAMCARKigewEgenD23U6gQ
+aORjuWnT1nqadqR+E5fa/viohey4g6mn6uPfVRPz5a7OsDOurQV9wHR/VEwvjpdlZzMcANbt28Ut3YvQ
+SWWwqALoLtSLOTgXmK9Higb+NSSO7hKtqKgDWREfQisn3xE9PGkMUlanu2es34+k43AQmJf2InvFNNcy
+PcKllikoMOldVeoF1BIKvbDI0+vE3SwSrD0UhUdDeeZTN33b0Y8f3I1UYtidwxcRRkvCaNEhphtr8hp8
+hXWQkuxVvF2TiQyHF4PnJkgb1Zr6GXydOmMgMJE1anPFKFKWH6PZWGnp8mw0F5zw==
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/security/krb5/auto/MSOID2.java	Tue May 05 21:14:12 2015 +0800
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8078439
+ * @summary SPNEGO auth fails if client proposes MS krb5 OID
+ * @compile -XDignore.symbol.file MSOID2.java
+ * @run main/othervm MSOID2
+ */
+
+import sun.security.jgss.GSSUtil;
+
+// The basic krb5 test skeleton you can copy from
+public class MSOID2 {
+
+    public static void main(String[] args) throws Exception {
+
+        new OneKDC(null).writeJAASConf();
+
+        Context c, s;
+        c = Context.fromJAAS("client");
+        s = Context.fromJAAS("server");
+
+        c.startAsClient(OneKDC.SERVER, GSSUtil.GSS_SPNEGO_MECH_OID);
+        s.startAsServer(GSSUtil.GSS_SPNEGO_MECH_OID);
+
+        byte[] t = new byte[0];
+        boolean first = true;
+        while (true) {
+            if (t != null || !c.x().isEstablished()) t = c.take(t);
+            if (first) {
+                // Tweak the packet to append an extra OID
+                int len = t.length;
+                byte[] nt = new byte[len + 11];
+                System.arraycopy(t, 0, nt, 0, 0x23);
+                System.arraycopy(t, 0x18, nt, 0x23, 11);    // dup the OID
+                System.arraycopy(t, 0x23, nt, 0x2e, len-0x23);
+                nt[0x1d] = (byte)0x82;  // change the 1st to MS OID
+                // Length bytes to be tweaked
+                for (int pos: new int[] {3, 0xf, 0x13, 0x15, 0x17}) {
+                    nt[pos] = (byte)(nt[pos] + 11);
+                }
+                t = nt;
+                new sun.misc.HexDumpEncoder().encodeBuffer(t, System.out);
+            }
+            if (t != null || !s.x().isEstablished()) t = s.take(t);
+            if (c.x().isEstablished() && s.x().isEstablished()) break;
+            first = false;
+        }
+
+        Context.transmit("i say high --", c, s);
+        Context.transmit("   you say low", s, c);
+
+        s.dispose();
+        c.dispose();
+    }
+}