Skip to content

Commit de3b253

Browse files
committed
use different values for nonce and transaction key
1 parent 11e3930 commit de3b253

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

src/main/java/org/kopi/ebics/utils/Utils.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,17 @@ public static byte[] zip(byte[] toZip) throws EbicsException {
122122
* @throws EbicsException nonce generation fails.
123123
*/
124124
public static byte[] generateNonce() throws EbicsException {
125-
SecureRandom secureRandom;
125+
try {
126+
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
127+
return secureRandom.generateSeed(16);
128+
} catch (NoSuchAlgorithmException e) {
129+
throw new EbicsException(e.getMessage());
130+
}
131+
}
126132

133+
public static byte[] generateKey() throws EbicsException {
127134
try {
128-
secureRandom = SecureRandom.getInstance("SHA1PRNG");
135+
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
129136
return secureRandom.generateSeed(16);
130137
} catch (NoSuchAlgorithmException e) {
131138
throw new EbicsException(e.getMessage());
@@ -135,7 +142,7 @@ public static byte[] generateNonce() throws EbicsException {
135142
/**
136143
* Uncompresses a given byte array input.
137144
*
138-
* <p>The Decompression is ensured via Universal compression
145+
* The Decompression is ensured via Universal compression
139146
* algorithm (RFC 1950, RFC 1951) As specified in the EBICS
140147
* specification (16 Appendix: Standards and references)
141148
*

src/main/java/org/kopi/ebics/xml/EbicsXmlFactory.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,6 @@ public static StaticHeaderType createStaticHeaderType(String hostId,
864864
* Creates a new <code>StaticHeaderType</code> XML object
865865
* @param hostId the host ID
866866
* @param nonce the random nonce
867-
* @param numSegments the segments number
868867
* @param partnerId the partner ID
869868
* @param product the <code>Product</code> element
870869
* @param securityMedium the security medium
@@ -989,7 +988,6 @@ public static FDLOrderParamsType createFDLOrderParamsType(FileFormatType fileFor
989988

990989
/**
991990
* Creates a new <code>StandardOrderParamsType</code> XML object
992-
* @param fileFormat the <code>FileFormatType</code> element
993991
* @return the <code>StandardOrderParamsType</code> XML object
994992
*/
995993
public static StandardOrderParamsType createStandardOrderParamsType() {

src/main/java/org/kopi/ebics/xml/InitializationRequestElement.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.security.NoSuchProviderException;
2525

2626
import javax.crypto.Cipher;
27+
import javax.crypto.spec.SecretKeySpec;
2728

2829
import org.apache.commons.codec.DecoderException;
2930
import org.apache.commons.codec.binary.Hex;
@@ -61,6 +62,8 @@ public InitializationRequestElement(EbicsSession session,
6162
this.type = type;
6263
this.name = name;
6364
nonce = Utils.generateNonce();
65+
key = Utils.generateKey();
66+
keySpec = new SecretKeySpec(key, "EAS");
6467
}
6568

6669
@Override
@@ -96,9 +99,7 @@ public byte[] getDigest() throws EbicsException {
9699

97100
try {
98101
return MessageDigest.getInstance("SHA-256", "BC").digest(Utils.canonize(toByteArray()));
99-
} catch (NoSuchAlgorithmException e) {
100-
throw new EbicsException(e.getMessage());
101-
} catch (NoSuchProviderException e) {
102+
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
102103
throw new EbicsException(e.getMessage());
103104
}
104105
}
@@ -135,12 +136,10 @@ protected byte[] decodeHex(byte[] hex) throws EbicsException {
135136
*/
136137
protected byte[] generateTransactionKey() throws EbicsException {
137138
try {
138-
Cipher cipher;
139-
140-
cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", BouncyCastleProvider.PROVIDER_NAME);
139+
Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", BouncyCastleProvider.PROVIDER_NAME);
141140
cipher.init(Cipher.ENCRYPT_MODE, session.getBankE002Key());
142141

143-
return cipher.doFinal(nonce);
142+
return cipher.doFinal(key);
144143
} catch (Exception e) {
145144
throw new EbicsException(e.getMessage());
146145
}
@@ -157,8 +156,10 @@ protected byte[] generateTransactionKey() throws EbicsException {
157156
// DATA MEMBERS
158157
// --------------------------------------------------------------------
159158

160-
private String name;
159+
private final String name;
161160
protected EbicsOrderType type;
162-
protected byte[] nonce;
163-
private static final long serialVersionUID = 8983807819242699280L;
161+
protected final byte[] nonce;
162+
private final byte[] key;
163+
protected final SecretKeySpec keySpec;
164+
private static final long serialVersionUID = 8983807819242699280L;
164165
}

src/main/java/org/kopi/ebics/xml/SPRRequestElement.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import java.util.Calendar;
2323

24-
import javax.crypto.spec.SecretKeySpec;
25-
2624
import org.kopi.ebics.exception.EbicsException;
2725
import org.kopi.ebics.schema.h003.DataEncryptionInfoType.EncryptionPubKeyDigest;
2826
import org.kopi.ebics.schema.h003.DataTransferRequestType;
@@ -60,7 +58,6 @@ public class SPRRequestElement extends InitializationRequestElement {
6058
*/
6159
public SPRRequestElement(EbicsSession session) throws EbicsException {
6260
super(session, org.kopi.ebics.session.OrderType.SPR, "SPRRequest.xml");
63-
keySpec = new SecretKeySpec(nonce, "EAS");
6461
}
6562

6663
@Override
@@ -136,6 +133,5 @@ public void buildInitialization() throws EbicsException {
136133
// DATA MEMBERS
137134
// --------------------------------------------------------------------
138135

139-
private SecretKeySpec keySpec;
140136
private static final long serialVersionUID = -6742241777786111337L;
141137
}

src/main/java/org/kopi/ebics/xml/UploadInitializationRequestElement.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import java.util.Calendar;
2424
import java.util.List;
2525

26-
import javax.crypto.spec.SecretKeySpec;
27-
2826
import org.kopi.ebics.exception.EbicsException;
2927
import org.kopi.ebics.interfaces.ContentFactory;
3028
import org.kopi.ebics.interfaces.EbicsOrderType;
@@ -78,7 +76,6 @@ public UploadInitializationRequestElement(EbicsSession session,
7876
{
7977
super(session, orderType, generateName(orderType));
8078
this.userData = userData;
81-
keySpec = new SecretKeySpec(nonce, "EAS");
8279
splitter = new Splitter(userData);
8380
this.orderAttribute = orderAttribute;
8481
}
@@ -130,13 +127,13 @@ public void buildInitialization() throws EbicsException {
130127
FULOrderParamsType fULOrderParams = EbicsXmlFactory.createFULOrderParamsType(fileFormat);
131128

132129
List<Parameter> parameters = new ArrayList<>();
133-
if (Boolean.valueOf(session.getSessionParam("TEST")).booleanValue()) {
130+
if (Boolean.parseBoolean(session.getSessionParam("TEST"))) {
134131
Value value = EbicsXmlFactory.createValue("String", "TRUE");
135132
Parameter parameter = EbicsXmlFactory.createParameter("TEST", value);
136133
parameters.add(parameter);
137134
}
138135

139-
if (Boolean.valueOf(session.getSessionParam("EBCDIC")).booleanValue()) {
136+
if (Boolean.parseBoolean(session.getSessionParam("EBCDIC"))) {
140137
Value value = EbicsXmlFactory.createValue("String", "TRUE");
141138
Parameter parameter = EbicsXmlFactory.createParameter("EBCDIC", value);
142139
parameters.add(parameter);
@@ -221,9 +218,8 @@ public int getSegmentNumber() {
221218
// --------------------------------------------------------------------
222219

223220
private final OrderAttributeType.Enum orderAttribute;
224-
private byte[] userData;
221+
private final byte[] userData;
225222
private UserSignature userSignature;
226-
private SecretKeySpec keySpec;
227-
private Splitter splitter;
223+
private final Splitter splitter;
228224
private static final long serialVersionUID = -8083183483311283608L;
229225
}

0 commit comments

Comments
 (0)