Search in sources :

Example 6 with PGPSecretKey

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

the class PGPDataFormatUtil method findSecretKeysWithPrivateKeyAndUserId.

public static List<PGPSecretKeyAndPrivateKeyAndUserId> findSecretKeysWithPrivateKeyAndUserId(Map<String, String> sigKeyUserId2Password, String provider, PGPSecretKeyRingCollection pgpSec) throws PGPException {
    List<PGPSecretKeyAndPrivateKeyAndUserId> result = new ArrayList<PGPSecretKeyAndPrivateKeyAndUserId>(sigKeyUserId2Password.size());
    for (Iterator<?> i = pgpSec.getKeyRings(); i.hasNext(); ) {
        Object data = i.next();
        if (data instanceof PGPSecretKeyRing) {
            PGPSecretKeyRing keyring = (PGPSecretKeyRing) data;
            PGPSecretKey primaryKey = keyring.getSecretKey();
            List<String> useridParts = new ArrayList<String>(sigKeyUserId2Password.keySet());
            String[] foundKeyUserIdForUserIdPart = findFirstKeyUserIdContainingOneOfTheParts(useridParts, primaryKey.getPublicKey());
            if (foundKeyUserIdForUserIdPart == null) {
                LOG.debug("No User ID found in primary key with key ID {} containing one of the parts {}", primaryKey.getKeyID(), useridParts);
                continue;
            }
            LOG.debug("User ID {} found in primary key with key ID {} containing one of the parts {}", new Object[] { foundKeyUserIdForUserIdPart[0], primaryKey.getKeyID(), useridParts });
            // add all signing keys
            for (Iterator<PGPSecretKey> iterKey = keyring.getSecretKeys(); iterKey.hasNext(); ) {
                PGPSecretKey secKey = iterKey.next();
                if (isSigningKey(secKey)) {
                    PGPPrivateKey privateKey = secKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider(provider).build(sigKeyUserId2Password.get(foundKeyUserIdForUserIdPart[1]).toCharArray()));
                    if (privateKey != null) {
                        result.add(new PGPSecretKeyAndPrivateKeyAndUserId(secKey, privateKey, foundKeyUserIdForUserIdPart[0]));
                        LOG.debug("Private key with user ID {} and key ID {} added to the signing keys", foundKeyUserIdForUserIdPart[0], Long.toString(privateKey.getKeyID()));
                    }
                }
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) JcePBESecretKeyDecryptorBuilder(org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder) PGPSecretKey(org.bouncycastle.openpgp.PGPSecretKey) PGPPrivateKey(org.bouncycastle.openpgp.PGPPrivateKey) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing)

Aggregations

PGPSecretKey (org.bouncycastle.openpgp.PGPSecretKey)6 PGPPrivateKey (org.bouncycastle.openpgp.PGPPrivateKey)4 PGPSecretKeyRing (org.bouncycastle.openpgp.PGPSecretKeyRing)4 JcePBESecretKeyDecryptorBuilder (org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 PGPSecretKeyRingCollection (org.bouncycastle.openpgp.PGPSecretKeyRingCollection)2 BcKeyFingerprintCalculator (org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 BCPGOutputStream (org.bouncycastle.bcpg.BCPGOutputStream)1 PGPEncryptedDataList (org.bouncycastle.openpgp.PGPEncryptedDataList)1 PGPException (org.bouncycastle.openpgp.PGPException)1 PGPObjectFactory (org.bouncycastle.openpgp.PGPObjectFactory)1 PGPPublicKeyEncryptedData (org.bouncycastle.openpgp.PGPPublicKeyEncryptedData)1 PGPSignatureGenerator (org.bouncycastle.openpgp.PGPSignatureGenerator)1 JcaPGPContentSignerBuilder (org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder)1 Nullable (org.gradle.api.Nullable)1