Search in sources :

Example 6 with PGPObjectFactory

use of org.bouncycastle.openpgp.PGPObjectFactory in project gradle by gradle.

the class PublicKeySerializer method read.

@Override
public PGPPublicKey read(Decoder decoder) throws Exception {
    byte[] encoded = decoder.readBinary();
    PGPObjectFactory objectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(new ByteArrayInputStream(encoded)), new BcKeyFingerprintCalculator());
    Object object = objectFactory.nextObject();
    if (object instanceof PGPPublicKey) {
        return (PGPPublicKey) object;
    } else if (object instanceof PGPPublicKeyRing) {
        return ((PGPPublicKeyRing) object).getPublicKey();
    }
    throw new IllegalStateException("Unexpected key in cache: " + object.getClass());
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) ByteArrayInputStream(java.io.ByteArrayInputStream) BcKeyFingerprintCalculator(org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) PGPObjectFactory(org.bouncycastle.openpgp.PGPObjectFactory)

Example 7 with PGPObjectFactory

use of org.bouncycastle.openpgp.PGPObjectFactory in project bisq-desktop by bisq-network.

the class BisqInstaller method verifySignature.

/**
 * Verifies detached PGP signatures against GPG/openPGP RSA public keys. Does currently not work with openssl or JCA/JCE keys.
 *
 * @param pubKeyFile Path to file providing the public key to use
 * @param sigFile    Path to detached signature file
 * @param dataFile   Path to signed data file
 * @return {@code true} if signature is valid, {@code false} if signature is not valid
 * @throws Exception throws various exceptions in case something went wrong. Main reason should be that key or
 *                   signature could be extracted from the provided files due to a "bad" format.<br>
 *                   <code>FileNotFoundException, IOException, SignatureException, PGPException</code>
 */
public static VerifyStatusEnum verifySignature(File pubKeyFile, File sigFile, File dataFile) throws Exception {
    InputStream inputStream;
    int bytesRead;
    PGPPublicKey publicKey;
    PGPSignature pgpSignature;
    boolean result;
    // Read keys from file
    inputStream = PGPUtil.getDecoderStream(new FileInputStream(pubKeyFile));
    PGPPublicKeyRingCollection publicKeyRingCollection = new PGPPublicKeyRingCollection(inputStream, new JcaKeyFingerprintCalculator());
    inputStream.close();
    Iterator<PGPPublicKeyRing> iterator = publicKeyRingCollection.getKeyRings();
    PGPPublicKeyRing pgpPublicKeyRing;
    if (iterator.hasNext()) {
        pgpPublicKeyRing = iterator.next();
    } else {
        throw new PGPException("Could not find public keyring in provided key file");
    }
    // Would be the solution for multiple keys in one file
    // Iterator<PGPPublicKey> kIt;
    // kIt = pgpPublicKeyRing.getPublicKeys();
    // publicKey = pgpPublicKeyRing.getPublicKey(0xF5B84436F379A1C6L);
    // Read signature from file
    inputStream = PGPUtil.getDecoderStream(new FileInputStream(sigFile));
    PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(inputStream, new JcaKeyFingerprintCalculator());
    Object o = pgpObjectFactory.nextObject();
    if (o instanceof PGPSignatureList) {
        PGPSignatureList signatureList = (PGPSignatureList) o;
        checkArgument(!signatureList.isEmpty(), "signatureList must not be empty");
        pgpSignature = signatureList.get(0);
    } else if (o instanceof PGPSignature) {
        pgpSignature = (PGPSignature) o;
    } else {
        throw new SignatureException("Could not find signature in provided signature file");
    }
    inputStream.close();
    log.debug("KeyID used in signature: %X\n", pgpSignature.getKeyID());
    publicKey = pgpPublicKeyRing.getPublicKey(pgpSignature.getKeyID());
    // If signature is not matching the key used for signing we fail
    if (publicKey == null)
        return VerifyStatusEnum.FAIL;
    log.debug("The ID of the selected key is %X\n", publicKey.getKeyID());
    pgpSignature.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
    // Read file to verify
    byte[] data = new byte[1024];
    inputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));
    while (true) {
        bytesRead = inputStream.read(data, 0, 1024);
        if (bytesRead == -1)
            break;
        pgpSignature.update(data, 0, bytesRead);
    }
    inputStream.close();
    // Verify the signature
    result = pgpSignature.verify();
    return result ? VerifyStatusEnum.OK : VerifyStatusEnum.FAIL;
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) PGPSignatureList(org.bouncycastle.openpgp.PGPSignatureList) PGPSignature(org.bouncycastle.openpgp.PGPSignature) SignatureException(java.security.SignatureException) JcaKeyFingerprintCalculator(org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) PGPObjectFactory(org.bouncycastle.openpgp.PGPObjectFactory) PGPException(org.bouncycastle.openpgp.PGPException) PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) BufferedInputStream(java.io.BufferedInputStream) BcPGPContentVerifierBuilderProvider(org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider)

Example 8 with PGPObjectFactory

use of org.bouncycastle.openpgp.PGPObjectFactory in project spring-roo by spring-projects.

the class PgpServiceImpl method getPublicKey.

public PGPPublicKeyRing getPublicKey(final InputStream in) {
    Object obj;
    try {
        final PGPObjectFactory pgpFact = new PGPObjectFactory(PGPUtil.getDecoderStream(in));
        obj = pgpFact.nextObject();
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
    if (obj instanceof PGPPublicKeyRing) {
        final PGPPublicKeyRing keyRing = (PGPPublicKeyRing) obj;
        rememberKey(keyRing);
        return keyRing;
    }
    throw new IllegalStateException("Pblic key not available");
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PGPObjectFactory(org.bouncycastle.openpgp.PGPObjectFactory)

Example 9 with PGPObjectFactory

use of org.bouncycastle.openpgp.PGPObjectFactory in project gradle by gradle.

the class PublicKeyDownloadService method extractKeyRing.

private void extractKeyRing(InputStream stream, PublicKeyResultBuilder builder, Consumer<? super PGPPublicKeyRing> onKeyring) throws IOException {
    try (InputStream decoderStream = PGPUtil.getDecoderStream(stream)) {
        PGPObjectFactory objectFactory = new PGPObjectFactory(decoderStream, new BcKeyFingerprintCalculator());
        PGPPublicKeyRing keyring = (PGPPublicKeyRing) objectFactory.nextObject();
        onKeyring.accept(keyring);
        builder.keyRing(keyring);
    }
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) InputStream(java.io.InputStream) BcKeyFingerprintCalculator(org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator) PGPObjectFactory(org.bouncycastle.openpgp.PGPObjectFactory)

Aggregations

PGPObjectFactory (org.bouncycastle.openpgp.PGPObjectFactory)9 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)5 BcKeyFingerprintCalculator (org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator)5 InputStream (java.io.InputStream)4 PGPException (org.bouncycastle.openpgp.PGPException)4 PGPSignatureList (org.bouncycastle.openpgp.PGPSignatureList)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 PGPCompressedData (org.bouncycastle.openpgp.PGPCompressedData)2 PGPEncryptedDataList (org.bouncycastle.openpgp.PGPEncryptedDataList)2 PGPPrivateKey (org.bouncycastle.openpgp.PGPPrivateKey)2 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)2 PGPPublicKeyEncryptedData (org.bouncycastle.openpgp.PGPPublicKeyEncryptedData)2 PGPSignature (org.bouncycastle.openpgp.PGPSignature)2 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 SignatureException (java.security.SignatureException)1 OutputStreamBuilder (org.apache.camel.converter.stream.OutputStreamBuilder)1