mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-23 20:42:06 +01:00
Add DryOxEncryption test
This commit is contained in:
parent
cdba5bdda9
commit
ef00f50d79
3 changed files with 76 additions and 15 deletions
|
@ -89,7 +89,10 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
|
||||||
SecretKeyRingProtector protector = getStore().getSecretKeyProtector();
|
SecretKeyRingProtector protector = getStore().getSecretKeyProtector();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
secretKeyRing = getStore().getSecretKeyRings(owner).getSecretKeyRing(signingKey.getKeyId());
|
secretKeyRing = getStore()
|
||||||
|
.getSecretKeyRings(owner)
|
||||||
|
.getSecretKeyRing(
|
||||||
|
signingKey.getKeyId());
|
||||||
} catch (PGPException e) {
|
} catch (PGPException e) {
|
||||||
LOGGER.log(Level.INFO, "Could not get secret key with id " + Long.toHexString(signingKey.getKeyId()), e);
|
LOGGER.log(Level.INFO, "Could not get secret key with id " + Long.toHexString(signingKey.getKeyId()), e);
|
||||||
throw new MissingOpenPgpKeyPairException(owner, signingKey, e);
|
throw new MissingOpenPgpKeyPairException(owner, signingKey, e);
|
||||||
|
|
|
@ -16,6 +16,10 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.ox.bouncycastle;
|
package org.jivesoftware.smackx.ox.bouncycastle;
|
||||||
|
|
||||||
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
import static junit.framework.TestCase.assertNotNull;
|
||||||
|
import static junit.framework.TestCase.assertTrue;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.security.InvalidAlgorithmParameterException;
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
|
@ -23,39 +27,56 @@ import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.NoSuchProviderException;
|
import java.security.NoSuchProviderException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smack.util.FileUtils;
|
import org.jivesoftware.smack.util.FileUtils;
|
||||||
import org.jivesoftware.smack.util.stringencoder.Base64;
|
import org.jivesoftware.smack.util.stringencoder.Base64;
|
||||||
|
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
|
||||||
|
import org.jivesoftware.smackx.ox.chat.OpenPgpContact;
|
||||||
|
import org.jivesoftware.smackx.ox.chat.OpenPgpFingerprints;
|
||||||
|
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
|
||||||
|
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
||||||
import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
||||||
|
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
||||||
|
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyPairException;
|
||||||
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpPublicKeyException;
|
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpPublicKeyException;
|
||||||
import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException;
|
import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException;
|
||||||
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
|
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
|
||||||
import org.jivesoftware.smackx.ox.util.KeyBytesAndFingerprint;
|
import org.jivesoftware.smackx.ox.util.KeyBytesAndFingerprint;
|
||||||
|
|
||||||
import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
|
import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
import org.junit.AfterClass;
|
||||||
import org.junit.After;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.jxmpp.jid.BareJid;
|
import org.jxmpp.jid.BareJid;
|
||||||
import org.jxmpp.jid.JidTestUtil;
|
import org.jxmpp.jid.JidTestUtil;
|
||||||
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
|
||||||
public class DryOxEncryptionTest extends OxTestSuite {
|
public class DryOxEncryptionTest extends OxTestSuite {
|
||||||
|
|
||||||
private final File alicePath = FileUtils.getTempDir("ox-alice");
|
private static final Logger LOGGER = Logger.getLogger(DryOxEncryptionTest.class.getName());
|
||||||
private final File bobPath = FileUtils.getTempDir("ox-bob");
|
|
||||||
|
|
||||||
@Before
|
private static final File alicePath = FileUtils.getTempDir("ox-alice");
|
||||||
@After
|
private static final File bobPath = FileUtils.getTempDir("ox-bob");
|
||||||
public void deletePath() {
|
|
||||||
|
@BeforeClass
|
||||||
|
@AfterClass
|
||||||
|
public static void deletePath() {
|
||||||
|
LOGGER.log(Level.INFO, "Delete paths " + alicePath.getAbsolutePath() + " " + bobPath.getAbsolutePath());
|
||||||
FileUtils.deleteDirectory(alicePath);
|
FileUtils.deleteDirectory(alicePath);
|
||||||
FileUtils.deleteDirectory(bobPath);
|
FileUtils.deleteDirectory(bobPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dryEncryptionTest()
|
public void dryEncryptionTest()
|
||||||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||||
IOException, SmackOpenPgpException, MissingUserIdOnKeyException, MissingOpenPgpPublicKeyException {
|
IOException, SmackOpenPgpException, MissingUserIdOnKeyException, MissingOpenPgpPublicKeyException,
|
||||||
|
MissingOpenPgpKeyPairException, XmlPullParserException {
|
||||||
BareJid alice = JidTestUtil.BARE_JID_1;
|
BareJid alice = JidTestUtil.BARE_JID_1;
|
||||||
BareJid bob = JidTestUtil.BARE_JID_2;
|
BareJid bob = JidTestUtil.BARE_JID_2;
|
||||||
|
|
||||||
|
@ -71,6 +92,14 @@ public class DryOxEncryptionTest extends OxTestSuite {
|
||||||
aliceProvider.importSecretKey(alice, aliceKey.getBytes());
|
aliceProvider.importSecretKey(alice, aliceKey.getBytes());
|
||||||
bobProvider.importSecretKey(bob, bobKey.getBytes());
|
bobProvider.importSecretKey(bob, bobKey.getBytes());
|
||||||
|
|
||||||
|
byte[] alicePubBytes = aliceStore.getPublicKeyRingBytes(alice, aliceKey.getFingerprint());
|
||||||
|
byte[] bobPubBytes = bobStore.getPublicKeyRingBytes(bob, bobKey.getFingerprint());
|
||||||
|
|
||||||
|
assertNotNull(alicePubBytes);
|
||||||
|
assertNotNull(bobPubBytes);
|
||||||
|
assertTrue(alicePubBytes.length != 0);
|
||||||
|
assertTrue(bobPubBytes.length != 0);
|
||||||
|
|
||||||
PubkeyElement alicePub = new PubkeyElement(new PubkeyElement.PubkeyDataElement(
|
PubkeyElement alicePub = new PubkeyElement(new PubkeyElement.PubkeyDataElement(
|
||||||
Base64.encode(aliceStore.getPublicKeyRingBytes(alice, aliceKey.getFingerprint()))),
|
Base64.encode(aliceStore.getPublicKeyRingBytes(alice, aliceKey.getFingerprint()))),
|
||||||
new Date());
|
new Date());
|
||||||
|
@ -83,5 +112,30 @@ public class DryOxEncryptionTest extends OxTestSuite {
|
||||||
|
|
||||||
aliceStore.setAnnouncedKeysFingerprints(bob, Collections.singletonMap(bobKey.getFingerprint(), new Date()));
|
aliceStore.setAnnouncedKeysFingerprints(bob, Collections.singletonMap(bobKey.getFingerprint(), new Date()));
|
||||||
bobStore.setAnnouncedKeysFingerprints(alice, Collections.singletonMap(aliceKey.getFingerprint(), new Date()));
|
bobStore.setAnnouncedKeysFingerprints(alice, Collections.singletonMap(aliceKey.getFingerprint(), new Date()));
|
||||||
|
|
||||||
|
OpenPgpFingerprints aliceFingerprints = new OpenPgpFingerprints(alice,
|
||||||
|
Collections.singleton(aliceKey.getFingerprint()),
|
||||||
|
Collections.singleton(aliceKey.getFingerprint()),
|
||||||
|
new HashMap<OpenPgpV4Fingerprint, Throwable>());
|
||||||
|
OpenPgpFingerprints bobFingerprints = new OpenPgpFingerprints(bob,
|
||||||
|
Collections.singleton(bobKey.getFingerprint()),
|
||||||
|
Collections.singleton(bobKey.getFingerprint()),
|
||||||
|
new HashMap<OpenPgpV4Fingerprint, Throwable>());
|
||||||
|
|
||||||
|
OpenPgpContact aliceForBob = new OpenPgpContact(bobProvider, alice, bobFingerprints, aliceFingerprints);
|
||||||
|
OpenPgpContact bobForAlice = new OpenPgpContact(aliceProvider, bob, aliceFingerprints, bobFingerprints);
|
||||||
|
|
||||||
|
String bodyText = "Finden wir eine Kompromisslösung – machen wir es so, wie ich es sage.";
|
||||||
|
List<ExtensionElement> payload = Collections.<ExtensionElement>singletonList(new Message.Body("de",
|
||||||
|
bodyText));
|
||||||
|
|
||||||
|
OpenPgpElement encrypted = bobForAlice.encryptAndSign(payload);
|
||||||
|
|
||||||
|
OpenPgpContentElement decrypted = aliceForBob.receive(encrypted);
|
||||||
|
assertTrue(decrypted instanceof SigncryptElement);
|
||||||
|
|
||||||
|
assertEquals(1, decrypted.getExtensions().size());
|
||||||
|
Message.Body body = (Message.Body) decrypted.getExtensions().get(0);
|
||||||
|
assertEquals(bodyText, body.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ public class OpenPgpContact {
|
||||||
OpenPgpFingerprints contactsFingerprints) {
|
OpenPgpFingerprints contactsFingerprints) {
|
||||||
this.cryptoProvider = cryptoProvider;
|
this.cryptoProvider = cryptoProvider;
|
||||||
this.jid = jid;
|
this.jid = jid;
|
||||||
this.singingKey = cryptoProvider.getStore().getPrimaryOpenPgpKeyPairFingerprint();
|
this.singingKey = ourFingerprints.getActiveKeys().iterator().next();
|
||||||
this.ourFingerprints = ourFingerprints;
|
this.ourFingerprints = ourFingerprints;
|
||||||
this.contactsFingerprints = contactsFingerprints;
|
this.contactsFingerprints = contactsFingerprints;
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ public class OpenPgpContact {
|
||||||
return contactsFingerprints;
|
return contactsFingerprints;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addSignedEncryptedPayloadTo(Message message, List<ExtensionElement> payload)
|
public OpenPgpElement encryptAndSign(List<ExtensionElement> payload)
|
||||||
throws IOException, SmackOpenPgpException, MissingOpenPgpKeyPairException {
|
throws IOException, SmackOpenPgpException, MissingOpenPgpKeyPairException {
|
||||||
MultiMap<BareJid, OpenPgpV4Fingerprint> fingerprints = oursAndRecipientFingerprints();
|
MultiMap<BareJid, OpenPgpV4Fingerprint> fingerprints = oursAndRecipientFingerprints();
|
||||||
|
|
||||||
|
@ -79,7 +79,6 @@ public class OpenPgpContact {
|
||||||
Collections.<Jid>singleton(getJid()),
|
Collections.<Jid>singleton(getJid()),
|
||||||
payload);
|
payload);
|
||||||
|
|
||||||
OpenPgpElement encryptedPayload;
|
|
||||||
byte[] encryptedBytes;
|
byte[] encryptedBytes;
|
||||||
|
|
||||||
// Encrypt the payload
|
// Encrypt the payload
|
||||||
|
@ -92,9 +91,14 @@ public class OpenPgpContact {
|
||||||
throw new AssertionError("Missing OpenPGP public key, even though this should not happen here.", e);
|
throw new AssertionError("Missing OpenPGP public key, even though this should not happen here.", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
encryptedPayload = new OpenPgpElement(Base64.encodeToString(encryptedBytes));
|
return new OpenPgpElement(Base64.encodeToString(encryptedBytes));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addSignedEncryptedPayloadTo(Message message, List<ExtensionElement> payload)
|
||||||
|
throws IOException, SmackOpenPgpException, MissingOpenPgpKeyPairException {
|
||||||
|
|
||||||
// Add encrypted payload to message
|
// Add encrypted payload to message
|
||||||
|
OpenPgpElement encryptedPayload = encryptAndSign(payload);
|
||||||
message.addExtension(encryptedPayload);
|
message.addExtension(encryptedPayload);
|
||||||
|
|
||||||
// Add additional information to the message
|
// Add additional information to the message
|
||||||
|
|
Loading…
Reference in a new issue