Search in sources :

Example 1 with NegTokenInit

use of sun.security.jgss.spnego.NegTokenInit in project jdk8u_jdk by JetBrains.

the class NotPreferredMech method main.

public static void main(String[] argv) throws Exception {
    // Generates a NegTokenInit mechTypes field, with an
    // unsupported mech as the preferred.
    DerOutputStream mech = new DerOutputStream();
    mech.write(new Oid("1.2.3.4").getDER());
    mech.write(GSSUtil.GSS_KRB5_MECH_OID.getDER());
    DerOutputStream mechTypeList = new DerOutputStream();
    mechTypeList.write(DerValue.tag_Sequence, mech);
    // Generates a NegTokenInit mechToken field for 1.2.3.4 mech
    GSSHeader h1 = new GSSHeader(new ObjectIdentifier("1.2.3.4"), 1);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    h1.encode(bout);
    bout.write(new byte[1]);
    // Generates the NegTokenInit token
    Constructor<NegTokenInit> ctor = NegTokenInit.class.getDeclaredConstructor(byte[].class, BitArray.class, byte[].class, byte[].class);
    ctor.setAccessible(true);
    NegTokenInit initToken = ctor.newInstance(mechTypeList.toByteArray(), new BitArray(0), bout.toByteArray(), null);
    Method m = Class.forName("sun.security.jgss.spnego.SpNegoToken").getDeclaredMethod("getEncoded");
    m.setAccessible(true);
    byte[] spnegoToken = (byte[]) m.invoke(initToken);
    // and wraps it into a GSSToken
    GSSHeader h = new GSSHeader(new ObjectIdentifier(GSSUtil.GSS_SPNEGO_MECH_OID.toString()), spnegoToken.length);
    bout = new ByteArrayOutputStream();
    h.encode(bout);
    bout.write(spnegoToken);
    byte[] token = bout.toByteArray();
    // and feeds it to a GSS acceptor
    GSSManager man = GSSManager.getInstance();
    GSSContext ctxt = man.createContext((GSSCredential) null);
    token = ctxt.acceptSecContext(token, 0, token.length);
    NegTokenTarg targ = new NegTokenTarg(token);
    // Make sure it's a GO-ON message
    Method m2 = NegTokenTarg.class.getDeclaredMethod("getNegotiatedResult");
    m2.setAccessible(true);
    int negResult = (int) m2.invoke(targ);
    if (negResult != 1) /* ACCEPT_INCOMPLETE */
    {
        throw new Exception("Not a continue");
    }
}
Also used : NegTokenTarg(sun.security.jgss.spnego.NegTokenTarg) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) DerOutputStream(sun.security.util.DerOutputStream) NegTokenInit(sun.security.jgss.spnego.NegTokenInit) BitArray(sun.security.util.BitArray) ObjectIdentifier(sun.security.util.ObjectIdentifier)

Example 2 with NegTokenInit

use of sun.security.jgss.spnego.NegTokenInit in project jdk8u_jdk by JetBrains.

the class NativeGSSContext method getMechFromSpNegoToken.

// Retrieve the (preferred) mech out of SPNEGO tokens, i.e.
// NegTokenInit & NegTokenTarg
private static Oid getMechFromSpNegoToken(byte[] token, boolean isInitiator) throws GSSException {
    Oid mech = null;
    if (isInitiator) {
        GSSHeader header = null;
        try {
            header = new GSSHeader(new ByteArrayInputStream(token));
        } catch (IOException ioe) {
            throw new GSSExceptionImpl(GSSException.FAILURE, ioe);
        }
        int negTokenLen = header.getMechTokenLength();
        byte[] negToken = new byte[negTokenLen];
        System.arraycopy(token, token.length - negTokenLen, negToken, 0, negToken.length);
        NegTokenInit ntok = new NegTokenInit(negToken);
        if (ntok.getMechToken() != null) {
            Oid[] mechList = ntok.getMechTypeList();
            mech = mechList[0];
        }
    } else {
        NegTokenTarg ntok = new NegTokenTarg(token);
        mech = ntok.getSupportedMech();
    }
    return mech;
}
Also used : NegTokenTarg(sun.security.jgss.spnego.NegTokenTarg) NegTokenInit(sun.security.jgss.spnego.NegTokenInit) GSSExceptionImpl(sun.security.jgss.GSSExceptionImpl) GSSHeader(sun.security.jgss.GSSHeader)

Aggregations

NegTokenInit (sun.security.jgss.spnego.NegTokenInit)2 NegTokenTarg (sun.security.jgss.spnego.NegTokenTarg)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Method (java.lang.reflect.Method)1 GSSExceptionImpl (sun.security.jgss.GSSExceptionImpl)1 GSSHeader (sun.security.jgss.GSSHeader)1 BitArray (sun.security.util.BitArray)1 DerOutputStream (sun.security.util.DerOutputStream)1 ObjectIdentifier (sun.security.util.ObjectIdentifier)1