author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 57487 | 643978a35f6e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55258
d65d3c37232c
8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, 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 |
/* |
|
27 |
* |
|
28 |
* (C) Copyright IBM Corp. 1999 All Rights Reserved. |
|
29 |
* Copyright 1997 The Open Group Research Institute. All rights reserved. |
|
30 |
*/ |
|
31 |
||
32 |
package sun.security.krb5; |
|
33 |
||
34 |
import sun.security.krb5.internal.*; |
|
35 |
import sun.security.krb5.internal.crypto.KeyUsage; |
|
36 |
import sun.security.krb5.internal.crypto.EType; |
|
37 |
import sun.security.util.*; |
|
38 |
import java.io.IOException; |
|
7183 | 39 |
import java.util.Objects; |
9499 | 40 |
import javax.security.auth.kerberos.KeyTab; |
41 |
import sun.security.jgss.krb5.Krb5Util; |
|
2 | 42 |
|
43 |
/** |
|
44 |
* This class encapsulates a AS-REP message that the KDC sends to the |
|
45 |
* client. |
|
46 |
*/ |
|
7183 | 47 |
class KrbAsRep extends KrbKdcRep { |
2 | 48 |
|
7183 | 49 |
private ASRep rep; // The AS-REP message |
50 |
private Credentials creds; // The Credentials provide by the AS-REP |
|
51 |
// message, created by initiator after calling |
|
52 |
// the decrypt() method |
|
2 | 53 |
|
54 |
private boolean DEBUG = Krb5.DEBUG; |
|
55 |
||
7183 | 56 |
KrbAsRep(byte[] ibuf) throws |
57 |
KrbException, Asn1Exception, IOException { |
|
2 | 58 |
DerValue encoding = new DerValue(ibuf); |
59 |
try { |
|
60 |
rep = new ASRep(encoding); |
|
61 |
} catch (Asn1Exception e) { |
|
62 |
rep = null; |
|
63 |
KRBError err = new KRBError(encoding); |
|
64 |
String errStr = err.getErrorString(); |
|
65 |
String eText = null; // pick up text sent by the server (if any) |
|
66 |
||
67 |
if (errStr != null && errStr.length() > 0) { |
|
68 |
if (errStr.charAt(errStr.length() - 1) == 0) |
|
69 |
eText = errStr.substring(0, errStr.length() - 1); |
|
70 |
else |
|
71 |
eText = errStr; |
|
72 |
} |
|
73 |
KrbException ke; |
|
74 |
if (eText == null) { |
|
75 |
// no text sent from server |
|
76 |
ke = new KrbException(err); |
|
77 |
} else { |
|
78 |
if (DEBUG) { |
|
79 |
System.out.println("KRBError received: " + eText); |
|
80 |
} |
|
81 |
// override default text with server text |
|
82 |
ke = new KrbException(err, eText); |
|
83 |
} |
|
84 |
ke.initCause(e); |
|
85 |
throw ke; |
|
86 |
} |
|
7183 | 87 |
} |
2 | 88 |
|
7183 | 89 |
// KrbAsReqBuilder need to read back the PA for key generation |
90 |
PAData[] getPA() { |
|
91 |
return rep.pAData; |
|
92 |
} |
|
93 |
||
94 |
/** |
|
9499 | 95 |
* Called by KrbAsReqBuilder to resolve a AS-REP message using a keytab. |
96 |
* @param ktab the keytab, not null |
|
7183 | 97 |
* @param asReq the original AS-REQ sent, used to validate AS-REP |
9499 | 98 |
* @param cname the user principal name, used to locate keys in ktab |
7183 | 99 |
*/ |
9499 | 100 |
void decryptUsingKeyTab(KeyTab ktab, KrbAsReq asReq, PrincipalName cname) |
7183 | 101 |
throws KrbException, Asn1Exception, IOException { |
102 |
EncryptionKey dkey = null; |
|
2 | 103 |
int encPartKeyType = rep.encPart.getEType(); |
7183 | 104 |
Integer encPartKvno = rep.encPart.kvno; |
9499 | 105 |
try { |
106 |
dkey = EncryptionKey.findKey(encPartKeyType, encPartKvno, |
|
107 |
Krb5Util.keysFromJavaxKeyTab(ktab, cname)); |
|
108 |
} catch (KrbException ke) { |
|
109 |
if (ke.returnCode() == Krb5.KRB_AP_ERR_BADKEYVER) { |
|
110 |
// Fallback to no kvno. In some cases, keytab is generated |
|
111 |
// not by sysadmin but Java's ktab command |
|
112 |
dkey = EncryptionKey.findKey(encPartKeyType, |
|
113 |
Krb5Util.keysFromJavaxKeyTab(ktab, cname)); |
|
114 |
} |
|
7183 | 115 |
} |
9499 | 116 |
if (dkey == null) { |
117 |
throw new KrbException(Krb5.API_INVALID_ARG, |
|
118 |
"Cannot find key for type/kvno to decrypt AS REP - " + |
|
119 |
EType.toString(encPartKeyType) + "/" + encPartKvno); |
|
120 |
} |
|
57487
643978a35f6e
8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents:
55258
diff
changeset
|
121 |
decrypt(dkey, asReq, cname); |
7183 | 122 |
} |
2 | 123 |
|
7183 | 124 |
/** |
125 |
* Called by KrbAsReqBuilder to resolve a AS-REP message using a password. |
|
126 |
* @param password user provided password. not null |
|
127 |
* @param asReq the original AS-REQ sent, used to validate AS-REP |
|
128 |
* @param cname the user principal name, used to provide salt |
|
129 |
*/ |
|
130 |
void decryptUsingPassword(char[] password, |
|
131 |
KrbAsReq asReq, PrincipalName cname) |
|
132 |
throws KrbException, Asn1Exception, IOException { |
|
133 |
int encPartKeyType = rep.encPart.getEType(); |
|
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
134 |
EncryptionKey dkey = EncryptionKey.acquireSecretKey( |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
135 |
cname, |
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
136 |
password, |
7183 | 137 |
encPartKeyType, |
10432
ef33e56c55a9
7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents:
9499
diff
changeset
|
138 |
PAData.getSaltAndParams(encPartKeyType, rep.pAData)); |
57487
643978a35f6e
8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents:
55258
diff
changeset
|
139 |
decrypt(dkey, asReq, cname); |
7183 | 140 |
} |
141 |
||
142 |
/** |
|
143 |
* Decrypts encrypted content inside AS-REP. Called by initiator. |
|
144 |
* @param dkey the decryption key to use |
|
145 |
* @param asReq the original AS-REQ sent, used to validate AS-REP |
|
146 |
*/ |
|
57487
643978a35f6e
8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents:
55258
diff
changeset
|
147 |
private void decrypt(EncryptionKey dkey, KrbAsReq asReq, |
643978a35f6e
8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents:
55258
diff
changeset
|
148 |
PrincipalName cname) |
7183 | 149 |
throws KrbException, Asn1Exception, IOException { |
2 | 150 |
byte[] enc_as_rep_bytes = rep.encPart.decrypt(dkey, |
151 |
KeyUsage.KU_ENC_AS_REP_PART); |
|
5975
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
152 |
byte[] enc_as_rep_part = rep.encPart.reset(enc_as_rep_bytes); |
2 | 153 |
|
7183 | 154 |
DerValue encoding = new DerValue(enc_as_rep_part); |
2 | 155 |
EncASRepPart enc_part = new EncASRepPart(encoding); |
156 |
rep.encKDCRepPart = enc_part; |
|
157 |
||
7183 | 158 |
ASReq req = asReq.getMessage(); |
55258
d65d3c37232c
8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents:
47216
diff
changeset
|
159 |
check(true, req, rep, dkey); |
2 | 160 |
|
57487
643978a35f6e
8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents:
55258
diff
changeset
|
161 |
PrincipalName clientAlias = cname; |
643978a35f6e
8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents:
55258
diff
changeset
|
162 |
if (clientAlias.equals(rep.cname)) |
643978a35f6e
8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents:
55258
diff
changeset
|
163 |
clientAlias = null; |
643978a35f6e
8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents:
55258
diff
changeset
|
164 |
|
2 | 165 |
creds = new Credentials( |
166 |
rep.ticket, |
|
55258
d65d3c37232c
8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents:
47216
diff
changeset
|
167 |
rep.cname, |
57487
643978a35f6e
8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents:
55258
diff
changeset
|
168 |
clientAlias, |
45276
633535faf95d
8178794: krb5 client should ignore sname in incoming tickets
weijun
parents:
25859
diff
changeset
|
169 |
enc_part.sname, |
57487
643978a35f6e
8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents:
55258
diff
changeset
|
170 |
null, // No server alias expected in a TGT |
2 | 171 |
enc_part.key, |
172 |
enc_part.flags, |
|
173 |
enc_part.authtime, |
|
174 |
enc_part.starttime, |
|
175 |
enc_part.endtime, |
|
176 |
enc_part.renewTill, |
|
177 |
enc_part.caddr); |
|
178 |
if (DEBUG) { |
|
179 |
System.out.println(">>> KrbAsRep cons in KrbAsReq.getReply " + |
|
180 |
req.reqBody.cname.getNameString()); |
|
181 |
} |
|
182 |
} |
|
183 |
||
7183 | 184 |
Credentials getCreds() { |
8166
13423c0952ad
7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents:
7183
diff
changeset
|
185 |
return Objects.requireNonNull(creds, "Creds not available yet."); |
2 | 186 |
} |
187 |
||
7183 | 188 |
sun.security.krb5.internal.ccache.Credentials getCCreds() { |
2 | 189 |
return new sun.security.krb5.internal.ccache.Credentials(rep); |
190 |
} |
|
191 |
} |