Search in sources :

Example 1 with PGPPublicKeyRingCollection

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

the class PublicKeyStoreTest method assertKeys.

private void assertKeys(long keyId, TestKey... expected) throws Exception {
    Set<String> expectedStrings = new TreeSet<>();
    for (TestKey k : expected) {
        expectedStrings.add(keyToString(k.getPublicKey()));
    }
    PGPPublicKeyRingCollection actual = store.get(keyId);
    Set<String> actualStrings = new TreeSet<>();
    for (PGPPublicKeyRing k : actual) {
        actualStrings.add(keyToString(k.getPublicKey()));
    }
    assertEquals(expectedStrings, actualStrings);
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) TestKey(com.google.gerrit.gpg.testutil.TestKey) PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) TreeSet(java.util.TreeSet) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) PublicKeyStore.keyIdToString(com.google.gerrit.gpg.PublicKeyStore.keyIdToString)

Example 2 with PGPPublicKeyRingCollection

use of org.bouncycastle.openpgp.PGPPublicKeyRingCollection 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 3 with PGPPublicKeyRingCollection

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

the class PushCertificateChecker method checkSignature.

private Result checkSignature(PGPSignature sig, PushCertificate cert, PublicKeyStore store) throws PGPException, IOException {
    PGPPublicKeyRingCollection keys = store.get(sig.getKeyID());
    if (!keys.getKeyRings().hasNext()) {
        return new Result(null, CheckResult.bad("No public keys found for key ID " + keyIdToString(sig.getKeyID())));
    }
    PGPPublicKey signer = PublicKeyStore.getSigner(keys, sig, Constants.encode(cert.toText()));
    if (signer == null) {
        return new Result(null, CheckResult.bad("Signature by " + keyIdToString(sig.getKeyID()) + " is not valid"));
    }
    CheckResult result = publicKeyChecker.setStore(store).setEffectiveTime(sig.getCreationTime()).check(signer);
    if (!result.getProblems().isEmpty()) {
        StringBuilder err = new StringBuilder("Invalid public key ").append(keyToString(signer)).append(":\n  ").append(Joiner.on("\n  ").join(result.getProblems()));
        return new Result(signer, CheckResult.create(result.getStatus(), err.toString()));
    }
    return new Result(signer, result);
}
Also used : PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey)

Example 4 with PGPPublicKeyRingCollection

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

the class PublicKeyChecker method getSigner.

private static PGPPublicKey getSigner(PublicKeyStore store, PGPSignature sig, String userId, PGPPublicKey key, List<CheckResult> results) {
    try {
        PGPPublicKeyRingCollection signers = store.get(sig.getKeyID());
        if (!signers.getKeyRings().hasNext()) {
            results.add(CheckResult.ok("Key " + keyIdToString(sig.getKeyID()) + " used for certification is not in store"));
            return null;
        }
        PGPPublicKey signer = PublicKeyStore.getSigner(signers, sig, userId, key);
        if (signer == null) {
            results.add(CheckResult.ok("Certification by " + keyIdToString(sig.getKeyID()) + " is not valid"));
            return null;
        }
        return signer;
    } catch (PGPException | IOException e) {
        results.add(CheckResult.ok("Error checking certification by " + keyIdToString(sig.getKeyID())));
        return null;
    }
}
Also used : PGPException(org.bouncycastle.openpgp.PGPException) PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) IOException(java.io.IOException)

Example 5 with PGPPublicKeyRingCollection

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

the class PublicKeyStore method deleteFromNotes.

private void deleteFromNotes(ObjectInserter ins, Fingerprint fp) throws PGPException, IOException {
    long keyId = fp.getId();
    PGPPublicKeyRingCollection existing = get(keyId);
    List<PGPPublicKeyRing> toWrite = new ArrayList<>(existing.size());
    for (PGPPublicKeyRing kr : existing) {
        if (!fp.equalsBytes(kr.getPublicKey().getFingerprint())) {
            toWrite.add(kr);
        }
    }
    if (toWrite.size() == existing.size()) {
        return;
    } else if (!toWrite.isEmpty()) {
        notes.set(keyObjectId(keyId), ins.insert(OBJ_BLOB, keysToArmored(toWrite)));
    } else {
        notes.remove(keyObjectId(keyId));
    }
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) ArrayList(java.util.ArrayList)

Aggregations

PGPPublicKeyRingCollection (org.bouncycastle.openpgp.PGPPublicKeyRingCollection)6 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)4 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)3 ArrayList (java.util.ArrayList)2 PublicKeyStore.keyIdToString (com.google.gerrit.gpg.PublicKeyStore.keyIdToString)1 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)1 TestKey (com.google.gerrit.gpg.testutil.TestKey)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Iterator (java.util.Iterator)1 TreeSet (java.util.TreeSet)1 PGPException (org.bouncycastle.openpgp.PGPException)1 BcKeyFingerprintCalculator (org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator)1