Search in sources :

Example 1 with CertStoreType

use of org.xipki.ca.dbtool.jaxb.ca.CertStoreType in project xipki by xipki.

the class CaCertStoreDbExporter method export.

@SuppressWarnings("unchecked")
public void export() throws Exception {
    CertStoreType certstore;
    if (resume) {
        JAXBElement<CertStoreType> root;
        try {
            root = (JAXBElement<CertStoreType>) unmarshaller.unmarshal(new File(baseDir, FILENAME_CA_CERTSTORE));
        } catch (JAXBException ex) {
            throw XmlUtil.convert(ex);
        }
        certstore = root.getValue();
        if (certstore.getVersion() > VERSION) {
            throw new InvalidInputException("could not continue with CertStore greater than " + VERSION + ": " + certstore.getVersion());
        }
    } else {
        certstore = new CertStoreType();
        certstore.setVersion(VERSION);
    }
    Exception exception = null;
    System.out.println("exporting CA certstore from database");
    try {
        if (!resume) {
            exportPublishQueue(certstore);
            exportDeltaCrlCache(certstore);
        }
        File processLogFile = new File(baseDir, DbPorter.EXPORT_PROCESS_LOG_FILENAME);
        Long idProcessedInLastProcess = null;
        CaDbEntryType typeProcessedInLastProcess = null;
        if (processLogFile.exists()) {
            byte[] content = IoUtil.read(processLogFile);
            if (content != null && content.length > 0) {
                String str = new String(content);
                int idx = str.indexOf(':');
                String typeName = str.substring(0, idx).trim();
                typeProcessedInLastProcess = CaDbEntryType.valueOf(typeName);
                idProcessedInLastProcess = Long.parseLong(str.substring(idx + 1).trim());
            }
        }
        if (CaDbEntryType.CRL == typeProcessedInLastProcess || typeProcessedInLastProcess == null) {
            exception = exportEntries(CaDbEntryType.CRL, certstore, processLogFile, idProcessedInLastProcess);
            typeProcessedInLastProcess = null;
            idProcessedInLastProcess = null;
        }
        CaDbEntryType[] types = { CaDbEntryType.CERT, CaDbEntryType.REQUEST, CaDbEntryType.REQCERT };
        for (CaDbEntryType type : types) {
            if (exception == null && (type == typeProcessedInLastProcess || typeProcessedInLastProcess == null)) {
                exception = exportEntries(type, certstore, processLogFile, idProcessedInLastProcess);
                typeProcessedInLastProcess = null;
                idProcessedInLastProcess = null;
            }
        }
        JAXBElement<CertStoreType> root = new ObjectFactory().createCertStore(certstore);
        try {
            marshaller.marshal(root, new File(baseDir + File.separator + FILENAME_CA_CERTSTORE));
        } catch (JAXBException ex) {
            throw XmlUtil.convert(ex);
        }
    } catch (Exception ex) {
        System.err.println("could not export CA certstore from database");
        exception = ex;
    }
    if (exception == null) {
        System.out.println(" exported CA certstore from database");
    } else {
        throw exception;
    }
}
Also used : InvalidInputException(org.xipki.dbtool.InvalidInputException) JAXBException(javax.xml.bind.JAXBException) DEROctetString(org.bouncycastle.asn1.DEROctetString) XMLStreamException(javax.xml.stream.XMLStreamException) DataAccessException(org.xipki.datasource.DataAccessException) JAXBException(javax.xml.bind.JAXBException) CRLException(java.security.cert.CRLException) InvalidInputException(org.xipki.dbtool.InvalidInputException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ObjectFactory(org.xipki.ca.dbtool.jaxb.ca.ObjectFactory) CertStoreType(org.xipki.ca.dbtool.jaxb.ca.CertStoreType) File(java.io.File)

Example 2 with CertStoreType

use of org.xipki.ca.dbtool.jaxb.ca.CertStoreType in project xipki by xipki.

the class CaCertStoreDbImporter method importToDb.

public void importToDb() throws Exception {
    CertStoreType certstore;
    try {
        @SuppressWarnings("unchecked") JAXBElement<CertStoreType> root = (JAXBElement<CertStoreType>) unmarshaller.unmarshal(new File(baseDir, FILENAME_CA_CERTSTORE));
        certstore = root.getValue();
    } catch (JAXBException ex) {
        throw XmlUtil.convert(ex);
    }
    if (certstore.getVersion() > VERSION) {
        throw new Exception("could not import CertStore greater than " + VERSION + ": " + certstore.getVersion());
    }
    File processLogFile = new File(baseDir, DbPorter.IMPORT_PROCESS_LOG_FILENAME);
    System.out.println("importing CA certstore to database");
    try {
        if (!resume) {
            dropIndexes();
        }
        CaDbEntryType typeProcessedInLastProcess = null;
        Integer numProcessedInLastProcess = null;
        Long idProcessedInLastProcess = null;
        if (processLogFile.exists()) {
            byte[] content = IoUtil.read(processLogFile);
            if (content != null && content.length > 5) {
                String str = new String(content);
                StringTokenizer st = new StringTokenizer(str, ":");
                String type = st.nextToken();
                typeProcessedInLastProcess = CaDbEntryType.valueOf(type);
                numProcessedInLastProcess = Integer.parseInt(st.nextToken());
                idProcessedInLastProcess = Long.parseLong(st.nextToken());
            }
        }
        boolean entriesFinished = false;
        // finished for the given type
        if (typeProcessedInLastProcess != null && (idProcessedInLastProcess != null && idProcessedInLastProcess == -1)) {
            numProcessedInLastProcess = 0;
            idProcessedInLastProcess = 0L;
            switch(typeProcessedInLastProcess) {
                case CRL:
                    typeProcessedInLastProcess = CaDbEntryType.CERT;
                    break;
                case CERT:
                    typeProcessedInLastProcess = CaDbEntryType.REQUEST;
                    break;
                case REQUEST:
                    typeProcessedInLastProcess = CaDbEntryType.REQCERT;
                    break;
                case REQCERT:
                    entriesFinished = true;
                    break;
                default:
                    throw new RuntimeException("unsupported CaDbEntryType " + typeProcessedInLastProcess);
            }
        }
        if (!entriesFinished) {
            Exception exception = null;
            if (CaDbEntryType.CRL == typeProcessedInLastProcess || typeProcessedInLastProcess == null) {
                exception = importEntries(CaDbEntryType.CRL, certstore, processLogFile, numProcessedInLastProcess, idProcessedInLastProcess);
                typeProcessedInLastProcess = null;
                numProcessedInLastProcess = null;
                idProcessedInLastProcess = null;
            }
            CaDbEntryType[] types = { CaDbEntryType.CERT, CaDbEntryType.REQUEST, CaDbEntryType.REQCERT };
            for (CaDbEntryType type : types) {
                if (exception == null && (type == typeProcessedInLastProcess || typeProcessedInLastProcess == null)) {
                    exception = importEntries(type, certstore, processLogFile, numProcessedInLastProcess, idProcessedInLastProcess);
                }
            }
            if (exception != null) {
                throw exception;
            }
        }
        importPublishQueue(certstore.getPublishQueue());
        importDeltaCrlCache(certstore.getDeltaCRLCache());
        recoverIndexes();
        processLogFile.delete();
    } catch (Exception ex) {
        System.err.println("could not import CA certstore to database");
        throw ex;
    }
    System.out.println(" imported CA certstore to database");
}
Also used : JAXBException(javax.xml.bind.JAXBException) JAXBElement(javax.xml.bind.JAXBElement) DEROctetString(org.bouncycastle.asn1.DEROctetString) XMLStreamException(javax.xml.stream.XMLStreamException) DataAccessException(org.xipki.datasource.DataAccessException) JAXBException(javax.xml.bind.JAXBException) InvalidDataObjectException(org.xipki.ca.dbtool.xmlio.InvalidDataObjectException) CRLException(java.security.cert.CRLException) SQLException(java.sql.SQLException) CertificateException(java.security.cert.CertificateException) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) BigInteger(java.math.BigInteger) StringTokenizer(java.util.StringTokenizer) CertStoreType(org.xipki.ca.dbtool.jaxb.ca.CertStoreType) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 3 with CertStoreType

use of org.xipki.ca.dbtool.jaxb.ca.CertStoreType in project xipki by xipki.

the class OcspCertStoreFromCaDbImporter method importToDb.

public void importToDb() throws Exception {
    CertStoreType certstore;
    try {
        @SuppressWarnings("unchecked") JAXBElement<CertStoreType> root = (JAXBElement<CertStoreType>) unmarshaller.unmarshal(new File(baseDir, FILENAME_CA_CERTSTORE));
        certstore = root.getValue();
    } catch (JAXBException ex) {
        throw XmlUtil.convert(ex);
    }
    if (certstore.getVersion() > VERSION) {
        throw new InvalidInputException("could not import CertStore greater than " + VERSION + ": " + certstore.getVersion());
    }
    CAConfigurationType caConf;
    try {
        File file = new File(baseDir + File.separator + FILENAME_CA_CONFIGURATION);
        @SuppressWarnings("unchecked") JAXBElement<CAConfigurationType> rootCaConf = (JAXBElement<CAConfigurationType>) unmarshaller.unmarshal(file);
        caConf = rootCaConf.getValue();
    } catch (JAXBException ex) {
        throw XmlUtil.convert(ex);
    }
    if (caConf.getVersion() > VERSION) {
        throw new InvalidInputException("could not import CA Configuration greater than " + VERSION + ": " + certstore.getVersion());
    }
    System.out.println("importing CA certstore to OCSP database");
    try {
        if (!resume) {
            dropIndexes();
        }
        PublisherType publisherType = null;
        for (PublisherType type : caConf.getPublishers().getPublisher()) {
            if (publisherName.equals(type.getName())) {
                publisherType = type;
                break;
            }
        }
        if (publisherType == null) {
            throw new InvalidInputException("unknown publisher " + publisherName);
        }
        String type = publisherType.getType();
        if (!"ocsp".equalsIgnoreCase(type)) {
            throw new InvalidInputException("Unkwown publisher type " + type);
        }
        ConfPairs confPairs = new ConfPairs(value(publisherType.getConf()));
        String str = confPairs.value("publish.goodcerts");
        boolean revokedOnly = false;
        if (str != null) {
            revokedOnly = !Boolean.parseBoolean(str);
        }
        Set<Integer> relatedCaIds = new HashSet<>();
        for (CaHasPublisherType ctype : caConf.getCaHasPublishers().getCaHasPublisher()) {
            if (ctype.getPublisherId() == publisherType.getId()) {
                relatedCaIds.add(ctype.getCaId());
            }
        }
        List<CaType> relatedCas = new LinkedList<>();
        for (CaType m : caConf.getCas().getCa()) {
            if (relatedCaIds.contains(m.getId())) {
                relatedCas.add(m);
            }
        }
        if (relatedCas.isEmpty()) {
            System.out.println("No CA has publisher " + publisherName);
            return;
        }
        Map<Integer, String> profileMap = new HashMap<Integer, String>();
        for (ProfileType ni : caConf.getProfiles().getProfile()) {
            profileMap.put(ni.getId(), ni.getName());
        }
        List<Integer> relatedCertStoreCaIds = resume ? getIssuerIds(relatedCas) : importIssuer(relatedCas);
        File processLogFile = new File(baseDir, DbPorter.IMPORT_TO_OCSP_PROCESS_LOG_FILENAME);
        importCert(certstore, profileMap, revokedOnly, relatedCertStoreCaIds, processLogFile);
        recoverIndexes();
        processLogFile.delete();
    } catch (Exception ex) {
        System.err.println("could not import OCSP certstore to database");
        throw ex;
    }
    System.out.println(" imported OCSP certstore to database");
}
Also used : CaHasPublisherType(org.xipki.ca.dbtool.jaxb.ca.CaHasPublisherType) PublisherType(org.xipki.ca.dbtool.jaxb.ca.PublisherType) HashMap(java.util.HashMap) CaType(org.xipki.ca.dbtool.jaxb.ca.CaType) CertStoreType(org.xipki.ca.dbtool.jaxb.ca.CertStoreType) HashSet(java.util.HashSet) CAConfigurationType(org.xipki.ca.dbtool.jaxb.ca.CAConfigurationType) InvalidInputException(org.xipki.dbtool.InvalidInputException) ProfileType(org.xipki.ca.dbtool.jaxb.ca.ProfileType) JAXBException(javax.xml.bind.JAXBException) ConfPairs(org.xipki.common.ConfPairs) JAXBElement(javax.xml.bind.JAXBElement) LinkedList(java.util.LinkedList) InvalidInputException(org.xipki.dbtool.InvalidInputException) SQLException(java.sql.SQLException) DataAccessException(org.xipki.datasource.DataAccessException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) JAXBException(javax.xml.bind.JAXBException) CaHasPublisherType(org.xipki.ca.dbtool.jaxb.ca.CaHasPublisherType) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

File (java.io.File)3 SQLException (java.sql.SQLException)3 JAXBException (javax.xml.bind.JAXBException)3 CertStoreType (org.xipki.ca.dbtool.jaxb.ca.CertStoreType)3 DataAccessException (org.xipki.datasource.DataAccessException)3 IOException (java.io.IOException)2 CRLException (java.security.cert.CRLException)2 CertificateException (java.security.cert.CertificateException)2 ZipFile (java.util.zip.ZipFile)2 JAXBElement (javax.xml.bind.JAXBElement)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 DEROctetString (org.bouncycastle.asn1.DEROctetString)2 InvalidInputException (org.xipki.dbtool.InvalidInputException)2 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 StringTokenizer (java.util.StringTokenizer)1 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)1 CAConfigurationType (org.xipki.ca.dbtool.jaxb.ca.CAConfigurationType)1