Search in sources :

Example 16 with PGPSecretKeyRing

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

the class OpenPgpManager method backupSecretKeyToServer.

/**
 * Upload the encrypted secret key to a private PEP node.
 * The backup is encrypted using the provided secret key passphrase.
 *
 * @see <a href="https://xmpp.org/extensions/xep-0373.html#synchro-pep">XEP-0373 ยง5</a>
 *
 * @param selectKeyCallback callback, which will receive the users choice of which keys will be backed up.
 * @param passphrase secret key passphrase
 *
 * @throws InterruptedException if the thread is interrupted.
 * @throws PubSubException.NotALeafNodeException if the private node is not a {@link LeafNode}.
 * @throws XMPPException.XMPPErrorException in case of an XMPP protocol error.
 * @throws SmackException.NotConnectedException if we are not connected.
 * @throws SmackException.NoResponseException if the server doesn't respond.
 * @throws SmackException.NotLoggedInException if we are not logged in.
 * @throws IOException IO is dangerous.
 * @throws SmackException.FeatureNotSupportedException if the server doesn't support the PubSub whitelist access model.
 * @throws PGPException PGP is brittle
 * @throws MissingOpenPgpKeyException in case we have no OpenPGP key pair to back up.
 */
public void backupSecretKeyToServer(SecretKeyBackupSelectionCallback selectKeyCallback, OpenPgpSecretKeyBackupPassphrase passphrase) throws InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException, SmackException.NotLoggedInException, IOException, SmackException.FeatureNotSupportedException, PGPException, MissingOpenPgpKeyException {
    throwIfNoProviderSet();
    throwIfNotAuthenticated();
    BareJid ownJid = connection().getUser().asBareJid();
    PGPSecretKeyRingCollection secretKeyRings = provider.getStore().getSecretKeysOf(ownJid);
    Set<OpenPgpV4Fingerprint> availableKeyPairs = new HashSet<>();
    for (PGPSecretKeyRing ring : secretKeyRings) {
        availableKeyPairs.add(new OpenPgpV4Fingerprint(ring));
    }
    Set<OpenPgpV4Fingerprint> selectedKeyPairs = selectKeyCallback.selectKeysToBackup(availableKeyPairs);
    SecretkeyElement secretKey = SecretKeyBackupHelper.createSecretkeyElement(provider, ownJid, selectedKeyPairs, passphrase);
    OpenPgpPubSubUtil.depositSecretKey(connection(), secretKey);
}
Also used : SecretkeyElement(org.jivesoftware.smackx.ox.element.SecretkeyElement) EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid) PGPSecretKeyRingCollection(org.bouncycastle.openpgp.PGPSecretKeyRingCollection) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) HashSet(java.util.HashSet)

Example 17 with PGPSecretKeyRing

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

the class OpenPgpManager method generateKeyRing.

public PGPSecretKeyRing generateKeyRing(BareJid ourJid) throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
    throwIfNoProviderSet();
    PGPSecretKeyRing keys = provider.getStore().generateKeyRing(ourJid);
    return keys;
}
Also used : PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing)

Example 18 with PGPSecretKeyRing

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

the class OpenPgpStoreTest method t05_key_wrongBareJidOnPublicKeyImportTest.

@Test
public void t05_key_wrongBareJidOnPublicKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
    PGPSecretKeyRing secretKeys = openPgpStoreInstance1.generateKeyRing(alice);
    PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
    assertThrows(MissingUserIdOnKeyException.class, () -> openPgpStoreInstance1.importPublicKey(bob, publicKeys));
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) Test(org.junit.Test)

Example 19 with PGPSecretKeyRing

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

the class OpenPgpStoreTest method t04_key_wrongBareJidOnSecretKeyImportTest.

@Test
public void t04_key_wrongBareJidOnSecretKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
    PGPSecretKeyRing secretKeys = openPgpStoreInstance1.generateKeyRing(alice);
    assertThrows(MissingUserIdOnKeyException.class, () -> openPgpStoreInstance1.importSecretKey(bob, secretKeys));
}
Also used : PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) Test(org.junit.Test)

Example 20 with PGPSecretKeyRing

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

the class SecretKeyBackupHelperTest method createAndDecryptSecretKeyElementTest.

@Test
public void createAndDecryptSecretKeyElementTest() throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException, MissingOpenPgpKeyException, InvalidBackupCodeException {
    // Prepare store and provider and so on...
    FileBasedOpenPgpStore store = new FileBasedOpenPgpStore(basePath);
    PainlessOpenPgpProvider provider = new PainlessOpenPgpProvider(store);
    // Generate and import key
    PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("xmpp:alice@wonderland.lit");
    BareJid jid = JidCreate.bareFrom("alice@wonderland.lit");
    provider.getStore().importSecretKey(jid, secretKeys);
    // Create encrypted backup
    OpenPgpSecretKeyBackupPassphrase backupCode = SecretKeyBackupHelper.generateBackupPassword();
    SecretkeyElement element = SecretKeyBackupHelper.createSecretkeyElement(provider, jid, Collections.singleton(new OpenPgpV4Fingerprint(secretKeys)), backupCode);
    // Decrypt backup and compare
    PGPSecretKeyRing secretKeyRing = SecretKeyBackupHelper.restoreSecretKeyBackup(element, backupCode);
    Assertions.assertArrayEquals(secretKeys.getEncoded(), secretKeyRing.getEncoded());
}
Also used : SecretkeyElement(org.jivesoftware.smackx.ox.element.SecretkeyElement) FileBasedOpenPgpStore(org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore) BareJid(org.jxmpp.jid.BareJid) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) PainlessOpenPgpProvider(org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider) Test(org.junit.jupiter.api.Test)

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