Search in sources :

Example 6 with UserCertStoreEntry

use of de.carne.certmgr.certs.UserCertStoreEntry in project certmgr by hdecarne.

the class StoreController method onCmdDeleteEntry.

@SuppressWarnings("unused")
@FXML
void onCmdDeleteEntry(ActionEvent evt) {
    UserCertStoreEntry entry = getSelectedStoreEntry();
    if (entry != null) {
        Optional<ButtonType> confirmation = Alerts.message(AlertType.CONFIRMATION, StoreI18N.formatSTR_MESSAGE_CONFIRM_DELETE(entry)).showAndWait();
        if (confirmation.isPresent() && confirmation.get().getButtonData() == ButtonData.OK_DONE) {
            try {
                this.storeProperty.get().deleteEntry(entry.id());
                if (entry.equals(getSelectedStoreEntry())) {
                    this.ctlStoreEntryView.getSelectionModel().clearSelection();
                }
            } catch (IOException e) {
                Alerts.unexpected(e).showAndWait();
            }
        }
        updateStoreEntryView();
    }
}
Also used : IOException(java.io.IOException) ButtonType(javafx.scene.control.ButtonType) UserCertStoreEntry(de.carne.certmgr.certs.UserCertStoreEntry) FXML(javafx.fxml.FXML)

Example 7 with UserCertStoreEntry

use of de.carne.certmgr.certs.UserCertStoreEntry in project certmgr by hdecarne.

the class UserCertStoreTest method testAccessStore.

/**
 * Test access store operations.
 */
@Test
public void testAccessStore() {
    try {
        UserCertStore store = UserCertStore.openStore(testStorePath.get());
        Assert.assertEquals(11, store.size());
        Assert.assertEquals(TestCerts.TEST_STORE_NAME, store.storeName());
        Assert.assertEquals(11, store.getEntries().size());
        Assert.assertEquals(1, traverseStore(store.getRootEntries()));
        // Check preferences access
        UserCertStorePreferences loadPreferences = Check.notNull(store.storePreferences());
        Assert.assertEquals(Integer.valueOf(365), loadPreferences.defaultCRTValidityPeriod.get());
        Assert.assertEquals(Integer.valueOf(30), loadPreferences.defaultCRLUpdatePeriod.get());
        Assert.assertEquals("EC", loadPreferences.defaultKeyPairAlgorithm.get());
        Assert.assertEquals(Integer.valueOf(384), loadPreferences.defaultKeySize.get());
        Assert.assertEquals("SHA256WITHECDSA", loadPreferences.defaultSignatureAlgorithm.get());
        UserCertStorePreferences setPreferences = Check.notNull(store.storePreferences());
        setPreferences.defaultCRTValidityPeriod.putInt(180);
        setPreferences.defaultCRLUpdatePeriod.putInt(7);
        setPreferences.defaultKeyPairAlgorithm.put("EC");
        setPreferences.defaultKeySize.putInt(521);
        setPreferences.defaultSignatureAlgorithm.put("SHA256WITHECDSA");
        setPreferences.sync();
        UserCertStorePreferences getPreferences = Check.notNull(store.storePreferences());
        Assert.assertEquals(Integer.valueOf(180), getPreferences.defaultCRTValidityPeriod.get());
        Assert.assertEquals(Integer.valueOf(7), getPreferences.defaultCRLUpdatePeriod.get());
        Assert.assertEquals("EC", getPreferences.defaultKeyPairAlgorithm.get());
        Assert.assertEquals(Integer.valueOf(521), getPreferences.defaultKeySize.get());
        Assert.assertEquals("SHA256WITHECDSA", getPreferences.defaultSignatureAlgorithm.get());
        // Import access (with already existing entries)
        UserCertStore importStore = UserCertStore.createFromFiles(collectDirectoryFiles(testStorePath.get()), TestCerts.password());
        for (UserCertStoreEntry importStoreEntry : importStore.getEntries()) {
            store.importEntry(importStoreEntry, TestCerts.password(), "Imported");
        }
        Assert.assertEquals(11, store.size());
        // Revoke access
        for (UserCertStoreEntry storeEntry : store.getEntries()) {
            if (storeEntry.hasCRT() && !storeEntry.isSelfSigned() && !storeEntry.isRevoked()) {
                UserCertStoreEntry issuerEntry = storeEntry.issuer();
                if (issuerEntry.canIssue()) {
                    Date lastUpdate = new Date(System.currentTimeMillis());
                    Date nextUpdate = new Date(lastUpdate.getTime() + 1000);
                    SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.getDefaultSet(issuerEntry.getPublicKey().getAlgorithm(), storeEntry.getCRT().getSigAlgName(), false).getDefault();
                    Assert.assertNotNull(signatureAlgorithm);
                    UpdateCRLRequest updateCRLRequest = new UpdateCRLRequest(lastUpdate, nextUpdate, signatureAlgorithm);
                    updateCRLRequest.addRevokeEntry(storeEntry.getCRT().getSerialNumber(), ReasonFlag.PRIVILEGE_WITHDRAWN);
                    issuerEntry.updateCRL(updateCRLRequest, TestCerts.password());
                    Assert.assertTrue(storeEntry.isRevoked());
                }
            }
        }
        // Delete access
        List<UserCertStoreEntryId> deleteIds = new ArrayList<>();
        for (UserCertStoreEntry storeEntry : store.getEntries()) {
            deleteIds.add(storeEntry.id());
        }
        for (UserCertStoreEntryId deleteId : deleteIds) {
            store.deleteEntry(deleteId);
        }
        Assert.assertEquals(0, store.size());
        // Import access (now with empty store)
        for (UserCertStoreEntry importStoreEntry : importStore.getEntries()) {
            store.importEntry(importStoreEntry, TestCerts.password(), "Imported");
        }
        Assert.assertEquals(11, store.size());
    } catch (IOException | BackingStoreException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : UserCertStorePreferences(de.carne.certmgr.certs.UserCertStorePreferences) UpdateCRLRequest(de.carne.certmgr.certs.x509.UpdateCRLRequest) ArrayList(java.util.ArrayList) BackingStoreException(java.util.prefs.BackingStoreException) SignatureAlgorithm(de.carne.certmgr.certs.security.SignatureAlgorithm) IOException(java.io.IOException) UserCertStoreEntryId(de.carne.certmgr.certs.UserCertStoreEntryId) Date(java.util.Date) UserCertStore(de.carne.certmgr.certs.UserCertStore) UserCertStoreEntry(de.carne.certmgr.certs.UserCertStoreEntry) Test(org.junit.Test)

Example 8 with UserCertStoreEntry

use of de.carne.certmgr.certs.UserCertStoreEntry in project certmgr by hdecarne.

the class UserCertStoreTest method testCreateAndOpenStore.

/**
 * Test create/open store operations.
 */
@Test
public void testCreateAndOpenStore() {
    Path storeHome = tempPath.get().resolve(NAME_STORE1);
    try {
        UserCertStore createdStore = UserCertStore.createStore(storeHome);
        GenerateCertRequest request1 = generateRequest(createdStore, CertGenerators.DEFAULT, true);
        UserCertStoreEntry generated1 = createdStore.generateEntry(CertGenerators.DEFAULT, request1, TestCerts.password(), TestCerts.password(), "TestCert");
        DefaultSet<Issuer> issuers1 = CertGenerators.DEFAULT.getIssuers(createdStore, generated1);
        GenerateCertRequest request2 = generateRequest(createdStore, CertGenerators.DEFAULT, false);
        request2.setIssuer(issuers1.getDefault());
        createdStore.generateEntry(CertGenerators.DEFAULT, request1, TestCerts.password(), TestCerts.password(), "TestCert");
        Assert.assertEquals(2, createdStore.size());
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
    try {
        UserCertStore.createStore(storeHome);
        Assert.fail("Re-creating store succeeded, but should not");
    } catch (FileAlreadyExistsException e) {
        Exceptions.ignore(e);
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
    try {
        UserCertStore openendStore = UserCertStore.openStore(storeHome);
        Assert.assertEquals(2, openendStore.size());
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) Issuer(de.carne.certmgr.certs.x509.generator.Issuer) IOException(java.io.IOException) GenerateCertRequest(de.carne.certmgr.certs.x509.GenerateCertRequest) UserCertStore(de.carne.certmgr.certs.UserCertStore) UserCertStoreEntry(de.carne.certmgr.certs.UserCertStoreEntry) Test(org.junit.Test)

Example 9 with UserCertStoreEntry

use of de.carne.certmgr.certs.UserCertStoreEntry in project certmgr by hdecarne.

the class UserCertStoreTest method traverseStore.

private int traverseStore(Set<UserCertStoreEntry> entries) {
    int entryCount = 1;
    try {
        for (UserCertStoreEntry entry : entries) {
            Attributes.toAttributes(entry);
            if (entry.hasCRT()) {
                X509CertificateHelper.toAttributes(entry.getCRT());
                accessExtensionValues(entry.getCRT());
            }
            if (entry.hasCSR()) {
                entry.getCSR().toAttributes();
                accessExtensionValues(entry.getCSR());
            }
            if (entry.hasCRL()) {
                X509CRLHelper.toAttributes(entry.getCRL());
                accessExtensionValues(entry.getCRL());
            }
            entry.canIssue();
            entry.isValid();
            entry.isRevoked();
            entryCount = traverseStore(entry.issuedEntries());
        }
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
    return entryCount;
}
Also used : IOException(java.io.IOException) UserCertStoreEntry(de.carne.certmgr.certs.UserCertStoreEntry)

Example 10 with UserCertStoreEntry

use of de.carne.certmgr.certs.UserCertStoreEntry in project certmgr by hdecarne.

the class CRLOptionsController method initEntries.

private void initEntries() throws IOException {
    ObservableList<CRLEntryModel> entryItems = this.ctlEntryOptions.getItems();
    for (UserCertStoreEntry issuedEntry : this.issuerEntryParam.get().issuedEntries()) {
        BigInteger issuedSerial = issuedEntry.getCRT().getSerialNumber();
        boolean revoked = false;
        ReasonFlag reason = ReasonFlag.UNSPECIFIED;
        Date date = null;
        if (this.issuerEntryParam.get().hasCRL()) {
            X509CRL crl = this.issuerEntryParam.get().getCRL();
            X509CRLEntry crlEntry = crl.getRevokedCertificate(issuedSerial);
            if (crlEntry != null) {
                revoked = true;
                CRLReason crlEntryReason = crlEntry.getRevocationReason();
                if (crlEntryReason != null) {
                    reason = ReasonFlag.fromCRLReason(crlEntryReason);
                }
                date = crlEntry.getRevocationDate();
            }
        }
        entryItems.add(new CRLEntryModel(issuedEntry, revoked, issuedSerial, reason, date));
    }
    entryItems.sort((o1, o2) -> o1.compareTo(o2));
}
Also used : X509CRLEntry(java.security.cert.X509CRLEntry) X509CRL(java.security.cert.X509CRL) BigInteger(java.math.BigInteger) CRLReason(java.security.cert.CRLReason) ReasonFlag(de.carne.certmgr.certs.x509.ReasonFlag) Date(java.util.Date) LocalDate(java.time.LocalDate) UserCertStoreEntry(de.carne.certmgr.certs.UserCertStoreEntry)

Aggregations

UserCertStoreEntry (de.carne.certmgr.certs.UserCertStoreEntry)19 IOException (java.io.IOException)10 FXML (javafx.fxml.FXML)6 UserCertStore (de.carne.certmgr.certs.UserCertStore)4 BigInteger (java.math.BigInteger)4 Date (java.util.Date)4 CRLOptionsController (de.carne.certmgr.jfx.crloptions.CRLOptionsController)3 X509CRL (java.security.cert.X509CRL)3 X509Certificate (java.security.cert.X509Certificate)3 CertObjectStore (de.carne.certmgr.certs.CertObjectStore)2 SignatureAlgorithm (de.carne.certmgr.certs.security.SignatureAlgorithm)2 PKCS10CertificateRequest (de.carne.certmgr.certs.x509.PKCS10CertificateRequest)2 ReasonFlag (de.carne.certmgr.certs.x509.ReasonFlag)2 CertExportController (de.carne.certmgr.jfx.certexport.CertExportController)2 Path (java.nio.file.Path)2 ButtonType (javafx.scene.control.ButtonType)2 Test (org.junit.Test)2 PasswordCallback (de.carne.certmgr.certs.PasswordCallback)1 UserCertStoreEntryId (de.carne.certmgr.certs.UserCertStoreEntryId)1 UserCertStorePreferences (de.carne.certmgr.certs.UserCertStorePreferences)1