Java Code Examples for sun.security.jgss.GSSUtil#GSS_SPNEGO_MECH_OID

The following examples show how to use sun.security.jgss.GSSUtil#GSS_SPNEGO_MECH_OID . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpnegoLifeTime.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        Oid oid = GSSUtil.GSS_SPNEGO_MECH_OID;
        new OneKDC(null).writeJAASConf();

        Context c, s;
        c = Context.fromJAAS("client");
        s = Context.fromJAAS("server");

        c.startAsClient(OneKDC.SERVER, oid);
        c.x().requestCredDeleg(true);
        s.startAsServer(oid);

        Context.handshake(c, s);

        GSSCredential cred = s.delegated().cred();
        cred.getRemainingInitLifetime(oid);
        cred.getUsage(oid);
    }
 
Example 2
Source File: SpnegoLifeTime.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        Oid oid = GSSUtil.GSS_SPNEGO_MECH_OID;
        new OneKDC(null).writeJAASConf();

        Context c, s;
        c = Context.fromJAAS("client");
        s = Context.fromJAAS("server");

        c.startAsClient(OneKDC.SERVER, oid);
        c.x().requestCredDeleg(true);
        s.startAsServer(oid);

        Context.handshake(c, s);

        GSSCredential cred = s.delegated().cred();
        cred.getRemainingInitLifetime(oid);
        cred.getUsage(oid);
    }
 
Example 3
Source File: S4U2selfAsServer.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Oid mech;
    if (args[0].equals("spnego")) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    } else if (args[0].contains("krb5")) {
        mech = GSSUtil.GSS_KRB5_MECH_OID;
    } else {
        throw new Exception("Unknown mech");
    }

    OneKDC kdc = new OneKDC(null);
    kdc.writeJAASConf();
    kdc.setOption(KDC.Option.PREAUTH_REQUIRED, false);
    Map<String,List<String>> map = new HashMap<>();
    map.put(OneKDC.SERVER + "@" + OneKDC.REALM, Arrays.asList(
            new String[]{OneKDC.BACKEND + "@" + OneKDC.REALM}));
    kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
    kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));

    Context s, b;
    s = Context.fromJAAS("server");
    b = Context.fromJAAS("backend");

    s.startAsServer(null, mech, false);

    Context p = s.impersonate(OneKDC.USER);

    p.startAsClient(OneKDC.BACKEND, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);

    p.startAsClient(OneKDC.BACKEND, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);
}
 
Example 4
Source File: S4U2selfAsServer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Oid mech;
    if (args[0].equals("spnego")) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    } else if (args[0].contains("krb5")) {
        mech = GSSUtil.GSS_KRB5_MECH_OID;
    } else {
        throw new Exception("Unknown mech");
    }

    OneKDC kdc = new OneKDC(null);
    kdc.writeJAASConf();
    kdc.setOption(KDC.Option.PREAUTH_REQUIRED, false);
    Map<String,List<String>> map = new HashMap<>();
    map.put(OneKDC.SERVER + "@" + OneKDC.REALM, Arrays.asList(
            new String[]{OneKDC.BACKEND + "@" + OneKDC.REALM}));
    kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
    kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));

    Context s, b;
    s = Context.fromJAAS("server");
    b = Context.fromJAAS("backend");

    s.startAsServer(null, mech, false);

    Context p = s.impersonate(OneKDC.USER);

    p.startAsClient(OneKDC.BACKEND, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);

    p.startAsClient(OneKDC.BACKEND, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);
}
 
Example 5
Source File: S4U2selfAsServer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Oid mech;
    if (args[0].equals("spnego")) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    } else if (args[0].contains("krb5")) {
        mech = GSSUtil.GSS_KRB5_MECH_OID;
    } else {
        throw new Exception("Unknown mech");
    }

    OneKDC kdc = new OneKDC(null);
    kdc.writeJAASConf();
    kdc.setOption(KDC.Option.PREAUTH_REQUIRED, false);
    Map<String,List<String>> map = new HashMap<>();
    map.put(OneKDC.SERVER + "@" + OneKDC.REALM, Arrays.asList(
            new String[]{OneKDC.BACKEND + "@" + OneKDC.REALM}));
    kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
    kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));

    Context s, b;
    s = Context.fromJAAS("server");
    b = Context.fromJAAS("backend");

    s.startAsServer(null, mech, false);

    Context p = s.impersonate(OneKDC.USER);

    p.startAsClient(OneKDC.BACKEND, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);

    p.startAsClient(OneKDC.BACKEND, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);
}
 
Example 6
Source File: S4U2proxyGSS.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Oid mech;
    if (args[0].equals("spnego")) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    } else if (args[0].contains("krb5")) {
        mech = GSSUtil.GSS_KRB5_MECH_OID;
    } else {
        throw new Exception("Unknown mech");
    }

    OneKDC kdc = new OneKDC(null);
    kdc.writeJAASConf();
    kdc.setOption(KDC.Option.PREAUTH_REQUIRED, false);
    Map<String,List<String>> map = new HashMap<>();
    map.put(OneKDC.SERVER + "@" + OneKDC.REALM, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));
    kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);

    Context c, s, b;
    System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
    System.setProperty("java.security.auth.login.config", OneKDC.JAAS_CONF);
    File f = new File(OneKDC.JAAS_CONF);
    FileOutputStream fos = new FileOutputStream(f);
    fos.write((
            "com.sun.security.jgss.krb5.initiate {\n" +
            "    com.sun.security.auth.module.Krb5LoginModule required;\n};\n" +
            "com.sun.security.jgss.krb5.accept {\n" +
            "    com.sun.security.auth.module.Krb5LoginModule required\n" +
            "    principal=\"" + OneKDC.SERVER + "\"\n" +
            "    useKeyTab=true\n" +
            "    storeKey=true;\n};\n"
            ).getBytes());
    fos.close();
    Security.setProperty("auth.login.defaultCallbackHandler", "OneKDC$CallbackForClient");
    c = Context.fromThinAir();
    s = Context.fromThinAir();
    b = Context.fromThinAir();
    c.startAsClient(OneKDC.SERVER, mech);
    c.x().requestCredDeleg(false);
    s.startAsServer(mech);

    Context.handshake(c, s);
    Context p = s.delegated();
    p.startAsClient(OneKDC.SERVER, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);

    String n1 = p.x().getSrcName().toString().split("@")[0];
    String n2 = b.x().getSrcName().toString().split("@")[0];
    if (!n1.equals(OneKDC.USER) || !n2.equals(OneKDC.USER)) {
        throw new Exception("Delegation failed");
    }
}
 
Example 7
Source File: S4U2selfAsServerGSS.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Oid mech;
    if (args[0].equals("spnego")) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    } else if (args[0].contains("krb5")) {
        mech = GSSUtil.GSS_KRB5_MECH_OID;
    } else {
        throw new Exception("Unknown mech");
    }

    OneKDC kdc = new OneKDC(null);
    kdc.writeJAASConf();
    kdc.setOption(KDC.Option.PREAUTH_REQUIRED, false);
    Map<String,List<String>> map = new HashMap<>();
    map.put(OneKDC.SERVER + "@" + OneKDC.REALM, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));
    kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
    kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));

    Context s, b;
    System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
    System.setProperty("java.security.auth.login.config", OneKDC.JAAS_CONF);
    File f = new File(OneKDC.JAAS_CONF);
    FileOutputStream fos = new FileOutputStream(f);
    fos.write((
            "com.sun.security.jgss.krb5.accept {\n" +
            "    com.sun.security.auth.module.Krb5LoginModule required\n" +
            "    principal=\"" + OneKDC.SERVER + "\"\n" +
            "    useKeyTab=true\n" +
            "    storeKey=true;\n};\n"
            ).getBytes());
    fos.close();
    Security.setProperty("auth.login.defaultCallbackHandler", "OneKDC$CallbackForClient");
    s = Context.fromThinAir();
    b = Context.fromThinAir();
    s.startAsServer(mech);

    Context p = s.impersonate(OneKDC.USER);

    p.startAsClient(OneKDC.SERVER, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);

    String n1 = p.x().getSrcName().toString().split("@")[0];
    String n2 = b.x().getSrcName().toString().split("@")[0];
    if (!n1.equals(OneKDC.USER) || !n2.equals(OneKDC.USER)) {
        throw new Exception("Delegation failed");
    }
}
 
Example 8
Source File: OkAsDelegate.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
void go(
        boolean forwardable,
        boolean requestDelegState,
        boolean requestDelegPolicyState,
        boolean delegState,
        boolean delegPolicyState,
        boolean delegated
        ) throws Exception {
    OneKDC kdc = new OneKDC(null);
    kdc.setOption(KDC.Option.OK_AS_DELEGATE,
            System.getProperty("test.kdc.policy.ok-as-delegate"));
    kdc.writeJAASConf();
    if (!forwardable) {
        // The default OneKDC always includes "forwardable = true"
        // in krb5.conf, override it.
        KDC.saveConfig(OneKDC.KRB5_CONF, kdc,
                "default_keytab_name = " + OneKDC.KTAB);
        Config.refresh();
    }

    Context c, s;
    c = Context.fromJAAS("client");
    s = Context.fromJAAS("com.sun.security.jgss.krb5.accept");

    Oid mech = GSSUtil.GSS_KRB5_MECH_OID;
    if (System.getProperty("test.spnego") != null) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    }
    c.startAsClient(OneKDC.SERVER, mech);
    ExtendedGSSContext cx = (ExtendedGSSContext)c.x();
    cx.requestCredDeleg(requestDelegState);
    cx.requestDelegPolicy(requestDelegPolicyState);
    s.startAsServer(mech);
    ExtendedGSSContext sx = (ExtendedGSSContext)s.x();

    Context.handshake(c, s);

    if (cx.getCredDelegState() != delegState) {
        throw new Exception("Initiator cred state error");
    }
    if (sx.getCredDelegState() != delegState) {
        throw new Exception("Acceptor cred state error");
    }
    if (cx.getDelegPolicyState() != delegPolicyState) {
        throw new Exception("Initiator cred policy state error");
    }

    GSSCredential cred = null;
    try {
        cred = s.x().getDelegCred();
    } catch (GSSException e) {
        // leave cred as null
    }

    if (delegated != (cred != null)) {
        throw new Exception("get cred error");
    }
}
 
Example 9
Source File: S4U2selfAsServerGSS.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Oid mech;
    if (args[0].equals("spnego")) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    } else if (args[0].contains("krb5")) {
        mech = GSSUtil.GSS_KRB5_MECH_OID;
    } else {
        throw new Exception("Unknown mech");
    }

    OneKDC kdc = new OneKDC(null);
    kdc.writeJAASConf();
    kdc.setOption(KDC.Option.PREAUTH_REQUIRED, false);
    Map<String,List<String>> map = new HashMap<>();
    map.put(OneKDC.SERVER + "@" + OneKDC.REALM, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));
    kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
    kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));

    Context s, b;
    System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
    System.setProperty("java.security.auth.login.config", OneKDC.JAAS_CONF);
    File f = new File(OneKDC.JAAS_CONF);
    FileOutputStream fos = new FileOutputStream(f);
    fos.write((
            "com.sun.security.jgss.krb5.accept {\n" +
            "    com.sun.security.auth.module.Krb5LoginModule required\n" +
            "    principal=\"" + OneKDC.SERVER + "\"\n" +
            "    useKeyTab=true\n" +
            "    storeKey=true;\n};\n"
            ).getBytes());
    fos.close();
    Security.setProperty("auth.login.defaultCallbackHandler", "OneKDC$CallbackForClient");
    s = Context.fromThinAir();
    b = Context.fromThinAir();
    s.startAsServer(mech);

    Context p = s.impersonate(OneKDC.USER);

    p.startAsClient(OneKDC.SERVER, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);

    String n1 = p.x().getSrcName().toString().split("@")[0];
    String n2 = b.x().getSrcName().toString().split("@")[0];
    if (!n1.equals(OneKDC.USER) || !n2.equals(OneKDC.USER)) {
        throw new Exception("Delegation failed");
    }
}
 
Example 10
Source File: SpNegoCredElement.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public Oid getMechanism() {
    return GSSUtil.GSS_SPNEGO_MECH_OID;
}
 
Example 11
Source File: SpNegoCredElement.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public Oid getMechanism() {
    return GSSUtil.GSS_SPNEGO_MECH_OID;
}
 
Example 12
Source File: NegotiatorImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Initialize the object, which includes:<ul>
 * <li>Find out what GSS mechanism to use from the system property
 * <code>http.negotiate.mechanism.oid</code>, defaults SPNEGO
 * <li>Creating the GSSName for the target host, "HTTP/"+hostname
 * <li>Creating GSSContext
 * <li>A first call to initSecContext</ul>
 */
private void init(HttpCallerInfo hci) throws GSSException {
    final Oid oid;

    if (hci.scheme.equalsIgnoreCase("Kerberos")) {
        // we can only use Kerberos mech when the scheme is kerberos
        oid = GSSUtil.GSS_KRB5_MECH_OID;
    } else {
        String pref = java.security.AccessController.doPrivileged(
                new java.security.PrivilegedAction<String>() {
                    public String run() {
                        return System.getProperty(
                            "http.auth.preference",
                            "spnego");
                    }
                });
        if (pref.equalsIgnoreCase("kerberos")) {
            oid = GSSUtil.GSS_KRB5_MECH_OID;
        } else {
            // currently there is no 3rd mech we can use
            oid = GSSUtil.GSS_SPNEGO_MECH_OID;
        }
    }

    GSSManagerImpl manager = new GSSManagerImpl(
            new HttpCaller(hci));

    // RFC 4559 4.1 uses uppercase service name "HTTP".
    // RFC 4120 6.2.1 demands the host be lowercase
    String peerName = "HTTP@" + hci.host.toLowerCase();

    GSSName serverName = manager.createName(peerName,
            GSSName.NT_HOSTBASED_SERVICE);
    context = manager.createContext(serverName,
                                    oid,
                                    null,
                                    GSSContext.DEFAULT_LIFETIME);

    // Always respect delegation policy in HTTP/SPNEGO.
    if (context instanceof GSSContextImpl) {
        ((GSSContextImpl)context).requestDelegPolicy(true);
    }
    oneToken = context.initSecContext(new byte[0], 0, 0);
}
 
Example 13
Source File: S4U2proxyGSS.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Oid mech;
    if (args[0].equals("spnego")) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    } else if (args[0].contains("krb5")) {
        mech = GSSUtil.GSS_KRB5_MECH_OID;
    } else {
        throw new Exception("Unknown mech");
    }

    OneKDC kdc = new OneKDC(null);
    kdc.writeJAASConf();
    kdc.setOption(KDC.Option.PREAUTH_REQUIRED, false);
    Map<String,List<String>> map = new HashMap<>();
    map.put(OneKDC.SERVER + "@" + OneKDC.REALM, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));
    kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);

    Context c, s, b;
    System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
    System.setProperty("java.security.auth.login.config", OneKDC.JAAS_CONF);
    File f = new File(OneKDC.JAAS_CONF);
    FileOutputStream fos = new FileOutputStream(f);
    fos.write((
            "com.sun.security.jgss.krb5.initiate {\n" +
            "    com.sun.security.auth.module.Krb5LoginModule required;\n};\n" +
            "com.sun.security.jgss.krb5.accept {\n" +
            "    com.sun.security.auth.module.Krb5LoginModule required\n" +
            "    principal=\"" + OneKDC.SERVER + "\"\n" +
            "    useKeyTab=true\n" +
            "    storeKey=true;\n};\n"
            ).getBytes());
    fos.close();
    Security.setProperty("auth.login.defaultCallbackHandler", "OneKDC$CallbackForClient");
    c = Context.fromThinAir();
    s = Context.fromThinAir();
    b = Context.fromThinAir();
    c.startAsClient(OneKDC.SERVER, mech);
    c.x().requestCredDeleg(false);
    s.startAsServer(mech);

    Context.handshake(c, s);
    Context p = s.delegated();
    p.startAsClient(OneKDC.SERVER, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);

    String n1 = p.x().getSrcName().toString().split("@")[0];
    String n2 = b.x().getSrcName().toString().split("@")[0];
    if (!n1.equals(OneKDC.USER) || !n2.equals(OneKDC.USER)) {
        throw new Exception("Delegation failed");
    }
}
 
Example 14
Source File: S4U2self.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Test case, different policy settings in KDC:
    //                   |     ALLOW_S4U2SELF on
    //                   |   USER    USER2    none
    // ALLOW_S4U2PORXY   |-------------------------
    // USER to BACKEND   |   0       1        2
    // USER2 to BACKEND  |   3
    // USER to SERVER    |   4
    //      none         |   5
    //
    // 0 should succeed, all other fail
    int test = 0;
    Oid mech;
    if (args[0].equals("spnego")) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    } else if (args[0].contains("krb5")) {
        mech = GSSUtil.GSS_KRB5_MECH_OID;
        test = Integer.parseInt(args[1]);
    } else {
        throw new Exception("Unknown mech");
    }

    OneKDC kdc = new OneKDC(null);
    kdc.writeJAASConf();

    switch (test) {
        case 1:
            kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
                    new String[]{OneKDC.USER2 + "@" + OneKDC.REALM}));
            break;
        case 2:
            // No S4U2self
            break;
        default:
            kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
                    new String[]{OneKDC.USER + "@" + OneKDC.REALM}));
            break;
    }

    Map<String,List<String>> map = new HashMap<>();
    switch (test) {
        case 3:
            map.put(OneKDC.USER2 + "@" + OneKDC.REALM, Arrays.asList(
                    new String[]{OneKDC.BACKEND + "@" + OneKDC.REALM}));
            kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
            break;
        case 4:
            map.put(OneKDC.USER + "@" + OneKDC.REALM, Arrays.asList(
                    new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));
            kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
            break;
        case 5:
            // No S4U2proxy set
            break;
        default:
            map.put(OneKDC.USER + "@" + OneKDC.REALM, Arrays.asList(
                    new String[]{OneKDC.BACKEND + "@" + OneKDC.REALM}));
            kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
            break;
    }

    Context c, s;
    c = Context.fromJAAS("client");

    c = c.impersonate(OneKDC.USER2);
    c.status();

    c.startAsClient(OneKDC.BACKEND, mech);

    s = Context.fromJAAS("backend");
    s.startAsServer(mech);

    Context.handshake(c, s);

    Context.transmit("i say high --", c, s);
    Context.transmit("   you say low", s, c);

    c.status();
    s.status();

    String n1 = c.x().getSrcName().toString().split("@")[0];
    String n2 = s.x().getSrcName().toString().split("@")[0];
    if (!n1.equals(OneKDC.USER2) || !n2.equals(OneKDC.USER2)) {
        throw new Exception("Impersonate failed");
    }

    s.dispose();
    c.dispose();
}
 
Example 15
Source File: SpNegoCredElement.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public Oid getMechanism() {
    return GSSUtil.GSS_SPNEGO_MECH_OID;
}
 
Example 16
Source File: S4U2selfAsServerGSS.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Oid mech;
    if (args[0].equals("spnego")) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    } else if (args[0].contains("krb5")) {
        mech = GSSUtil.GSS_KRB5_MECH_OID;
    } else {
        throw new Exception("Unknown mech");
    }

    OneKDC kdc = new OneKDC(null);
    kdc.writeJAASConf();
    kdc.setOption(KDC.Option.PREAUTH_REQUIRED, false);
    Map<String,List<String>> map = new HashMap<>();
    map.put(OneKDC.SERVER + "@" + OneKDC.REALM, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));
    kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
    kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));

    Context s, b;
    System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
    System.setProperty("java.security.auth.login.config", OneKDC.JAAS_CONF);
    File f = new File(OneKDC.JAAS_CONF);
    FileOutputStream fos = new FileOutputStream(f);
    fos.write((
            "com.sun.security.jgss.krb5.accept {\n" +
            "    com.sun.security.auth.module.Krb5LoginModule required\n" +
            "    principal=\"" + OneKDC.SERVER + "\"\n" +
            "    useKeyTab=true\n" +
            "    storeKey=true;\n};\n"
            ).getBytes());
    fos.close();
    Security.setProperty("auth.login.defaultCallbackHandler", "OneKDC$CallbackForClient");
    s = Context.fromThinAir();
    b = Context.fromThinAir();
    s.startAsServer(mech);

    Context p = s.impersonate(OneKDC.USER);

    p.startAsClient(OneKDC.SERVER, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);

    String n1 = p.x().getSrcName().toString().split("@")[0];
    String n2 = b.x().getSrcName().toString().split("@")[0];
    if (!n1.equals(OneKDC.USER) || !n2.equals(OneKDC.USER)) {
        throw new Exception("Delegation failed");
    }
}
 
Example 17
Source File: NegotiatorImpl.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Initialize the object, which includes:<ul>
 * <li>Find out what GSS mechanism to use from the system property
 * <code>http.negotiate.mechanism.oid</code>, defaults SPNEGO
 * <li>Creating the GSSName for the target host, "HTTP/"+hostname
 * <li>Creating GSSContext
 * <li>A first call to initSecContext</ul>
 */
private void init(HttpCallerInfo hci) throws GSSException {
    final Oid oid;

    if (hci.scheme.equalsIgnoreCase("Kerberos")) {
        // we can only use Kerberos mech when the scheme is kerberos
        oid = GSSUtil.GSS_KRB5_MECH_OID;
    } else {
        String pref = java.security.AccessController.doPrivileged(
                new java.security.PrivilegedAction<String>() {
                    public String run() {
                        return System.getProperty(
                            "http.auth.preference",
                            "spnego");
                    }
                });
        if (pref.equalsIgnoreCase("kerberos")) {
            oid = GSSUtil.GSS_KRB5_MECH_OID;
        } else {
            // currently there is no 3rd mech we can use
            oid = GSSUtil.GSS_SPNEGO_MECH_OID;
        }
    }

    GSSManagerImpl manager = new GSSManagerImpl(
            new HttpCaller(hci));

    // RFC 4559 4.1 uses uppercase service name "HTTP".
    // RFC 4120 6.2.1 demands the host be lowercase
    String peerName = "HTTP@" + hci.host.toLowerCase();

    GSSName serverName = manager.createName(peerName,
            GSSName.NT_HOSTBASED_SERVICE);
    context = manager.createContext(serverName,
                                    oid,
                                    null,
                                    GSSContext.DEFAULT_LIFETIME);

    // Always respect delegation policy in HTTP/SPNEGO.
    if (context instanceof ExtendedGSSContext) {
        ((ExtendedGSSContext)context).requestDelegPolicy(true);
    }
    oneToken = context.initSecContext(new byte[0], 0, 0);
}
 
Example 18
Source File: S4U2self.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Test case, different policy settings in KDC:
    //                   |     ALLOW_S4U2SELF on
    //                   |   USER    USER2    none
    // ALLOW_S4U2PORXY   |-------------------------
    // USER to BACKEND   |   0       1        2
    // USER2 to BACKEND  |   3
    // USER to SERVER    |   4
    //      none         |   5
    //
    // 0 should succeed, all other fail
    int test = 0;
    Oid mech;
    if (args[0].equals("spnego")) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    } else if (args[0].contains("krb5")) {
        mech = GSSUtil.GSS_KRB5_MECH_OID;
        test = Integer.parseInt(args[1]);
    } else {
        throw new Exception("Unknown mech");
    }

    OneKDC kdc = new OneKDC(null);
    kdc.writeJAASConf();

    switch (test) {
        case 1:
            kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
                    new String[]{OneKDC.USER2 + "@" + OneKDC.REALM}));
            break;
        case 2:
            // No S4U2self
            break;
        default:
            kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
                    new String[]{OneKDC.USER + "@" + OneKDC.REALM}));
            break;
    }

    Map<String,List<String>> map = new HashMap<>();
    switch (test) {
        case 3:
            map.put(OneKDC.USER2 + "@" + OneKDC.REALM, Arrays.asList(
                    new String[]{OneKDC.BACKEND + "@" + OneKDC.REALM}));
            kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
            break;
        case 4:
            map.put(OneKDC.USER + "@" + OneKDC.REALM, Arrays.asList(
                    new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));
            kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
            break;
        case 5:
            // No S4U2proxy set
            break;
        default:
            map.put(OneKDC.USER + "@" + OneKDC.REALM, Arrays.asList(
                    new String[]{OneKDC.BACKEND + "@" + OneKDC.REALM}));
            kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
            break;
    }

    Context c, s;
    c = Context.fromJAAS("client");

    c = c.impersonate(OneKDC.USER2);
    c.status();

    c.startAsClient(OneKDC.BACKEND, mech);

    s = Context.fromJAAS("backend");
    s.startAsServer(mech);

    Context.handshake(c, s);

    Context.transmit("i say high --", c, s);
    Context.transmit("   you say low", s, c);

    c.status();
    s.status();

    String n1 = c.x().getSrcName().toString().split("@")[0];
    String n2 = s.x().getSrcName().toString().split("@")[0];
    if (!n1.equals(OneKDC.USER2) || !n2.equals(OneKDC.USER2)) {
        throw new Exception("Impersonate failed");
    }

    s.dispose();
    c.dispose();
}
 
Example 19
Source File: S4U2proxyGSS.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Oid mech;
    if (args[0].equals("spnego")) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    } else if (args[0].contains("krb5")) {
        mech = GSSUtil.GSS_KRB5_MECH_OID;
    } else {
        throw new Exception("Unknown mech");
    }

    OneKDC kdc = new OneKDC(null);
    kdc.writeJAASConf();
    kdc.setOption(KDC.Option.PREAUTH_REQUIRED, false);
    Map<String,List<String>> map = new HashMap<>();
    map.put(OneKDC.SERVER + "@" + OneKDC.REALM, Arrays.asList(
            new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));
    kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);

    Context c, s, b;
    System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
    System.setProperty("java.security.auth.login.config", OneKDC.JAAS_CONF);
    File f = new File(OneKDC.JAAS_CONF);
    FileOutputStream fos = new FileOutputStream(f);
    fos.write((
            "com.sun.security.jgss.krb5.initiate {\n" +
            "    com.sun.security.auth.module.Krb5LoginModule required;\n};\n" +
            "com.sun.security.jgss.krb5.accept {\n" +
            "    com.sun.security.auth.module.Krb5LoginModule required\n" +
            "    principal=\"" + OneKDC.SERVER + "\"\n" +
            "    useKeyTab=true\n" +
            "    storeKey=true;\n};\n"
            ).getBytes());
    fos.close();
    Security.setProperty("auth.login.defaultCallbackHandler", "OneKDC$CallbackForClient");
    c = Context.fromThinAir();
    s = Context.fromThinAir();
    b = Context.fromThinAir();
    c.startAsClient(OneKDC.SERVER, mech);
    c.x().requestCredDeleg(false);
    s.startAsServer(mech);

    Context.handshake(c, s);
    Context p = s.delegated();
    p.startAsClient(OneKDC.SERVER, mech);
    b.startAsServer(mech);
    Context.handshake(p, b);

    String n1 = p.x().getSrcName().toString().split("@")[0];
    String n2 = b.x().getSrcName().toString().split("@")[0];
    if (!n1.equals(OneKDC.USER) || !n2.equals(OneKDC.USER)) {
        throw new Exception("Delegation failed");
    }
}
 
Example 20
Source File: OkAsDelegate.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
void go(
        boolean forwardable,
        boolean requestDelegState,
        boolean requestDelegPolicyState,
        boolean delegState,
        boolean delegPolicyState,
        boolean delegated
        ) throws Exception {
    OneKDC kdc = new OneKDC(null);
    kdc.setOption(KDC.Option.OK_AS_DELEGATE,
            System.getProperty("test.kdc.policy.ok-as-delegate"));
    kdc.writeJAASConf();
    if (!forwardable) {
        // The default OneKDC always includes "forwardable = true"
        // in krb5.conf, override it.
        KDC.saveConfig(OneKDC.KRB5_CONF, kdc,
                "default_keytab_name = " + OneKDC.KTAB);
        Config.refresh();
    }

    Context c, s;
    c = Context.fromJAAS("client");
    s = Context.fromJAAS("com.sun.security.jgss.krb5.accept");

    Oid mech = GSSUtil.GSS_KRB5_MECH_OID;
    if (System.getProperty("test.spnego") != null) {
        mech = GSSUtil.GSS_SPNEGO_MECH_OID;
    }
    c.startAsClient(OneKDC.SERVER, mech);
    ExtendedGSSContext cx = (ExtendedGSSContext)c.x();
    cx.requestCredDeleg(requestDelegState);
    cx.requestDelegPolicy(requestDelegPolicyState);
    s.startAsServer(mech);
    ExtendedGSSContext sx = (ExtendedGSSContext)s.x();

    Context.handshake(c, s);

    if (cx.getCredDelegState() != delegState) {
        throw new Exception("Initiator cred state error");
    }
    if (sx.getCredDelegState() != delegState) {
        throw new Exception("Acceptor cred state error");
    }
    if (cx.getDelegPolicyState() != delegPolicyState) {
        throw new Exception("Initiator cred policy state error");
    }

    GSSCredential cred = null;
    try {
        cred = s.x().getDelegCred();
    } catch (GSSException e) {
        // leave cred as null
    }

    if (delegated != (cred != null)) {
        throw new Exception("get cred error");
    }
}