// mac (tag=0x444d, len=20) (1..1)
HBCIUtils.log("found mac field",HBCIUtils.LOG_INTERN);
tlv=new MACField(tlv);
} else {
throw new HBCI_Exception("*** invalid field tag found: 0x"+Long.toString(tlv.getTag(),16));
}
addField(tlv);
posi+=4+tlv.getLength();
}
if (getField(FileHeader.class)==null) {
// ohne header-field geht gar nichts
throw new HBCI_Exception("*** RDH-2/10-file does not contain a header field - aborting");
}
MACField macfield=(MACField)getField(MACField.class);
if (macfield!=null) {
byte[] storedMac=macfield.getMac();
byte[] calculatedMac=calculateMAC();
boolean macOK=Arrays.equals(storedMac,calculatedMac);
HBCIUtils.log("MAC field ok: "+macOK,HBCIUtils.LOG_DEBUG);
if (!macOK) {
throw new InvalidPassphraseException();
}
} else {
HBCIUtils.log("RDH-2/10-file does not contain a MAC field - ignoring this for now",HBCIUtils.LOG_ERR);
}
// decrypt private user keys
try {
// calculate decryption key
FileHeader fileHeader=(FileHeader)getField(FileHeader.class);
String algname=(fileHeader.getProfileVersion()==2)?"HmacSHA1":"HmacSHA256";
byte[] derivedKey=deriveKey(24, algname);
SecretKeyFactory keyfac=SecretKeyFactory.getInstance("DESede");
DESedeKeySpec desKeyspec=new DESedeKeySpec(derivedKey);
SecretKey key=keyfac.generateSecret(desKeyspec);
// loop through all userkeys to decrypt them
TLV[] accounts=getFields(HBCIAccount.class);
for (int i=0;i<accounts.length;i++) {
HBCIAccount account=(HBCIAccount)accounts[i];
List<UserKeys> userkeys=account.getUserKeys();
for (Iterator<UserKeys> j=userkeys.iterator();j.hasNext();) {
HBCIAccount.UserKeys userkey= j.next();
userkey.decrypt(key);
HBCIUtils.log(userkey.toString(),HBCIUtils.LOG_INTERN);
}
}
} catch (Exception e) {
throw new HBCI_Exception(e);
}
}