author | weijun |
Wed, 20 Apr 2011 18:41:32 +0800 | |
changeset 9499 | f3115698a012 |
parent 9035 | 1255eb81cc2f |
child 10432 | ef33e56c55a9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8166
diff
changeset
|
2 |
* Copyright (c) 2000, 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 |
/* |
|
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 |
} |
|
7183 | 121 |
decrypt(dkey, asReq); |
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(); |
|
134 |
PAData.SaltAndParams snp = |
|
135 |
PAData.getSaltAndParams(encPartKeyType, rep.pAData); |
|
136 |
EncryptionKey dkey = null; |
|
137 |
dkey = EncryptionKey.acquireSecretKey(password, |
|
138 |
snp.salt == null ? cname.getSalt() : snp.salt, |
|
139 |
encPartKeyType, |
|
140 |
snp.params); |
|
141 |
decrypt(dkey, asReq); |
|
142 |
} |
|
143 |
||
144 |
/** |
|
145 |
* Decrypts encrypted content inside AS-REP. Called by initiator. |
|
146 |
* @param dkey the decryption key to use |
|
147 |
* @param asReq the original AS-REQ sent, used to validate AS-REP |
|
148 |
*/ |
|
149 |
private void decrypt(EncryptionKey dkey, KrbAsReq asReq) |
|
150 |
throws KrbException, Asn1Exception, IOException { |
|
2 | 151 |
byte[] enc_as_rep_bytes = rep.encPart.decrypt(dkey, |
152 |
KeyUsage.KU_ENC_AS_REP_PART); |
|
5975
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
153 |
byte[] enc_as_rep_part = rep.encPart.reset(enc_as_rep_bytes); |
2 | 154 |
|
7183 | 155 |
DerValue encoding = new DerValue(enc_as_rep_part); |
2 | 156 |
EncASRepPart enc_part = new EncASRepPart(encoding); |
157 |
rep.ticket.sname.setRealm(rep.ticket.realm); |
|
158 |
rep.encKDCRepPart = enc_part; |
|
159 |
||
7183 | 160 |
ASReq req = asReq.getMessage(); |
2 | 161 |
check(req, rep); |
162 |
||
163 |
creds = new Credentials( |
|
164 |
rep.ticket, |
|
165 |
req.reqBody.cname, |
|
166 |
rep.ticket.sname, |
|
167 |
enc_part.key, |
|
168 |
enc_part.flags, |
|
169 |
enc_part.authtime, |
|
170 |
enc_part.starttime, |
|
171 |
enc_part.endtime, |
|
172 |
enc_part.renewTill, |
|
173 |
enc_part.caddr); |
|
174 |
if (DEBUG) { |
|
175 |
System.out.println(">>> KrbAsRep cons in KrbAsReq.getReply " + |
|
176 |
req.reqBody.cname.getNameString()); |
|
177 |
} |
|
178 |
} |
|
179 |
||
7183 | 180 |
Credentials getCreds() { |
8166
13423c0952ad
7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents:
7183
diff
changeset
|
181 |
return Objects.requireNonNull(creds, "Creds not available yet."); |
2 | 182 |
} |
183 |
||
7183 | 184 |
sun.security.krb5.internal.ccache.Credentials getCCreds() { |
2 | 185 |
return new sun.security.krb5.internal.ccache.Credentials(rep); |
186 |
} |
|
187 |
} |