Search in sources :

Example 21 with PGPException

use of org.bouncycastle.openpgp.PGPException in project gerrit by GerritCodeReview.

the class PublicKeyChecker method gatherRevocationProblems.

private void gatherRevocationProblems(PGPPublicKey key, Instant now, List<String> problems) {
    try {
        List<PGPSignature> revocations = new ArrayList<>();
        Map<Long, RevocationKey> revokers = new HashMap<>();
        PGPSignature selfRevocation = scanRevocations(key, now, revocations, revokers);
        if (selfRevocation != null) {
            RevocationReason reason = getRevocationReason(selfRevocation);
            if (isRevocationValid(selfRevocation, reason, now)) {
                problems.add(reasonToString(reason));
            }
        } else {
            checkRevocations(key, revocations, revokers, problems);
        }
    } catch (PGPException | IOException e) {
        problems.add("Error checking key revocation");
    }
}
Also used : RevocationReason(org.bouncycastle.bcpg.sig.RevocationReason) PGPException(org.bouncycastle.openpgp.PGPException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RevocationKey(org.bouncycastle.bcpg.sig.RevocationKey) PGPSignature(org.bouncycastle.openpgp.PGPSignature) IOException(java.io.IOException)

Example 22 with PGPException

use of org.bouncycastle.openpgp.PGPException in project ant-ivy by apache.

the class OpenPGPSignatureGenerator method sign.

public void sign(File src, File dest) throws IOException {
    OutputStream out = null;
    InputStream in = null;
    InputStream keyIn = null;
    try {
        if (secring == null) {
            secring = System.getProperty("user.home") + "/.gnupg/secring.gpg";
        }
        if (pgpSec == null) {
            keyIn = new FileInputStream(secring);
            pgpSec = readSecretKey(keyIn);
        }
        PBESecretKeyDecryptor decryptor = new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(password.toCharArray());
        PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(decryptor);
        PGPSignatureGenerator sGen = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(), PGPUtil.SHA1));
        sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
        in = new FileInputStream(src);
        out = new BCPGOutputStream(new ArmoredOutputStream(new FileOutputStream(dest)));
        int ch = 0;
        while ((ch = in.read()) >= 0) {
            sGen.update((byte) ch);
        }
        sGen.generate().encode(out);
    } catch (PGPException e) {
        throw new IOException(e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
        if (keyIn != null) {
            try {
                keyIn.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : PGPSignatureGenerator(org.bouncycastle.openpgp.PGPSignatureGenerator) PBESecretKeyDecryptor(org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor) BcPGPContentSignerBuilder(org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArmoredOutputStream(org.bouncycastle.bcpg.ArmoredOutputStream) BCPGOutputStream(org.bouncycastle.bcpg.BCPGOutputStream) ArmoredOutputStream(org.bouncycastle.bcpg.ArmoredOutputStream) BcPGPDigestCalculatorProvider(org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider) BCPGOutputStream(org.bouncycastle.bcpg.BCPGOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PGPException(org.bouncycastle.openpgp.PGPException) FileOutputStream(java.io.FileOutputStream) BcPBESecretKeyDecryptorBuilder(org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder) PGPPrivateKey(org.bouncycastle.openpgp.PGPPrivateKey)

Aggregations

PGPException (org.bouncycastle.openpgp.PGPException)22 IOException (java.io.IOException)14 InputStream (java.io.InputStream)7 BcKeyFingerprintCalculator (org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator)7 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)6 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)6 FileInputStream (java.io.FileInputStream)4 ArrayList (java.util.ArrayList)4 PGPPublicKeyRingCollection (org.bouncycastle.openpgp.PGPPublicKeyRingCollection)4 PGPSignature (org.bouncycastle.openpgp.PGPSignature)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3 PGPEncryptedDataList (org.bouncycastle.openpgp.PGPEncryptedDataList)3 PGPObjectFactory (org.bouncycastle.openpgp.PGPObjectFactory)3 PGPPrivateKey (org.bouncycastle.openpgp.PGPPrivateKey)3 PGPPublicKeyEncryptedData (org.bouncycastle.openpgp.PGPPublicKeyEncryptedData)3 PGPSecretKeyRingCollection (org.bouncycastle.openpgp.PGPSecretKeyRingCollection)3 PublicKeyStore.keyIdToString (com.google.gerrit.gpg.PublicKeyStore.keyIdToString)2