Search in sources :

Example 1 with CertListInfo

use of org.xipki.ca.server.mgmt.api.CertListInfo in project xipki by xipki.

the class CertStoreQueryExecutor method listCertificates.

List<CertListInfo> listCertificates(NameId ca, X500Name subjectPattern, Date validFrom, Date validTo, CertListOrderBy orderBy, int numEntries) throws DataAccessException, OperationException {
    ParamUtil.requireNonNull("ca", ca);
    ParamUtil.requireMin("numEntries", numEntries, 1);
    StringBuilder sb = new StringBuilder(200);
    sb.append("SN,NBEFORE,NAFTER,SUBJECT FROM CERT WHERE CA_ID=?");
    // .append(caId)
    Integer idxNotBefore = null;
    Integer idxNotAfter = null;
    Integer idxSubject = null;
    int idx = 2;
    if (validFrom != null) {
        idxNotBefore = idx++;
        sb.append(" AND NBEFORE<?");
    }
    if (validTo != null) {
        idxNotAfter = idx++;
        sb.append(" AND NAFTER>?");
    }
    String subjectLike = null;
    if (subjectPattern != null) {
        idxSubject = idx++;
        sb.append(" AND SUBJECT LIKE ?");
        StringBuilder buffer = new StringBuilder(100);
        buffer.append("%");
        RDN[] rdns = subjectPattern.getRDNs();
        for (int i = 0; i < rdns.length; i++) {
            X500Name rdnName = new X500Name(new RDN[] { rdns[i] });
            String rdnStr = X509Util.getRfc4519Name(rdnName);
            if (rdnStr.indexOf('%') != -1) {
                throw new OperationException(ErrorCode.BAD_REQUEST, "the character '%' is not allowed in subjectPattern");
            }
            if (rdnStr.indexOf('*') != -1) {
                rdnStr = rdnStr.replace('*', '%');
            }
            buffer.append(rdnStr);
            buffer.append("%");
        }
        subjectLike = buffer.toString();
    }
    String sortByStr = null;
    if (orderBy != null) {
        switch(orderBy) {
            case NOT_BEFORE:
                sortByStr = "NBEFORE";
                break;
            case NOT_BEFORE_DESC:
                sortByStr = "NBEFORE DESC";
                break;
            case NOT_AFTER:
                sortByStr = "NAFTER";
                break;
            case NOT_AFTER_DESC:
                sortByStr = "NAFTER DESC";
                break;
            case SUBJECT:
                sortByStr = "SUBJECT";
                break;
            case SUBJECT_DESC:
                sortByStr = "SUBJECT DESC";
                break;
            default:
                throw new RuntimeException("unknown CertListOrderBy " + orderBy);
        }
    }
    final String sql = datasource.buildSelectFirstSql(numEntries, sortByStr, sb.toString());
    ResultSet rs = null;
    PreparedStatement ps = borrowPreparedStatement(sql);
    try {
        ps.setInt(1, ca.getId());
        if (idxNotBefore != null) {
            long time = validFrom.getTime() / 1000;
            ps.setLong(idxNotBefore, time - 1);
        }
        if (idxNotAfter != null) {
            long time = validTo.getTime() / 1000;
            ps.setLong(idxNotAfter, time);
        }
        if (idxSubject != null) {
            ps.setString(idxSubject, subjectLike);
        }
        rs = ps.executeQuery();
        List<CertListInfo> ret = new LinkedList<>();
        while (rs.next()) {
            String snStr = rs.getString("SN");
            BigInteger sn = new BigInteger(snStr, 16);
            Date notBefore = new Date(rs.getLong("NBEFORE") * 1000);
            Date notAfter = new Date(rs.getLong("NAFTER") * 1000);
            String subject = rs.getString("SUBJECT");
            CertListInfo info = new CertListInfo(sn, subject, notBefore, notAfter);
            ret.add(info);
        }
        return ret;
    } catch (SQLException ex) {
        throw datasource.translate(sql, ex);
    } finally {
        releaseDbResources(ps, rs);
    }
}
Also used : SQLException(java.sql.SQLException) CertListInfo(org.xipki.ca.server.mgmt.api.CertListInfo) PreparedStatement(java.sql.PreparedStatement) DERPrintableString(org.bouncycastle.asn1.DERPrintableString) DEROctetString(org.bouncycastle.asn1.DEROctetString) X500Name(org.bouncycastle.asn1.x500.X500Name) LinkedList(java.util.LinkedList) Date(java.util.Date) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) BigInteger(java.math.BigInteger) ResultSet(java.sql.ResultSet) BigInteger(java.math.BigInteger) RDN(org.bouncycastle.asn1.x500.RDN) OperationException(org.xipki.ca.api.OperationException)

Example 2 with CertListInfo

use of org.xipki.ca.server.mgmt.api.CertListInfo in project xipki by xipki.

the class ListCertCmd method execute0.

/**
 * TODO.
 * @return comma-separated serial numbers (in hex).
 */
@Override
protected Object execute0() throws Exception {
    Date validFrom = getDate(validFromS);
    Date validTo = getDate(validToS);
    X500Name subjectPattern = null;
    if (StringUtil.isNotBlank(subjectPatternS)) {
        subjectPattern = new X500Name(subjectPatternS);
    }
    CertListOrderBy orderBy = null;
    if (orderByS != null) {
        orderBy = CertListOrderBy.forValue(orderByS);
        if (orderBy == null) {
            throw new IllegalCmdParamException("invalid order '" + orderByS + "'");
        }
    }
    List<CertListInfo> certInfos = caManager.listCertificates(caName, subjectPattern, validFrom, validTo, orderBy, num);
    final int n = certInfos.size();
    if (n == 0) {
        println("found no certificate");
        return null;
    }
    println("     | serial               | notBefore      | notAfter       | subject");
    println("-----+----------------------+----------------+----------------+-----------------");
    for (int i = 0; i < n; i++) {
        CertListInfo info = certInfos.get(i);
        println(format(i + 1, info));
    }
    return null;
}
Also used : CertListOrderBy(org.xipki.ca.server.mgmt.api.CertListOrderBy) IllegalCmdParamException(org.xipki.console.karaf.IllegalCmdParamException) CertListInfo(org.xipki.ca.server.mgmt.api.CertListInfo) X500Name(org.bouncycastle.asn1.x500.X500Name) Date(java.util.Date)

Aggregations

Date (java.util.Date)2 X500Name (org.bouncycastle.asn1.x500.X500Name)2 CertListInfo (org.xipki.ca.server.mgmt.api.CertListInfo)2 BigInteger (java.math.BigInteger)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)1 DEROctetString (org.bouncycastle.asn1.DEROctetString)1 DERPrintableString (org.bouncycastle.asn1.DERPrintableString)1 RDN (org.bouncycastle.asn1.x500.RDN)1 OperationException (org.xipki.ca.api.OperationException)1 CertListOrderBy (org.xipki.ca.server.mgmt.api.CertListOrderBy)1 IllegalCmdParamException (org.xipki.console.karaf.IllegalCmdParamException)1