001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.security.jgss.spnego;
027:
028: import java.io.*;
029: import java.util.*;
030: import org.ietf.jgss.*;
031: import sun.security.util.*;
032: import sun.security.jgss.*;
033:
034: /**
035: * Astract class for SPNEGO tokens.
036: * Implementation is based on RFC 2478
037: *
038: * NegotiationToken ::= CHOICE {
039: * negTokenInit [0] NegTokenInit,
040: * negTokenTarg [1] NegTokenTarg }
041: *
042: *
043: * @author Seema Malkani
044: * @version 1.8, 05/05/07
045: * @since 1.6
046: */
047:
048: abstract class SpNegoToken extends GSSToken {
049:
050: static final int NEG_TOKEN_INIT_ID = 0x00;
051: static final int NEG_TOKEN_TARG_ID = 0x01;
052:
053: static enum NegoResult {
054: ACCEPT_COMPLETE, ACCEPT_INCOMPLETE, REJECT,
055: };
056:
057: private int tokenType;
058:
059: // property
060: static final boolean DEBUG = SpNegoContext.DEBUG;
061:
062: /**
063: * The object identifier corresponding to the SPNEGO GSS-API
064: * mechanism.
065: */
066: public static ObjectIdentifier OID;
067:
068: static {
069: try {
070: OID = new ObjectIdentifier(
071: SpNegoMechFactory.GSS_SPNEGO_MECH_OID.toString());
072: } catch (IOException ioe) {
073: // should not happen
074: }
075: }
076:
077: /**
078: * Creates SPNEGO token of the specified type.
079: */
080: protected SpNegoToken(int tokenType) {
081: this .tokenType = tokenType;
082: }
083:
084: /**
085: * Returns the individual encoded SPNEGO token
086: *
087: * @return the encoded token
088: * @exception GSSException
089: */
090: abstract byte[] encode() throws GSSException;
091:
092: /**
093: * Returns the encoded SPNEGO token
094: * Note: inserts the required CHOICE tags
095: *
096: * @return the encoded token
097: * @exception GSSException
098: */
099: byte[] getEncoded() throws IOException, GSSException {
100:
101: // get the token encoded value
102: DerOutputStream token = new DerOutputStream();
103: token.write(encode());
104:
105: // now insert the CHOICE
106: switch (tokenType) {
107: case NEG_TOKEN_INIT_ID:
108: // Insert CHOICE of Negotiation Token
109: DerOutputStream initToken = new DerOutputStream();
110: initToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,
111: true, (byte) NEG_TOKEN_INIT_ID), token);
112: return initToken.toByteArray();
113:
114: case NEG_TOKEN_TARG_ID:
115: // Insert CHOICE of Negotiation Token
116: DerOutputStream targToken = new DerOutputStream();
117: targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,
118: true, (byte) NEG_TOKEN_TARG_ID), token);
119: return targToken.toByteArray();
120: default:
121: return token.toByteArray();
122: }
123: }
124:
125: /**
126: * Returns the SPNEGO token type
127: *
128: * @return the token type
129: */
130: final int getType() {
131: return tokenType;
132: }
133:
134: /**
135: * Returns a string representing the token type.
136: *
137: * @param tokenType the token type for which a string name is desired
138: * @return the String name of this token type
139: */
140: static String getTokenName(int type) {
141: switch (type) {
142: case NEG_TOKEN_INIT_ID:
143: return "SPNEGO NegTokenInit";
144: case NEG_TOKEN_TARG_ID:
145: return "SPNEGO NegTokenTarg";
146: default:
147: return "SPNEGO Mechanism Token";
148: }
149: }
150:
151: /**
152: * Returns the enumerated type of the Negotiation result.
153: *
154: * @param result the negotiated result represented by integer
155: * @return the enumerated type of Negotiated result
156: */
157: static NegoResult getNegoResultType(int result) {
158: switch (result) {
159: case 0:
160: return NegoResult.ACCEPT_COMPLETE;
161: case 1:
162: return NegoResult.ACCEPT_INCOMPLETE;
163: case 2:
164: return NegoResult.REJECT;
165: default:
166: // unknown - return optimistic result
167: return NegoResult.ACCEPT_COMPLETE;
168: }
169: }
170:
171: /**
172: * Returns a string representing the negotiation result.
173: *
174: * @param result the negotiated result
175: * @return the String message of this negotiated result
176: */
177: static String getNegoResultString(int result) {
178: switch (result) {
179: case 0:
180: return "Accept Complete";
181: case 1:
182: return "Accept InComplete";
183: case 2:
184: return "Reject";
185: default:
186: return ("Unknown Negotiated Result: " + result);
187: }
188: }
189: }
|