1 /*
   2  * Copyright (c) 2012, 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 6355584
  27  * @summary Introduce constrained Kerberos delegation
  28  * @modules java.base/sun.net.spi.nameservice
  29  *          java.base/sun.security.util
  30  *          java.security.jgss/sun.security.jgss
  31  *          java.security.jgss/sun.security.krb5
  32  *          java.security.jgss/sun.security.krb5.internal
  33  *          java.security.jgss/sun.security.krb5.internal.ccache
  34  *          java.security.jgss/sun.security.krb5.internal.crypto
  35  *          java.security.jgss/sun.security.krb5.internal.ktab
  36  * @compile -XDignore.symbol.file S4U2selfGSS.java
  37  * @run main/othervm -Dsun.security.krb5.debug=false S4U2selfGSS krb5
  38  * @run main/othervm -Dsun.security.krb5.debug=false S4U2selfGSS spnego
  39  */
  40 
  41 import java.util.Arrays;
  42 import java.util.HashMap;
  43 import java.util.List;
  44 import java.util.Map;
  45 import org.ietf.jgss.Oid;
  46 import sun.security.jgss.GSSUtil;
  47 
  48 public class S4U2selfGSS {
  49 
  50     public static void main(String[] args) throws Exception {
  51         Oid mech;
  52         if (args[0].equals("spnego")) {
  53             mech = GSSUtil.GSS_SPNEGO_MECH_OID;
  54         } else if (args[0].contains("krb5")) {
  55             mech = GSSUtil.GSS_KRB5_MECH_OID;
  56         } else {
  57             throw new Exception("Unknown mech");
  58         }
  59 
  60         OneKDC kdc = new OneKDC(null);
  61         kdc.writeJAASConf();
  62         kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
  63                 new String[]{OneKDC.USER + "@" + OneKDC.REALM}));
  64         Map<String,List<String>> map = new HashMap<>();
  65         map.put(OneKDC.USER + "@" + OneKDC.REALM, Arrays.asList(
  66                 new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));
  67         kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
  68 
  69         Context c, s;
  70         System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
  71         c = Context.fromThinAir();
  72         s = Context.fromThinAir();
  73 
  74         c = c.impersonate(OneKDC.USER2);
  75 
  76         c.startAsClient(OneKDC.SERVER, mech);
  77         s.startAsServer(mech);
  78 
  79         Context.handshake(c, s);
  80 
  81         String n1 = c.x().getSrcName().toString().split("@")[0];
  82         String n2 = s.x().getSrcName().toString().split("@")[0];
  83         if (!n1.equals(OneKDC.USER2) || !n2.equals(OneKDC.USER2)) {
  84             throw new Exception("Impersonate failed");
  85         }
  86 
  87         s.dispose();
  88         c.dispose();
  89     }
  90 }