author | igerasim |
Tue, 12 Nov 2019 01:36:17 -0800 | |
changeset 59024 | b046ba510bbc |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
14413 | 1 |
/* |
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. |
14413 | 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.krb5.internal; |
|
27 |
||
28 |
import java.io.ByteArrayOutputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import sun.security.krb5.*; |
|
31 |
import sun.security.krb5.internal.crypto.KeyUsage; |
|
32 |
import sun.security.krb5.internal.util.KerberosString; |
|
33 |
import sun.security.util.DerOutputStream; |
|
34 |
import sun.security.util.DerValue; |
|
35 |
||
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
36 |
import static java.nio.charset.StandardCharsets.UTF_8; |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
37 |
|
14413 | 38 |
/** |
39 |
* Implements the ASN.1 PA-FOR-USER type. |
|
40 |
* |
|
32003 | 41 |
* <pre>{@code |
14413 | 42 |
* padata-type ::= PA-FOR-USER |
43 |
* -- value 129 |
|
44 |
* padata-value ::= EncryptedData |
|
45 |
* -- PA-FOR-USER-ENC |
|
46 |
* PA-FOR-USER-ENC ::= SEQUENCE { |
|
47 |
* userName[0] PrincipalName, |
|
48 |
* userRealm[1] Realm, |
|
49 |
* cksum[2] Checksum, |
|
50 |
* auth-package[3] KerberosString |
|
51 |
* } |
|
32003 | 52 |
* }</pre> |
14413 | 53 |
* |
54 |
* <p> |
|
55 |
* This definition reflects MS-SFU. |
|
56 |
*/ |
|
57 |
||
58 |
public class PAForUserEnc { |
|
59 |
final public PrincipalName name; |
|
60 |
final private EncryptionKey key; |
|
61 |
final public static String AUTH_PACKAGE = "Kerberos"; |
|
62 |
||
63 |
public PAForUserEnc(PrincipalName name, EncryptionKey key) { |
|
64 |
this.name = name; |
|
65 |
this.key = key; |
|
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Constructs a PA-FOR-USER object from a DER encoding. |
|
70 |
* @param encoding the input object |
|
71 |
* @param key the key to verify the checksum inside encoding |
|
72 |
* @throws KrbException if the verification fails. |
|
73 |
* Note: this method is now only used by test KDC, therefore |
|
74 |
* the verification is ignored (at the moment). |
|
75 |
*/ |
|
76 |
public PAForUserEnc(DerValue encoding, EncryptionKey key) |
|
77 |
throws Asn1Exception, KrbException, IOException { |
|
78 |
DerValue der = null; |
|
79 |
this.key = key; |
|
80 |
||
81 |
if (encoding.getTag() != DerValue.tag_Sequence) { |
|
82 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
83 |
} |
|
84 |
||
85 |
// Realm after name? Quite abnormal. |
|
86 |
PrincipalName tmpName = null; |
|
87 |
der = encoding.getData().getDerValue(); |
|
88 |
if ((der.getTag() & 0x1F) == 0x00) { |
|
89 |
try { |
|
90 |
tmpName = new PrincipalName(der.getData().getDerValue(), |
|
91 |
new Realm("PLACEHOLDER")); |
|
92 |
} catch (RealmException re) { |
|
93 |
// Impossible |
|
94 |
} |
|
95 |
} else { |
|
96 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
97 |
} |
|
98 |
||
99 |
der = encoding.getData().getDerValue(); |
|
100 |
if ((der.getTag() & 0x1F) == 0x01) { |
|
101 |
try { |
|
102 |
Realm realm = new Realm(der.getData().getDerValue()); |
|
103 |
name = new PrincipalName( |
|
104 |
tmpName.getNameType(), tmpName.getNameStrings(), realm); |
|
105 |
} catch (RealmException re) { |
|
106 |
throw new IOException(re); |
|
107 |
} |
|
108 |
} else { |
|
109 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
110 |
} |
|
111 |
||
112 |
der = encoding.getData().getDerValue(); |
|
113 |
if ((der.getTag() & 0x1F) == 0x02) { |
|
114 |
// Deal with the checksum |
|
115 |
} else { |
|
116 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
117 |
} |
|
118 |
||
119 |
der = encoding.getData().getDerValue(); |
|
120 |
if ((der.getTag() & 0x1F) == 0x03) { |
|
121 |
String authPackage = new KerberosString(der.getData().getDerValue()).toString(); |
|
122 |
if (!authPackage.equalsIgnoreCase(AUTH_PACKAGE)) { |
|
123 |
throw new IOException("Incorrect auth-package"); |
|
124 |
} |
|
125 |
} else { |
|
126 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
127 |
} |
|
128 |
if (encoding.getData().available() > 0) |
|
129 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
130 |
} |
|
131 |
||
132 |
public byte[] asn1Encode() throws Asn1Exception, IOException { |
|
133 |
DerOutputStream bytes = new DerOutputStream(); |
|
134 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), name.asn1Encode()); |
|
135 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), name.getRealm().asn1Encode()); |
|
136 |
||
137 |
try { |
|
138 |
Checksum cks = new Checksum( |
|
139 |
Checksum.CKSUMTYPE_HMAC_MD5_ARCFOUR, |
|
140 |
getS4UByteArray(), |
|
141 |
key, |
|
142 |
KeyUsage.KU_PA_FOR_USER_ENC_CKSUM); |
|
143 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), cks.asn1Encode()); |
|
144 |
} catch (KrbException ke) { |
|
145 |
throw new IOException(ke); |
|
146 |
} |
|
147 |
||
148 |
DerOutputStream temp = new DerOutputStream(); |
|
149 |
temp.putDerValue(new KerberosString(AUTH_PACKAGE).toDerValue()); |
|
150 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), temp); |
|
151 |
||
152 |
temp = new DerOutputStream(); |
|
153 |
temp.write(DerValue.tag_Sequence, bytes); |
|
154 |
return temp.toByteArray(); |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* Returns S4UByteArray, the block to calculate checksum inside a |
|
159 |
* PA-FOR-USER-ENC data structure. It includes: |
|
160 |
* 1. userName.name-type encoded as a 4-byte integer in little endian |
|
161 |
* byte order |
|
162 |
* 2. all string values in the sequence of strings contained in the |
|
163 |
* userName.name-string field |
|
164 |
* 3. the string value of the userRealm field |
|
165 |
* 4. the string value of auth-package field |
|
166 |
*/ |
|
167 |
public byte[] getS4UByteArray() { |
|
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
168 |
ByteArrayOutputStream ba = new ByteArrayOutputStream(); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
169 |
ba.writeBytes(new byte[4]); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
170 |
for (String s: name.getNameStrings()) { |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
171 |
ba.writeBytes(s.getBytes(UTF_8)); |
14413 | 172 |
} |
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
173 |
ba.writeBytes(name.getRealm().toString().getBytes(UTF_8)); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
174 |
ba.writeBytes(AUTH_PACKAGE.getBytes(UTF_8)); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
175 |
byte[] output = ba.toByteArray(); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
176 |
int pnType = name.getNameType(); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
177 |
output[0] = (byte)(pnType & 0xff); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
178 |
output[1] = (byte)((pnType>>8) & 0xff); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
179 |
output[2] = (byte)((pnType>>16) & 0xff); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
180 |
output[3] = (byte)((pnType>>24) & 0xff); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
181 |
return output; |
14413 | 182 |
} |
183 |
||
184 |
public String toString() { |
|
185 |
return "PA-FOR-USER: " + name; |
|
186 |
} |
|
187 |
} |