Search in sources :

Example 1 with CsrType

use of org.kse.crypto.csr.CsrType in project keystore-explorer by kaikramer.

the class GenerateCsrAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    File csrFile = null;
    FileOutputStream fos = null;
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        Provider provider = history.getExplicitProvider();
        String alias = kseFrame.getSelectedEntryAlias();
        Password password = getEntryPassword(alias, currentState);
        if (password == null) {
            return;
        }
        KeyStore keyStore = currentState.getKeyStore();
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
        String keyPairAlg = privateKey.getAlgorithm();
        KeyPairType keyPairType = KeyPairUtil.getKeyPairType(privateKey);
        if (keyPairType == null) {
            throw new CryptoException(MessageFormat.format(res.getString("GenerateCsrAction.NoCsrForKeyPairAlg.message"), keyPairAlg));
        }
        // determine dir of current keystore as proposal for CSR file location
        String path = CurrentDirectory.get().getAbsolutePath();
        File keyStoreFile = history.getFile();
        if (keyStoreFile != null) {
            path = keyStoreFile.getAbsoluteFile().getParent();
        }
        DGenerateCsr dGenerateCsr = new DGenerateCsr(frame, alias, privateKey, keyPairType, path, provider);
        dGenerateCsr.setLocationRelativeTo(frame);
        dGenerateCsr.setVisible(true);
        if (!dGenerateCsr.generateSelected()) {
            return;
        }
        CsrType format = dGenerateCsr.getFormat();
        SignatureType signatureType = dGenerateCsr.getSignatureType();
        String challenge = dGenerateCsr.getChallenge();
        String unstructuredName = dGenerateCsr.getUnstructuredName();
        boolean useCertificateExtensions = dGenerateCsr.isAddExtensionsWanted();
        csrFile = dGenerateCsr.getCsrFile();
        X509Certificate firstCertInChain = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(keyStore.getCertificateChain(alias)))[0];
        fos = new FileOutputStream(csrFile);
        if (format == CsrType.PKCS10) {
            String csr = Pkcs10Util.getCsrEncodedDerPem(Pkcs10Util.generateCsr(firstCertInChain, privateKey, signatureType, challenge, unstructuredName, useCertificateExtensions, provider));
            fos.write(csr.getBytes());
        } else {
            SpkacSubject subject = new SpkacSubject(X500NameUtils.x500PrincipalToX500Name(firstCertInChain.getSubjectX500Principal()));
            PublicKey publicKey = firstCertInChain.getPublicKey();
            // TODO handle other providers (PKCS11 etc)
            Spkac spkac = new Spkac(challenge, signatureType, subject, publicKey, privateKey);
            spkac.output(fos);
        }
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("GenerateCsrAction.NoWriteFile.message"), csrFile), res.getString("GenerateCsrAction.GenerateCsr.Title"), JOptionPane.WARNING_MESSAGE);
        return;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return;
    } finally {
        IOUtils.closeQuietly(fos);
    }
    JOptionPane.showMessageDialog(frame, res.getString("GenerateCsrAction.CsrGenerationSuccessful.message"), res.getString("GenerateCsrAction.GenerateCsr.Title"), JOptionPane.INFORMATION_MESSAGE);
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) SpkacSubject(org.kse.crypto.csr.spkac.SpkacSubject) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) FileNotFoundException(java.io.FileNotFoundException) SignatureType(org.kse.crypto.signing.SignatureType) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) CryptoException(org.kse.crypto.CryptoException) FileNotFoundException(java.io.FileNotFoundException) Provider(java.security.Provider) CsrType(org.kse.crypto.csr.CsrType) Spkac(org.kse.crypto.csr.spkac.Spkac) FileOutputStream(java.io.FileOutputStream) DGenerateCsr(org.kse.gui.dialogs.DGenerateCsr) KeyPairType(org.kse.crypto.keypair.KeyPairType) CryptoException(org.kse.crypto.CryptoException) File(java.io.File) Password(org.kse.crypto.Password)

Example 2 with CsrType

use of org.kse.crypto.csr.CsrType in project keystore-explorer by kaikramer.

the class CryptoFileUtil method detectFileType.

/**
 * Detect the cryptographic file type of the supplied input stream.
 *
 * @param is
 *            Input stream to detect type for
 * @return Type or null if file not of a recognised type
 * @throws IOException
 *             If an I/O problem occurred
 */
public static CryptoFileType detectFileType(InputStream is) throws IOException {
    byte[] contents = ReadUtil.readFully(is);
    EncryptionType pkcs8EncType = Pkcs8Util.getEncryptionType(new ByteArrayInputStream(contents));
    if (pkcs8EncType != null) {
        if (pkcs8EncType == ENCRYPTED) {
            return ENC_PKCS8_PVK;
        } else if (pkcs8EncType == UNENCRYPTED) {
            return UNENC_PKCS8_PVK;
        }
    }
    EncryptionType msPvkEncType = MsPvkUtil.getEncryptionType(new ByteArrayInputStream(contents));
    if (msPvkEncType != null) {
        if (msPvkEncType == ENCRYPTED) {
            return ENC_MS_PVK;
        } else if (msPvkEncType == UNENCRYPTED) {
            return UNENC_MS_PVK;
        }
    }
    EncryptionType openSslPvkEncType = OpenSslPvkUtil.getEncryptionType(new ByteArrayInputStream(contents));
    if (openSslPvkEncType != null) {
        if (openSslPvkEncType == ENCRYPTED) {
            return ENC_OPENSSL_PVK;
        } else if (openSslPvkEncType == UNENCRYPTED) {
            return UNENC_OPENSSL_PVK;
        }
    }
    try {
        OpenSslPubUtil.load(new ByteArrayInputStream(contents));
        return OPENSSL_PUB;
    } catch (Exception ex) {
    // Ignore - not an OpenSSL public key file
    } catch (OutOfMemoryError ex) {
    // Ignore - not an OpenSSL public key file, some files cause the
    // heap space to fill up with the load call
    }
    try {
        if (X509CertUtil.loadCertificates(new ByteArrayInputStream(contents)).length > 0) {
            return CERT;
        }
    } catch (Exception ex) {
    // Ignore - not a certificate file
    }
    try {
        X509CertUtil.loadCRL(new ByteArrayInputStream(contents));
        return CRL;
    } catch (Exception ex) {
    // Ignore - not a CRL file
    }
    CsrType csrType = detectCsrType(contents);
    if (csrType != null) {
        return csrType.getCryptoFileType();
    }
    KeyStoreType keyStoreType = detectKeyStoreType(new ByteArrayInputStream(contents));
    if (keyStoreType != null) {
        return keyStoreType.getCryptoFileType();
    }
    // Not a recognised type
    return UNKNOWN;
}
Also used : EncryptionType(org.kse.crypto.privatekey.EncryptionType) CsrType(org.kse.crypto.csr.CsrType) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SpkacException(org.kse.crypto.csr.spkac.SpkacException)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 CsrType (org.kse.crypto.csr.CsrType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 KeyStore (java.security.KeyStore)1 PrivateKey (java.security.PrivateKey)1 Provider (java.security.Provider)1 PublicKey (java.security.PublicKey)1 X509Certificate (java.security.cert.X509Certificate)1 CryptoException (org.kse.crypto.CryptoException)1 Password (org.kse.crypto.Password)1 Spkac (org.kse.crypto.csr.spkac.Spkac)1 SpkacException (org.kse.crypto.csr.spkac.SpkacException)1 SpkacSubject (org.kse.crypto.csr.spkac.SpkacSubject)1 KeyPairType (org.kse.crypto.keypair.KeyPairType)1 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)1 EncryptionType (org.kse.crypto.privatekey.EncryptionType)1 SignatureType (org.kse.crypto.signing.SignatureType)1