|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
/* |
|
* |
|
* (C) Copyright IBM Corp. 1999 All Rights Reserved. |
|
* Copyright 1997 The Open Group Research Institute. All rights reserved. |
|
*/ |
|
|
|
package sun.security.krb5; |
|
|
|
import sun.security.krb5.internal.*; |
|
import sun.security.krb5.internal.crypto.KeyUsage; |
|
import sun.security.krb5.internal.crypto.EType; |
|
import sun.security.util.*; |
|
import java.io.IOException; |
|
import java.util.Objects; |
|
import javax.security.auth.kerberos.KeyTab; |
|
import sun.security.jgss.krb5.Krb5Util; |
|
|
|
|
|
|
|
|
|
*/ |
|
class KrbAsRep extends KrbKdcRep { |
|
|
|
private ASRep rep; |
|
private Credentials creds; |
|
// message, created by initiator after calling |
|
// the decrypt() method |
|
|
|
private boolean DEBUG = Krb5.DEBUG; |
|
|
|
KrbAsRep(byte[] ibuf) throws |
|
KrbException, Asn1Exception, IOException { |
|
DerValue encoding = new DerValue(ibuf); |
|
try { |
|
rep = new ASRep(encoding); |
|
} catch (Asn1Exception e) { |
|
rep = null; |
|
KRBError err = new KRBError(encoding); |
|
String errStr = err.getErrorString(); |
|
String eText = null; |
|
|
|
if (errStr != null && errStr.length() > 0) { |
|
if (errStr.charAt(errStr.length() - 1) == 0) |
|
eText = errStr.substring(0, errStr.length() - 1); |
|
else |
|
eText = errStr; |
|
} |
|
KrbException ke; |
|
if (eText == null) { |
|
|
|
ke = new KrbException(err); |
|
} else { |
|
if (DEBUG) { |
|
System.out.println("KRBError received: " + eText); |
|
} |
|
|
|
ke = new KrbException(err, eText); |
|
} |
|
ke.initCause(e); |
|
throw ke; |
|
} |
|
} |
|
|
|
|
|
PAData[] getPA() { |
|
return rep.pAData; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
void decryptUsingKeyTab(KeyTab ktab, KrbAsReq asReq, PrincipalName cname) |
|
throws KrbException, Asn1Exception, IOException { |
|
EncryptionKey dkey = null; |
|
int encPartKeyType = rep.encPart.getEType(); |
|
Integer encPartKvno = rep.encPart.kvno; |
|
try { |
|
dkey = EncryptionKey.findKey(encPartKeyType, encPartKvno, |
|
Krb5Util.keysFromJavaxKeyTab(ktab, cname)); |
|
} catch (KrbException ke) { |
|
if (ke.returnCode() == Krb5.KRB_AP_ERR_BADKEYVER) { |
|
// Fallback to no kvno. In some cases, keytab is generated |
|
|
|
dkey = EncryptionKey.findKey(encPartKeyType, |
|
Krb5Util.keysFromJavaxKeyTab(ktab, cname)); |
|
} |
|
} |
|
if (dkey == null) { |
|
throw new KrbException(Krb5.API_INVALID_ARG, |
|
"Cannot find key for type/kvno to decrypt AS REP - " + |
|
EType.toString(encPartKeyType) + "/" + encPartKvno); |
|
} |
|
decrypt(dkey, asReq, cname); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
void decryptUsingPassword(char[] password, |
|
KrbAsReq asReq, PrincipalName cname) |
|
throws KrbException, Asn1Exception, IOException { |
|
int encPartKeyType = rep.encPart.getEType(); |
|
EncryptionKey dkey = EncryptionKey.acquireSecretKey( |
|
cname, |
|
password, |
|
encPartKeyType, |
|
PAData.getSaltAndParams(encPartKeyType, rep.pAData)); |
|
decrypt(dkey, asReq, cname); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private void decrypt(EncryptionKey dkey, KrbAsReq asReq, |
|
PrincipalName cname) |
|
throws KrbException, Asn1Exception, IOException { |
|
byte[] enc_as_rep_bytes = rep.encPart.decrypt(dkey, |
|
KeyUsage.KU_ENC_AS_REP_PART); |
|
byte[] enc_as_rep_part = rep.encPart.reset(enc_as_rep_bytes); |
|
|
|
DerValue encoding = new DerValue(enc_as_rep_part); |
|
EncASRepPart enc_part = new EncASRepPart(encoding); |
|
rep.encKDCRepPart = enc_part; |
|
|
|
ASReq req = asReq.getMessage(); |
|
check(true, req, rep, dkey); |
|
|
|
PrincipalName clientAlias = cname; |
|
if (clientAlias.equals(rep.cname)) |
|
clientAlias = null; |
|
|
|
creds = new Credentials( |
|
rep.ticket, |
|
rep.cname, |
|
clientAlias, |
|
enc_part.sname, |
|
null, |
|
enc_part.key, |
|
enc_part.flags, |
|
enc_part.authtime, |
|
enc_part.starttime, |
|
enc_part.endtime, |
|
enc_part.renewTill, |
|
enc_part.caddr); |
|
if (DEBUG) { |
|
System.out.println(">>> KrbAsRep cons in KrbAsReq.getReply " + |
|
req.reqBody.cname.getNameString()); |
|
} |
|
} |
|
|
|
Credentials getCreds() { |
|
return Objects.requireNonNull(creds, "Creds not available yet."); |
|
} |
|
|
|
sun.security.krb5.internal.ccache.Credentials getCCreds() { |
|
return new sun.security.krb5.internal.ccache.Credentials(rep); |
|
} |
|
} |