Skip to content

Commit 50ad89c

Browse files
authored
Merge pull request #31 from ebics-java/nonce-key-split
use different values for nonce and transaction key
2 parents 933925d + 50f0ef7 commit 50ad89c

File tree

8 files changed

+40
-57
lines changed

8 files changed

+40
-57
lines changed

src/main/java/org/kopi/ebics/certificate/KeyUtil.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.commons.codec.binary.Base64;
3232
import org.apache.commons.codec.binary.Hex;
3333
import org.kopi.ebics.exception.EbicsException;
34+
import org.kopi.ebics.utils.Utils;
3435

3536
/**
3637
* Some key utilities
@@ -50,15 +51,13 @@ private KeyUtil() {
5051
* @return KeyPair the key pair
5152
* @throws NoSuchAlgorithmException
5253
*/
53-
public static KeyPair makeKeyPair(int keyLen) throws NoSuchAlgorithmException{
54+
public static KeyPair makeKeyPair(int keyLen) throws NoSuchAlgorithmException {
5455
KeyPairGenerator keyGen;
5556

5657
keyGen = KeyPairGenerator.getInstance("RSA");
57-
keyGen.initialize(keyLen, new SecureRandom());
58+
keyGen.initialize(keyLen, Utils.secureRandom);
5859

59-
KeyPair keypair = keyGen.generateKeyPair();
60-
61-
return keypair;
60+
return keyGen.generateKeyPair();
6261

6362
}
6463

@@ -68,16 +67,8 @@ public static KeyPair makeKeyPair(int keyLen) throws NoSuchAlgorithmException{
6867
* @return the password
6968
*/
7069
public static String generatePassword() {
71-
SecureRandom random;
72-
73-
try {
74-
random = SecureRandom.getInstance("SHA1PRNG");
75-
String pwd = Base64.encodeBase64String(random.generateSeed(5));
76-
70+
String pwd = Base64.encodeBase64String(Utils.secureRandom.generateSeed(5));
7771
return pwd.substring(0, pwd.length() - 2);
78-
} catch (NoSuchAlgorithmException e) {
79-
throw new RuntimeException(e);
80-
}
8172
}
8273

8374
/**

src/main/java/org/kopi/ebics/certificate/X509Generator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.security.GeneralSecurityException;
2727
import java.security.KeyPair;
2828
import java.security.PublicKey;
29-
import java.security.SecureRandom;
3029
import java.security.cert.X509Certificate;
3130
import java.text.SimpleDateFormat;
3231
import java.util.Date;
@@ -49,6 +48,7 @@
4948
import org.bouncycastle.asn1.x509.X509Name;
5049
import org.bouncycastle.jce.X509Principal;
5150
import org.bouncycastle.x509.X509V3CertificateGenerator;
51+
import org.kopi.ebics.utils.Utils;
5252

5353
/**
5454
* An X509 certificate generator for EBICS protocol.
@@ -194,7 +194,7 @@ public X509Certificate generate(KeyPair keypair,
194194
break;
195195
}
196196

197-
certificate = generator.generate(keypair.getPrivate(), "BC", new SecureRandom());
197+
certificate = generator.generate(keypair.getPrivate(), "BC", Utils.secureRandom);
198198
certificate.checkValidity(new Date());
199199
certificate.verify(keypair.getPublic());
200200

src/main/java/org/kopi/ebics/security/EbicsSocketFactory.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
import java.net.UnknownHostException;
2727
import java.security.GeneralSecurityException;
2828
import java.security.KeyStore;
29-
import java.security.SecureRandom;
3029

3130
import javax.net.ssl.KeyManagerFactory;
3231
import javax.net.ssl.SSLContext;
3332
import javax.net.ssl.SSLSocketFactory;
3433
import javax.net.ssl.TrustManagerFactory;
3534

35+
import org.kopi.ebics.utils.Utils;
36+
3637
/**
3738
* A simple SSL socket factory for EBICS client.
3839
*
@@ -81,7 +82,7 @@ public EbicsSocketFactory(byte[] keystore,
8182
* Returns the <code>SSLContext</code> from key store information.
8283
* @param keystore the key store
8384
* @param keystoreType the key store type
84-
* @param keystrorePass the key store password
85+
* @param keystorePass the key store password
8586
* @param truststore the trust store
8687
* @param truststoreType the trust store type
8788
* @param truststorePass the trust store password
@@ -91,7 +92,7 @@ public EbicsSocketFactory(byte[] keystore,
9192
*/
9293
public SSLContext getSSLContext(byte[] keystore,
9394
String keystoreType,
94-
char[] keystrorePass,
95+
char[] keystorePass,
9596
byte[] truststore,
9697
String truststoreType,
9798
char[] truststorePass)
@@ -103,15 +104,15 @@ public SSLContext getSSLContext(byte[] keystore,
103104
TrustManagerFactory tmf;
104105
SSLContext context;
105106

106-
kstore = initKeyStore(keystore, keystrorePass, keystoreType);
107+
kstore = initKeyStore(keystore, keystorePass, keystoreType);
107108
kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
108-
kmf.init(kstore, keystrorePass);
109+
kmf.init(kstore, keystorePass);
109110

110111
tstore = initKeyStore(truststore, truststorePass, truststoreType);
111112
tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
112113
tmf.init(tstore);
113114
context = SSLContext.getInstance("TLS");
114-
context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
115+
context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), Utils.secureRandom);
115116

116117
return context;
117118
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public final class Utils {
6060
org.apache.xml.security.Init.init();
6161
}
6262

63+
public static final SecureRandom secureRandom = new SecureRandom();
64+
6365
private Utils() {
6466
}
6567

@@ -119,23 +121,19 @@ public static byte[] zip(byte[] toZip) throws EbicsException {
119121
* be at least 100 bits.
120122
*
121123
* @return a random nonce.
122-
* @throws EbicsException nonce generation fails.
123124
*/
124-
public static byte[] generateNonce() throws EbicsException {
125-
SecureRandom secureRandom;
125+
public static byte[] generateNonce() {
126+
return secureRandom.generateSeed(16);
127+
}
126128

127-
try {
128-
secureRandom = SecureRandom.getInstance("SHA1PRNG");
129-
return secureRandom.generateSeed(16);
130-
} catch (NoSuchAlgorithmException e) {
131-
throw new EbicsException(e.getMessage());
132-
}
129+
public static byte[] generateKey() {
130+
return secureRandom.generateSeed(16);
133131
}
134132

135133
/**
136134
* Uncompresses a given byte array input.
137135
*
138-
* <p>The Decompression is ensured via Universal compression
136+
* The Decompression is ensured via Universal compression
139137
* algorithm (RFC 1950, RFC 1951) As specified in the EBICS
140138
* specification (16 Appendix: Standards and references)
141139
*

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.io.OutputStream;
2727
import java.io.PrintStream;
2828
import java.math.BigInteger;
29-
import java.security.SecureRandom;
3029
import java.util.ArrayList;
3130
import java.util.HashMap;
3231
import java.util.Iterator;
@@ -47,6 +46,7 @@
4746
import org.kopi.ebics.interfaces.EbicsOrderType;
4847
import org.kopi.ebics.interfaces.EbicsRootElement;
4948
import org.kopi.ebics.session.EbicsSession;
49+
import org.kopi.ebics.utils.Utils;
5050

5151
public abstract class DefaultEbicsRootElement implements EbicsRootElement {
5252

@@ -134,7 +134,7 @@ public void insertSchemaLocation(String namespaceURI,
134134
* @return the generated file name.
135135
*/
136136
public static String generateName(EbicsOrderType type) {
137-
return type.getCode() + new BigInteger(130, new SecureRandom()).toString(32);
137+
return type.getCode() + new BigInteger(130, Utils.secureRandom).toString(32);
138138
}
139139

140140
/**
@@ -143,7 +143,7 @@ public static String generateName(EbicsOrderType type) {
143143
* @return the generated file name.
144144
*/
145145
public static String generateName(String prefix) {
146-
return prefix + new BigInteger(130, new SecureRandom()).toString(32);
146+
return prefix + new BigInteger(130, Utils.secureRandom).toString(32);
147147
}
148148

149149
@Override

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)