Search in sources :

Example 21 with CertificateManagementException

use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.

the class CertificateMgtUtils method getCertificateInformation.

/**
 * Method to get the information of the certificate.
 *
 * @param alias : Alias of the certificate which information should be retrieved
 * @return : The details of the certificate as a MAP.
 */
public synchronized CertificateInformationDTO getCertificateInformation(String alias) throws CertificateManagementException {
    CertificateInformationDTO certificateInformation = new CertificateInformationDTO();
    File trustStoreFile = new File(trustStoreLocation);
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        try (InputStream localTrustStoreStream = new FileInputStream(trustStoreFile)) {
            trustStore.load(localTrustStoreStream, trustStorePassword);
        }
        if (trustStore.containsAlias(alias)) {
            X509Certificate certificate = (X509Certificate) trustStore.getCertificate(alias);
            certificateInformation = getCertificateMetaData(certificate);
        }
    } catch (IOException e) {
        throw new CertificateManagementException("Error wile loading the keystore.", e);
    } catch (CertificateException e) {
        throw new CertificateManagementException("Error loading the keystore from the stream.", e);
    } catch (NoSuchAlgorithmException e) {
        throw new CertificateManagementException("Could not find the algorithm to load the certificate.", e);
    } catch (KeyStoreException e) {
        throw new CertificateManagementException("Error reading certificate contents.", e);
    }
    return certificateInformation;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CertificateManagementException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException) CertificateInformationDTO(org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) File(java.io.File) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate)

Example 22 with CertificateManagementException

use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException in project carbon-apimgt by wso2.

the class CertificateMgtUtils method backupOriginalTrustStore.

public static void backupOriginalTrustStore() throws CertificateManagementException {
    try {
        TrustStoreDTO senderProfileTrustStore = getSenderProfileTrustStore();
        TrustStoreDTO listenerProfileTrustStore = getListenerProfileTrustStore();
        File srcFile = new File(senderProfileTrustStore.getLocation());
        if (senderProfileTrustStore.getLocation().equals(listenerProfileTrustStore.getLocation())) {
            String parent = srcFile.getParent();
            String destPath = parent + File.separator + COMMON_CERT_NAME;
            File destFile = new File(destPath);
            deletePreviousBackupJKSFile(destFile);
            FileUtils.copyFile(srcFile, destFile);
            updateSenderProfileTrustStoreLocation(destPath);
            updateListenerProfileTrustStoreLocation(destPath);
        } else {
            if (srcFile.exists()) {
                String parent = srcFile.getParent();
                String destPath = parent + File.separator + SENDER_PROFILE_JKS_NAME;
                File destFile = new File(destPath);
                deletePreviousBackupJKSFile(destFile);
                FileUtils.copyFile(srcFile, destFile);
                updateSenderProfileTrustStoreLocation(destPath);
            }
            File listenerProfileTrustStoreFile = new File(listenerProfileTrustStore.getLocation());
            if (listenerProfileTrustStoreFile.exists()) {
                String parent = listenerProfileTrustStoreFile.getParent();
                String destPath = parent + File.separator + LISTER_PROFILE_JKS_NAME;
                File destFile = new File(destPath);
                deletePreviousBackupJKSFile(destFile);
                FileUtils.copyFile(listenerProfileTrustStoreFile, destFile);
                updateListenerProfileTrustStoreLocation(destPath);
            }
        }
    } catch (XMLStreamException | IOException e) {
        throw new CertificateManagementException("Error while backup truststore", e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) TrustStoreDTO(org.wso2.carbon.apimgt.impl.dto.TrustStoreDTO) CertificateManagementException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException) IOException(java.io.IOException) File(java.io.File)

Aggregations

Test (org.junit.Test)11 CertificateMetadataDTO (org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO)9 CertificateManagementException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException)8 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 Connection (java.sql.Connection)5 SQLException (java.sql.SQLException)5 File (java.io.File)4 PreparedStatement (java.sql.PreparedStatement)4 ResponseCode (org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode)4 CertificateAliasExistsException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException)4 FileInputStream (java.io.FileInputStream)3 KeyStore (java.security.KeyStore)3 KeyStoreException (java.security.KeyStoreException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 CertificateException (java.security.cert.CertificateException)3 X509Certificate (java.security.cert.X509Certificate)3