author | weijun |
Fri, 12 Nov 2010 21:33:14 +0800 | |
changeset 7183 | d8ccc1c73358 |
parent 5975 | 076cd013e5e4 |
child 8166 | 13423c0952ad |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5975
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2000, 2010, 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; |
2 | 40 |
|
41 |
/** |
|
42 |
* This class encapsulates a AS-REP message that the KDC sends to the |
|
43 |
* client. |
|
44 |
*/ |
|
7183 | 45 |
class KrbAsRep extends KrbKdcRep { |
2 | 46 |
|
7183 | 47 |
private ASRep rep; // The AS-REP message |
48 |
private Credentials creds; // The Credentials provide by the AS-REP |
|
49 |
// message, created by initiator after calling |
|
50 |
// the decrypt() method |
|
2 | 51 |
|
52 |
private boolean DEBUG = Krb5.DEBUG; |
|
53 |
||
7183 | 54 |
KrbAsRep(byte[] ibuf) throws |
55 |
KrbException, Asn1Exception, IOException { |
|
2 | 56 |
DerValue encoding = new DerValue(ibuf); |
57 |
try { |
|
58 |
rep = new ASRep(encoding); |
|
59 |
} catch (Asn1Exception e) { |
|
60 |
rep = null; |
|
61 |
KRBError err = new KRBError(encoding); |
|
62 |
String errStr = err.getErrorString(); |
|
63 |
String eText = null; // pick up text sent by the server (if any) |
|
64 |
||
65 |
if (errStr != null && errStr.length() > 0) { |
|
66 |
if (errStr.charAt(errStr.length() - 1) == 0) |
|
67 |
eText = errStr.substring(0, errStr.length() - 1); |
|
68 |
else |
|
69 |
eText = errStr; |
|
70 |
} |
|
71 |
KrbException ke; |
|
72 |
if (eText == null) { |
|
73 |
// no text sent from server |
|
74 |
ke = new KrbException(err); |
|
75 |
} else { |
|
76 |
if (DEBUG) { |
|
77 |
System.out.println("KRBError received: " + eText); |
|
78 |
} |
|
79 |
// override default text with server text |
|
80 |
ke = new KrbException(err, eText); |
|
81 |
} |
|
82 |
ke.initCause(e); |
|
83 |
throw ke; |
|
84 |
} |
|
7183 | 85 |
} |
2 | 86 |
|
7183 | 87 |
// KrbAsReqBuilder need to read back the PA for key generation |
88 |
PAData[] getPA() { |
|
89 |
return rep.pAData; |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Called by KrbAsReqBuilder to resolve a AS-REP message using keys. |
|
94 |
* @param keys user provided keys, not null |
|
95 |
* @param asReq the original AS-REQ sent, used to validate AS-REP |
|
96 |
*/ |
|
97 |
void decryptUsingKeys(EncryptionKey[] keys, KrbAsReq asReq) |
|
98 |
throws KrbException, Asn1Exception, IOException { |
|
99 |
EncryptionKey dkey = null; |
|
2 | 100 |
int encPartKeyType = rep.encPart.getEType(); |
7183 | 101 |
Integer encPartKvno = rep.encPart.kvno; |
102 |
try { |
|
103 |
dkey = EncryptionKey.findKey(encPartKeyType, encPartKvno, keys); |
|
104 |
} catch (KrbException ke) { |
|
105 |
if (ke.returnCode() == Krb5.KRB_AP_ERR_BADKEYVER) { |
|
106 |
// Fallback to no kvno. In some cases, keytab is generated |
|
107 |
// not by sysadmin but Java's ktab command |
|
108 |
dkey = EncryptionKey.findKey(encPartKeyType, keys); |
|
109 |
} |
|
110 |
} |
|
2 | 111 |
if (dkey == null) { |
112 |
throw new KrbException(Krb5.API_INVALID_ARG, |
|
7183 | 113 |
"Cannot find key for type/kvno to decrypt AS REP - " + |
114 |
EType.toString(encPartKeyType) + "/" + encPartKvno); |
|
2 | 115 |
} |
7183 | 116 |
decrypt(dkey, asReq); |
117 |
} |
|
2 | 118 |
|
7183 | 119 |
/** |
120 |
* Called by KrbAsReqBuilder to resolve a AS-REP message using a password. |
|
121 |
* @param password user provided password. not null |
|
122 |
* @param asReq the original AS-REQ sent, used to validate AS-REP |
|
123 |
* @param cname the user principal name, used to provide salt |
|
124 |
*/ |
|
125 |
void decryptUsingPassword(char[] password, |
|
126 |
KrbAsReq asReq, PrincipalName cname) |
|
127 |
throws KrbException, Asn1Exception, IOException { |
|
128 |
int encPartKeyType = rep.encPart.getEType(); |
|
129 |
PAData.SaltAndParams snp = |
|
130 |
PAData.getSaltAndParams(encPartKeyType, rep.pAData); |
|
131 |
EncryptionKey dkey = null; |
|
132 |
dkey = EncryptionKey.acquireSecretKey(password, |
|
133 |
snp.salt == null ? cname.getSalt() : snp.salt, |
|
134 |
encPartKeyType, |
|
135 |
snp.params); |
|
136 |
decrypt(dkey, asReq); |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Decrypts encrypted content inside AS-REP. Called by initiator. |
|
141 |
* @param dkey the decryption key to use |
|
142 |
* @param asReq the original AS-REQ sent, used to validate AS-REP |
|
143 |
*/ |
|
144 |
private void decrypt(EncryptionKey dkey, KrbAsReq asReq) |
|
145 |
throws KrbException, Asn1Exception, IOException { |
|
2 | 146 |
byte[] enc_as_rep_bytes = rep.encPart.decrypt(dkey, |
147 |
KeyUsage.KU_ENC_AS_REP_PART); |
|
5975
076cd013e5e4
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents:
5506
diff
changeset
|
148 |
byte[] enc_as_rep_part = rep.encPart.reset(enc_as_rep_bytes); |
2 | 149 |
|
7183 | 150 |
DerValue encoding = new DerValue(enc_as_rep_part); |
2 | 151 |
EncASRepPart enc_part = new EncASRepPart(encoding); |
152 |
rep.ticket.sname.setRealm(rep.ticket.realm); |
|
153 |
rep.encKDCRepPart = enc_part; |
|
154 |
||
7183 | 155 |
ASReq req = asReq.getMessage(); |
2 | 156 |
check(req, rep); |
157 |
||
158 |
creds = new Credentials( |
|
159 |
rep.ticket, |
|
160 |
req.reqBody.cname, |
|
161 |
rep.ticket.sname, |
|
162 |
enc_part.key, |
|
163 |
enc_part.flags, |
|
164 |
enc_part.authtime, |
|
165 |
enc_part.starttime, |
|
166 |
enc_part.endtime, |
|
167 |
enc_part.renewTill, |
|
168 |
enc_part.caddr); |
|
169 |
if (DEBUG) { |
|
170 |
System.out.println(">>> KrbAsRep cons in KrbAsReq.getReply " + |
|
171 |
req.reqBody.cname.getNameString()); |
|
172 |
} |
|
173 |
} |
|
174 |
||
7183 | 175 |
Credentials getCreds() { |
176 |
return Objects.nonNull(creds, "Creds not available yet."); |
|
2 | 177 |
} |
178 |
||
7183 | 179 |
sun.security.krb5.internal.ccache.Credentials getCCreds() { |
2 | 180 |
return new sun.security.krb5.internal.ccache.Credentials(rep); |
181 |
} |
|
182 |
} |