Search in sources :

Example 6 with CertificateInformationDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO in project carbon-apimgt by wso2.

the class CertificateMappingUtil method fromCertificateInformationToDTO.

/**
 * To convert Instance of {@link CertificateInformationDTO} to {@link CertificateInfoDTO};
 *
 * @param certificateInformationDTO Instance of {@link CertificateInformationDTO}
 * @return converted instance of {@link CertificateInfoDTO}.
 */
public static CertificateInfoDTO fromCertificateInformationToDTO(CertificateInformationDTO certificateInformationDTO) {
    CertificateValidityDTO certificateValidityDTO = new CertificateValidityDTO();
    certificateValidityDTO.setFrom(certificateInformationDTO.getFrom());
    certificateValidityDTO.setTo(certificateInformationDTO.getTo());
    CertificateInfoDTO certificateInfoDTO = new CertificateInfoDTO();
    certificateInfoDTO.setValidity(certificateValidityDTO);
    certificateInfoDTO.setStatus(certificateInformationDTO.getStatus());
    certificateInfoDTO.setSubject(certificateInformationDTO.getSubject());
    certificateInfoDTO.setVersion(certificateInformationDTO.getVersion());
    return certificateInfoDTO;
}
Also used : CertificateInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertificateInfoDTO) CertificateValidityDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertificateValidityDTO)

Example 7 with CertificateInformationDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO 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 8 with CertificateInformationDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO in project carbon-apimgt by wso2.

the class CertificateMgtUtils method getCertificateInfo.

/**
 * To get the certificate information from base64 encoded certificate.
 *
 * @param base64EncodedCertificate Base 64 encoded certificate.
 * @return Certificate information.
 */
public CertificateInformationDTO getCertificateInfo(String base64EncodedCertificate) {
    CertificateInformationDTO certificateInformationDTO = null;
    try {
        byte[] cert = (Base64.decodeBase64(base64EncodedCertificate.getBytes(StandardCharsets.UTF_8)));
        InputStream serverCert = new ByteArrayInputStream(cert);
        if (serverCert.available() == 0) {
            log.error("Provided certificate is empty for getting certificate information. Hence please provide a " + "non-empty certificate to overcome this issue.");
        }
        CertificateFactory cf = CertificateFactory.getInstance(certificateType);
        while (serverCert.available() > 0) {
            Certificate certificate = cf.generateCertificate(serverCert);
            certificateInformationDTO = getCertificateMetaData((X509Certificate) certificate);
        }
    } catch (IOException | CertificateException e) {
        log.error("Error while getting the certificate information from the certificate", e);
    }
    return certificateInformationDTO;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CertificateInformationDTO(org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 9 with CertificateInformationDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO in project carbon-apimgt by wso2.

the class CertificateMgtUtils method getCertificateMetaData.

/**
 * To get the certificate meta data information such as version expiry data
 *
 * @param certificate Relevant certificate to get certificate meta data information.
 * @return Certificate meta data information.
 */
private CertificateInformationDTO getCertificateMetaData(X509Certificate certificate) {
    CertificateInformationDTO certificateInformation = new CertificateInformationDTO();
    certificateInformation.setStatus(certificate.getNotAfter().getTime() > System.currentTimeMillis() ? "Active" : "Expired");
    certificateInformation.setFrom(certificate.getNotBefore().toString());
    certificateInformation.setTo(certificate.getNotAfter().toString());
    certificateInformation.setSubject(certificate.getSubjectDN().toString());
    certificateInformation.setVersion(String.valueOf(certificate.getVersion()));
    return certificateInformation;
}
Also used : CertificateInformationDTO(org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO)

Aggregations

CertificateInformationDTO (org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO)8 CertificateInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertificateInfoDTO)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 CertificateException (java.security.cert.CertificateException)2 X509Certificate (java.security.cert.X509Certificate)2 Test (org.junit.Test)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 CertificateValidityDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertificateValidityDTO)2 File (java.io.File)1 KeyStore (java.security.KeyStore)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Certificate (java.security.cert.Certificate)1 CertificateFactory (java.security.cert.CertificateFactory)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 ClientCertificateDTO (org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO)1