|
30 | 30 | import org.jetbrains.annotations.NotNull;
|
31 | 31 | import org.w3c.dom.Document;
|
32 | 32 | import org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO;
|
| 33 | +import org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO; |
33 | 34 | import org.wso2.carbon.apimgt.impl.APIConstants;
|
34 | 35 | import org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode;
|
35 | 36 | import org.wso2.carbon.apimgt.impl.certificatemgt.TrustStoreUtils;
|
|
38 | 39 | import org.wso2.carbon.apimgt.impl.dto.TrustStoreDTO;
|
39 | 40 | import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
|
40 | 41 | import org.wso2.carbon.apimgt.impl.wsdl.util.SOAPToRESTConstants;
|
| 42 | +import org.wso2.carbon.base.MultitenantConstants; |
| 43 | +import org.wso2.carbon.context.CarbonContext; |
41 | 44 | import org.wso2.securevault.SecretResolver;
|
42 | 45 | import org.wso2.securevault.SecretResolverFactory;
|
43 | 46 | import org.wso2.securevault.commons.MiscellaneousUtil;
|
|
68 | 71 | import java.security.cert.CertificateFactory;
|
69 | 72 | import java.security.cert.X509Certificate;
|
70 | 73 | import java.util.Iterator;
|
| 74 | +import java.util.List; |
71 | 75 | import java.util.Optional;
|
72 | 76 | import javax.xml.namespace.QName;
|
73 | 77 | import javax.xml.parsers.DocumentBuilder;
|
@@ -954,4 +958,97 @@ public static Optional<X509Certificate> convert(Certificate cert) {
|
954 | 958 | }
|
955 | 959 | return Optional.ofNullable(null);
|
956 | 960 | }
|
| 961 | + |
| 962 | + public void deployTenantCertsToGatewaySenderInABatch(List<CertificateMetadataDTO> certificateMetadataDTOList) { |
| 963 | + //add cert to sender profile truststore |
| 964 | + try { |
| 965 | + TrustStoreDTO trustStoreDTO = getSenderProfileTrustStore(); |
| 966 | + addCertificatesToTrustStore(trustStoreDTO, certificateMetadataDTOList); |
| 967 | + } catch (FileNotFoundException | XMLStreamException e) { |
| 968 | + log.error("Error reading/writing to the truststore file.", e); |
| 969 | + } catch (CertificateManagementException e) { |
| 970 | + log.error("Error while storing certificates to the truststore file.", e); |
| 971 | + } |
| 972 | + } |
| 973 | + |
| 974 | + private void addCertificatesToTrustStore(TrustStoreDTO trustStoreDTO, |
| 975 | + List<CertificateMetadataDTO> certificateMetadataDTOList) throws CertificateManagementException { |
| 976 | + //Read the client-truststore.jks into a KeyStore. |
| 977 | + File trustStoreFile = new File(trustStoreDTO.getLocation()); |
| 978 | + int loggedInTenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); |
| 979 | + try (InputStream localTrustStoreStream = new FileInputStream(trustStoreFile)) { |
| 980 | + KeyStore trustStore = KeyStore.getInstance(trustStoreDTO.getType()); |
| 981 | + trustStore.load(localTrustStoreStream, trustStoreDTO.getPassword()); |
| 982 | + |
| 983 | + String base64Cert, alias; |
| 984 | + for (CertificateMetadataDTO dto : certificateMetadataDTOList) { |
| 985 | + base64Cert = dto.getCertificate(); |
| 986 | + alias = dto.getAlias(); |
| 987 | + if (loggedInTenantId != MultitenantConstants.SUPER_TENANT_ID) { |
| 988 | + alias = alias + "_" + loggedInTenantId; |
| 989 | + } |
| 990 | + |
| 991 | + byte[] cert = (Base64.decodeBase64(base64Cert.getBytes(StandardCharsets.UTF_8))); |
| 992 | + try (InputStream serverCert = new ByteArrayInputStream(cert)) { |
| 993 | + if (serverCert.available() == 0) { |
| 994 | + log.error("Certificate is empty for the provided alias " + alias + ". Hence skipping it. "); |
| 995 | + } |
| 996 | + |
| 997 | + CertificateFactory cf = CertificateFactory.getInstance(certificateType); |
| 998 | + while (serverCert.available() > 0) { |
| 999 | + Certificate certificate = cf.generateCertificate(serverCert); |
| 1000 | + //Check whether the Alias exists in the trust store. |
| 1001 | + if (trustStore.containsAlias(alias)) { |
| 1002 | + log.info("Provided certificate alias: " + alias + " already exists in the " + |
| 1003 | + "truststore."); |
| 1004 | + } else { |
| 1005 | + /* |
| 1006 | + * If alias is not exists, check whether the certificate is expired or not. If expired |
| 1007 | + * set the |
| 1008 | + * expired flag. |
| 1009 | + * */ |
| 1010 | + X509Certificate x509Certificate = (X509Certificate) certificate; |
| 1011 | + if (x509Certificate.getNotAfter().getTime() <= System.currentTimeMillis()) { |
| 1012 | + log.info("Provided certificate " + alias + " is expired."); |
| 1013 | + } else { |
| 1014 | + //If not expired add the certificate to trust store. |
| 1015 | + trustStore.setCertificateEntry(alias, certificate); |
| 1016 | + } |
| 1017 | + } |
| 1018 | + } |
| 1019 | + } catch (CertificateException | KeyStoreException e) { |
| 1020 | + String msg = "Error loading certificate."; |
| 1021 | + log.error(msg, e); |
| 1022 | + } |
| 1023 | + } |
| 1024 | + |
| 1025 | + try (OutputStream fileOutputStream = new FileOutputStream(trustStoreFile)) { |
| 1026 | + trustStore.store(fileOutputStream, trustStoreDTO.getPassword()); |
| 1027 | + } |
| 1028 | + } catch (CertificateException e) { |
| 1029 | + String msg = "Error storing certificate."; |
| 1030 | + log.error(msg, e); |
| 1031 | + throw new CertificateManagementException(msg); |
| 1032 | + } catch (FileNotFoundException e) { |
| 1033 | + String msg = "Error reading/ writing to the certificate file."; |
| 1034 | + log.error(msg, e); |
| 1035 | + throw new CertificateManagementException(msg); |
| 1036 | + } catch (NoSuchAlgorithmException e) { |
| 1037 | + String msg = "Could not find the algorithm to load the certificate."; |
| 1038 | + log.error(msg, e); |
| 1039 | + throw new CertificateManagementException(msg); |
| 1040 | + } catch (UnsupportedEncodingException e) { |
| 1041 | + String msg = "Error retrieving certificate from String."; |
| 1042 | + log.error(msg, e); |
| 1043 | + throw new CertificateManagementException(msg); |
| 1044 | + } catch (KeyStoreException e) { |
| 1045 | + String msg = "Error loading certificate."; |
| 1046 | + log.error(msg, e); |
| 1047 | + throw new CertificateManagementException(msg); |
| 1048 | + } catch (IOException e) { |
| 1049 | + String msg = "Error in loading the certificate."; |
| 1050 | + log.error(msg, e); |
| 1051 | + throw new CertificateManagementException(msg); |
| 1052 | + } |
| 1053 | + } |
957 | 1054 | }
|
0 commit comments