|
10 | 10 | import org.bouncycastle.jce.provider.BouncyCastleProvider; |
11 | 11 | import org.w3c.dom.Document; |
12 | 12 |
|
| 13 | +import java.io.File; |
13 | 14 | import java.io.FileInputStream; |
| 15 | +import java.io.FileNotFoundException; |
14 | 16 | import java.io.IOException; |
15 | 17 | import java.security.KeyStore; |
16 | 18 | import java.security.KeyStoreException; |
17 | 19 | import java.security.NoSuchAlgorithmException; |
| 20 | +import java.security.PrivateKey; |
18 | 21 | import java.security.Security; |
19 | 22 | import java.security.UnrecoverableEntryException; |
| 23 | +import java.security.UnrecoverableKeyException; |
20 | 24 | import java.security.cert.CertificateException; |
21 | 25 | import java.security.cert.X509Certificate; |
22 | 26 | import java.util.Collections; |
@@ -239,4 +243,117 @@ public static Document createSignedDoc(Document workingDocument,String merchantI |
239 | 243 | throw new SignException(e.getMessage()); |
240 | 244 | } |
241 | 245 | } |
| 246 | + |
| 247 | + |
| 248 | + public static void readJdkCert(MerchantConfig merchantConfig, Logger logger) throws SignEncryptException, SignException{ |
| 249 | + KeyStore keystore=null; |
| 250 | + |
| 251 | + String path=merchantConfig.getKeysDirectory()+"/"+merchantConfig.getKeyFilename(); |
| 252 | + String pass=merchantConfig.getKeyPassword(); |
| 253 | + |
| 254 | + if (merchantConfig.getcacert()){ |
| 255 | + path = System.getProperty("java.home") + "/jre/lib/security/cacerts".replace('/', File.separatorChar); |
| 256 | + getAlias(path, merchantConfig,logger); |
| 257 | + |
| 258 | + } |
| 259 | + |
| 260 | + else{ |
| 261 | + try{ |
| 262 | + FileInputStream is = new FileInputStream(path); |
| 263 | + keystore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 264 | + keystore.load(is, pass.toCharArray()); |
| 265 | + } |
| 266 | + catch(Exception e){ |
| 267 | + System.out.println("exception "+e.getMessage()); |
| 268 | + e.printStackTrace(); |
| 269 | + } |
| 270 | + |
| 271 | + |
| 272 | + String merchantKeyAlias = null; |
| 273 | + try { |
| 274 | + Enumeration enumKeyStore = keystore.aliases(); |
| 275 | + while (enumKeyStore.hasMoreElements()) { |
| 276 | + KeyStore.PrivateKeyEntry keyEntry = null; |
| 277 | + merchantKeyAlias = (String) enumKeyStore.nextElement(); |
| 278 | + if (merchantKeyAlias.contains(merchantConfig.getKeyAlias())){ |
| 279 | + try { |
| 280 | + keyEntry = (KeyStore.PrivateKeyEntry) keystore.getEntry |
| 281 | + (merchantKeyAlias, new KeyStore.PasswordProtection(merchantConfig.getKeyPassword().toCharArray())); |
| 282 | + } catch (NoSuchAlgorithmException e) { |
| 283 | + logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'"); |
| 284 | + throw new SignException(e); |
| 285 | + } catch (UnrecoverableEntryException e) { |
| 286 | + logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'"); |
| 287 | + throw new SignException(e); |
| 288 | + } catch (KeyStoreException e) { |
| 289 | + logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'"); |
| 290 | + throw new SignException(e); |
| 291 | + } |
| 292 | + |
| 293 | + Identity identity = new Identity(merchantConfig,(X509Certificate) keyEntry.getCertificate(),keyEntry.getPrivateKey()); |
| 294 | + localKeyStoreHandler.addIdentityToKeyStore(identity, logger); |
| 295 | + identities.put(identity.getName(), identity); |
| 296 | + continue; |
| 297 | + } |
| 298 | + Identity identity = new Identity(merchantConfig, (X509Certificate) keystore.getCertificate(merchantKeyAlias)); |
| 299 | + localKeyStoreHandler.addIdentityToKeyStore(identity, logger); |
| 300 | + identities.put(identity.getName(), identity); |
| 301 | + } |
| 302 | + } catch (KeyStoreException e) { |
| 303 | + logger.log(Logger.LT_EXCEPTION, "Exception while obtaining private key from KeyStore with alias, '" + merchantConfig.getKeyAlias() + "'"); |
| 304 | + throw new SignException(e); |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + private static java.security.cert.Certificate getAlias(String keystore_location, MerchantConfig merchantConfig,Logger logger) throws SignException, SignEncryptException{ |
| 310 | + FileInputStream is = null; |
| 311 | + try { |
| 312 | + File file = new File(keystore_location); |
| 313 | + is = new FileInputStream(file); |
| 314 | + KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 315 | + String password = merchantConfig.getcacertpassword(); |
| 316 | + keystore.load(is, password.toCharArray()); |
| 317 | + |
| 318 | + Identity identity; |
| 319 | + |
| 320 | + java.security.cert.Certificate[] cert = keystore.getCertificateChain(merchantConfig.getKeyAlias()); |
| 321 | + PrivateKey key = null; |
| 322 | + try { |
| 323 | + key = (PrivateKey)keystore.getKey(merchantConfig.getKeyAlias(), merchantConfig.getKeyAlias().toCharArray()); |
| 324 | + } catch (UnrecoverableKeyException e) { |
| 325 | + e.printStackTrace(); |
| 326 | + } |
| 327 | + identity = new Identity(merchantConfig,(X509Certificate) cert[0],key); |
| 328 | + localKeyStoreHandler.addIdentityToKeyStore(identity, logger); |
| 329 | + identities.put(identity.getName(), identity); |
| 330 | + |
| 331 | + |
| 332 | + identity = new Identity(merchantConfig, (X509Certificate) cert[1]); |
| 333 | + localKeyStoreHandler.addIdentityToKeyStore(identity, logger); |
| 334 | + identities.put(identity.getName(), identity); |
| 335 | + } |
| 336 | + |
| 337 | + catch (java.security.cert.CertificateException e) { |
| 338 | + e.printStackTrace(); |
| 339 | + } catch (NoSuchAlgorithmException e) { |
| 340 | + e.printStackTrace(); |
| 341 | + } catch (FileNotFoundException e) { |
| 342 | + e.printStackTrace(); |
| 343 | + } catch (KeyStoreException e) { |
| 344 | + e.printStackTrace(); |
| 345 | + } catch (IOException e) { |
| 346 | + e.printStackTrace(); |
| 347 | + }finally { |
| 348 | + if(null != is) |
| 349 | + try { |
| 350 | + is.close(); |
| 351 | + } catch (IOException e) { |
| 352 | + // TODO Auto-generated catch block |
| 353 | + e.printStackTrace(); |
| 354 | + } |
| 355 | + } |
| 356 | + |
| 357 | + return null; |
| 358 | + } |
242 | 359 | } |
0 commit comments