Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOSIP-38439: p7b file download error for some cert-id #337

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public enum PartnerCertManagerErrorConstants {
CA_CERT_ID_NOT_FOUND("KER-PMS-019", "CA Certificate not found for the given ID."),

FUTURE_DATED_CERT_NOT_ALLOWED("KER-PMS-020", "Future Dated Certificate not allowed to upload."),

P7B_CONVERSION_ERROR("KER-KMS-021", "Failed to create p7b file format."),
;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.*;

import javax.security.auth.x500.X500Principal;

Expand Down Expand Up @@ -253,7 +250,7 @@ public static String buildP7BCertificateChain(List<? extends Certificate> certLi
}

public static String buildp7bFile(Certificate[] chain) {
return buildCertChain(chain);
return buildCertChainWithPKCS7(chain);
}

private static String buildCertChain(Certificate[] chain) {
Expand All @@ -274,6 +271,33 @@ private static String buildCertChain(Certificate[] chain) {
}
}

public static String buildCertChainWithPKCS7(Certificate[] chain) {
try {
CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
JcaCertStore jcaStore = new JcaCertStore(Arrays.asList(chain));
generator.addCertificates(jcaStore);

CMSTypedData cmsTypedData = new CMSAbsentContent();
CMSSignedData cmsSignedData = generator.generate(cmsTypedData);

byte[] encodedData = cmsSignedData.getEncoded();
String base64Encoded = Base64.getEncoder().encodeToString(encodedData);

StringBuilder pkcs7Formatted = new StringBuilder();
pkcs7Formatted.append("-----BEGIN PKCS7-----\n");
pkcs7Formatted.append(base64Encoded.replaceAll("(.{64})", "$1\n"));
pkcs7Formatted.append("\n-----END PKCS7-----");

return pkcs7Formatted.toString();
} catch (CertificateEncodingException | CMSException | IOException e) {
LOGGER.error(PartnerCertManagerConstants.SESSIONID, PartnerCertManagerConstants.GET_CA_CERT_TRUST,
PartnerCertManagerConstants.PCM_UTIL, "Error generating p7b certificates chain.");
throw new PartnerCertManagerException(
PartnerCertManagerErrorConstants.P7B_CONVERSION_ERROR.getErrorCode(),
PartnerCertManagerErrorConstants.P7B_CONVERSION_ERROR.getErrorMessage(), e);
}
}

public static String handleNullOrEmpty(String value) {
return (value == null || value.trim().isEmpty()) ? null : value;
}
Expand Down
Loading