Search in sources :

Example 11 with PGPSecretKeyRing

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

the class PGPDataFormatUtil method findPrivateKeyWithkeyId.

public static PGPPrivateKey findPrivateKeyWithkeyId(long keyid, String passphrase, PGPPassphraseAccessor passphraseAccessor, String provider, PGPSecretKeyRingCollection pgpSec) throws PGPException {
    for (Iterator<?> i = pgpSec.getKeyRings(); i.hasNext(); ) {
        Object data = i.next();
        if (data instanceof PGPSecretKeyRing) {
            PGPSecretKeyRing keyring = (PGPSecretKeyRing) data;
            PGPSecretKey secKey = keyring.getSecretKey(keyid);
            if (secKey != null) {
                if (passphrase == null && passphraseAccessor != null) {
                    // get passphrase from accessor // only primary/master key has user IDS
                    @SuppressWarnings("unchecked") Iterator<String> userIDs = keyring.getSecretKey().getUserIDs();
                    while (passphrase == null && userIDs.hasNext()) {
                        passphrase = passphraseAccessor.getPassphrase(userIDs.next());
                    }
                }
                if (passphrase != null) {
                    PGPPrivateKey privateKey = secKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider(provider).build(passphrase.toCharArray()));
                    if (privateKey != null) {
                        return privateKey;
                    }
                }
            }
        }
    }
    return null;
}
Also used : PGPSecretKey(org.bouncycastle.openpgp.PGPSecretKey) PGPPrivateKey(org.bouncycastle.openpgp.PGPPrivateKey) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) JcePBESecretKeyDecryptorBuilder(org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder)

Example 12 with PGPSecretKeyRing

use of org.bouncycastle.openpgp.PGPSecretKeyRing 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)

Example 13 with PGPSecretKeyRing

use of org.bouncycastle.openpgp.PGPSecretKeyRing in project gradle by gradle.

the class PgpSignatoryFactory method findSecretKey.

@Nullable
private PGPSecretKey findSecretKey(PGPSecretKeyRingCollection keyRings, PgpKeyId keyId) {
    Iterator<PGPSecretKeyRing> keyRingIterator = uncheckedCast(keyRings.getKeyRings());
    while (keyRingIterator.hasNext()) {
        PGPSecretKeyRing keyRing = keyRingIterator.next();
        Iterator<PGPSecretKey> secretKeyIterator = uncheckedCast(keyRing.getSecretKeys());
        while (secretKeyIterator.hasNext()) {
            PGPSecretKey secretKey = secretKeyIterator.next();
            if (hasId(keyId, secretKey)) {
                return secretKey;
            }
        }
    }
    return null;
}
Also used : PGPSecretKey(org.bouncycastle.openpgp.PGPSecretKey) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) Nullable(javax.annotation.Nullable)

Example 14 with PGPSecretKeyRing

use of org.bouncycastle.openpgp.PGPSecretKeyRing in project ant-ivy by apache.

the class OpenPGPSignatureGenerator method readSecretKey.

private PGPSecretKey readSecretKey(InputStream in) throws IOException, PGPException {
    in = PGPUtil.getDecoderStream(in);
    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in, new BcKeyFingerprintCalculator());
    PGPSecretKey key = null;
    Iterator<PGPSecretKeyRing> it = pgpSec.getKeyRings();
    while (key == null && it.hasNext()) {
        PGPSecretKeyRing kRing = it.next();
        Iterator<PGPSecretKey> it2 = kRing.getSecretKeys();
        while (key == null && it2.hasNext()) {
            PGPSecretKey k = it2.next();
            if (keyId == null && k.isSigningKey()) {
                key = k;
            }
            if (keyId != null && Long.valueOf(keyId, 16) == (k.getKeyID() & MASK)) {
                key = k;
            }
        }
    }
    if (key == null) {
        throw new IllegalArgumentException("Can't find encryption key" + (keyId != null ? " '" + keyId + "' " : " ") + "in key ring.");
    }
    return key;
}
Also used : PGPSecretKey(org.bouncycastle.openpgp.PGPSecretKey) PGPSecretKeyRingCollection(org.bouncycastle.openpgp.PGPSecretKeyRingCollection) BcKeyFingerprintCalculator(org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing)

Example 15 with PGPSecretKeyRing

use of org.bouncycastle.openpgp.PGPSecretKeyRing in project Smack by igniterealtime.

the class OXSecretKeyBackupIntegrationTest method test.

@SmackIntegrationTest
public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException, InterruptedException, PubSubException.NotALeafNodeException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NotLoggedInException, SmackException.FeatureNotSupportedException, MissingUserIdOnKeyException, NoBackupFoundException, InvalidBackupCodeException, PGPException, MissingOpenPgpKeyException {
    OpenPgpStore beforeStore = new FileBasedOpenPgpStore(beforePath);
    beforeStore.setKeyRingProtector(new UnprotectedKeysProtector());
    PainlessOpenPgpProvider beforeProvider = new PainlessOpenPgpProvider(beforeStore);
    OpenPgpManager openPgpManager = OpenPgpManager.getInstanceFor(aliceConnection);
    openPgpManager.setOpenPgpProvider(beforeProvider);
    OpenPgpSelf self = openPgpManager.getOpenPgpSelf();
    assertNull(self.getSigningKeyFingerprint());
    OpenPgpV4Fingerprint keyFingerprint = openPgpManager.generateAndImportKeyPair(alice);
    assertEquals(keyFingerprint, self.getSigningKeyFingerprint());
    assertTrue(self.getSecretKeys().contains(keyFingerprint.getKeyId()));
    PGPSecretKeyRing beforeSec = beforeStore.getSecretKeyRing(alice, keyFingerprint);
    assertNotNull(beforeSec);
    PGPPublicKeyRing beforePub = beforeStore.getPublicKeyRing(alice, keyFingerprint);
    assertNotNull(beforePub);
    OpenPgpSecretKeyBackupPassphrase backupPassphrase = openPgpManager.backupSecretKeyToServer(availableSecretKeys -> availableSecretKeys);
    FileBasedOpenPgpStore afterStore = new FileBasedOpenPgpStore(afterPath);
    afterStore.setKeyRingProtector(new UnprotectedKeysProtector());
    PainlessOpenPgpProvider afterProvider = new PainlessOpenPgpProvider(afterStore);
    openPgpManager.setOpenPgpProvider(afterProvider);
    OpenPgpV4Fingerprint fingerprint = openPgpManager.restoreSecretKeyServerBackup(() -> backupPassphrase);
    assertEquals(keyFingerprint, fingerprint);
    assertTrue(self.getSecretKeys().contains(keyFingerprint.getKeyId()));
    assertEquals(keyFingerprint, self.getSigningKeyFingerprint());
    PGPSecretKeyRing afterSec = afterStore.getSecretKeyRing(alice, keyFingerprint);
    assertNotNull(afterSec);
    assertArrayEquals(beforeSec.getEncoded(), afterSec.getEncoded());
    PGPPublicKeyRing afterPub = afterStore.getPublicKeyRing(alice, keyFingerprint);
    assertNotNull(afterPub);
    assertArrayEquals(beforePub.getEncoded(), afterPub.getEncoded());
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) FileBasedOpenPgpStore(org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore) UnprotectedKeysProtector(org.pgpainless.key.protection.UnprotectedKeysProtector) FileBasedOpenPgpStore(org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore) OpenPgpStore(org.jivesoftware.smackx.ox.store.definition.OpenPgpStore) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) PainlessOpenPgpProvider(org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Aggregations

PGPSecretKeyRing (org.bouncycastle.openpgp.PGPSecretKeyRing)22 OpenPgpV4Fingerprint (org.pgpainless.key.OpenPgpV4Fingerprint)11 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)6 PGPSecretKey (org.bouncycastle.openpgp.PGPSecretKey)6 Test (org.junit.Test)6 PGPSecretKeyRingCollection (org.bouncycastle.openpgp.PGPSecretKeyRingCollection)5 BcKeyFingerprintCalculator (org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator)3 JcePBESecretKeyDecryptorBuilder (org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder)3 PainlessOpenPgpProvider (org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider)3 SecretkeyElement (org.jivesoftware.smackx.ox.element.SecretkeyElement)3 OpenPgpStore (org.jivesoftware.smackx.ox.store.definition.OpenPgpStore)3 FileBasedOpenPgpStore (org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore)3 PGPPrivateKey (org.bouncycastle.openpgp.PGPPrivateKey)2 Test (org.junit.jupiter.api.Test)2 BareJid (org.jxmpp.jid.BareJid)2 UnprotectedKeysProtector (org.pgpainless.key.protection.UnprotectedKeysProtector)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1