Package org.ejbca.core.model.ca.caadmin

Examples of org.ejbca.core.model.ca.caadmin.X509CAInfo


        ArrayList<ExtendedCAServiceInfo> extendedcaservices = new ArrayList<ExtendedCAServiceInfo>();
        extendedcaservices.add(new OCSPCAServiceInfo(ExtendedCAServiceInfo.STATUS_ACTIVE));
        extendedcaservices.add(new XKMSCAServiceInfo(ExtendedCAServiceInfo.STATUS_INACTIVE, "CN=XKMSCertificate, " + "CN=TEST", "", "1024",
            AlgorithmConstants.KEYALGORITHM_RSA));

        X509CAInfo cainfo = new X509CAInfo("CN=TESTFAIL", "TESTFAIL", SecConst.CA_ACTIVE, new Date(), "", SecConst.CERTPROFILE_FIXED_ROOTCA, 3650, null, // Expiretime
            CAInfo.CATYPE_X509, CAInfo.SELFSIGNED, (Collection<Certificate>) null, catokeninfo, "JUnit RSA CA", -1, null, null, // PolicyId
            24, // CRLPeriod
            0, // CRLIssueInterval
            10, // CRLOverlapTime
            10, // Delta CRL period
View Full Code Here


     * @param caname The name this CA-info will be assigned
     * @param catokeninfo The tokeninfo for this CA-info
     * @return The new X509CAInfo for testing.
     */
  private X509CAInfo getNewCAInfo(String caname, CATokenInfo catokeninfo) {
        cainfo = new X509CAInfo("CN="+caname,
            caname, SecConst.CA_ACTIVE, new Date(),
                "", SecConst.CERTPROFILE_FIXED_ROOTCA,
                365,
                new Date(System.currentTimeMillis()+364*24*3600*1000), // Expiretime
                CAInfo.CATYPE_X509,
View Full Code Here

        catokeninfo1.setSignKeyAlgorithm(AlgorithmConstants.KEYALGORITHM_RSA);
        catokeninfo1.setSignKeySpec("2048");
        catokeninfo1.setEncryptionAlgorithm(AlgorithmConstants.SIGALG_SHA1_WITH_RSA);
        catokeninfo1.setEncKeyAlgorithm(AlgorithmConstants.KEYALGORITHM_RSA);
        catokeninfo1.setEncKeySpec("2048");
        X509CAInfo cainfo1 = getNewCAInfo(caname1, catokeninfo1);

        // This CA uses DSA instead
        String caname2 = "TestExportRemoveRestoreCA2";
        SoftCATokenInfo catokeninfo2 = new SoftCATokenInfo();
        catokeninfo2.setSignatureAlgorithm(AlgorithmConstants.SIGALG_SHA1_WITH_DSA);
        catokeninfo2.setSignKeyAlgorithm(AlgorithmConstants.KEYALGORITHM_DSA);
        catokeninfo2.setSignKeySpec("1024");
        catokeninfo2.setEncryptionAlgorithm(AlgorithmConstants.SIGALG_SHA1_WITH_RSA);
        catokeninfo2.setEncKeyAlgorithm(AlgorithmConstants.KEYALGORITHM_RSA);
        catokeninfo2.setEncKeySpec("1024");
        X509CAInfo cainfo2 = getNewCAInfo(caname2, catokeninfo2);

        // This CA uses RSA but with 1024 bits
        String caname3 = "TestExportRemoveRestoreCA3";
        SoftCATokenInfo catokeninfo3 = new SoftCATokenInfo();
        catokeninfo3.setSignatureAlgorithm(AlgorithmConstants.SIGALG_SHA1_WITH_RSA);
        catokeninfo3.setSignKeyAlgorithm(AlgorithmConstants.KEYALGORITHM_RSA);
        catokeninfo3.setSignKeySpec("1024");
        catokeninfo3.setEncryptionAlgorithm(AlgorithmConstants.SIGALG_SHA1_WITH_RSA);
        catokeninfo3.setEncKeyAlgorithm(AlgorithmConstants.KEYALGORITHM_RSA);
        catokeninfo3.setEncKeySpec("1024");
        X509CAInfo cainfo3 = getNewCAInfo(caname3, catokeninfo3);

        // Remove CAs if they already exists
        try {
            caSession.removeCA(admin, cainfo1.getCAId());
        } catch (Exception ignored) {
        }
        try {
            caSession.removeCA(admin, cainfo2.getCAId());
        } catch (Exception ignored) {
        }
        try {
            caSession.removeCA(admin, cainfo3.getCAId());
        } catch (Exception ignored) {
        }

        // Create CAs
        try {
            caAdminSession.createCA(admin, cainfo1);
        } catch (Exception e) {
            log.error("createCA", e);
            fail("createCA: " + e.getMessage());
        }
        try {
            caAdminSession.createCA(admin, cainfo2);
        } catch (Exception e) {
            log.error("createCA", e);
            fail("createCA: " + e.getMessage());
        }
        try {
            caAdminSession.createCA(admin, cainfo3);
        } catch (Exception e) {
            log.error("createCA", e);
            fail("createCA: " + e.getMessage());
        }

        // Export keystores
        try {
            keystorebytes1 = caAdminSession.exportCAKeyStore(admin, caname1, capassword, capassword, "SignatureKeyAlias", "EncryptionKeyAlias");
        } catch (Exception e) {
            log.error("exportCAKeyStore", e);
            fail("exportCAKeyStore: " + e.getMessage());
        }
        try {
            keystorebytes2 = caAdminSession.exportCAKeyStore(admin, caname2, capassword, capassword, "SignatureKeyAlias", "EncryptionKeyAlias");
        } catch (Exception e) {
            log.error("exportCAKeyStore", e);
            fail("exportCAKeyStore: " + e.getMessage());
        }
        try {
            keystorebytes3 = caAdminSession.exportCAKeyStore(admin, caname3, capassword, capassword, "SignatureKeyAlias", "EncryptionKeyAlias");
        } catch (Exception e) {
            log.error("exportCAKeyStore", e);
            fail("exportCAKeyStore: " + e.getMessage());
        }

        // Remove keystore from CA1
        try {
            caAdminSession.removeCAKeyStore(admin, caname1);
        } catch (Exception e) {
            log.error("removeKeyStores", e);
            fail("removeKeyStores: " + e.getMessage());
        }

        // Try to restore with wrong keystore
        try {
            caAdminSession.restoreCAKeyStore(admin, caname1, keystorebytes2, capassword, capassword, "SignatureKeyAlias", "EncryptionKeyAlias");
            fail("Should not be possible to restore with a keystore with different parameters");
        } catch (Exception e) {
            // OK
        }
        try {
            caAdminSession.restoreCAKeyStore(admin, caname1, keystorebytes3, capassword, capassword, "SignatureKeyAlias", "EncryptionKeyAlias");
            fail("Should not be possible to restore with a keystore with different parameters");
        } catch (Exception e) {
            // OK
        }

        // Finally try with the right keystore to see that it works
        try {
            caAdminSession.restoreCAKeyStore(admin, caname1, keystorebytes1, capassword, capassword, "SignatureKeyAlias", "EncryptionKeyAlias");
        } catch (Exception e) {
            log.error("restoreCAKeyStore", e);
            fail("restoreCAKeyStore: " + e.getMessage());
        }

        // Clean up
        try {
            caSession.removeCA(admin, cainfo1.getCAId());
        } catch (Exception e) {
            log.error("removeCA", e);
            fail("removeCA: " + e.getMessage());
        }
        try {
            caSession.removeCA(admin, cainfo2.getCAId());
        } catch (Exception e) {
            log.error("removeCA", e);
            fail("removeCA: " + e.getMessage());
        }
        try {
            caSession.removeCA(admin, cainfo3.getCAId());
        } catch (Exception e) {
            log.error("removeCA", e);
            fail("removeCA: " + e.getMessage());
        }
View Full Code Here

        catokeninfo1.setSignKeyAlgorithm(AlgorithmConstants.KEYALGORITHM_RSA);
        catokeninfo1.setSignKeySpec("2048");
        catokeninfo1.setEncryptionAlgorithm(AlgorithmConstants.SIGALG_SHA1_WITH_RSA);
        catokeninfo1.setEncKeyAlgorithm(AlgorithmConstants.KEYALGORITHM_RSA);
        catokeninfo1.setEncKeySpec("2048");
        X509CAInfo cainfo1 = getNewCAInfo(caname1, catokeninfo1);

        // Remove if they already exists
        try {
            caSession.removeCA(admin, cainfo1.getCAId());
        } catch (Exception ignored) {
        }

        // Create CAs
        try {
            caAdminSession.createCA(admin, cainfo1);
        } catch (Exception e) {
            log.error("createCA", e);
            fail("createCA: " + e.getMessage());
        }

        // Export keystore
        try {
            keystorebytes1 = caAdminSession.exportCAKeyStore(admin, caname1, capassword, capassword, "SignatureKeyAlias", "EncryptionKeyAlias");
        } catch (Exception e) {
            log.error("exportCAKeyStore", e);
            fail("exportCAKeyStore: " + e.getMessage());
        }

        // Just created CA should be active
        CAInfo info = caAdminSession.getCAInfo(admin, caname1);
        assertEquals("active token", ICAToken.STATUS_ACTIVE, info.getCATokenInfo().getCATokenStatus());

        // Try to restore the first CA even do it has not been removed
        try {
            caAdminSession.restoreCAKeyStore(admin, caname1, keystorebytes1, capassword, capassword, "SignatureKeyAlias", "EncryptionKeyAlias");
            fail("Should fail when trying to restore an online CA");
        } catch (Exception e) {
            // OK
        }

        // Clean up
        try {
            caSession.removeCA(admin, cainfo1.getCAId());
        } catch (Exception e) {
            log.error("removeCA", e);
            fail("removeCA: " + e.getMessage());
        }
View Full Code Here

        byte[] keystorebytes = null;
        String caname = "TestExportRemoveRestoreCA1";
        String capassword = "foo123";
        String keyFingerPrint = null;

        X509CAInfo cainfo = getNewCAInfo(caname, catokeninfo);
        Admin admin = new Admin(Admin.TYPE_CACOMMANDLINE_USER);

        // Remove if it already exists
        try {
            caSession.removeCA(admin, cainfo.getCAId());
        } catch (Exception ignored) {
        }

        // Create CA
        try {
            caAdminSession.createCA(admin, cainfo);
        } catch (Exception e) {
            log.error("createCA", e);
            fail("createCA: " + e.getMessage());
        }

        try {
            keyFingerPrint = caAdminSession.getKeyFingerPrint(admin, caname);
        } catch (Exception e) {
            log.error("getKeyFingerPrint", e);
            fail("getKeyFingerPrint: " + e.getMessage());
        }

        try {
            keystorebytes = caAdminSession.exportCAKeyStore(admin, caname, capassword, capassword, "SignatureKeyAlias", "EncryptionKeyAlias");

        } catch (Exception e) {
            log.error("exportCAKeyStore", e);
            fail("exportCAKeyStore: " + e.getMessage());
        }

        // Remove the ca token soft keystore
        try {
            caAdminSession.removeCAKeyStore(admin, caname);
        } catch (Exception e) {
            log.error("removeKeyStores", e);
            fail("removeKeyStores: " + e.getMessage());
        }

        // The token should now be offline
        CAInfo info = caAdminSession.getCAInfo(admin, caname);
        assertEquals("offline token", ICAToken.STATUS_OFFLINE, info.getCATokenInfo().getCATokenStatus());

        // Should not be possible to activate
        caAdminSession.activateCAToken(admin, cainfo.getCAId(), capassword, globalConfigurationSession.getCachedGlobalConfiguration(admin));
        info = caAdminSession.getCAInfo(admin, caname);
        assertEquals("offline token", ICAToken.STATUS_OFFLINE, info.getCATokenInfo().getCATokenStatus());

        // Should not be possible to export
        try {
            byte[] emptyBytes = caAdminSession.exportCAKeyStore(admin, caname, capassword, capassword, "SignatureKeyAlias", "EncryptionKeyAlias");
            assertEquals("empty keystore", 0, emptyBytes.length);
        } catch (Exception ignored) {
            // OK
        }

        try {
            String emptyFingerprint = caAdminSession.getKeyFingerPrint(admin, caname);
            log.error("Got fingerprint: " + emptyFingerprint);
            fail("Should not have got a fingerprint");
        } catch (Exception e) {
            Throwable root = e;
            while (root.getCause() != null) {
                root = root.getCause();
            }
            if (root instanceof CATokenOfflineException) {
                // OK
            } else {
                log.error("getKeyFingerPrint", e);
                fail("getKeyFingerPrint: " + e.getMessage());
            }
        }

        // Restore keystore
        try {
            caAdminSession.restoreCAKeyStore(admin, caname, keystorebytes, capassword, capassword, "SignatureKeyAlias", "EncryptionKeyAlias");
        } catch (Exception e) {
            log.error("restoreKeyStores", e);
            fail("restoreKeyStores: " + e.getMessage());
        }

        // Compare fingerprints
        try {
            String restoredFingerprint = caAdminSession.getKeyFingerPrint(admin, caname);
            assertEquals("restored fingerprint", keyFingerPrint, restoredFingerprint);
        } catch (Exception e) {
            log.error("getKeyFingerPrint2", e);
            fail("getKeyFingerPrint2: " + e.getMessage());
        }

        // Clean up
        try {
            caSession.removeCA(admin, cainfo.getCAId());
        } catch (Exception e) {
            log.error("removeCA", e);
            fail("removeCA: " + e.getMessage());
        }
    }
View Full Code Here

     * @param catokeninfo
     *            The tokeninfo for this CA-info
     * @return The new X509CAInfo for testing.
     */
    private X509CAInfo getNewCAInfo(String caname, CATokenInfo catokeninfo) {
        X509CAInfo cainfo = new X509CAInfo("CN=" + caname, caname, SecConst.CA_ACTIVE, new Date(), "", SecConst.CERTPROFILE_FIXED_ROOTCA, 365, new Date(System
                .currentTimeMillis()
                + 364 * 24 * 3600 * 1000), // Expiretime
                CAInfo.CATYPE_X509, CAInfo.SELFSIGNED, null, // certificatechain
                catokeninfo, "Used for testing CA keystore export, remove and restore",
                -1, // revocationReason
View Full Code Here

     * @throws Exception error
     */
    public void test01renewCA() throws Exception {
        log.trace(">test01renewCA()");

        X509CAInfo info = (X509CAInfo) caAdminSession.getCAInfo(admin, "TEST");
        X509Certificate orgcert = (X509Certificate) info.getCertificateChain().iterator().next();
        // Sleep at least for one second so we are not so fast that we create a new cert with the same time
        Thread.sleep(2000);
        caAdminSession.renewCA(admin,info.getCAId(),null,false);
        X509CAInfo newinfo = (X509CAInfo) caAdminSession.getCAInfo(admin, "TEST");
        X509Certificate newcertsamekeys = (X509Certificate) newinfo.getCertificateChain().iterator().next();
        assertTrue(!orgcert.getSerialNumber().equals(newcertsamekeys.getSerialNumber()));
        byte[] orgkey = orgcert.getPublicKey().getEncoded();
        byte[] samekey = newcertsamekeys.getPublicKey().getEncoded();
        assertTrue(Arrays.equals(orgkey,samekey));
        // The new certificate must have a validity greater than the old cert
        assertTrue("newcertsamekeys.getNotAfter: " + newcertsamekeys.getNotAfter() + " orgcert.getNotAfter: "+orgcert.getNotAfter(), newcertsamekeys.getNotAfter().after(orgcert.getNotAfter()));

        // This assumes that the default system keystore password is not changed from foo123
        caAdminSession.renewCA(admin,info.getCAId(),"foo123",true);
        X509CAInfo newinfo2 = (X509CAInfo) caAdminSession.getCAInfo(admin, "TEST");
        X509Certificate newcertnewkeys = (X509Certificate) newinfo2.getCertificateChain().iterator().next();
        assertTrue(!orgcert.getSerialNumber().equals(newcertnewkeys.getSerialNumber()));
        byte[] newkey = newcertnewkeys.getPublicKey().getEncoded();
        assertFalse(Arrays.equals(orgkey,newkey));       
       
        log.trace("<test01renewCA()");
View Full Code Here

    public void test04RevocationApprovals() throws Exception {
        // Generate random username and CA name
        String randomPostfix = Integer.toString((new Random(new Date().getTime() + 4711)).nextInt(999999));
        String caname = "cmpRevocationCA" + randomPostfix;
        String username = "cmpRevocationUser" + randomPostfix;
        X509CAInfo cainfo = null;
        try {
            // Generate CA with approvals for revocation enabled
            int caID = RevocationApprovalTest.createApprovalCA(admin, caname, CAInfo.REQ_APPROVAL_REVOCATION, caAdminSession, caSession);
            // Get CA cert
            cainfo = (X509CAInfo) caAdminSession.getCAInfo(admin, caID);
            assertNotNull(cainfo);
            X509Certificate newCACert = (X509Certificate) cainfo.getCertificateChain().iterator().next();
            // Create a user and generate the cert
            UserDataVO userdata = new UserDataVO(username, "CN=" + username, cainfo.getCAId(), null, null, 1, SecConst.EMPTY_ENDENTITYPROFILE,
                    SecConst.CERTPROFILE_FIXED_ENDUSER, SecConst.TOKEN_SOFT_P12, 0, null);
            userdata.setPassword("foo123");
            userAdminSession.addUser(admin, userdata, true);
            BatchMakeP12 makep12 = new BatchMakeP12();
            File tmpfile = File.createTempFile("ejbca", "p12");
            makep12.setMainStoreDir(tmpfile.getParent());
            makep12.createAllNew();
            Collection<java.security.cert.Certificate> userCerts = certificateStoreSession.findCertificatesByUsername(admin, username);
            assertTrue(userCerts.size() == 1);
            X509Certificate cert = (X509Certificate) userCerts.iterator().next();
            // revoke via CMP and verify response
            byte[] nonce = CmpMessageHelper.createSenderNonce();
            byte[] transid = CmpMessageHelper.createSenderNonce();
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            DEROutputStream out = new DEROutputStream(bao);
            PKIMessage rev = genRevReq(cainfo.getSubjectDN(), userdata.getDN(), cert.getSerialNumber(), newCACert, nonce, transid, true);
            PKIMessage revReq = protectPKIMessage(rev, false, PBEPASSWORD, 567);
            assertNotNull(revReq);
            bao = new ByteArrayOutputStream();
            out = new DEROutputStream(bao);
            out.writeObject(revReq);
            byte[] ba = bao.toByteArray();
            byte[] resp = sendCmpHttp(ba, 200);
            checkCmpResponseGeneral(resp, cainfo.getSubjectDN(), userdata.getDN(), newCACert, nonce, transid, false, PBEPASSWORD);
            checkCmpRevokeConfirmMessage(cainfo.getSubjectDN(), userdata.getDN(), cert.getSerialNumber(), newCACert, resp, true);
            int reason = checkRevokeStatus(cainfo.getSubjectDN(), cert.getSerialNumber());
            assertEquals(reason, RevokedCertInfo.NOT_REVOKED);
            // try to revoke one more via CMP and verify error
            nonce = CmpMessageHelper.createSenderNonce();
            transid = CmpMessageHelper.createSenderNonce();
            bao = new ByteArrayOutputStream();
            out = new DEROutputStream(bao);
            rev = genRevReq(cainfo.getSubjectDN(), userdata.getDN(), cert.getSerialNumber(), newCACert, nonce, transid, true);
            revReq = protectPKIMessage(rev, false, PBEPASSWORD, 567);
            assertNotNull(revReq);
            bao = new ByteArrayOutputStream();
            out = new DEROutputStream(bao);
            out.writeObject(revReq);
            ba = bao.toByteArray();
            resp = sendCmpHttp(ba, 200);
            checkCmpResponseGeneral(resp, cainfo.getSubjectDN(), userdata.getDN(), newCACert, nonce, transid, false, PBEPASSWORD);
            checkCmpFailMessage(resp, "The request is already awaiting approval.", CmpPKIBodyConstants.REVOCATIONRESPONSE, 0, ResponseStatus.FAILURE
                    .getIntValue());
            reason = checkRevokeStatus(cainfo.getSubjectDN(), cert.getSerialNumber());
            assertEquals(reason, RevokedCertInfo.NOT_REVOKED);
            // Approve revocation and verify success
            Admin approvingAdmin = new Admin((X509Certificate) certificateStoreSession.findCertificatesByUsername(admin, APPROVINGADMINNAME).iterator().next(),
                    APPROVINGADMINNAME, null);
            approveRevocation(admin, approvingAdmin, username, RevokedCertInfo.REVOCATION_REASON_CESSATIONOFOPERATION,
                    ApprovalDataVO.APPROVALTYPE_REVOKECERTIFICATE, certificateStoreSession, approvalSession, approvalExecutionSession, cainfo.getCAId());
            // try to revoke the now revoked cert via CMP and verify error
            nonce = CmpMessageHelper.createSenderNonce();
            transid = CmpMessageHelper.createSenderNonce();
            bao = new ByteArrayOutputStream();
            out = new DEROutputStream(bao);
            rev = genRevReq(cainfo.getSubjectDN(), userdata.getDN(), cert.getSerialNumber(), newCACert, nonce, transid, true);
            revReq = protectPKIMessage(rev, false, PBEPASSWORD, 567);
            assertNotNull(revReq);
            bao = new ByteArrayOutputStream();
            out = new DEROutputStream(bao);
            out.writeObject(revReq);
            ba = bao.toByteArray();
            resp = sendCmpHttp(ba, 200);
            checkCmpResponseGeneral(resp, cainfo.getSubjectDN(), userdata.getDN(), newCACert, nonce, transid, false, PBEPASSWORD);
            checkCmpFailMessage(resp, "Already revoked.", CmpPKIBodyConstants.REVOCATIONRESPONSE, 0, ResponseStatus.FAILURE.getIntValue());
        } finally {
            // Delete user
            userAdminSession.deleteUser(admin, username);
            // Nuke CA
            try {
                caAdminSession.revokeCA(admin, cainfo.getCAId(), RevokedCertInfo.REVOCATION_REASON_UNSPECIFIED);
            } finally {
                caSession.removeCA(admin, cainfo.getCAId());
            }
        }
    } // test04RevocationApprovals
View Full Code Here

        // AltName is not implemented for all CA types
        String caAltName = null;
        // X509 CA is the normal type of CA
        if (cainfo instanceof X509CAInfo) {
            log.info("Creating an X509 CA");
            X509CAInfo x509cainfo = (X509CAInfo) cainfo;
            // Create X509CA
            ca = new X509CA(x509cainfo);
            X509CA x509ca = (X509CA) ca;
            ca.setCAToken(catoken);

            // getCertificateProfile
            if ((x509cainfo.getPolicies() != null) && (x509cainfo.getPolicies().size() > 0)) {
                certprofile.setUseCertificatePolicies(true);
                certprofile.setCertificatePolicies(x509cainfo.getPolicies());
            } else if (certprofile.getUseCertificatePolicies()) {
                x509ca.setPolicies(certprofile.getCertificatePolicies());
            }
            caAltName = x509cainfo.getSubjectAltName();
        } else {
            // CVC CA is a special type of CA for EAC electronic passports
            log.info("Creating a CVC CA");
            CVCCAInfo cvccainfo = (CVCCAInfo) cainfo;
            // Create CVCCA
View Full Code Here

            boolean useprintablestringsubjectdn = false;
            boolean useldapdnorder = true; // Default value
            boolean usecrldistpointoncrl = false;
            boolean crldistpointoncrlcritical = false;

            cainfo = new X509CAInfo(subjectdn, caname, SecConst.CA_EXTERNAL, new Date(), subjectaltname, certprofileid, validity, CertTools
                    .getNotAfter(x509CaCertificate), CAInfo.CATYPE_X509, signedby, null, null, description, -1, null, policies, crlperiod, crlIssueInterval,
                    crlOverlapTime, deltacrlperiod, crlpublishers, useauthoritykeyidentifier, authoritykeyidentifiercritical, usecrlnumber, crlnumbercritical,
                    "", "", "", "", finishuser, extendedcaserviceinfos, useutf8policytext, approvalsettings, numofreqapprovals, useprintablestringsubjectdn,
                    useldapdnorder, usecrldistpointoncrl, crldistpointoncrlcritical, false, true, // isDoEnforceUniquePublicKeys
                    true, // isDoEnforceUniqueDistinguishedName
View Full Code Here

TOP

Related Classes of org.ejbca.core.model.ca.caadmin.X509CAInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.