Skip to content

Commit e6c6eb0

Browse files
authored
Merge pull request #104 from mahendya1002/future
Checkmarx findings
2 parents d09bf95 + d948298 commit e6c6eb0

File tree

4 files changed

+147
-148
lines changed

4 files changed

+147
-148
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ You do not need to download and build the source to use the SDK but if you want
5858
- `sendToProduction` is initially set to false. Set it to true only when you are ready to send live transactions.
5959
- Set `sendToAkamai` config parameter with toggle value "true/false" to turn on/off routing requests through Akamai to Cybersource. By default, it is set to true.
6060
- `serverURL` config parameter will take precedence over `sendToProduction` and `sendToAkamai` config parameters. By default the `serverURL` configuration is commented out.
61-
- if `enableJdkcert` parameter is set to true, certificates will be read from the JKS file specified at keysDirectory location. The JKS file should be of the same name as specified in keyFilename.
61+
- If `enableJdkcert` parameter is set to true, certificates will be read from the JKS file specified at keysDirectory location. The JKS file should be of the same name as specified in keyFilename.
6262
- To know how to convert p12 to JKS refer the JKS creation section of this document.
6363
- If 'enableCacert' property parameter is set to true, certificates will be read from the cacerts file specified at keysDirectory location.If keysDirectory path is not set,certificate will be loaded from Java Installation cacerts file. The cacerts file should be of the same name as specified in keyFilename.
64-
- if `certificateCacheEnabled` parameter is set to false (default is true), the p12 certificate of a merchant will be reloaded from filesystem every time a transaction is made
64+
- If `certificateCacheEnabled` parameter is set to false (default is true), the p12 certificate of a merchant will be reloaded from filesystem every time a transaction is made
6565
- `allowRetry` config parameter will only work for HttpClient. Set `allowRetry` config parameter to "true" to enable retry mechanism and set merchant specific values for the retry.
6666
- Set integer values for config parameter `numberOfRetries` *and* `retryInterval`. Retry Interval is time delay for next retry in seconds.
6767
- Number of retry parameter should be set between 1 to 5. Any other value will throw an Error Message.
@@ -183,10 +183,10 @@ Retry Pattern allows to retry sending a failed request and it will only work wit
183183

184184
## Changes
185185

186-
Version Cybersource-sdk-java 6.2.6 (JAN,2018)
186+
Version Cybersource-sdk-java 6.2.6 (MAY,2018)
187187
_______________________________
188188
1) Added certificateCacheEnabled optional feature. certificateCacheEnabled parameter is set to false (default is true), the p12 certificate of a merchant will be reloaded from filesystem every time a transaction is made.If the certificateCacheEnabled is true then only at the first time certificate of a merchant will loaded from filesystem.
189-
2) Intreduced a new feature to check merchant .p12 certificate file validity at run time.If it is not valid and replaced at runtime then SDK will be able to reload the new certificate data into cache.
189+
2) Intreduced a new feature to check merchant .p12 certificate file validity at run time. If it is replaced at runtime then SDK will reload the new certificate into the cache.
190190
3) Changed clientLibrary version to 6.2.6;
191191

192192
Version Cybersource-sdk-java 6.2.5 (OCT,2017)

java/src/main/java/com/cybersource/ws/client/Identity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ public Identity(MerchantConfig merchantConfig,X509Certificate x509Certificate, P
117117
}
118118

119119
/**
120-
* Replace of merchant certificate not happened at runtime then isValid method will return true and certificate reload will not happen.
121-
* But replace of merchant certificate happened at at runtime then isValid method will return false and certificate reload will happen.
120+
* If merchant uploads a new key then isValid method will return false and certificate reload will happen.
121+
* else isValid method will return true and certificate reload will not occur.
122122
*/
123123

124124
public boolean isValid(File keyFile) {

java/src/main/java/com/cybersource/ws/client/SecurityUtil.java

Lines changed: 135 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -265,142 +265,140 @@ public static Document createSignedDoc(Document workingDocument,String merchantI
265265
}
266266

267267

268-
public static void readJdkCert(MerchantConfig merchantConfig, Logger logger) throws SignEncryptException, SignException, ConfigException{
269-
KeyStore keystore=null;
270-
271-
String pass=merchantConfig.getKeyPassword();
272-
273-
try{
274-
FileInputStream is = new FileInputStream(merchantConfig.getKeyFile());
275-
keystore = KeyStore.getInstance(KeyStore.getDefaultType());
276-
keystore.load(is, pass.toCharArray());
277-
}
278-
catch (Exception e) {
279-
logger.log(Logger.LT_EXCEPTION,
280-
"Failed to load the key , '" + merchantConfig.getKeyAlias() + "'");
281-
throw new SignException(e);
282-
}
283-
284-
285-
String merchantKeyAlias = null;
286-
try {
287-
Enumeration enumKeyStore = keystore.aliases();
288-
if(!enumKeyStore.hasMoreElements()){
289-
throw new SignException("Empty Keystore or Missing Certificate ");
290-
}
291-
while (enumKeyStore.hasMoreElements()) {
292-
KeyStore.PrivateKeyEntry keyEntry = null;
293-
merchantKeyAlias = (String) enumKeyStore.nextElement();
294-
if (merchantKeyAlias.contains(merchantConfig.getKeyAlias())){
295-
try {
296-
keyEntry = (KeyStore.PrivateKeyEntry) keystore.getEntry
297-
(merchantKeyAlias, new KeyStore.PasswordProtection(merchantConfig.getKeyPassword().toCharArray()));
298-
} catch (NoSuchAlgorithmException e) {
299-
logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'");
300-
throw new SignException(e);
301-
} catch (UnrecoverableEntryException e) {
302-
logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'");
303-
throw new SignException(e);
304-
} catch (KeyStoreException e) {
305-
logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'");
306-
throw new SignException(e);
307-
}
308-
309-
Identity identity = new Identity(merchantConfig,(X509Certificate) keyEntry.getCertificate(),keyEntry.getPrivateKey(),logger);
310-
localKeyStoreHandler.addIdentityToKeyStore(identity, logger);
311-
identities.put(identity.getName(), identity);
312-
continue;
313-
}
314-
Identity identity = new Identity(merchantConfig, (X509Certificate) keystore.getCertificate(merchantKeyAlias),logger);
315-
localKeyStoreHandler.addIdentityToKeyStore(identity, logger);
316-
identities.put(identity.getName(), identity);
317-
}
318-
} catch (KeyStoreException e) {
319-
logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'");
320-
throw new SignException(e);
321-
}
322-
323-
}
268+
public static void readJdkCert(MerchantConfig merchantConfig, Logger logger)
269+
throws SignEncryptException, SignException, ConfigException {
270+
KeyStore keystore = null;
271+
try {
272+
FileInputStream is = new FileInputStream(merchantConfig.getKeyFile());
273+
keystore = KeyStore.getInstance(KeyStore.getDefaultType());
274+
keystore.load(is, merchantConfig.getKeyPassword().toCharArray());
275+
} catch (Exception e) {
276+
logger.log(Logger.LT_EXCEPTION, "Failed to load the key , '" + merchantConfig.getKeyAlias() + "'");
277+
throw new SignException(e);
278+
}
279+
280+
String merchantKeyAlias = null;
281+
try {
282+
Enumeration enumKeyStore = keystore.aliases();
283+
if (!enumKeyStore.hasMoreElements()) {
284+
throw new SignException("Empty Keystore or Missing Certificate ");
285+
}
286+
while (enumKeyStore.hasMoreElements()) {
287+
KeyStore.PrivateKeyEntry keyEntry = null;
288+
merchantKeyAlias = (String) enumKeyStore.nextElement();
289+
if (merchantKeyAlias.contains(merchantConfig.getKeyAlias())) {
290+
try {
291+
keyEntry = (KeyStore.PrivateKeyEntry) keystore.getEntry(merchantKeyAlias,
292+
new KeyStore.PasswordProtection(merchantConfig.getKeyPassword().toCharArray()));
293+
} catch (NoSuchAlgorithmException e) {
294+
logger.log(Logger.LT_EXCEPTION,
295+
"Exception while obtaining private key from KeyStore with alias, '"
296+
+ merchantConfig.getKeyAlias() + "'");
297+
throw new SignException(e);
298+
} catch (UnrecoverableEntryException e) {
299+
logger.log(Logger.LT_EXCEPTION,
300+
"Exception while obtaining private key from KeyStore with alias, '"
301+
+ merchantConfig.getKeyAlias() + "'");
302+
throw new SignException(e);
303+
} catch (KeyStoreException e) {
304+
logger.log(Logger.LT_EXCEPTION,
305+
"Exception while obtaining private key from KeyStore with alias, '"
306+
+ merchantConfig.getKeyAlias() + "'");
307+
throw new SignException(e);
308+
}
309+
310+
Identity identity = new Identity(merchantConfig, (X509Certificate) keyEntry.getCertificate(),
311+
keyEntry.getPrivateKey(), logger);
312+
localKeyStoreHandler.addIdentityToKeyStore(identity, logger);
313+
identities.put(identity.getName(), identity);
314+
continue;
315+
}
316+
Identity identity = new Identity(merchantConfig,
317+
(X509Certificate) keystore.getCertificate(merchantKeyAlias), logger);
318+
localKeyStoreHandler.addIdentityToKeyStore(identity, logger);
319+
identities.put(identity.getName(), identity);
320+
}
321+
} catch (KeyStoreException e) {
322+
logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore with alias, '"
323+
+ merchantConfig.getKeyAlias() + "'");
324+
throw new SignException(e);
325+
}
326+
327+
}
324328

325-
private static void loadJavaKeystore(MerchantConfig merchantConfig,Logger logger) throws SignException, SignEncryptException, ConfigException{
326-
FileInputStream is = null;
327-
try {
328-
File file = new File(merchantConfig.getKeyFile().getCanonicalPath());
329-
is = new FileInputStream(file);
330-
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
331-
String password = merchantConfig.getCacertPassword();
332-
keystore.load(is, password.toCharArray());
333-
334-
Identity identity;
335-
336-
java.security.cert.Certificate[] cert = keystore.getCertificateChain(merchantConfig.getKeyAlias());
337-
if (cert == null) {
338-
throw new SignException("Empty Keystore or Missing Certificate ");
339-
}
340-
PrivateKey key = null;
341-
try {
342-
key = (PrivateKey)keystore.getKey(merchantConfig.getKeyAlias(), merchantConfig.getKeyAlias().toCharArray());
343-
} catch (UnrecoverableKeyException e) {
344-
logger.log(Logger.LT_EXCEPTION,
345-
"Exception while obtaining private key from KeyStore with alias, '"
346-
+ merchantConfig.getKeyAlias() + "'");
347-
throw new SignException(e);
348-
}
349-
350-
for (int i = 0; i < cert.length; i++) {
351-
352-
if (merchantConfig.getKeyAlias().equals(
353-
keystore.getCertificateAlias(cert[i]))) {
354-
identity = new Identity(merchantConfig,
355-
(X509Certificate) cert[i], key,logger);
356-
localKeyStoreHandler
357-
.addIdentityToKeyStore(identity, logger);
358-
identities.put(identity.getName(), identity);
359-
} else {
360-
identity = new Identity(merchantConfig,
361-
(X509Certificate) cert[i],logger);
362-
localKeyStoreHandler
363-
.addIdentityToKeyStore(identity, logger);
364-
identities.put(identity.getName(), identity);
365-
}
366-
}
367-
java.security.cert.Certificate serverCert = keystore.getCertificate(SERVER_ALIAS);
368-
if(serverCert == null){
369-
throw new SignException("Missing Server Certificate ");
370-
}
371-
identity = new Identity(merchantConfig,
372-
(X509Certificate) serverCert,logger);
373-
localKeyStoreHandler
374-
.addIdentityToKeyStore(identity, logger);
375-
identities.put(identity.getName(), identity);
376-
377-
}
378-
379-
catch (java.security.cert.CertificateException e) {
380-
logger.log(Logger.LT_EXCEPTION, "Unable to load the certificate,"+ merchantConfig.getKeyFilename() + "'");
381-
throw new SignException(e);
382-
} catch (NoSuchAlgorithmException e) {
383-
logger.log(Logger.LT_EXCEPTION, "Unable to find the certificate with the specified algorithm");
384-
throw new SignException(e);
385-
} catch (FileNotFoundException e) {
386-
logger.log(Logger.LT_EXCEPTION, "File Not found ");
387-
throw new SignException(e);
388-
} catch (KeyStoreException e) {
389-
logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore"+ merchantConfig.getKeyFilename() + "'");
390-
throw new SignException(e);
391-
} catch (IOException e) {
392-
logger.log(Logger.LT_EXCEPTION, "Exception while loading KeyStore, '" + merchantConfig.getKeyFilename() + "'");
393-
throw new SignException(e);
394-
}finally {
395-
if(null != is)
396-
try {
397-
is.close();
398-
} catch (IOException e) {
399-
logger.log(Logger.LT_EXCEPTION, "Exception while closing FileStream, '" + merchantConfig.getKeyFilename() + "'");
400-
throw new SignException(e);
401-
}
402-
}
403-
404-
405-
}
329+
private static void loadJavaKeystore(MerchantConfig merchantConfig, Logger logger)
330+
throws SignException, SignEncryptException, ConfigException {
331+
FileInputStream is = null;
332+
try {
333+
is = new FileInputStream(merchantConfig.getKeyFile());
334+
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
335+
keystore.load(is, merchantConfig.getCacertPassword().toCharArray());
336+
337+
Identity identity;
338+
339+
java.security.cert.Certificate[] cert = keystore.getCertificateChain(merchantConfig.getKeyAlias());
340+
if (cert == null) {
341+
throw new SignException("Empty Keystore or Missing Certificate ");
342+
}
343+
PrivateKey key = null;
344+
try {
345+
key = (PrivateKey) keystore.getKey(merchantConfig.getKeyAlias(),
346+
merchantConfig.getKeyAlias().toCharArray());
347+
} catch (UnrecoverableKeyException e) {
348+
logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore with alias, '"
349+
+ merchantConfig.getKeyAlias() + "'");
350+
throw new SignException(e);
351+
}
352+
353+
for (int i = 0; i < cert.length; i++) {
354+
355+
if (merchantConfig.getKeyAlias().equals(keystore.getCertificateAlias(cert[i]))) {
356+
identity = new Identity(merchantConfig, (X509Certificate) cert[i], key, logger);
357+
localKeyStoreHandler.addIdentityToKeyStore(identity, logger);
358+
identities.put(identity.getName(), identity);
359+
} else {
360+
identity = new Identity(merchantConfig, (X509Certificate) cert[i], logger);
361+
localKeyStoreHandler.addIdentityToKeyStore(identity, logger);
362+
identities.put(identity.getName(), identity);
363+
}
364+
}
365+
java.security.cert.Certificate serverCert = keystore.getCertificate(SERVER_ALIAS);
366+
if (serverCert == null) {
367+
throw new SignException("Missing Server Certificate ");
368+
}
369+
identity = new Identity(merchantConfig, (X509Certificate) serverCert, logger);
370+
localKeyStoreHandler.addIdentityToKeyStore(identity, logger);
371+
identities.put(identity.getName(), identity);
372+
373+
}
374+
375+
catch (java.security.cert.CertificateException e) {
376+
logger.log(Logger.LT_EXCEPTION, "Unable to load the certificate," + merchantConfig.getKeyFilename() + "'");
377+
throw new SignException(e);
378+
} catch (NoSuchAlgorithmException e) {
379+
logger.log(Logger.LT_EXCEPTION, "Unable to find the certificate with the specified algorithm");
380+
throw new SignException(e);
381+
} catch (FileNotFoundException e) {
382+
logger.log(Logger.LT_EXCEPTION, "File Not found ");
383+
throw new SignException(e);
384+
} catch (KeyStoreException e) {
385+
logger.log(Logger.LT_EXCEPTION,
386+
"Exception while obtaining private key from KeyStore" + merchantConfig.getKeyFilename() + "'");
387+
throw new SignException(e);
388+
} catch (IOException e) {
389+
logger.log(Logger.LT_EXCEPTION,
390+
"Exception while loading KeyStore, '" + merchantConfig.getKeyFilename() + "'");
391+
throw new SignException(e);
392+
} finally {
393+
if (null != is)
394+
try {
395+
is.close();
396+
} catch (IOException e) {
397+
logger.log(Logger.LT_EXCEPTION,
398+
"Exception while closing FileStream, '" + merchantConfig.getKeyFilename() + "'");
399+
throw new SignException(e);
400+
}
401+
}
402+
403+
}
406404
}

0 commit comments

Comments
 (0)