From a0133182d3a1ae3219991098cf7d1ed0a5e6d3f9 Mon Sep 17 00:00:00 2001 From: Philipp Riemer Date: Sat, 22 Jan 2022 16:17:52 +0100 Subject: [PATCH] Refactoring: extrahiere Logik aus HBCIInstitute::extractKeys in eigene Methode --- .../kapott/hbci/manager/HBCIInstitute.java | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/kapott/hbci/manager/HBCIInstitute.java b/src/main/java/org/kapott/hbci/manager/HBCIInstitute.java index 9ed512f3..b8e151de 100644 --- a/src/main/java/org/kapott/hbci/manager/HBCIInstitute.java +++ b/src/main/java/org/kapott/hbci/manager/HBCIInstitute.java @@ -21,9 +21,12 @@ 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; @@ -112,30 +115,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; } } @@ -152,7 +138,31 @@ void extractKeys(Properties result) acknowledgeNewKeys(); } } - + + private HBCIKey getHbciKey(final Properties result, final String head, final String type) + throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException + { + 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();