Search in sources :

Example 1 with UserCertStore

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

the class CertImportController method validateAndReloadFileSource.

private void validateAndReloadFileSource() {
    try {
        Path fileSource = validateFileSourceInput();
        getExecutorService().submit(new ReloadTask<Path>(fileSource) {

            @Override
            protected UserCertStore createStore(Path params) throws IOException {
                return UserCertStore.createFromFile(params, PasswordDialog.enterPassword(CertImportController.this));
            }
        });
    } catch (ValidationException e) {
        ValidationAlerts.error(e).showAndWait();
    }
}
Also used : Path(java.nio.file.Path) ValidationException(de.carne.jfx.util.validation.ValidationException) IOException(java.io.IOException) UserCertStore(de.carne.certmgr.certs.UserCertStore)

Example 2 with UserCertStore

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

the class CertImportController method validateAndReloadClipboardSource.

private void validateAndReloadClipboardSource() {
    try {
        Clipboard clipboard = Clipboard.getSystemClipboard();
        if (clipboard.hasFiles()) {
            List<Path> filesSource = clipboard.getFiles().stream().map((f) -> f.toPath()).collect(Collectors.toList());
            getExecutorService().submit(new ReloadTask<List<Path>>(filesSource) {

                @Override
                protected UserCertStore createStore(List<Path> params) throws IOException {
                    return UserCertStore.createFromFiles(params, PasswordDialog.enterPassword(CertImportController.this));
                }
            });
        } else if (clipboard.hasUrl()) {
            URL urlSource = new URL(clipboard.getUrl());
            getExecutorService().submit(new ReloadTask<URL>(urlSource) {

                @Override
                protected UserCertStore createStore(URL params) throws IOException {
                    return UserCertStore.createFromURL(params, PasswordDialog.enterPassword(CertImportController.this));
                }
            });
        } else if (clipboard.hasString()) {
            String stringSource = clipboard.getString();
            getExecutorService().submit(new ReloadTask<String>(stringSource) {

                @Override
                protected UserCertStore createStore(String params) throws IOException {
                    return UserCertStore.createFromData(params, CertImportI18N.formatSTR_TEXT_CLIPBOARD(), PasswordDialog.enterPassword(CertImportController.this));
                }
            });
        }
    } catch (IOException e) {
        Alerts.error(AlertType.ERROR, CertImportI18N.formatSTR_MESSAGE_CREATE_STORE_ERROR(), e);
    }
}
Also used : Path(java.nio.file.Path) Button(javafx.scene.control.Button) URL(java.net.URL) Alerts(de.carne.jfx.scene.control.Alerts) PathValidator(de.carne.jfx.util.validation.PathValidator) VBox(javafx.scene.layout.VBox) UserCertStoreTreeTableViewHelper(de.carne.certmgr.jfx.util.UserCertStoreTreeTableViewHelper) ValidationAlerts(de.carne.jfx.util.validation.ValidationAlerts) LogRecorder(de.carne.boot.logging.LogRecorder) AlertType(javafx.scene.control.Alert.AlertType) UserCertStoreEntry(de.carne.certmgr.certs.UserCertStoreEntry) Lazy(de.carne.util.Lazy) Path(java.nio.file.Path) Strings(de.carne.util.Strings) SSLPeer(de.carne.certmgr.certs.net.SSLPeer) PathPreference(de.carne.certmgr.util.PathPreference) StageController(de.carne.jfx.stage.StageController) TextField(javafx.scene.control.TextField) Set(java.util.Set) LogRecord(java.util.logging.LogRecord) Collectors(java.util.stream.Collectors) Images(de.carne.certmgr.jfx.resources.Images) FXML(javafx.fxml.FXML) List(java.util.List) Stream(java.util.stream.Stream) Clipboard(javafx.scene.input.Clipboard) RadioButton(javafx.scene.control.RadioButton) Pattern(java.util.regex.Pattern) Nullable(de.carne.check.Nullable) CertReader(de.carne.certmgr.certs.spi.CertReader) TreeItem(javafx.scene.control.TreeItem) FileChooserHelper(de.carne.jfx.util.FileChooserHelper) Late(de.carne.util.Late) CheckBoxTreeTableCell(javafx.scene.control.cell.CheckBoxTreeTableCell) Bindings(javafx.beans.binding.Bindings) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TreeTableView(javafx.scene.control.TreeTableView) PasswordDialog(de.carne.certmgr.jfx.password.PasswordDialog) UserCertStore(de.carne.certmgr.certs.UserCertStore) CertReaders(de.carne.certmgr.certs.io.CertReaders) PlatformHelper(de.carne.jfx.application.PlatformHelper) GridPane(javafx.scene.layout.GridPane) PasswordCallback(de.carne.certmgr.certs.PasswordCallback) DirectoryChooser(javafx.stage.DirectoryChooser) Label(javafx.scene.control.Label) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) CheckBox(javafx.scene.control.CheckBox) IOException(java.io.IOException) ValidationException(de.carne.jfx.util.validation.ValidationException) File(java.io.File) ChoiceBox(javafx.scene.control.ChoiceBox) Preferences(java.util.prefs.Preferences) DefaultSet(de.carne.jfx.util.DefaultSet) Consumer(java.util.function.Consumer) TreeTableColumn(javafx.scene.control.TreeTableColumn) FileChooser(javafx.stage.FileChooser) ActionEvent(javafx.event.ActionEvent) TreeItemPropertyValueFactory(javafx.scene.control.cell.TreeItemPropertyValueFactory) Stage(javafx.stage.Stage) ImageView(javafx.scene.image.ImageView) InputValidator(de.carne.jfx.util.validation.InputValidator) ExtensionFilter(javafx.stage.FileChooser.ExtensionFilter) Collections(java.util.Collections) LogLevel(de.carne.boot.logging.LogLevel) PlatformKeyStore(de.carne.certmgr.certs.security.PlatformKeyStore) IOException(java.io.IOException) URL(java.net.URL) UserCertStore(de.carne.certmgr.certs.UserCertStore) List(java.util.List) ArrayList(java.util.ArrayList) Clipboard(javafx.scene.input.Clipboard)

Example 3 with UserCertStore

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

the class StoreController method onCmdNewStore.

@SuppressWarnings("unused")
@FXML
void onCmdNewStore(ActionEvent evt) {
    try {
        StorePreferencesController createStore = StorePreferencesDialog.load(this).init(this.userPreferences.expertMode.getBoolean(false));
        Optional<UserCertStore> createStoreResult = createStore.showAndWait();
        if (createStoreResult.isPresent()) {
            UserCertStore store = createStoreResult.get();
            this.storeProperty.set(store);
            updateStoreEntryView();
            this.ctlStoreStatusLabel.setText(StoreI18N.formatSTR_TEXT_STORE_STATUS(store.storeHome()));
        }
    } catch (IOException e) {
        Alerts.unexpected(e).showAndWait();
    }
}
Also used : StorePreferencesController(de.carne.certmgr.jfx.storepreferences.StorePreferencesController) IOException(java.io.IOException) UserCertStore(de.carne.certmgr.certs.UserCertStore) FXML(javafx.fxml.FXML)

Example 4 with UserCertStore

use of de.carne.certmgr.certs.UserCertStore 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 5 with UserCertStore

use of de.carne.certmgr.certs.UserCertStore 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)

Aggregations

UserCertStore (de.carne.certmgr.certs.UserCertStore)17 IOException (java.io.IOException)13 Test (org.junit.Test)6 ValidationException (de.carne.jfx.util.validation.ValidationException)5 Path (java.nio.file.Path)5 UserCertStoreEntry (de.carne.certmgr.certs.UserCertStoreEntry)4 PlatformKeyStore (de.carne.certmgr.certs.security.PlatformKeyStore)3 ArrayList (java.util.ArrayList)3 FXML (javafx.fxml.FXML)3 PasswordCallback (de.carne.certmgr.certs.PasswordCallback)2 UserCertStoreEntryId (de.carne.certmgr.certs.UserCertStoreEntryId)2 LogLevel (de.carne.boot.logging.LogLevel)1 LogRecorder (de.carne.boot.logging.LogRecorder)1 UserCertStorePreferences (de.carne.certmgr.certs.UserCertStorePreferences)1 CertReaders (de.carne.certmgr.certs.io.CertReaders)1 SSLPeer (de.carne.certmgr.certs.net.SSLPeer)1 SignatureAlgorithm (de.carne.certmgr.certs.security.SignatureAlgorithm)1 CertReader (de.carne.certmgr.certs.spi.CertReader)1 GenerateCertRequest (de.carne.certmgr.certs.x509.GenerateCertRequest)1 UpdateCRLRequest (de.carne.certmgr.certs.x509.UpdateCRLRequest)1