0N/A/*
4152N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.jgss.spnego;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport org.ietf.jgss.*;
0N/Aimport sun.security.util.*;
0N/Aimport sun.security.jgss.*;
0N/A
0N/A/**
0N/A * Astract class for SPNEGO tokens.
0N/A * Implementation is based on RFC 2478
0N/A *
0N/A * NegotiationToken ::= CHOICE {
0N/A * negTokenInit [0] NegTokenInit,
0N/A * negTokenTarg [1] NegTokenTarg }
0N/A *
0N/A *
0N/A * @author Seema Malkani
0N/A * @since 1.6
0N/A */
0N/A
0N/Aabstract class SpNegoToken extends GSSToken {
0N/A
0N/A static final int NEG_TOKEN_INIT_ID = 0x00;
0N/A static final int NEG_TOKEN_TARG_ID = 0x01;
0N/A
0N/A static enum NegoResult {
0N/A ACCEPT_COMPLETE,
0N/A ACCEPT_INCOMPLETE,
0N/A REJECT,
0N/A };
0N/A
0N/A private int tokenType;
0N/A
0N/A // property
0N/A static final boolean DEBUG = SpNegoContext.DEBUG;
0N/A
0N/A /**
0N/A * The object identifier corresponding to the SPNEGO GSS-API
0N/A * mechanism.
0N/A */
0N/A public static ObjectIdentifier OID;
0N/A
0N/A static {
0N/A try {
0N/A OID = new ObjectIdentifier(SpNegoMechFactory.
0N/A GSS_SPNEGO_MECH_OID.toString());
0N/A } catch (IOException ioe) {
0N/A // should not happen
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates SPNEGO token of the specified type.
0N/A */
0N/A protected SpNegoToken(int tokenType) {
0N/A this.tokenType = tokenType;
0N/A }
0N/A
0N/A /**
0N/A * Returns the individual encoded SPNEGO token
0N/A *
0N/A * @return the encoded token
0N/A * @exception GSSException
0N/A */
0N/A abstract byte[] encode() throws GSSException;
0N/A
0N/A /**
0N/A * Returns the encoded SPNEGO token
0N/A * Note: inserts the required CHOICE tags
0N/A *
0N/A * @return the encoded token
0N/A * @exception GSSException
0N/A */
0N/A byte[] getEncoded() throws IOException, GSSException {
0N/A
0N/A // get the token encoded value
0N/A DerOutputStream token = new DerOutputStream();
0N/A token.write(encode());
0N/A
0N/A // now insert the CHOICE
0N/A switch (tokenType) {
0N/A case NEG_TOKEN_INIT_ID:
0N/A // Insert CHOICE of Negotiation Token
0N/A DerOutputStream initToken = new DerOutputStream();
0N/A initToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,
0N/A true, (byte) NEG_TOKEN_INIT_ID), token);
0N/A return initToken.toByteArray();
0N/A
0N/A case NEG_TOKEN_TARG_ID:
0N/A // Insert CHOICE of Negotiation Token
0N/A DerOutputStream targToken = new DerOutputStream();
0N/A targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,
0N/A true, (byte) NEG_TOKEN_TARG_ID), token);
0N/A return targToken.toByteArray();
0N/A default:
0N/A return token.toByteArray();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the SPNEGO token type
0N/A *
0N/A * @return the token type
0N/A */
0N/A final int getType() {
0N/A return tokenType;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representing the token type.
0N/A *
0N/A * @param tokenType the token type for which a string name is desired
0N/A * @return the String name of this token type
0N/A */
0N/A static String getTokenName(int type) {
0N/A switch (type) {
0N/A case NEG_TOKEN_INIT_ID:
0N/A return "SPNEGO NegTokenInit";
0N/A case NEG_TOKEN_TARG_ID:
0N/A return "SPNEGO NegTokenTarg";
0N/A default:
0N/A return "SPNEGO Mechanism Token";
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the enumerated type of the Negotiation result.
0N/A *
0N/A * @param result the negotiated result represented by integer
0N/A * @return the enumerated type of Negotiated result
0N/A */
0N/A static NegoResult getNegoResultType(int result) {
0N/A switch (result) {
0N/A case 0:
0N/A return NegoResult.ACCEPT_COMPLETE;
0N/A case 1:
0N/A return NegoResult.ACCEPT_INCOMPLETE;
0N/A case 2:
0N/A return NegoResult.REJECT;
0N/A default:
0N/A // unknown - return optimistic result
0N/A return NegoResult.ACCEPT_COMPLETE;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representing the negotiation result.
0N/A *
0N/A * @param result the negotiated result
0N/A * @return the String message of this negotiated result
0N/A */
0N/A static String getNegoResultString(int result) {
0N/A switch (result) {
0N/A case 0:
0N/A return "Accept Complete";
0N/A case 1:
0N/A return "Accept InComplete";
0N/A case 2:
0N/A return "Reject";
0N/A default:
0N/A return ("Unknown Negotiated Result: " + result);
0N/A }
0N/A }
4152N/A
4152N/A /**
4152N/A * Checks if the context tag in a sequence is in correct order. The "last"
4152N/A * value must be smaller than "current".
4152N/A * @param last the last tag seen
4152N/A * @param current the current tag
4152N/A * @return the current tag, used as the next value for last
4152N/A * @throws GSSException if there's a wrong order
4152N/A */
4152N/A static int checkNextField(int last, int current) throws GSSException {
4152N/A if (last < current) {
4152N/A return current;
4152N/A } else {
4152N/A throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
4152N/A "Invalid SpNegoToken token : wrong order");
4152N/A }
4152N/A }
0N/A}