Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBCIInstitute::extractKeys -- Vorarbeit gekapselt und ausgelagert #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions src/main/java/org/kapott/hbci/manager/HBCIInstitute.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@

package org.kapott.hbci.manager;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Objects;

import org.kapott.hbci.callback.HBCICallback;
import org.kapott.hbci.comm.Comm;
Expand Down Expand Up @@ -112,30 +116,13 @@ void extractKeys(Properties result)
if (keyType==null)
continue;

String keyCountry=result.getProperty(head+".KeyName.KIK.country");
String keyBLZ=result.getProperty(head+".KeyName.KIK.blz");
String keyUserId=result.getProperty(head+".KeyName.userid");
String keyNum=result.getProperty(head+".KeyName.keynum");
String keyVersion=result.getProperty(head+".KeyName.keyversion");

HBCIUtils.log("found key "+
keyCountry+"_"+keyBLZ+"_"+keyUserId+"_"+keyType+"_"+
keyNum+"_"+keyVersion,
HBCIUtils.LOG_DEBUG);

byte[] keyExponent=result.getProperty(head+".PubKey.exponent").getBytes(Comm.ENCODING);
byte[] keyModulus=result.getProperty(head+".PubKey.modulus").getBytes(Comm.ENCODING);

KeyFactory fac=KeyFactory.getInstance("RSA");
KeySpec spec=new RSAPublicKeySpec(new BigInteger(+1,keyModulus),
new BigInteger(+1,keyExponent));
Key key=fac.generatePublic(spec);

if (keyType.equals("S")) {
passport.setInstSigKey(new HBCIKey(keyCountry,keyBLZ,keyUserId,keyNum,keyVersion,key));
foundChanges=true;
} else if (keyType.equals("V")) {
passport.setInstEncKey(new HBCIKey(keyCountry,keyBLZ,keyUserId,keyNum,keyVersion,key));
if (keyType.equals("S") || keyType.equals("V")) {
final HBCIKey hbciKey = getHbciKey(result, head, keyType);
if (keyType.equals("S")) {
passport.setInstSigKey(hbciKey);
} else if (keyType.equals("V")) {
passport.setInstEncKey(hbciKey);
}
foundChanges=true;
}
}
Expand All @@ -152,7 +139,35 @@ void extractKeys(Properties result)
acknowledgeNewKeys();
}
}


private static HBCIKey getHbciKey(final Properties result, final String head, final String type)
throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException
{
Objects.requireNonNull(result);
Objects.requireNonNull(head);
Objects.requireNonNull(type);

String country = result.getProperty(head + ".KeyName.KIK.country");
String blz = result.getProperty(head + ".KeyName.KIK.blz");
String userId = result.getProperty(head + ".KeyName.userid");
String keyNum = result.getProperty(head + ".KeyName.keynum");
String keyVersion = result.getProperty(head + ".KeyName.keyversion");

HBCIUtils.log("found key "+
country+"_"+blz+"_"+userId+"_"+type+"_"+
keyNum+"_"+keyVersion,
HBCIUtils.LOG_DEBUG);

byte[] keyExponent = result.getProperty(head + ".PubKey.exponent").getBytes(Comm.ENCODING);
byte[] keyModulus = result.getProperty(head + ".PubKey.modulus").getBytes(Comm.ENCODING);

KeyFactory fac = KeyFactory.getInstance("RSA");
KeySpec spec = new RSAPublicKeySpec(new BigInteger(+1, keyModulus), new BigInteger(+1, keyExponent));
Key key = fac.generatePublic(spec);

return new HBCIKey(country, blz, userId, keyNum, keyVersion, key);
}

private void acknowledgeNewKeys()
{
StringBuffer answer=new StringBuffer();
Expand Down