Search in sources :

Example 1 with SpkacSubject

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

the class DSignCsr method populateSpkacCsrDetails.

private void populateSpkacCsrDetails() throws CryptoException {
    jtfCsrFormat.setText(res.getString("DSignCsr.jtfCsrFormat.Spkac.text"));
    jtfCsrFormat.setCaretPosition(0);
    SpkacSubject subject = spkacCsr.getSubject();
    jdnCsrSubject.setDistinguishedName(subject.getName());
    csrPublicKey = spkacCsr.getPublicKey();
    populatePublicKey();
    jtfCsrSignatureAlgorithm.setText(spkacCsr.getSignatureAlgorithm().friendly());
    jtfCsrSignatureAlgorithm.setCaretPosition(0);
    jtfCsrChallenge.setText(spkacCsr.getChallenge());
    jtfCsrChallenge.setCaretPosition(0);
}
Also used : SpkacSubject(org.kse.crypto.csr.spkac.SpkacSubject)

Example 2 with SpkacSubject

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

the class DViewCsr method populateSpkacCsrDetails.

private void populateSpkacCsrDetails() throws CryptoException {
    jtfFormat.setText(res.getString("DViewCsr.jtfFormat.Spkac.text"));
    jtfFormat.setCaretPosition(0);
    jbExtensions.setEnabled(false);
    jbPem.setEnabled(false);
    jbAsn1.setEnabled(true);
    SpkacSubject subject = spkacCsr.getSubject();
    jdnSubject.setDistinguishedName(subject.getName());
    populatePublicKey(spkacCsr.getPublicKey());
    jtfSignatureAlgorithm.setText(spkacCsr.getSignatureAlgorithm().friendly());
    jtfSignatureAlgorithm.setCaretPosition(0);
    jtfChallenge.setText(spkacCsr.getChallenge());
    jtfChallenge.setCaretPosition(0);
}
Also used : SpkacSubject(org.kse.crypto.csr.spkac.SpkacSubject)

Example 3 with SpkacSubject

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

the class SignatureAlgorithmsTest method doTest.

private void doTest(KeyPairType keyPairType, SignatureType signatureType, CsrType csrType, X509CertificateVersion version) throws Exception {
    KeyPair keyPair = null;
    switch(keyPairType) {
        case RSA:
            keyPair = rsaKeyPair;
            break;
        case DSA:
            keyPair = dsaKeyPair;
            break;
        case EC:
            keyPair = ecKeyPair;
            break;
        default:
            throw new InvalidParameterException();
    }
    X500Name name = new X500Name("cn=this");
    PublicKey publicKey = keyPair.getPublic();
    PrivateKey privateKey = keyPair.getPrivate();
    X509Certificate cert = null;
    if (version == X509CertificateVersion.VERSION1) {
        cert = generatorv1.generateSelfSigned(name, 1000, publicKey, privateKey, signatureType, BigInteger.ONE);
    } else {
        cert = generatorv3.generateSelfSigned(name, 1000, publicKey, privateKey, signatureType, BigInteger.ONE);
    }
    if (csrType == CsrType.SPKAC) {
        Spkac spkac = new Spkac("whatever", signatureType, new SpkacSubject(name), publicKey, privateKey);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        spkac.output(baos);
        spkac = new Spkac(new ByteArrayInputStream(baos.toByteArray()));
        assertThat(spkac.verify()).isTrue();
    } else {
        PKCS10CertificationRequest pkcs10 = Pkcs10Util.generateCsr(cert, privateKey, signatureType, "w/e", "w/e", false, new BouncyCastleProvider());
        byte[] encoded = Pkcs10Util.getCsrEncodedDer(pkcs10);
        pkcs10 = Pkcs10Util.loadCsr(new ByteArrayInputStream(encoded));
        assertThat(Pkcs10Util.verifyCsr(pkcs10)).isTrue();
    }
}
Also used : PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) KeyPair(java.security.KeyPair) SpkacSubject(org.kse.crypto.csr.spkac.SpkacSubject) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) X500Name(org.bouncycastle.asn1.x500.X500Name) ByteArrayOutputStream(java.io.ByteArrayOutputStream) X509Certificate(java.security.cert.X509Certificate) InvalidParameterException(java.security.InvalidParameterException) ByteArrayInputStream(java.io.ByteArrayInputStream) Spkac(org.kse.crypto.csr.spkac.Spkac) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 4 with SpkacSubject

use of org.kse.crypto.csr.spkac.SpkacSubject 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)

Aggregations

SpkacSubject (org.kse.crypto.csr.spkac.SpkacSubject)4 PrivateKey (java.security.PrivateKey)2 PublicKey (java.security.PublicKey)2 X509Certificate (java.security.cert.X509Certificate)2 Spkac (org.kse.crypto.csr.spkac.Spkac)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InvalidParameterException (java.security.InvalidParameterException)1 KeyPair (java.security.KeyPair)1 KeyStore (java.security.KeyStore)1 Provider (java.security.Provider)1 X500Name (org.bouncycastle.asn1.x500.X500Name)1 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)1 PKCS10CertificationRequest (org.bouncycastle.pkcs.PKCS10CertificationRequest)1 CryptoException (org.kse.crypto.CryptoException)1 Password (org.kse.crypto.Password)1 CsrType (org.kse.crypto.csr.CsrType)1