Skip to content

Commit

Permalink
Fix Cert REST api v2 output format
Browse files Browse the repository at this point in the history
  • Loading branch information
fmarco76 committed Dec 11, 2023
1 parent dd3b909 commit 1ea0985
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,9 @@ public Enumeration<CertRecord> searchCertificates(String filter, int maxSize,
* {@Code (&(certRecordId=5)(x509Cert.notBefore=934398398))}
*
* @param filter search filter
* @param maxSize max size to return
* @param timeLimit timeout value
* @param start first entry to return from the list
* @param size max size to return
* @return a list of certificates
* @exception EBaseException failed to search
*/
Expand All @@ -1133,7 +1135,7 @@ public Iterator<CertRecord> searchCertificates(String filter, int timeLimit, int
ArrayList<CertRecord> records = new ArrayList<>();
logger.debug("searchCertificates filter {filter}, start {start} and size {size}", filter, start, size);
try (DBSSession s = dbSubsystem.createSession()) {
DBSearchResults sr = s.pagedSearch(mBaseDN, filter, start, size);
DBSearchResults sr = s.pagedSearch(mBaseDN, filter, start, size, timeLimit);
while (sr.hasMoreElements()) {
records.add((CertRecord) sr.nextElement());
}
Expand Down
21 changes: 9 additions & 12 deletions base/ca/src/main/java/org/dogtagpki/server/ca/v2/CertServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.security.Principal;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
Expand All @@ -26,12 +27,10 @@

import org.dogtagpki.server.ca.CAEngine;
import org.dogtagpki.server.ca.CAServlet;
import org.dogtagpki.util.cert.CertUtil;
import org.mozilla.jss.netscape.security.pkcs.ContentInfo;
import org.mozilla.jss.netscape.security.pkcs.PKCS7;
import org.mozilla.jss.netscape.security.pkcs.SignerInfo;
import org.mozilla.jss.netscape.security.provider.RSAPublicKey;
import org.mozilla.jss.netscape.security.util.CertPrettyPrint;
import org.mozilla.jss.netscape.security.util.Utils;
import org.mozilla.jss.netscape.security.x509.AlgorithmId;
import org.mozilla.jss.netscape.security.x509.CRLExtensions;
Expand Down Expand Up @@ -138,11 +137,9 @@ private CertData getCertData(CertId id, Locale loc) throws Exception {
Principal subjectDN = cert.getSubjectName();
if (subjectDN != null) certData.setSubjectDN(subjectDN.toString());

String base64 = CertUtil.toPEM(cert);
certData.setEncoded(base64);
String base64 = Utils.base64encode(cert.getEncoded(), true);

CertPrettyPrint print = new CertPrettyPrint(cert);
certData.setPrettyPrint(print.toString(loc));
certData.setEncoded(base64);

X509Certificate[] certChain = engine.getCertChain(cert);

Expand All @@ -159,11 +156,12 @@ private CertData getCertData(CertId id, Locale loc) throws Exception {
String p7Str = Utils.base64encode(p7Bytes, true);
certData.setPkcs7CertChain(p7Str);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
Date notBefore = cert.getNotBefore();
if (notBefore != null) certData.setNotBefore(notBefore.toString());
if (notBefore != null) certData.setNotBefore(sdf.format(notBefore));

Date notAfter = cert.getNotAfter();
if (notAfter != null) certData.setNotAfter(notAfter.toString());
if (notAfter != null) certData.setNotAfter(sdf.format(notAfter));

certData.setRevokedOn(certRecord.getRevokedOn());
certData.setRevokedBy(certRecord.getRevokedBy());
Expand All @@ -177,7 +175,7 @@ private CertData getCertData(CertId id, Locale loc) throws Exception {
revExts.get(CRLReasonExtension.NAME);
certData.setRevocationReason(ext.getReason().getCode());
} catch (X509ExtensionException e) {
// nothing to do
logger.debug("CRL extension error for certificate {}", id.toHexString());
}
}
}
Expand Down Expand Up @@ -216,9 +214,8 @@ private CertDataInfos listCerts(CertSearchRequest searchReq, int maxTime, int st
results.add(createCertDataInfo(rec));
}

int total = results.size();
logger.info("Search results: " + total);
infos.setTotal(total);
infos.setTotal(results.size());
logger.info("Search results: " + results.size());
infos.setEntries(results);
} catch (Exception e) {
logger.error("Unable to list certificates: " + e.getMessage(), e);
Expand Down

0 comments on commit 1ea0985

Please sign in to comment.