author | weijun |
Tue, 03 May 2011 02:48:59 +0800 | |
changeset 9549 | 24b7de36d243 |
parent 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9549
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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.jgss.*; |
|
32 |
import sun.security.util.*; |
|
33 |
||
34 |
/** |
|
35 |
* Implements the SPNEGO NegTokenTarg token |
|
36 |
* as specified in RFC 2478 |
|
37 |
* |
|
38 |
* NegTokenTarg ::= SEQUENCE { |
|
39 |
* negResult [0] ENUMERATED { |
|
40 |
* accept_completed (0), |
|
41 |
* accept_incomplete (1), |
|
42 |
* reject (2) } OPTIONAL, |
|
43 |
* supportedMech [1] MechType OPTIONAL, |
|
44 |
* responseToken [2] OCTET STRING OPTIONAL, |
|
45 |
* mechListMIC [3] OCTET STRING OPTIONAL |
|
46 |
* } |
|
47 |
* |
|
48 |
* MechType::= OBJECT IDENTIFIER |
|
49 |
* |
|
50 |
* |
|
51 |
* @author Seema Malkani |
|
52 |
* @since 1.6 |
|
53 |
*/ |
|
54 |
||
55 |
public class NegTokenTarg extends SpNegoToken { |
|
56 |
||
57 |
private int negResult = 0; |
|
58 |
private Oid supportedMech = null; |
|
59 |
private byte[] responseToken = null; |
|
60 |
private byte[] mechListMIC = null; |
|
61 |
||
62 |
NegTokenTarg(int result, Oid mech, byte[] token, byte[] mechListMIC) |
|
63 |
{ |
|
64 |
super(NEG_TOKEN_TARG_ID); |
|
65 |
this.negResult = result; |
|
66 |
this.supportedMech = mech; |
|
67 |
this.responseToken = token; |
|
68 |
this.mechListMIC = mechListMIC; |
|
69 |
} |
|
70 |
||
71 |
// Used by sun.security.jgss.wrapper.NativeGSSContext |
|
72 |
// to parse SPNEGO tokens |
|
73 |
public NegTokenTarg(byte[] in) throws GSSException { |
|
74 |
super(NEG_TOKEN_TARG_ID); |
|
75 |
parseToken(in); |
|
76 |
} |
|
77 |
||
78 |
final byte[] encode() throws GSSException { |
|
79 |
try { |
|
80 |
// create negTargToken |
|
81 |
DerOutputStream targToken = new DerOutputStream(); |
|
82 |
||
83 |
// write the negotiated result with CONTEXT 00 |
|
84 |
DerOutputStream result = new DerOutputStream(); |
|
85 |
result.putEnumerated(negResult); |
|
86 |
targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
87 |
true, (byte) 0x00), result); |
|
88 |
||
89 |
// supportedMech with CONTEXT 01 |
|
90 |
if (supportedMech != null) { |
|
91 |
DerOutputStream mech = new DerOutputStream(); |
|
92 |
byte[] mechType = supportedMech.getDER(); |
|
93 |
mech.write(mechType); |
|
94 |
targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
95 |
true, (byte) 0x01), mech); |
|
96 |
} |
|
97 |
||
98 |
// response Token with CONTEXT 02 |
|
99 |
if (responseToken != null) { |
|
100 |
DerOutputStream rspToken = new DerOutputStream(); |
|
101 |
rspToken.putOctetString(responseToken); |
|
102 |
targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
103 |
true, (byte) 0x02), rspToken); |
|
104 |
} |
|
105 |
||
106 |
// mechListMIC with CONTEXT 03 |
|
107 |
if (mechListMIC != null) { |
|
108 |
if (DEBUG) { |
|
109 |
System.out.println("SpNegoToken NegTokenTarg: " + |
|
110 |
"sending MechListMIC"); |
|
111 |
} |
|
112 |
DerOutputStream mic = new DerOutputStream(); |
|
113 |
mic.putOctetString(mechListMIC); |
|
114 |
targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
115 |
true, (byte) 0x03), mic); |
|
116 |
} else if (GSSUtil.useMSInterop()) { |
|
117 |
// required for MS-interoperability |
|
118 |
if (responseToken != null) { |
|
119 |
if (DEBUG) { |
|
120 |
System.out.println("SpNegoToken NegTokenTarg: " + |
|
121 |
"sending additional token for MS Interop"); |
|
122 |
} |
|
123 |
DerOutputStream rspToken = new DerOutputStream(); |
|
124 |
rspToken.putOctetString(responseToken); |
|
125 |
targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
126 |
true, (byte) 0x03), rspToken); |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
// insert in a SEQUENCE |
|
131 |
DerOutputStream out = new DerOutputStream(); |
|
132 |
out.write(DerValue.tag_Sequence, targToken); |
|
133 |
||
134 |
return out.toByteArray(); |
|
135 |
||
136 |
} catch (IOException e) { |
|
137 |
throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1, |
|
138 |
"Invalid SPNEGO NegTokenTarg token : " + e.getMessage()); |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
private void parseToken(byte[] in) throws GSSException { |
|
143 |
try { |
|
144 |
DerValue der = new DerValue(in); |
|
145 |
// verify NegotiationToken type token |
|
146 |
if (!der.isContextSpecific((byte) NEG_TOKEN_TARG_ID)) { |
|
147 |
throw new IOException("SPNEGO NegoTokenTarg : " + |
|
148 |
"did not have the right token type"); |
|
149 |
} |
|
150 |
DerValue tmp1 = der.data.getDerValue(); |
|
151 |
if (tmp1.tag != DerValue.tag_Sequence) { |
|
152 |
throw new IOException("SPNEGO NegoTokenTarg : " + |
|
153 |
"did not have the Sequence tag"); |
|
154 |
} |
|
155 |
||
9549
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
156 |
// parse various fields if present |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
157 |
int lastField = -1; |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
158 |
while (tmp1.data.available() > 0) { |
2 | 159 |
DerValue tmp2 = tmp1.data.getDerValue(); |
9549
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
160 |
if (tmp2.isContextSpecific((byte)0x00)) { |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
161 |
lastField = checkNextField(lastField, 0); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
162 |
negResult = tmp2.data.getEnumerated(); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
163 |
if (DEBUG) { |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
164 |
System.out.println("SpNegoToken NegTokenTarg: negotiated" + |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
165 |
" result = " + getNegoResultString(negResult)); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
166 |
} |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
167 |
} else if (tmp2.isContextSpecific((byte)0x01)) { |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
168 |
lastField = checkNextField(lastField, 1); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
169 |
ObjectIdentifier mech = tmp2.data.getOID(); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
170 |
supportedMech = new Oid(mech.toString()); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
171 |
if (DEBUG) { |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
172 |
System.out.println("SpNegoToken NegTokenTarg: " + |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
173 |
"supported mechanism = " + supportedMech); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
174 |
} |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
175 |
} else if (tmp2.isContextSpecific((byte)0x02)) { |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
176 |
lastField = checkNextField(lastField, 2); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
177 |
responseToken = tmp2.data.getOctetString(); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
178 |
} else if (tmp2.isContextSpecific((byte)0x03)) { |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
179 |
lastField = checkNextField(lastField, 3); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
180 |
if (!GSSUtil.useMSInterop()) { |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
181 |
mechListMIC = tmp2.data.getOctetString(); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
182 |
if (DEBUG) { |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
183 |
System.out.println("SpNegoToken NegTokenTarg: " + |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
184 |
"MechListMIC Token = " + |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
185 |
getHexBytes(mechListMIC)); |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
186 |
} |
24b7de36d243
7040151: SPNEGO GSS code does not parse tokens in accordance to RFC 2478
weijun
parents:
5506
diff
changeset
|
187 |
} |
2 | 188 |
} |
189 |
} |
|
190 |
} catch (IOException e) { |
|
191 |
throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1, |
|
192 |
"Invalid SPNEGO NegTokenTarg token : " + e.getMessage()); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
int getNegotiatedResult() { |
|
197 |
return negResult; |
|
198 |
} |
|
199 |
||
200 |
// Used by sun.security.jgss.wrapper.NativeGSSContext |
|
201 |
// to find the supported mech in SPNEGO tokens |
|
202 |
public Oid getSupportedMech() { |
|
203 |
return supportedMech; |
|
204 |
} |
|
205 |
||
206 |
byte[] getResponseToken() { |
|
207 |
return responseToken; |
|
208 |
} |
|
209 |
||
210 |
byte[] getMechListMIC() { |
|
211 |
return mechListMIC; |
|
212 |
} |
|
213 |
} |