J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.security.jgss.spnego; |
| 27 | |
| 28 | import java.io.*; |
| 29 | import java.util.*; |
| 30 | import org.ietf.jgss.*; |
| 31 | import sun.security.util.*; |
| 32 | import sun.security.jgss.*; |
| 33 | |
| 34 | /** |
| 35 | * Astract class for SPNEGO tokens. |
| 36 | * Implementation is based on RFC 2478 |
| 37 | * |
| 38 | * NegotiationToken ::= CHOICE { |
| 39 | * negTokenInit [0] NegTokenInit, |
| 40 | * negTokenTarg [1] NegTokenTarg } |
| 41 | * |
| 42 | * |
| 43 | * @author Seema Malkani |
| 44 | * @since 1.6 |
| 45 | */ |
| 46 | |
| 47 | abstract class SpNegoToken extends GSSToken { |
| 48 | |
| 49 | static final int NEG_TOKEN_INIT_ID = 0x00; |
| 50 | static final int NEG_TOKEN_TARG_ID = 0x01; |
| 51 | |
| 52 | static enum NegoResult { |
| 53 | ACCEPT_COMPLETE, |
| 54 | ACCEPT_INCOMPLETE, |
| 55 | REJECT, |
| 56 | }; |
| 57 | |
| 58 | private int tokenType; |
| 59 | |
| 60 | // property |
| 61 | static final boolean DEBUG = SpNegoContext.DEBUG; |
| 62 | |
| 63 | /** |
| 64 | * The object identifier corresponding to the SPNEGO GSS-API |
| 65 | * mechanism. |
| 66 | */ |
| 67 | public static ObjectIdentifier OID; |
| 68 | |
| 69 | static { |
| 70 | try { |
| 71 | OID = new ObjectIdentifier(SpNegoMechFactory. |
| 72 | GSS_SPNEGO_MECH_OID.toString()); |
| 73 | } catch (IOException ioe) { |
| 74 | // should not happen |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Creates SPNEGO token of the specified type. |
| 80 | */ |
| 81 | protected SpNegoToken(int tokenType) { |
| 82 | this.tokenType = tokenType; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the individual encoded SPNEGO token |
| 87 | * |
| 88 | * @return the encoded token |
| 89 | * @exception GSSException |
| 90 | */ |
| 91 | abstract byte[] encode() throws GSSException; |
| 92 | |
| 93 | /** |
| 94 | * Returns the encoded SPNEGO token |
| 95 | * Note: inserts the required CHOICE tags |
| 96 | * |
| 97 | * @return the encoded token |
| 98 | * @exception GSSException |
| 99 | */ |
| 100 | byte[] getEncoded() throws IOException, GSSException { |
| 101 | |
| 102 | // get the token encoded value |
| 103 | DerOutputStream token = new DerOutputStream(); |
| 104 | token.write(encode()); |
| 105 | |
| 106 | // now insert the CHOICE |
| 107 | switch (tokenType) { |
| 108 | case NEG_TOKEN_INIT_ID: |
| 109 | // Insert CHOICE of Negotiation Token |
| 110 | DerOutputStream initToken = new DerOutputStream(); |
| 111 | initToken.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
| 112 | true, (byte) NEG_TOKEN_INIT_ID), token); |
| 113 | return initToken.toByteArray(); |
| 114 | |
| 115 | case NEG_TOKEN_TARG_ID: |
| 116 | // Insert CHOICE of Negotiation Token |
| 117 | DerOutputStream targToken = new DerOutputStream(); |
| 118 | targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
| 119 | true, (byte) NEG_TOKEN_TARG_ID), token); |
| 120 | return targToken.toByteArray(); |
| 121 | default: |
| 122 | return token.toByteArray(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns the SPNEGO token type |
| 128 | * |
| 129 | * @return the token type |
| 130 | */ |
| 131 | final int getType() { |
| 132 | return tokenType; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns a string representing the token type. |
| 137 | * |
| 138 | * @param tokenType the token type for which a string name is desired |
| 139 | * @return the String name of this token type |
| 140 | */ |
| 141 | static String getTokenName(int type) { |
| 142 | switch (type) { |
| 143 | case NEG_TOKEN_INIT_ID: |
| 144 | return "SPNEGO NegTokenInit"; |
| 145 | case NEG_TOKEN_TARG_ID: |
| 146 | return "SPNEGO NegTokenTarg"; |
| 147 | default: |
| 148 | return "SPNEGO Mechanism Token"; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns the enumerated type of the Negotiation result. |
| 154 | * |
| 155 | * @param result the negotiated result represented by integer |
| 156 | * @return the enumerated type of Negotiated result |
| 157 | */ |
| 158 | static NegoResult getNegoResultType(int result) { |
| 159 | switch (result) { |
| 160 | case 0: |
| 161 | return NegoResult.ACCEPT_COMPLETE; |
| 162 | case 1: |
| 163 | return NegoResult.ACCEPT_INCOMPLETE; |
| 164 | case 2: |
| 165 | return NegoResult.REJECT; |
| 166 | default: |
| 167 | // unknown - return optimistic result |
| 168 | return NegoResult.ACCEPT_COMPLETE; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns a string representing the negotiation result. |
| 174 | * |
| 175 | * @param result the negotiated result |
| 176 | * @return the String message of this negotiated result |
| 177 | */ |
| 178 | static String getNegoResultString(int result) { |
| 179 | switch (result) { |
| 180 | case 0: |
| 181 | return "Accept Complete"; |
| 182 | case 1: |
| 183 | return "Accept InComplete"; |
| 184 | case 2: |
| 185 | return "Reject"; |
| 186 | default: |
| 187 | return ("Unknown Negotiated Result: " + result); |
| 188 | } |
| 189 | } |
| 190 | } |