Search in sources :

Example 11 with PGPPublicKey

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

the class PublicKeyChecker method checkRevocations.

private void checkRevocations(PGPPublicKey key, List<PGPSignature> revocations, Map<Long, RevocationKey> revokers, List<String> problems) throws PGPException, IOException {
    for (PGPSignature revocation : revocations) {
        RevocationKey revoker = revokers.get(revocation.getKeyID());
        if (revoker == null) {
            // Not a designated revoker.
            continue;
        }
        byte[] rfp = revoker.getFingerprint();
        PGPPublicKeyRing revokerKeyRing = store.get(rfp);
        if (revokerKeyRing == null) {
            // Revoker is authorized and there is a revocation signature by this
            // revoker, but the key is not in the store so we can't verify the
            // signature.
            log.info("Key " + Fingerprint.toString(key.getFingerprint()) + " is revoked by " + Fingerprint.toString(rfp) + ", which is not in the store. Assuming revocation is valid.");
            problems.add(reasonToString(getRevocationReason(revocation)));
            continue;
        }
        PGPPublicKey rk = revokerKeyRing.getPublicKey();
        if (rk.getAlgorithm() != revoker.getAlgorithm()) {
            continue;
        }
        if (!checkBasic(rk, revocation.getCreationTime()).isOk()) {
            // revocation is invalid.
            continue;
        }
        revocation.init(new BcPGPContentVerifierBuilderProvider(), rk);
        if (revocation.verifyCertification(key)) {
            problems.add(reasonToString(getRevocationReason(revocation)));
        }
    }
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) RevocationKey(org.bouncycastle.bcpg.sig.RevocationKey) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) BcPGPContentVerifierBuilderProvider(org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider) PGPSignature(org.bouncycastle.openpgp.PGPSignature)

Example 12 with PGPPublicKey

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

the class PublicKeyStoreTest method testKeyToString.

@Test
public void testKeyToString() throws Exception {
    PGPPublicKey key = validKeyWithoutExpiration().getPublicKey();
    assertEquals("46328A8C Testuser One <test1@example.com>" + " (04AE A7ED 2F82 1133 E5B1  28D1 ED06 25DC 4632 8A8C)", keyToString(key));
}
Also used : PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) Test(org.junit.Test)

Example 13 with PGPPublicKey

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

the class PublicKeyStoreTest method updateExisting.

@Test
public void updateExisting() throws Exception {
    TestKey key5 = validKeyWithSecondUserId();
    PGPPublicKeyRing keyRing = key5.getPublicKeyRing();
    PGPPublicKey key = keyRing.getPublicKey();
    store.add(keyRing);
    assertEquals(RefUpdate.Result.NEW, store.save(newCommitBuilder()));
    assertUserIds(store.get(key5.getKeyId()).iterator().next(), "Testuser Five <test5@example.com>", "foo:myId");
    keyRing = PGPPublicKeyRing.removePublicKey(keyRing, key);
    key = PGPPublicKey.removeCertification(key, "foo:myId");
    keyRing = PGPPublicKeyRing.insertPublicKey(keyRing, key);
    store.add(keyRing);
    assertEquals(RefUpdate.Result.FAST_FORWARD, store.save(newCommitBuilder()));
    Iterator<PGPPublicKeyRing> keyRings = store.get(key.getKeyID()).iterator();
    keyRing = keyRings.next();
    assertFalse(keyRings.hasNext());
    assertUserIds(keyRing, "Testuser Five <test5@example.com>");
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) TestKey(com.google.gerrit.gpg.testutil.TestKey) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) Test(org.junit.Test)

Example 14 with PGPPublicKey

use of org.bouncycastle.openpgp.PGPPublicKey in project camel by apache.

the class PGPDataFormatTest method readPublicKey.

static PGPPublicKey readPublicKey(String keyringPath) throws Exception {
    InputStream input = new ByteArrayInputStream(getKeyRing(keyringPath));
    PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input), new BcKeyFingerprintCalculator());
    @SuppressWarnings("rawtypes") Iterator keyRingIter = pgpPub.getKeyRings();
    while (keyRingIter.hasNext()) {
        PGPPublicKeyRing keyRing = (PGPPublicKeyRing) keyRingIter.next();
        @SuppressWarnings("rawtypes") Iterator keyIter = keyRing.getPublicKeys();
        while (keyIter.hasNext()) {
            PGPPublicKey key = (PGPPublicKey) keyIter.next();
            if (key.isEncryptionKey()) {
                return key;
            }
        }
    }
    throw new IllegalArgumentException("Can't find encryption key in key ring.");
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Iterator(java.util.Iterator) BcKeyFingerprintCalculator(org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey)

Example 15 with PGPPublicKey

use of org.bouncycastle.openpgp.PGPPublicKey in project camel by apache.

the class PGPDataFormatUtil method findPublicKeyWithKeyId.

@Deprecated
public static PGPPublicKey findPublicKeyWithKeyId(CamelContext context, String filename, byte[] keyRing, long keyid, boolean forEncryption) throws IOException, PGPException, NoSuchProviderException {
    InputStream is = determineKeyRingInputStream(context, filename, keyRing, forEncryption);
    PGPPublicKey pubKey;
    try {
        pubKey = findPublicKeyWithKeyId(is, keyid);
    } finally {
        IOHelper.close(is);
    }
    return pubKey;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey)

Aggregations

PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)26 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)12 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)8 PublicKeyStore.keyIdToString (com.google.gerrit.gpg.PublicKeyStore.keyIdToString)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 PublicKeyStore (com.google.gerrit.gpg.PublicKeyStore)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 PGPException (org.bouncycastle.openpgp.PGPException)4 GpgKeyInfo (com.google.gerrit.extensions.common.GpgKeyInfo)3 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 CheckResult (com.google.gerrit.gpg.CheckResult)3 Fingerprint (com.google.gerrit.gpg.Fingerprint)3 TestKey (com.google.gerrit.gpg.testutil.TestKey)3 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)3 IOException (java.io.IOException)3 PGPPublicKeyRingCollection (org.bouncycastle.openpgp.PGPPublicKeyRingCollection)3 BcPGPContentVerifierBuilderProvider (org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider)3 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)3