Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.security.jgss/share/classes/sun/security/jgss/spnego/NegTokenTarg.java
41161 views
1
/*
2
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 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.
24
*/
25
26
package sun.security.jgss.spnego;
27
28
import java.io.*;
29
import org.ietf.jgss.*;
30
import sun.security.jgss.*;
31
import sun.security.util.*;
32
33
/**
34
* Implements the SPNEGO NegTokenTarg token
35
* as specified in RFC 2478
36
*
37
* NegTokenTarg ::= SEQUENCE {
38
* negResult [0] ENUMERATED {
39
* accept_completed (0),
40
* accept_incomplete (1),
41
* reject (2) } OPTIONAL,
42
* supportedMech [1] MechType OPTIONAL,
43
* responseToken [2] OCTET STRING OPTIONAL,
44
* mechListMIC [3] OCTET STRING OPTIONAL
45
* }
46
*
47
* MechType::= OBJECT IDENTIFIER
48
*
49
*
50
* @author Seema Malkani
51
* @since 1.6
52
*/
53
54
public class NegTokenTarg extends SpNegoToken {
55
56
private int negResult = 0;
57
private Oid supportedMech = null;
58
private byte[] responseToken = null;
59
private byte[] mechListMIC = null;
60
61
NegTokenTarg(int result, Oid mech, byte[] token, byte[] mechListMIC)
62
{
63
super(NEG_TOKEN_TARG_ID);
64
this.negResult = result;
65
this.supportedMech = mech;
66
this.responseToken = token;
67
this.mechListMIC = mechListMIC;
68
}
69
70
// Used by sun.security.jgss.wrapper.NativeGSSContext
71
// to parse SPNEGO tokens
72
public NegTokenTarg(byte[] in) throws GSSException {
73
super(NEG_TOKEN_TARG_ID);
74
parseToken(in);
75
}
76
77
final byte[] encode() throws GSSException {
78
try {
79
// create negTargToken
80
DerOutputStream targToken = new DerOutputStream();
81
82
// write the negotiated result with CONTEXT 00
83
DerOutputStream result = new DerOutputStream();
84
result.putEnumerated(negResult);
85
targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,
86
true, (byte) 0x00), result);
87
88
// supportedMech with CONTEXT 01
89
if (supportedMech != null) {
90
DerOutputStream mech = new DerOutputStream();
91
byte[] mechType = supportedMech.getDER();
92
mech.write(mechType);
93
targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,
94
true, (byte) 0x01), mech);
95
}
96
97
// response Token with CONTEXT 02
98
if (responseToken != null) {
99
DerOutputStream rspToken = new DerOutputStream();
100
rspToken.putOctetString(responseToken);
101
targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,
102
true, (byte) 0x02), rspToken);
103
}
104
105
// mechListMIC with CONTEXT 03
106
if (mechListMIC != null) {
107
if (DEBUG) {
108
System.out.println("SpNegoToken NegTokenTarg: " +
109
"sending MechListMIC");
110
}
111
DerOutputStream mic = new DerOutputStream();
112
mic.putOctetString(mechListMIC);
113
targToken.write(DerValue.createTag(DerValue.TAG_CONTEXT,
114
true, (byte) 0x03), mic);
115
}
116
117
// insert in a SEQUENCE
118
DerOutputStream out = new DerOutputStream();
119
out.write(DerValue.tag_Sequence, targToken);
120
121
return out.toByteArray();
122
123
} catch (IOException e) {
124
throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
125
"Invalid SPNEGO NegTokenTarg token : " + e.getMessage());
126
}
127
}
128
129
private void parseToken(byte[] in) throws GSSException {
130
try {
131
DerValue der = new DerValue(in);
132
// verify NegotiationToken type token
133
if (!der.isContextSpecific((byte) NEG_TOKEN_TARG_ID)) {
134
throw new IOException("SPNEGO NegoTokenTarg : " +
135
"did not have the right token type");
136
}
137
DerValue tmp1 = der.data.getDerValue();
138
if (tmp1.tag != DerValue.tag_Sequence) {
139
throw new IOException("SPNEGO NegoTokenTarg : " +
140
"did not have the Sequence tag");
141
}
142
143
// parse various fields if present
144
int lastField = -1;
145
while (tmp1.data.available() > 0) {
146
DerValue tmp2 = tmp1.data.getDerValue();
147
if (tmp2.isContextSpecific((byte)0x00)) {
148
lastField = checkNextField(lastField, 0);
149
negResult = tmp2.data.getEnumerated();
150
if (DEBUG) {
151
System.out.println("SpNegoToken NegTokenTarg: negotiated" +
152
" result = " + getNegoResultString(negResult));
153
}
154
} else if (tmp2.isContextSpecific((byte)0x01)) {
155
lastField = checkNextField(lastField, 1);
156
ObjectIdentifier mech = tmp2.data.getOID();
157
supportedMech = new Oid(mech.toString());
158
if (DEBUG) {
159
System.out.println("SpNegoToken NegTokenTarg: " +
160
"supported mechanism = " + supportedMech);
161
}
162
} else if (tmp2.isContextSpecific((byte)0x02)) {
163
lastField = checkNextField(lastField, 2);
164
responseToken = tmp2.data.getOctetString();
165
} else if (tmp2.isContextSpecific((byte)0x03)) {
166
lastField = checkNextField(lastField, 3);
167
if (!GSSUtil.useMSInterop()) {
168
mechListMIC = tmp2.data.getOctetString();
169
if (DEBUG) {
170
System.out.println("SpNegoToken NegTokenTarg: " +
171
"MechListMIC Token = " +
172
getHexBytes(mechListMIC));
173
}
174
}
175
}
176
}
177
} catch (IOException e) {
178
throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
179
"Invalid SPNEGO NegTokenTarg token : " + e.getMessage());
180
}
181
}
182
183
int getNegotiatedResult() {
184
return negResult;
185
}
186
187
// Used by sun.security.jgss.wrapper.NativeGSSContext
188
// to find the supported mech in SPNEGO tokens
189
public Oid getSupportedMech() {
190
return supportedMech;
191
}
192
193
byte[] getResponseToken() {
194
return responseToken;
195
}
196
197
byte[] getMechListMIC() {
198
return mechListMIC;
199
}
200
}
201
202