Search in sources :

Example 1 with JcaPGPPublicKeyRingCollection

use of org.bouncycastle.openpgp.jcajce.JcaPGPPublicKeyRingCollection in project keywhiz by square.

the class ExpirationExtractor method expirationFromOpenPGP.

@Nullable
public static Instant expirationFromOpenPGP(byte[] content) {
    JcaPGPPublicKeyRingCollection collection;
    try {
        collection = new JcaPGPPublicKeyRingCollection(new ByteArrayInputStream(content));
    } catch (IOException | PGPException e) {
        // Unable to parse
        logger.info("Failed to parse OpenPGP keyring", e);
        return null;
    }
    Instant earliest = null;
    // Iterate over all key rings in file
    Iterator rings = collection.getKeyRings();
    while (rings.hasNext()) {
        Object ringItem = rings.next();
        if (ringItem instanceof PGPPublicKeyRing) {
            PGPPublicKeyRing ring = (PGPPublicKeyRing) ringItem;
            // Iterate over all keys in ring
            Iterator keys = ring.getPublicKeys();
            while (keys.hasNext()) {
                Object keyItem = keys.next();
                if (keyItem instanceof PGPPublicKey) {
                    PGPPublicKey key = (PGPPublicKey) keyItem;
                    // Get validity for key (zero means no expiry)
                    long validSeconds = key.getValidSeconds();
                    if (validSeconds > 0) {
                        Instant expiry = key.getCreationTime().toInstant().plusSeconds(validSeconds);
                        if (earliest == null || expiry.isBefore(earliest)) {
                            earliest = expiry;
                        }
                    }
                }
            }
        }
    }
    return earliest;
}
Also used : PGPException(org.bouncycastle.openpgp.PGPException) PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) ByteArrayInputStream(java.io.ByteArrayInputStream) Instant(java.time.Instant) Iterator(java.util.Iterator) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) PemObject(org.bouncycastle.util.io.pem.PemObject) IOException(java.io.IOException) JcaPGPPublicKeyRingCollection(org.bouncycastle.openpgp.jcajce.JcaPGPPublicKeyRingCollection) Nullable(javax.annotation.Nullable)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1 Iterator (java.util.Iterator)1 Nullable (javax.annotation.Nullable)1 PGPException (org.bouncycastle.openpgp.PGPException)1 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)1 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)1 JcaPGPPublicKeyRingCollection (org.bouncycastle.openpgp.jcajce.JcaPGPPublicKeyRingCollection)1 PemObject (org.bouncycastle.util.io.pem.PemObject)1