2018-05-15 21:12:03 +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-05-28 20:07:06 +02:00
|
|
|
|
import static junit.framework.TestCase.assertTrue;
|
|
|
|
|
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
|
|
|
|
|
2018-05-15 21:12:03 +02:00
|
|
|
|
import java.security.Security;
|
2018-05-28 20:07:06 +02:00
|
|
|
|
import java.util.Collections;
|
2018-05-15 21:12:03 +02:00
|
|
|
|
|
2018-05-28 20:07:06 +02:00
|
|
|
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
|
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2018-05-15 21:12:03 +02:00
|
|
|
|
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
2018-06-13 18:39:09 +02:00
|
|
|
|
import org.jivesoftware.smackx.ox.chat.OpenPgpMessage;
|
2018-05-28 20:07:06 +02:00
|
|
|
|
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.SigncryptElement;
|
2018-05-15 21:12:03 +02:00
|
|
|
|
|
|
|
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
2018-05-28 00:58:13 +02:00
|
|
|
|
import org.junit.Ignore;
|
2018-05-15 21:12:03 +02:00
|
|
|
|
import org.jxmpp.jid.BareJid;
|
2018-05-28 20:07:06 +02:00
|
|
|
|
import org.jxmpp.jid.Jid;
|
2018-05-15 21:12:03 +02:00
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
|
|
2018-05-21 15:42:04 +02:00
|
|
|
|
public class BouncyCastleOpenPgpProviderTest extends SmackTestSuite {
|
2018-05-15 21:12:03 +02:00
|
|
|
|
|
2018-05-28 00:58:13 +02:00
|
|
|
|
@Ignore
|
2018-05-15 21:12:03 +02:00
|
|
|
|
public void encryptAndSign_decryptAndVerifyElementTest() throws Exception {
|
|
|
|
|
Security.addProvider(new BouncyCastleProvider());
|
|
|
|
|
|
|
|
|
|
// Create providers for alice and the cat
|
|
|
|
|
BareJid alice = JidCreate.bareFrom("alice@wonderland.lit");
|
|
|
|
|
BareJid cheshire = JidCreate.bareFrom("cheshire@wonderland.lit");
|
2018-05-28 20:07:06 +02:00
|
|
|
|
BCOpenPgpProvider aliceProvider = new BCOpenPgpProvider(alice);
|
|
|
|
|
BCOpenPgpProvider cheshireProvider = new BCOpenPgpProvider(cheshire);
|
2018-05-15 21:12:03 +02:00
|
|
|
|
|
2018-05-28 20:07:06 +02:00
|
|
|
|
aliceProvider.createOpenPgpKeyPair();
|
|
|
|
|
cheshireProvider.createOpenPgpKeyPair();
|
2018-05-24 16:01:33 +02:00
|
|
|
|
|
2018-05-15 21:12:03 +02:00
|
|
|
|
// dry exchange keys
|
2018-05-28 20:07:06 +02:00
|
|
|
|
|
2018-06-13 18:39:09 +02:00
|
|
|
|
PubkeyElement aliceKeys = aliceProvider.createPubkeyElement(aliceProvider.getPrimaryOpenPgpKeyPairFingerprint());
|
|
|
|
|
PubkeyElement cheshireKeys = cheshireProvider.createPubkeyElement(cheshireProvider.getPrimaryOpenPgpKeyPairFingerprint());
|
|
|
|
|
aliceProvider.storePublicKey(cheshire, cheshireProvider.getPrimaryOpenPgpKeyPairFingerprint(), cheshireKeys);
|
|
|
|
|
cheshireProvider.storePublicKey(alice, aliceProvider.getPrimaryOpenPgpKeyPairFingerprint(), aliceKeys);
|
2018-05-15 21:12:03 +02:00
|
|
|
|
|
2018-05-21 12:44:27 +02:00
|
|
|
|
// Create signed and encrypted message from alice to the cheshire cat
|
2018-05-15 21:12:03 +02:00
|
|
|
|
SigncryptElement signcryptElement = new SigncryptElement(
|
|
|
|
|
Collections.<Jid>singleton(cheshire),
|
|
|
|
|
Collections.<ExtensionElement>singletonList(
|
|
|
|
|
new Message.Body("en", "How do you know I’m mad?")));
|
|
|
|
|
OpenPgpElement encrypted = aliceProvider.signAndEncrypt(
|
2018-05-22 13:54:53 +02:00
|
|
|
|
signcryptElement,
|
2018-06-13 18:39:09 +02:00
|
|
|
|
aliceProvider.getPrimaryOpenPgpKeyPairFingerprint(),
|
|
|
|
|
Collections.singleton(cheshireProvider.getPrimaryOpenPgpKeyPairFingerprint()));
|
2018-05-15 21:12:03 +02:00
|
|
|
|
|
2018-05-21 12:44:27 +02:00
|
|
|
|
// Decrypt the message as the cheshire cat
|
2018-06-13 18:39:09 +02:00
|
|
|
|
OpenPgpMessage decrypted = cheshireProvider.decryptAndVerify(encrypted, Collections.singleton(aliceProvider.getPrimaryOpenPgpKeyPairFingerprint()));
|
2018-05-15 21:12:03 +02:00
|
|
|
|
OpenPgpContentElement content = decrypted.getOpenPgpContentElement();
|
|
|
|
|
|
2018-05-21 12:44:27 +02:00
|
|
|
|
assertTrue(content instanceof SigncryptElement);
|
2018-05-15 21:12:03 +02:00
|
|
|
|
assertXMLEqual(signcryptElement.toXML(null).toString(), content.toXML(null).toString());
|
|
|
|
|
}
|
|
|
|
|
}
|