/** * Encode an SPNEGO NegTokenInit blob * * @return byte[] * @exception IOException */ public byte[] encode() throws IOException { // Create the list of objects to be encoded List objList = new ArrayList(); objList.add(new DEROid(OID.ID_SPNEGO)); // Build the sequence of tagged objects DERSequence derSeq = new DERSequence(); derSeq.setTagNo(0); // mechTypes sequence DERSequence mechTypesSeq = new DERSequence(); mechTypesSeq.setTagNo(0); for (int i = 0; i < m_mechTypes.length; i++) { Oid mechType = m_mechTypes[i]; mechTypesSeq.addObject(new DEROid(mechType.toString())); } derSeq.addObject(mechTypesSeq); // mechListMIC // // Note: This field is not as specified if (m_mecListMICPrincipal != null) { DERSequence derMecSeq = new DERSequence(); derMecSeq.setTagNo(3); DERGeneralString mecStr = new DERGeneralString(m_mecListMICPrincipal); mecStr.setTagNo(0); derMecSeq.addObject(mecStr); derSeq.addObject(derMecSeq); } // Add the sequence to the object list objList.add(derSeq); // Pack the objects DERBuffer derBuf = new DERBuffer(); derBuf.packApplicationSpecific(objList); // Return the packed negTokenInit blob return derBuf.getBytes(); }
/** * Decode an SPNEGO NegTokenInit blob * * @param buf byte[] * @param off int * @param len int * @exception IOException */ public void decode(byte[] buf, int off, int len) throws IOException { // Create a DER buffer to decode the blob DERBuffer derBuf = new DERBuffer(buf, off, len); // Get the first object from the blob DERObject derObj = derBuf.unpackApplicationSpecific(); if (derObj instanceof DEROid) { // Check that the OID indicates SPNEGO DEROid derOid = (DEROid) derObj; if (derOid.getOid().equals(OID.ID_SPNEGO) == false) throw new IOException("Not an SPNEGO blob"); // Get the remaining objects from the DER buffer derObj = derBuf.unpackObject(); if (derObj instanceof DERSequence) { // Access the sequence, should be a sequence of tagged values DERSequence derSeq = (DERSequence) derObj; // Get the mechTypes list derObj = derSeq.getTaggedObject(0); if (derObj == null) throw new IOException("No mechTypes list in blob"); if (derObj instanceof DERSequence == false) throw new IOException("Invalid mechTypes object"); // Unpack the OID list (required) DERSequence derOidSeq = (DERSequence) derObj; m_mechTypes = new Oid[derOidSeq.numberOfObjects()]; int idx = 0; for (int i = 0; i < derOidSeq.numberOfObjects(); i++) { derObj = derOidSeq.getObjectAt(i); if (derObj instanceof DEROid) { derOid = (DEROid) derObj; try { m_mechTypes[idx++] = new Oid(derOid.getOid()); } catch (GSSException ex) { throw new IOException("Bad mechType OID"); } } } // Unpack the context flags (optional) derObj = derSeq.getTaggedObject(1); if (derObj != null) { // Check the type if (derObj instanceof DERBitString) { // Get the bit flags DERBitString derBitStr = (DERBitString) derObj; m_contextFlags = derBitStr.intValue(); } } // Unpack the mechToken (required) derObj = derSeq.getTaggedObject(2); if (derObj == null) throw new IOException("No mechToken in blob"); if (derObj instanceof DEROctetString == false) throw new IOException("Invalid mechToken object"); DEROctetString derStr = (DEROctetString) derObj; m_mechToken = derStr.getValue(); // Unpack the mechListMIC (optional) /** * derObj = derSeq.getTaggedObject( 3); * * <p>if ( derObj != null) { * * <p>// Check for the Microsoft format mechListMIC * * <p>if ( derObj instanceof DERSequence) { } } */ } else throw new IOException("Bad object type in blob"); } else throw new IOException("Invalid security blob"); }