1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 20:42:06 +01:00
This commit is contained in:
Paul Schaub 2018-06-19 16:41:02 +02:00
parent 63e98cb4d6
commit e23cf88082
4 changed files with 84 additions and 7 deletions

View file

@ -239,12 +239,11 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
} }
} }
return decryptImpl(bytes, secretKeyRings, protector, trustedKeyIds, trustedKeys); return decryptImpl(bytes, secretKeyRings, protector, trustedKeys);
} }
DecryptedBytesAndMetadata decryptImpl(byte[] bytes, PGPSecretKeyRingCollection decryptionKeys, DecryptedBytesAndMetadata decryptImpl(byte[] bytes, PGPSecretKeyRingCollection decryptionKeys,
SecretKeyRingProtector protector, SecretKeyRingProtector protector,
Set<Long> trustedKeyIds,
Set<PGPPublicKeyRing> verificationKeys) Set<PGPPublicKeyRing> verificationKeys)
throws SmackOpenPgpException, IOException { throws SmackOpenPgpException, IOException {
@ -255,7 +254,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
fromEncrypted = PGPainless.createDecryptor() fromEncrypted = PGPainless.createDecryptor()
.onInputStream(encryptedBytes) .onInputStream(encryptedBytes)
.decryptWith(decryptionKeys, protector) .decryptWith(decryptionKeys, protector)
.verifyWith(trustedKeyIds, verificationKeys) .verifyWith(verificationKeys)
.ignoreMissingPublicKeys() .ignoreMissingPublicKeys()
.build(); .build();
} catch (IOException | PGPException e) { } catch (IOException | PGPException e) {

View file

@ -0,0 +1,67 @@
package org.jivesoftware.smackx.ox.bouncycastle;
import static junit.framework.TestCase.assertTrue;
import java.io.File;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Arrays;
import java.util.Collections;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import de.vanitasvitae.crypto.pgpainless.PGPainless;
import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
import de.vanitasvitae.crypto.pgpainless.key.generation.type.length.RsaLength;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.junit.Test;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
public class FileBasedPainlessOpenPgpStoreTest extends SmackTestSuite {
private static final File basePath;
private static final BareJid alice;
private static final BareJid bob;
static {
String userHome = System.getProperty("user.home");
if (userHome != null) {
File f = new File(userHome);
basePath = new File(f, ".config/smack-integration-test/ox/painless_ox_store");
} else {
basePath = new File("painless_ox_store");
}
try {
alice = JidCreate.bareFrom("alice@wonderland.lit");
bob = JidCreate.bareFrom("bob@builder.tv");
} catch (XmppStringprepException e) {
throw new AssertionError(e);
}
Security.addProvider(new BouncyCastleProvider());
}
@Test
public void storeSecretKeyRingsTest()
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
IOException {
FileBasedPainlessOpenPgpStore store = new FileBasedPainlessOpenPgpStore(basePath, new UnprotectedKeysProtector());
PGPSecretKeyRing secretKey = PGPainless.generateKeyRing().simpleRsaKeyRing("xmpp:" + alice.toString(), RsaLength._3072);
PGPSecretKeyRingCollection saving = new PGPSecretKeyRingCollection(Collections.singleton(secretKey));
store.storeSecretKeyRing(alice, saving);
PGPSecretKeyRingCollection restored = store.getSecretKeyRings(alice);
assertTrue(Arrays.equals(saving.getEncoded(), restored.getEncoded()));
}
}

View file

@ -78,10 +78,12 @@ public final class OXInstantMessagingManager extends Manager {
public static OXInstantMessagingManager getInstanceFor(XMPPConnection connection) { public static OXInstantMessagingManager getInstanceFor(XMPPConnection connection) {
OXInstantMessagingManager manager = INSTANCES.get(connection); OXInstantMessagingManager manager = INSTANCES.get(connection);
if (manager == null) { if (manager == null) {
manager = new OXInstantMessagingManager(connection); manager = new OXInstantMessagingManager(connection);
INSTANCES.put(connection, manager); INSTANCES.put(connection, manager);
} }
return manager; return manager;
} }
@ -123,10 +125,17 @@ public final class OXInstantMessagingManager extends Manager {
throws SmackOpenPgpException, InterruptedException, XMPPException.XMPPErrorException, throws SmackOpenPgpException, InterruptedException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException { SmackException.NotConnectedException, SmackException.NoResponseException {
OpenPgpEncryptedChat encryptedChat = chats.get(jid);
if (encryptedChat == null) {
OpenPgpFingerprints theirKeys = openPgpManager.determineContactsKeys(jid); OpenPgpFingerprints theirKeys = openPgpManager.determineContactsKeys(jid);
OpenPgpFingerprints ourKeys = openPgpManager.determineContactsKeys(connection().getUser().asBareJid()); OpenPgpFingerprints ourKeys = openPgpManager.determineContactsKeys(connection().getUser().asBareJid());
Chat chat = chatManager.chatWith(jid); Chat chat = chatManager.chatWith(jid);
return new OpenPgpEncryptedChat(openPgpManager.getOpenPgpProvider(), chat, ourKeys, theirKeys); encryptedChat = new OpenPgpEncryptedChat(openPgpManager.getOpenPgpProvider(), chat, ourKeys, theirKeys);
chats.put(jid, encryptedChat);
}
return encryptedChat;
} }
public boolean addOpenPgpEncryptedMessageListener(OpenPgpEncryptedMessageListener listener) { public boolean addOpenPgpEncryptedMessageListener(OpenPgpEncryptedMessageListener listener) {

View file

@ -53,6 +53,8 @@ public interface OpenPgpStore {
* *
* @param owner owner. * @param owner owner.
* @return set of fingerprints of available OpenPGP key pairs master keys. * @return set of fingerprints of available OpenPGP key pairs master keys.
*
* @throws SmackOpenPgpException
*/ */
Set<OpenPgpV4Fingerprint> getAvailableKeyPairFingerprints(BareJid owner) throws SmackOpenPgpException; Set<OpenPgpV4Fingerprint> getAvailableKeyPairFingerprints(BareJid owner) throws SmackOpenPgpException;