Search in sources :

Example 1 with BcPGPObjectFactory

use of org.bouncycastle.openpgp.bc.BcPGPObjectFactory in project gerrit by GerritCodeReview.

the class PostGpgKeys method readKeysToAdd.

private List<PGPPublicKeyRing> readKeysToAdd(Input input, Set<Fingerprint> toRemove) throws BadRequestException, IOException {
    if (input.add == null || input.add.isEmpty()) {
        return ImmutableList.of();
    }
    List<PGPPublicKeyRing> keyRings = new ArrayList<>(input.add.size());
    for (String armored : input.add) {
        try (InputStream in = new ByteArrayInputStream(armored.getBytes(UTF_8));
            ArmoredInputStream ain = new ArmoredInputStream(in)) {
            @SuppressWarnings("unchecked") List<Object> objs = Lists.newArrayList(new BcPGPObjectFactory(ain));
            if (objs.size() != 1 || !(objs.get(0) instanceof PGPPublicKeyRing)) {
                throw new BadRequestException("Expected exactly one PUBLIC KEY BLOCK");
            }
            PGPPublicKeyRing keyRing = (PGPPublicKeyRing) objs.get(0);
            if (toRemove.contains(new Fingerprint(keyRing.getPublicKey().getFingerprint()))) {
                throw new BadRequestException("Cannot both add and delete key: " + keyToString(keyRing.getPublicKey()));
            }
            keyRings.add(keyRing);
        }
    }
    return keyRings;
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) Fingerprint(com.google.gerrit.gpg.Fingerprint) ByteArrayInputStream(java.io.ByteArrayInputStream) ArmoredInputStream(org.bouncycastle.bcpg.ArmoredInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) PublicKeyStore.keyIdToString(com.google.gerrit.gpg.PublicKeyStore.keyIdToString) ByteArrayInputStream(java.io.ByteArrayInputStream) ArmoredInputStream(org.bouncycastle.bcpg.ArmoredInputStream) BcPGPObjectFactory(org.bouncycastle.openpgp.bc.BcPGPObjectFactory) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Example 2 with BcPGPObjectFactory

use of org.bouncycastle.openpgp.bc.BcPGPObjectFactory in project gerrit by GerritCodeReview.

the class PublicKeyStore method get.

private List<PGPPublicKeyRing> get(long keyId, byte[] fp) throws IOException {
    if (reader == null) {
        load();
    }
    if (notes == null) {
        return Collections.emptyList();
    }
    Note note = notes.getNote(keyObjectId(keyId));
    if (note == null) {
        return Collections.emptyList();
    }
    List<PGPPublicKeyRing> keys = new ArrayList<>();
    try (InputStream in = reader.open(note.getData(), OBJ_BLOB).openStream()) {
        while (true) {
            @SuppressWarnings("unchecked") Iterator<Object> it = new BcPGPObjectFactory(new ArmoredInputStream(in)).iterator();
            if (!it.hasNext()) {
                break;
            }
            Object obj = it.next();
            if (obj instanceof PGPPublicKeyRing) {
                PGPPublicKeyRing kr = (PGPPublicKeyRing) obj;
                if (fp == null || Arrays.equals(fp, kr.getPublicKey().getFingerprint())) {
                    keys.add(kr);
                }
            }
            checkState(!it.hasNext(), "expected one PGP object per ArmoredInputStream");
        }
        return keys;
    }
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) ArmoredInputStream(org.bouncycastle.bcpg.ArmoredInputStream) InputStream(java.io.InputStream) Note(org.eclipse.jgit.notes.Note) ArrayList(java.util.ArrayList) ArmoredInputStream(org.bouncycastle.bcpg.ArmoredInputStream) BcPGPObjectFactory(org.bouncycastle.openpgp.bc.BcPGPObjectFactory)

Example 3 with BcPGPObjectFactory

use of org.bouncycastle.openpgp.bc.BcPGPObjectFactory in project gerrit by GerritCodeReview.

the class PublicKeyStore method readKeysFromNote.

private List<PGPPublicKeyRing> readKeysFromNote(Note note, byte[] fp) throws IOException, MissingObjectException, IncorrectObjectTypeException {
    boolean foundAtLeastOneKey = false;
    List<PGPPublicKeyRing> keys = new ArrayList<>();
    ObjectId data = note.getData();
    try (InputStream stream = reader.open(data, OBJ_BLOB).openStream()) {
        byte[] bytes = ByteStreams.toByteArray(stream);
        InputStream in = new ByteArrayInputStream(bytes);
        while (true) {
            @SuppressWarnings("unchecked") Iterator<Object> it = new BcPGPObjectFactory(new ArmoredInputStream(in)).iterator();
            if (!it.hasNext()) {
                break;
            }
            foundAtLeastOneKey = true;
            Object obj = it.next();
            if (obj instanceof PGPPublicKeyRing) {
                PGPPublicKeyRing kr = (PGPPublicKeyRing) obj;
                if (fp == null || Arrays.equals(fp, kr.getPublicKey().getFingerprint())) {
                    keys.add(kr);
                }
            }
            checkState(!it.hasNext(), "expected one PGP object per ArmoredInputStream");
        }
        if (foundAtLeastOneKey) {
            return keys;
        }
        // Subkey handling
        String id = new String(bytes, UTF_8);
        Preconditions.checkArgument(ObjectId.isId(id), "Not valid SHA1: " + id);
        return get(ObjectId.fromString(id), fp);
    }
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) ObjectId(org.eclipse.jgit.lib.ObjectId) ByteArrayInputStream(java.io.ByteArrayInputStream) ArmoredInputStream(org.bouncycastle.bcpg.ArmoredInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) ArmoredInputStream(org.bouncycastle.bcpg.ArmoredInputStream) BcPGPObjectFactory(org.bouncycastle.openpgp.bc.BcPGPObjectFactory)

Example 4 with BcPGPObjectFactory

use of org.bouncycastle.openpgp.bc.BcPGPObjectFactory in project gerrit by GerritCodeReview.

the class PushCertificateChecker method readSignature.

private PGPSignature readSignature(PushCertificate cert) throws IOException {
    ArmoredInputStream in = new ArmoredInputStream(new ByteArrayInputStream(Constants.encode(cert.getSignature())));
    PGPObjectFactory factory = new BcPGPObjectFactory(in);
    Object obj;
    while ((obj = factory.nextObject()) != null) {
        if (obj instanceof PGPSignatureList) {
            PGPSignatureList sigs = (PGPSignatureList) obj;
            if (!sigs.isEmpty()) {
                return sigs.get(0);
            }
        }
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ArmoredInputStream(org.bouncycastle.bcpg.ArmoredInputStream) BcPGPObjectFactory(org.bouncycastle.openpgp.bc.BcPGPObjectFactory) PGPSignatureList(org.bouncycastle.openpgp.PGPSignatureList) PGPObjectFactory(org.bouncycastle.openpgp.PGPObjectFactory) BcPGPObjectFactory(org.bouncycastle.openpgp.bc.BcPGPObjectFactory)

Example 5 with BcPGPObjectFactory

use of org.bouncycastle.openpgp.bc.BcPGPObjectFactory in project gerrit by GerritCodeReview.

the class PostGpgKeys method readKeysToAdd.

private ImmutableList<PGPPublicKeyRing> readKeysToAdd(GpgKeysInput input, Collection<Fingerprint> toRemove) throws BadRequestException, IOException {
    if (input.add == null || input.add.isEmpty()) {
        return ImmutableList.of();
    }
    List<PGPPublicKeyRing> keyRings = new ArrayList<>(input.add.size());
    for (String armored : input.add) {
        try (InputStream in = new ByteArrayInputStream(armored.getBytes(UTF_8));
            ArmoredInputStream ain = new ArmoredInputStream(in)) {
            @SuppressWarnings("unchecked") List<Object> objs = Lists.newArrayList(new BcPGPObjectFactory(ain));
            if (objs.size() != 1 || !(objs.get(0) instanceof PGPPublicKeyRing)) {
                throw new BadRequestException("Expected exactly one PUBLIC KEY BLOCK");
            }
            PGPPublicKeyRing keyRing = (PGPPublicKeyRing) objs.get(0);
            if (toRemove.contains(new Fingerprint(keyRing.getPublicKey().getFingerprint()))) {
                throw new BadRequestException("Cannot both add and delete key: " + keyToString(keyRing.getPublicKey()));
            }
            keyRings.add(keyRing);
        } catch (PGPRuntimeOperationException e) {
            throw new BadRequestException("Failed to parse GPG keys", e);
        }
    }
    return ImmutableList.copyOf(keyRings);
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) Fingerprint(com.google.gerrit.gpg.Fingerprint) PGPRuntimeOperationException(org.bouncycastle.openpgp.PGPRuntimeOperationException) ByteArrayInputStream(java.io.ByteArrayInputStream) ArmoredInputStream(org.bouncycastle.bcpg.ArmoredInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) PublicKeyStore.keyIdToString(com.google.gerrit.gpg.PublicKeyStore.keyIdToString) ByteArrayInputStream(java.io.ByteArrayInputStream) ArmoredInputStream(org.bouncycastle.bcpg.ArmoredInputStream) BcPGPObjectFactory(org.bouncycastle.openpgp.bc.BcPGPObjectFactory) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Aggregations

ArmoredInputStream (org.bouncycastle.bcpg.ArmoredInputStream)5 BcPGPObjectFactory (org.bouncycastle.openpgp.bc.BcPGPObjectFactory)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)4 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 Fingerprint (com.google.gerrit.gpg.Fingerprint)2 PublicKeyStore.keyIdToString (com.google.gerrit.gpg.PublicKeyStore.keyIdToString)2 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)2 PGPObjectFactory (org.bouncycastle.openpgp.PGPObjectFactory)1 PGPRuntimeOperationException (org.bouncycastle.openpgp.PGPRuntimeOperationException)1 PGPSignatureList (org.bouncycastle.openpgp.PGPSignatureList)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 Note (org.eclipse.jgit.notes.Note)1