Smack/smack-openpgp-bouncycastle/src/test/java/org/jivesoftware/smackx/ox/bouncycastle/DryOxEncryptionTest.java

140 lines
6.3 KiB
Java
Raw Normal View History

2018-06-20 11:02:30 +02:00
/**
*
* Copyright 2018 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.ox.bouncycastle;
2018-06-21 15:20:04 +02:00
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
2018-06-20 11:02:30 +02:00
import java.io.File;
import java.io.IOException;
2018-06-26 17:59:42 +02:00
import java.nio.charset.Charset;
2018-06-20 11:02:30 +02:00
import java.util.Collections;
import java.util.Date;
2018-06-21 15:20:04 +02:00
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
2018-06-20 11:02:30 +02:00
2018-07-03 18:07:54 +02:00
import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
2018-06-21 15:20:04 +02:00
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message;
2018-06-20 12:45:05 +02:00
import org.jivesoftware.smack.util.FileUtils;
2018-06-21 15:20:04 +02:00
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
2018-06-26 17:59:42 +02:00
import org.jivesoftware.smackx.ox.TestKeys;
2018-07-04 16:02:03 +02:00
import org.jivesoftware.smackx.ox.OpenPgpContact;
2018-06-21 15:20:04 +02:00
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
2018-06-20 11:02:30 +02:00
import org.jivesoftware.smackx.ox.element.PubkeyElement;
2018-06-21 15:20:04 +02:00
import org.jivesoftware.smackx.ox.element.SigncryptElement;
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyPairException;
2018-06-20 11:02:30 +02:00
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpPublicKeyException;
import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException;
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
import org.bouncycastle.util.encoders.Base64;
2018-06-21 15:20:04 +02:00
import org.junit.AfterClass;
import org.junit.BeforeClass;
2018-06-20 11:02:30 +02:00
import org.junit.Test;
import org.jxmpp.jid.BareJid;
import org.pgpainless.pgpainless.key.UnprotectedKeysProtector;
import org.pgpainless.pgpainless.util.BCUtil;
2018-06-21 15:20:04 +02:00
import org.xmlpull.v1.XmlPullParserException;
2018-06-20 11:02:30 +02:00
2018-06-20 11:02:30 +02:00
public class DryOxEncryptionTest extends OxTestSuite {
2018-06-21 15:20:04 +02:00
private static final Logger LOGGER = Logger.getLogger(DryOxEncryptionTest.class.getName());
2018-06-26 17:59:42 +02:00
private static final Charset UTF8 = Charset.forName("UTF-8");
2018-06-21 15:20:04 +02:00
2018-06-26 17:59:42 +02:00
private static final File julietPath = FileUtils.getTempDir("ox-juliet");
private static final File romeoPath = FileUtils.getTempDir("ox-romeo");
2018-06-20 11:02:30 +02:00
2018-06-21 15:20:04 +02:00
@BeforeClass
@AfterClass
public static void deletePath() {
2018-06-26 17:59:42 +02:00
LOGGER.log(Level.INFO, "Delete paths " + julietPath.getAbsolutePath() + " " + romeoPath.getAbsolutePath());
FileUtils.deleteDirectory(julietPath);
FileUtils.deleteDirectory(romeoPath);
2018-06-20 11:02:30 +02:00
}
@Test
public void dryEncryptionTest()
2018-06-27 11:56:08 +02:00
throws IOException, SmackOpenPgpException, MissingUserIdOnKeyException, MissingOpenPgpPublicKeyException,
2018-07-03 18:07:54 +02:00
MissingOpenPgpKeyPairException, XmlPullParserException, SmackException.NotLoggedInException {
BareJid julietJid = TestKeys.JULIET_JID;
BareJid romeoJid = TestKeys.ROMEO_JID;
XMPPConnection julietCon = new DummyConnection();
XMPPConnection romeoCon = new DummyConnection();
2018-06-20 11:02:30 +02:00
2018-06-26 17:59:42 +02:00
FileBasedPainlessOpenPgpStore julietStore = new FileBasedPainlessOpenPgpStore(julietPath, new UnprotectedKeysProtector());
FileBasedPainlessOpenPgpStore romeoStore = new FileBasedPainlessOpenPgpStore(romeoPath, new UnprotectedKeysProtector());
2018-06-20 11:02:30 +02:00
2018-07-03 18:07:54 +02:00
PainlessOpenPgpProvider julietProvider = new PainlessOpenPgpProvider(julietJid, julietStore);
PainlessOpenPgpProvider romeoProvider = new PainlessOpenPgpProvider(romeoJid, romeoStore);
2018-06-20 11:02:30 +02:00
2018-07-03 18:07:54 +02:00
OpenPgpV4Fingerprint julietFinger = julietProvider.importSecretKey(julietJid,
2018-06-26 17:59:42 +02:00
BCUtil.getDecodedBytes(TestKeys.JULIET_PRIV.getBytes(UTF8)));
2018-07-03 18:07:54 +02:00
OpenPgpV4Fingerprint romeoFinger = romeoProvider.importSecretKey(romeoJid,
2018-06-26 17:59:42 +02:00
BCUtil.getDecodedBytes(TestKeys.ROMEO_PRIV.getBytes(UTF8)));
2018-06-20 11:02:30 +02:00
julietStore.setSigningKeyPairFingerprint(julietFinger);
romeoStore.setSigningKeyPairFingerprint(romeoFinger);
2018-06-20 11:02:30 +02:00
2018-07-03 18:07:54 +02:00
byte[] julietPubBytes = julietStore.getPublicKeyRingBytes(julietJid, julietFinger);
byte[] romeoPubBytes = romeoStore.getPublicKeyRingBytes(romeoJid, romeoFinger);
2018-06-26 17:59:42 +02:00
assertNotNull(julietPubBytes);
assertNotNull(romeoPubBytes);
assertTrue(julietPubBytes.length != 0);
assertTrue(romeoPubBytes.length != 0);
2018-06-21 15:20:04 +02:00
2018-06-26 17:59:42 +02:00
PubkeyElement julietPub = new PubkeyElement(new PubkeyElement.PubkeyDataElement(
2018-07-03 18:07:54 +02:00
Base64.encode(julietStore.getPublicKeyRingBytes(julietJid, julietFinger))),
2018-06-20 11:02:30 +02:00
new Date());
2018-06-26 17:59:42 +02:00
PubkeyElement romeoPub = new PubkeyElement(new PubkeyElement.PubkeyDataElement(
2018-07-03 18:07:54 +02:00
Base64.encode(romeoStore.getPublicKeyRingBytes(romeoJid, romeoFinger))),
2018-06-20 11:02:30 +02:00
new Date());
2018-07-03 18:07:54 +02:00
julietProvider.importPublicKey(romeoJid, Base64.decode(romeoPub.getDataElement().getB64Data()));
romeoProvider.importPublicKey(julietJid, Base64.decode(julietPub.getDataElement().getB64Data()));
2018-06-21 15:20:04 +02:00
2018-07-03 18:07:54 +02:00
julietStore.setAnnouncedKeysFingerprints(romeoJid, Collections.singletonMap(romeoFinger, new Date()));
romeoStore.setAnnouncedKeysFingerprints(julietJid, Collections.singletonMap(julietFinger, new Date()));
2018-06-21 15:20:04 +02:00
2018-07-03 18:07:54 +02:00
OpenPgpContact julietForRomeo = new OpenPgpContact(romeoProvider, julietJid, romeoCon);
OpenPgpContact romeoForJuliet = new OpenPgpContact(julietProvider, romeoJid, julietCon);
2018-06-21 15:20:04 +02:00
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));
2018-06-26 17:59:42 +02:00
OpenPgpElement encrypted = romeoForJuliet.encryptAndSign(payload);
LOGGER.log(Level.INFO, encrypted.toXML(null).toString());
2018-06-21 15:20:04 +02:00
2018-06-26 17:59:42 +02:00
OpenPgpContentElement decrypted = julietForRomeo.receive(encrypted);
2018-06-21 15:20:04 +02:00
assertTrue(decrypted instanceof SigncryptElement);
assertEquals(1, decrypted.getExtensions().size());
Message.Body body = (Message.Body) decrypted.getExtensions().get(0);
assertEquals(bodyText, body.getMessage());
2018-06-20 11:02:30 +02:00
}
}