Search in sources :

Example 21 with Nullable

use of de.carne.check.Nullable in project certmgr by hdecarne.

the class CertReaders method readFile.

/**
 * Read all available certificate objects from a file.
 * <p>
 * All registered {@link CertReader}s are considered for reading certificate object until one recognizes the file
 * data.
 *
 * @param file The file to read from.
 * @param password The callback to use for querying passwords (if needed).
 * @return The read certificate objects, or {@code null} if no certificate data was recognized.
 * @throws IOException if an I/O error occurs during reading/decoding.
 */
@Nullable
public static CertObjectStore readFile(Path file, PasswordCallback password) throws IOException {
    Deque<CertReader> certReaders = new ArrayDeque<>();
    Path fileName = file.getFileName();
    for (CertReader reader : REGISTERED.providers()) {
        if (matchFileName(reader, fileName)) {
            certReaders.addFirst(reader);
        } else {
            certReaders.addLast(reader);
        }
    }
    CertObjectStore certObjects = null;
    for (CertReader reader : certReaders) {
        try (IOResource<InputStream> in = IOResource.newInputStream(file.toString(), file, StandardOpenOption.READ)) {
            certObjects = reader.readBinary(in, password);
            if (certObjects != null) {
                break;
            }
        }
    }
    return certObjects;
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) CertObjectStore(de.carne.certmgr.certs.CertObjectStore) CertReader(de.carne.certmgr.certs.spi.CertReader) ArrayDeque(java.util.ArrayDeque) Nullable(de.carne.check.Nullable)

Example 22 with Nullable

use of de.carne.check.Nullable in project certmgr by hdecarne.

the class KeyHelper method getKeySize.

/**
 * Get the public key's key size.
 *
 * @param publicKey The public key to get the key size for.
 * @return The public key's key size or {@code null} if the key size is indeterminable.
 */
@Nullable
public static Integer getKeySize(PublicKey publicKey) {
    Integer keySize = null;
    if (publicKey instanceof RSAPublicKey) {
        RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
        keySize = rsaPublicKey.getModulus().bitLength();
    } else if (publicKey instanceof ECPublicKey) {
        ECPublicKey ecPublicKey = (ECPublicKey) publicKey;
        keySize = ecPublicKey.getParams().getCurve().getField().getFieldSize();
    }
    return keySize;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) Nullable(de.carne.check.Nullable)

Example 23 with Nullable

use of de.carne.check.Nullable in project certmgr by hdecarne.

the class CertExportController method onCmdExport.

@SuppressWarnings("unused")
@FXML
void onCmdExport(ActionEvent evt) {
    try {
        CertWriter exportFormat = validateAndGetFormat();
        boolean encrypt = this.ctlEncryptOption.isSelected();
        boolean exportCert = this.ctlExportCertOption.isSelected();
        boolean exportChain = this.ctlExportChainOption.isSelected();
        boolean exportChainRoot = this.ctlExportChainRootOption.isSelected();
        boolean exportKey = this.ctlExportKeyOption.isSelected();
        boolean exportCSR = this.ctlExportCSROption.isSelected();
        boolean exportCRL = this.ctlExportCRLOption.isSelected();
        if (this.ctlFileDestinationOption.isSelected()) {
            Path exportFile = validateFileDestinationInput();
            getExecutorService().submit(new ExportTask<Path>(exportCert, exportChain, exportChainRoot, exportKey, exportCSR, exportCRL, exportFormat, exportFile, encrypt) {

                @Override
                protected void export(CertWriter format, @Nullable Path param, CertObjectStore exportObjects, boolean encryptExport) throws IOException {
                    exportToFile(format, Check.notNull(param), exportObjects, encryptExport);
                }
            });
        } else if (this.ctlDirectoryDestinationOption.isSelected()) {
            Path exportDirectory = validateDirectoryDestinationInput();
            getExecutorService().submit(new ExportTask<Path>(exportCert, exportChain, exportChainRoot, exportKey, exportCSR, exportCRL, exportFormat, exportDirectory, encrypt) {

                @Override
                protected void export(CertWriter format, @Nullable Path param, CertObjectStore exportObjects, boolean encryptExport) throws IOException {
                    exportToDirectory(format, Check.notNull(param), exportObjects, encryptExport);
                }
            });
        } else if (this.ctlClipboardDestinationOption.isSelected()) {
            getExecutorService().submit(new ExportTask<Void>(exportCert, exportChain, exportChainRoot, exportKey, exportCSR, exportCRL, exportFormat, null, encrypt) {

                @Override
                protected void export(CertWriter format, @Nullable Void param, CertObjectStore exportObjects, boolean encryptExport) throws IOException {
                    exportToClipboard(format, exportObjects, encryptExport);
                }
            });
        }
    } catch (ValidationException e) {
        ValidationAlerts.error(e).showAndWait();
    }
}
Also used : Path(java.nio.file.Path) ValidationException(de.carne.jfx.util.validation.ValidationException) CertWriter(de.carne.certmgr.certs.spi.CertWriter) CertObjectStore(de.carne.certmgr.certs.CertObjectStore) IOException(java.io.IOException) Nullable(de.carne.check.Nullable) FXML(javafx.fxml.FXML)

Example 24 with Nullable

use of de.carne.check.Nullable in project certmgr by hdecarne.

the class SignatureAlgorithm method getDefaultSet.

/**
 * Get the available signature algorithms.
 *
 * @param keyPairAlgorithm The key pair algorithm to get the signature algorithms for.
 * @param defaultHint The default to return (may be {@code null}). If this algorithm is contained in the default
 *        set, it is also set as the default.
 * @param expertMode Whether only standard algorithms are considered ({@code false}) or all algorithms available on
 *        the current platform ({@code true}).
 * @return The available signature algorithms
 */
public static DefaultSet<SignatureAlgorithm> getDefaultSet(String keyPairAlgorithm, @Nullable String defaultHint, boolean expertMode) {
    DefaultSet<SignatureAlgorithm> signatureAlgorithms = new DefaultSet<>();
    DefaultSet<String> defaultNames = SecurityDefaults.getSignatureAlgorithmNames(keyPairAlgorithm);
    @Nullable String defaultName = (defaultHint != null && defaultNames.contains(defaultHint) ? defaultHint : defaultNames.getDefault());
    if (defaultName != null) {
        defaultName = defaultName.toUpperCase();
    }
    for (Provider provider : SecurityDefaults.getProviders(expertMode)) {
        for (Provider.Service service : provider.getServices()) {
            if (!SERVICE_TYPE_SIGNATURE.equals(service.getType())) {
                continue;
            }
            String upperCaseAlgorithm = service.getAlgorithm().toUpperCase();
            if (!expertMode && !defaultNames.contains(upperCaseAlgorithm)) {
                continue;
            }
            SignatureAlgorithm signatureAlgorithm = (expertMode ? new ExpertKeyPairAlgorithm(service) : new StandardKeyPairAlgorithm(service));
            if (upperCaseAlgorithm.equals(defaultName)) {
                signatureAlgorithms.addDefault(signatureAlgorithm);
            } else {
                signatureAlgorithms.add(signatureAlgorithm);
            }
        }
    }
    return signatureAlgorithms;
}
Also used : DefaultSet(de.carne.jfx.util.DefaultSet) Service(java.security.Provider.Service) Nullable(de.carne.check.Nullable) Provider(java.security.Provider)

Aggregations

Nullable (de.carne.check.Nullable)24 IOException (java.io.IOException)9 CertObjectStore (de.carne.certmgr.certs.CertObjectStore)7 PrivateKey (java.security.PrivateKey)4 CertProviderException (de.carne.certmgr.certs.CertProviderException)3 InputStream (java.io.InputStream)3 Path (java.nio.file.Path)3 PublicKey (java.security.PublicKey)3 BackingStoreException (java.util.prefs.BackingStoreException)3 X500Principal (javax.security.auth.x500.X500Principal)3 PasswordRequiredException (de.carne.certmgr.certs.PasswordRequiredException)2 CertReader (de.carne.certmgr.certs.spi.CertReader)2 DefaultSet (de.carne.jfx.util.DefaultSet)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Key (java.security.Key)2 KeyPair (java.security.KeyPair)2 Provider (java.security.Provider)2 Service (java.security.Provider.Service)2 X509Certificate (java.security.cert.X509Certificate)2 ArrayDeque (java.util.ArrayDeque)2