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/krb5/internal/KRBCred.java
41161 views
1
/*
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3
*
4
* This code is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 2 only, as
6
* published by the Free Software Foundation. Oracle designates this
7
* particular file as subject to the "Classpath" exception as provided
8
* by Oracle in the LICENSE file that accompanied this code.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*/
24
25
/*
26
*
27
* (C) Copyright IBM Corp. 1999 All Rights Reserved.
28
* Copyright 1997 The Open Group Research Institute. All rights reserved.
29
*/
30
31
package sun.security.krb5.internal;
32
33
import sun.security.krb5.EncryptedData;
34
import sun.security.krb5.Asn1Exception;
35
import sun.security.krb5.RealmException;
36
import sun.security.util.*;
37
import java.util.Vector;
38
import java.io.IOException;
39
import java.math.BigInteger;
40
41
/**
42
* Implements the ASN.1 Authenticator type.
43
*
44
* <pre>{@code
45
* KRB-CRED ::= [APPLICATION 22] SEQUENCE {
46
* pvno [0] INTEGER (5),
47
* msg-type [1] INTEGER (22),
48
* tickets [2] SEQUENCE OF Ticket,
49
* enc-part [3] EncryptedData -- EncKrbCredPart
50
* }
51
* }</pre>
52
*
53
* <p>
54
* This definition reflects the Network Working Group RFC 4120
55
* specification available at
56
* <a href="http://www.ietf.org/rfc/rfc4120.txt">
57
* http://www.ietf.org/rfc/rfc4120.txt</a>.
58
*/
59
public class KRBCred {
60
61
public Ticket[] tickets = null;
62
public EncryptedData encPart;
63
private int pvno;
64
private int msgType;
65
66
public KRBCred(Ticket[] new_tickets, EncryptedData new_encPart) throws IOException {
67
pvno = Krb5.PVNO;
68
msgType = Krb5.KRB_CRED;
69
if (new_tickets != null) {
70
tickets = new Ticket[new_tickets.length];
71
for (int i = 0; i < new_tickets.length; i++) {
72
if (new_tickets[i] == null) {
73
throw new IOException("Cannot create a KRBCred");
74
} else {
75
tickets[i] = (Ticket) new_tickets[i].clone();
76
}
77
}
78
}
79
encPart = new_encPart;
80
}
81
82
public KRBCred(byte[] data) throws Asn1Exception,
83
RealmException, KrbApErrException, IOException {
84
init(new DerValue(data));
85
}
86
87
public KRBCred(DerValue encoding) throws Asn1Exception,
88
RealmException, KrbApErrException, IOException {
89
init(encoding);
90
}
91
92
/**
93
* Initializes an KRBCred object.
94
* @param encoding a single DER-encoded value.
95
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
96
* @exception IOException if an I/O error occurs while reading encoded data.
97
* @exception KrbApErrException if the value read from the DER-encoded data
98
* stream does not match the pre-defined value.
99
* @exception RealmException if an error occurs while parsing a Realm object.
100
*/
101
private void init(DerValue encoding) throws Asn1Exception,
102
RealmException, KrbApErrException, IOException {
103
if (((encoding.getTag() & (byte) 0x1F) != (byte) 0x16)
104
|| (encoding.isApplication() != true)
105
|| (encoding.isConstructed() != true)) {
106
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
107
}
108
DerValue der, subDer;
109
der = encoding.getData().getDerValue();
110
if (der.getTag() != DerValue.tag_Sequence) {
111
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
112
}
113
subDer = der.getData().getDerValue();
114
if ((subDer.getTag() & 0x1F) == 0x00) {
115
pvno = subDer.getData().getBigInteger().intValue();
116
if (pvno != Krb5.PVNO) {
117
throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
118
}
119
} else {
120
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
121
}
122
subDer = der.getData().getDerValue();
123
if ((subDer.getTag() & 0x1F) == 0x01) {
124
msgType = subDer.getData().getBigInteger().intValue();
125
if (msgType != Krb5.KRB_CRED) {
126
throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);
127
}
128
} else {
129
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
130
}
131
subDer = der.getData().getDerValue();
132
if ((subDer.getTag() & 0x1F) == 0x02) {
133
DerValue subsubDer = subDer.getData().getDerValue();
134
if (subsubDer.getTag() != DerValue.tag_SequenceOf) {
135
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
136
}
137
Vector<Ticket> v = new Vector<>();
138
while (subsubDer.getData().available() > 0) {
139
v.addElement(new Ticket(subsubDer.getData().getDerValue()));
140
}
141
if (v.size() > 0) {
142
tickets = new Ticket[v.size()];
143
v.copyInto(tickets);
144
}
145
} else {
146
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
147
}
148
encPart = EncryptedData.parse(der.getData(), (byte) 0x03, false);
149
150
if (der.getData().available() > 0) {
151
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
152
}
153
}
154
155
/**
156
* Encodes an KRBCred object.
157
* @return the data of encoded EncAPRepPart object.
158
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
159
* @exception IOException if an I/O error occurs while reading encoded data.
160
*/
161
public byte[] asn1Encode() throws Asn1Exception, IOException {
162
DerOutputStream temp, bytes, out;
163
temp = new DerOutputStream();
164
temp.putInteger(BigInteger.valueOf(pvno));
165
out = new DerOutputStream();
166
out.write(DerValue.createTag(DerValue.TAG_CONTEXT,
167
true, (byte) 0x00), temp);
168
temp = new DerOutputStream();
169
temp.putInteger(BigInteger.valueOf(msgType));
170
out.write(DerValue.createTag(DerValue.TAG_CONTEXT,
171
true, (byte) 0x01), temp);
172
temp = new DerOutputStream();
173
for (int i = 0; i < tickets.length; i++) {
174
temp.write(tickets[i].asn1Encode());
175
}
176
bytes = new DerOutputStream();
177
bytes.write(DerValue.tag_SequenceOf, temp);
178
out.write(DerValue.createTag(DerValue.TAG_CONTEXT,
179
true, (byte) 0x02), bytes);
180
out.write(DerValue.createTag(DerValue.TAG_CONTEXT,
181
true, (byte) 0x03), encPart.asn1Encode());
182
bytes = new DerOutputStream();
183
bytes.write(DerValue.tag_Sequence, out);
184
out = new DerOutputStream();
185
out.write(DerValue.createTag(DerValue.TAG_APPLICATION,
186
true, (byte) 0x16), bytes);
187
return out.toByteArray();
188
}
189
}
190
191