From d5199dbd62c56d4f666401a86e6d18554eb570b8 Mon Sep 17 00:00:00 2001 From: Dilshan Edirisuriya Date: Tue, 1 Sep 2015 16:13:17 +0530 Subject: [PATCH] Fixing issues --- .../mgt/core/impl/CertificateGenerator.java | 18 ++++++- .../mgt/core/impl/KeyStoreReader.java | 51 ++++++++++++++++++- ...CertificateManagementServiceComponent.java | 3 +- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java index 34dfe941fa3..a1ddb3c20e8 100755 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java @@ -52,9 +52,9 @@ import org.jscep.transaction.Nonce; import org.jscep.transaction.TransactionId; import org.wso2.carbon.certificate.mgt.core.dto.CAStatus; import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse; -import org.wso2.carbon.certificate.mgt.core.util.ConfigurationUtil; import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import org.wso2.carbon.certificate.mgt.core.util.CommonUtil; +import org.wso2.carbon.certificate.mgt.core.util.ConfigurationUtil; import javax.security.auth.x500.X500Principal; import java.io.ByteArrayInputStream; @@ -292,9 +292,21 @@ public class CertificateGenerator { Date validityBeginDate = commonUtil.getValidityStartDate(); Date validityEndDate = commonUtil.getValidityEndDate(); + X500Name certSubject = request.getSubject(); + + if (certSubject == null) { + certSubject = new X500Name(ConfigurationUtil.DEFAULT_PRINCIPAL); + } else { + org.bouncycastle.asn1.x500.RDN[] rdn = certSubject.getRDNs(); + + if (rdn == null || rdn.length == 0) { + certSubject = new X500Name(ConfigurationUtil.DEFAULT_PRINCIPAL); + } + } + X509v3CertificateBuilder certificateBuilder = new X509v3CertificateBuilder( new X500Name(issueSubject), BigInteger.valueOf(System.currentTimeMillis()), - validityBeginDate, validityEndDate, request.getSubject(), request.getSubjectPublicKeyInfo()); + validityBeginDate, validityEndDate, certSubject, request.getSubjectPublicKeyInfo()); ContentSigner sigGen; X509Certificate issuedCert; @@ -461,6 +473,8 @@ public class CertificateGenerator { KeyStoreReader keyStoreReader = new KeyStoreReader(); KeyStore keyStore = keyStoreReader.loadCertificateKeyStore(); keyStore.setCertificateEntry(certificate.getSerialNumber().toString(), certificate); + + keyStoreReader.saveCertificateKeyStore(keyStore); } catch (KeyStoreException e) { String errorMsg = "KeySKeyStoreException occurred when saving the generated certificate"; log.error(errorMsg, e); diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java index 5c23eb7deae..f714a4746b2 100755 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java @@ -24,6 +24,7 @@ import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.security.KeyStore; @@ -62,7 +63,7 @@ public class KeyStoreReader { log.error(errorMsg, e); throw new KeystoreException(errorMsg, e); } catch (CertificateException e) { - String errorMsg = "Certificate expired when loading KeyStore"; + String errorMsg = "CertificateException when loading KeyStore"; log.error(errorMsg, e); throw new KeystoreException(errorMsg, e); } catch (IOException e) { @@ -82,11 +83,59 @@ public class KeyStoreReader { return keystore; } + private synchronized void saveKeyStore(KeyStore keyStore, String configEntryKeyStorePath, + String configEntryKeyStorePassword) throws KeystoreException { + + FileOutputStream outputStream = null; + + try { + outputStream = new FileOutputStream( + ConfigurationUtil.getConfigEntry(configEntryKeyStorePath)); + keyStore.store(outputStream, ConfigurationUtil.getConfigEntry(configEntryKeyStorePassword).toCharArray()); + outputStream.close(); + + } catch (KeyStoreException e) { + String errorMsg = "KeyStore issue occurred when loading KeyStore"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } catch (FileNotFoundException e) { + String errorMsg = "KeyStore file not found when loading KeyStore"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } catch (NoSuchAlgorithmException e) { + String errorMsg = "Algorithm not found when loading KeyStore"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } catch (CertificateException e) { + String errorMsg = "CertificateException when loading KeyStore"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } catch (IOException e) { + String errorMsg = "Input output issue occurred when loading KeyStore"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } finally { + try { + if (outputStream != null) { + outputStream.close(); + } + } catch (IOException e) { + log.error("Error closing KeyStore output stream", e); + } + } + } + + KeyStore loadCertificateKeyStore() throws KeystoreException { return loadKeyStore(ConfigurationUtil.CERTIFICATE_KEYSTORE, ConfigurationUtil.PATH_CERTIFICATE_KEYSTORE, ConfigurationUtil.CERTIFICATE_KEYSTORE_PASSWORD); } + void saveCertificateKeyStore(KeyStore keyStore) throws KeystoreException { + saveKeyStore(keyStore, ConfigurationUtil.PATH_CERTIFICATE_KEYSTORE, + ConfigurationUtil.CERTIFICATE_KEYSTORE_PASSWORD); + } + public Certificate getCACertificate() throws KeystoreException { KeyStore keystore = loadCertificateKeyStore(); diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/internal/CertificateManagementServiceComponent.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/internal/CertificateManagementServiceComponent.java index 5996028f7d6..ccf2f776498 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/internal/CertificateManagementServiceComponent.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/internal/CertificateManagementServiceComponent.java @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementService; import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementServiceImpl; /** @@ -38,7 +39,7 @@ public class CertificateManagementServiceComponent { } BundleContext bundleContext = componentContext.getBundleContext(); - bundleContext.registerService(CertificateManagementServiceImpl.class.getName(), + bundleContext.registerService(CertificateManagementService.class.getName(), CertificateManagementServiceImpl.getInstance(), null); if (log.isDebugEnabled()) {