mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-23 20:42:06 +01:00
Still temp
This commit is contained in:
parent
171cb07430
commit
693ced1fe4
14 changed files with 157 additions and 265 deletions
|
@ -31,7 +31,7 @@ import org.jivesoftware.smackx.ox.OpenPgpManager;
|
||||||
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
|
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
|
||||||
import org.jivesoftware.smackx.ox.bouncycastle.FileBasedPainlessOpenPgpStore;
|
import org.jivesoftware.smackx.ox.bouncycastle.FileBasedPainlessOpenPgpStore;
|
||||||
import org.jivesoftware.smackx.ox.bouncycastle.PainlessOpenPgpProvider;
|
import org.jivesoftware.smackx.ox.bouncycastle.PainlessOpenPgpProvider;
|
||||||
import org.jivesoftware.smackx.ox.chat.OpenPgpContact;
|
import org.jivesoftware.smackx.ox.OpenPgpContact;
|
||||||
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
||||||
import org.jivesoftware.smackx.ox.listener.OxMessageListener;
|
import org.jivesoftware.smackx.ox.listener.OxMessageListener;
|
||||||
import org.jivesoftware.smackx.ox.util.PubSubDelegate;
|
import org.jivesoftware.smackx.ox.util.PubSubDelegate;
|
||||||
|
@ -110,16 +110,11 @@ public class BasicOpenPgpInstantMessagingIntegrationTest extends AbstractOpenPgp
|
||||||
aliceOpenPgp.announceSupportAndPublish();
|
aliceOpenPgp.announceSupportAndPublish();
|
||||||
bobOpenPgp.announceSupportAndPublish();
|
bobOpenPgp.announceSupportAndPublish();
|
||||||
|
|
||||||
LOGGER.log(Level.INFO, "Request metadata of bob");
|
|
||||||
aliceOpenPgp.requestMetadataUpdate(bob);
|
|
||||||
LOGGER.log(Level.INFO, "Request metadata of alice");
|
|
||||||
bobOpenPgp.requestMetadataUpdate(alice);
|
|
||||||
|
|
||||||
OpenPgpContact bobForAlice = aliceOpenPgp.getOpenPgpContact(bob.asEntityBareJidIfPossible());
|
OpenPgpContact bobForAlice = aliceOpenPgp.getOpenPgpContact(bob.asEntityBareJidIfPossible());
|
||||||
OpenPgpContact aliceForBob = bobOpenPgp.getOpenPgpContact(alice.asEntityBareJidIfPossible());
|
OpenPgpContact aliceForBob = bobOpenPgp.getOpenPgpContact(alice.asEntityBareJidIfPossible());
|
||||||
|
|
||||||
assertTrue(bobForAlice.getFingerprints().getActiveKeys().contains(bobFingerprint));
|
assertTrue(bobForAlice.getActiveKeys().contains(bobFingerprint));
|
||||||
assertTrue(aliceForBob.getFingerprints().getActiveKeys().contains(aliceFingerprint));
|
assertTrue(aliceForBob.getActiveKeys().contains(aliceFingerprint));
|
||||||
|
|
||||||
aliceInstantMessaging.sendOxMessage(bobForAlice, body);
|
aliceInstantMessaging.sendOxMessage(bobForAlice, body);
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smack.util.FileUtils;
|
import org.jivesoftware.smack.util.FileUtils;
|
||||||
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
|
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
|
||||||
import org.jivesoftware.smackx.ox.TestKeys;
|
import org.jivesoftware.smackx.ox.TestKeys;
|
||||||
import org.jivesoftware.smackx.ox.chat.OpenPgpContact;
|
import org.jivesoftware.smackx.ox.OpenPgpContact;
|
||||||
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
|
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
|
||||||
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
||||||
import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
||||||
|
|
|
@ -28,18 +28,17 @@ import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
|
import org.jivesoftware.smack.chat2.ChatManager;
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||||
import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement;
|
import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement;
|
||||||
import org.jivesoftware.smackx.hints.element.StoreHint;
|
import org.jivesoftware.smackx.hints.element.StoreHint;
|
||||||
import org.jivesoftware.smackx.ox.chat.OpenPgpContact;
|
|
||||||
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
||||||
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyPairException;
|
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyPairException;
|
||||||
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
|
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
|
||||||
import org.jivesoftware.smackx.ox.listener.OxMessageListener;
|
import org.jivesoftware.smackx.ox.listener.OxMessageListener;
|
||||||
import org.jivesoftware.smackx.ox.listener.internal.SigncryptElementReceivedListener;
|
import org.jivesoftware.smackx.ox.listener.internal.SigncryptElementReceivedListener;
|
||||||
|
|
||||||
import org.jxmpp.jid.BareJid;
|
import org.jxmpp.jid.BareJid;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -138,6 +137,8 @@ public final class OXInstantMessagingManager extends Manager implements Signcryp
|
||||||
StoreHint.set(message);
|
StoreHint.set(message);
|
||||||
message.setBody("This message is encrypted using XEP-0374: OpenPGP for XMPP: Instant Messaging.");
|
message.setBody("This message is encrypted using XEP-0374: OpenPGP for XMPP: Instant Messaging.");
|
||||||
|
|
||||||
contact.send(connection(), message, payload);
|
contact.addSignedEncryptedPayloadTo(message, payload);
|
||||||
|
|
||||||
|
ChatManager.getInstanceFor(connection()).chatWith(contact.getJid().asEntityBareJidIfPossible()).send(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.ox.chat;
|
package org.jivesoftware.smackx.ox;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -29,16 +29,12 @@ import java.util.logging.Logger;
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.chat2.ChatManager;
|
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smack.util.MultiMap;
|
import org.jivesoftware.smack.util.MultiMap;
|
||||||
import org.jivesoftware.smack.util.stringencoder.Base64;
|
import org.jivesoftware.smack.util.stringencoder.Base64;
|
||||||
import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement;
|
import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement;
|
||||||
import org.jivesoftware.smackx.hints.element.StoreHint;
|
import org.jivesoftware.smackx.hints.element.StoreHint;
|
||||||
import org.jivesoftware.smackx.ox.OpenPgpManager;
|
|
||||||
import org.jivesoftware.smackx.ox.OpenPgpProvider;
|
|
||||||
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
|
|
||||||
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
|
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
|
||||||
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
||||||
import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
||||||
|
@ -50,7 +46,6 @@ 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.DecryptedBytesAndMetadata;
|
import org.jivesoftware.smackx.ox.util.DecryptedBytesAndMetadata;
|
||||||
import org.jivesoftware.smackx.ox.util.PubSubDelegate;
|
import org.jivesoftware.smackx.ox.util.PubSubDelegate;
|
||||||
|
|
||||||
import org.jxmpp.jid.BareJid;
|
import org.jxmpp.jid.BareJid;
|
||||||
import org.jxmpp.jid.Jid;
|
import org.jxmpp.jid.Jid;
|
||||||
import org.xmlpull.v1.XmlPullParserException;
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
@ -67,18 +62,41 @@ public class OpenPgpContact {
|
||||||
private Map<OpenPgpV4Fingerprint, Date> availableKeys = null;
|
private Map<OpenPgpV4Fingerprint, Date> availableKeys = null;
|
||||||
private final Map<OpenPgpV4Fingerprint, Throwable> unfetchableKeys = new HashMap<>();
|
private final Map<OpenPgpV4Fingerprint, Throwable> unfetchableKeys = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a OpenPgpContact.
|
||||||
|
*
|
||||||
|
* @param cryptoProvider {@link OpenPgpProvider}
|
||||||
|
* @param jid {@link BareJid} of the contact
|
||||||
|
* @param connection our authenticated {@link XMPPConnection}
|
||||||
|
*/
|
||||||
public OpenPgpContact(OpenPgpProvider cryptoProvider,
|
public OpenPgpContact(OpenPgpProvider cryptoProvider,
|
||||||
BareJid jid,
|
BareJid jid,
|
||||||
XMPPConnection connection) {
|
XMPPConnection connection) {
|
||||||
this.jid = jid;
|
this.jid = jid;
|
||||||
this.cryptoProvider = cryptoProvider;
|
this.cryptoProvider = cryptoProvider;
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.updateKeys();
|
||||||
|
} catch (SmackOpenPgpException | InterruptedException | XMPPException.XMPPErrorException | SmackException e) {
|
||||||
|
LOGGER.log(Level.WARNING, "Initial key update for contact " + getJid() + " failed.", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the {@link BareJid} of the contact.
|
||||||
|
*
|
||||||
|
* @return jid
|
||||||
|
*/
|
||||||
public BareJid getJid() {
|
public BareJid getJid() {
|
||||||
return jid;
|
return jid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a {@link Map} of the announced keys of the contact and their last update dates.
|
||||||
|
*
|
||||||
|
* @return announced keys
|
||||||
|
*/
|
||||||
public Map<OpenPgpV4Fingerprint, Date> getAnnouncedKeys() {
|
public Map<OpenPgpV4Fingerprint, Date> getAnnouncedKeys() {
|
||||||
if (announcedKeys == null) {
|
if (announcedKeys == null) {
|
||||||
announcedKeys = cryptoProvider.getStore().getAnnouncedKeysFingerprints(getJid());
|
announcedKeys = cryptoProvider.getStore().getAnnouncedKeysFingerprints(getJid());
|
||||||
|
@ -86,6 +104,15 @@ public class OpenPgpContact {
|
||||||
return announcedKeys;
|
return announcedKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a {@link Map} of all locally available keys of the contact.
|
||||||
|
*
|
||||||
|
* Note: This list might contain keys which are no longer associated to the contact.
|
||||||
|
* For encryption please use {@link #getActiveKeys()} instead.
|
||||||
|
*
|
||||||
|
* @return available keys
|
||||||
|
* @throws SmackOpenPgpException if we cannot read the locally available keys for some reason
|
||||||
|
*/
|
||||||
public Map<OpenPgpV4Fingerprint, Date> getAvailableKeys() throws SmackOpenPgpException {
|
public Map<OpenPgpV4Fingerprint, Date> getAvailableKeys() throws SmackOpenPgpException {
|
||||||
if (availableKeys == null) {
|
if (availableKeys == null) {
|
||||||
availableKeys = cryptoProvider.getStore().getAvailableKeysFingerprints(getJid());
|
availableKeys = cryptoProvider.getStore().getAvailableKeysFingerprints(getJid());
|
||||||
|
@ -93,21 +120,47 @@ public class OpenPgpContact {
|
||||||
return availableKeys;
|
return availableKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a {@link Map} of all the keys which cannot be fetched and the reason why this is.
|
||||||
|
*
|
||||||
|
* @return unfetched keys
|
||||||
|
*/
|
||||||
public Map<OpenPgpV4Fingerprint, Throwable> getUnfetchableKeys() {
|
public Map<OpenPgpV4Fingerprint, Throwable> getUnfetchableKeys() {
|
||||||
return unfetchableKeys;
|
return unfetchableKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a {@link Set} of all active keys of this contact.
|
||||||
|
* Active keys are keys which are announced, not revoked or otherwise invalidated and locally available.
|
||||||
|
*
|
||||||
|
* @return active keys.
|
||||||
|
* @throws SmackOpenPgpException if we cannot access the keys of the contact.
|
||||||
|
*/
|
||||||
public Set<OpenPgpV4Fingerprint> getActiveKeys() throws SmackOpenPgpException {
|
public Set<OpenPgpV4Fingerprint> getActiveKeys() throws SmackOpenPgpException {
|
||||||
Set<OpenPgpV4Fingerprint> fingerprints = getAvailableKeys().keySet();
|
Set<OpenPgpV4Fingerprint> fingerprints = getAvailableKeys().keySet();
|
||||||
fingerprints.retainAll(getAnnouncedKeys().keySet());
|
fingerprints.retainAll(getAnnouncedKeys().keySet());
|
||||||
return fingerprints;
|
return fingerprints;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the metadata node to get a {@link PublicKeysListElement} and update any missing or outdated keys.
|
||||||
|
*
|
||||||
|
* @throws InterruptedException
|
||||||
|
* @throws XMPPException.XMPPErrorException
|
||||||
|
* @throws SmackException
|
||||||
|
* @throws SmackOpenPgpException
|
||||||
|
*/
|
||||||
public void updateKeys()
|
public void updateKeys()
|
||||||
throws InterruptedException, XMPPException.XMPPErrorException, SmackException, SmackOpenPgpException {
|
throws InterruptedException, XMPPException.XMPPErrorException, SmackException, SmackOpenPgpException {
|
||||||
updateKeys(PubSubDelegate.fetchPubkeysList(connection, getJid()));
|
updateKeys(PubSubDelegate.fetchPubkeysList(connection, getJid()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update any missing or outdated keys based on the given {@link PublicKeysListElement}.
|
||||||
|
*
|
||||||
|
* @param metadata
|
||||||
|
* @throws SmackOpenPgpException
|
||||||
|
*/
|
||||||
public void updateKeys(PublicKeysListElement metadata)
|
public void updateKeys(PublicKeysListElement metadata)
|
||||||
throws SmackOpenPgpException {
|
throws SmackOpenPgpException {
|
||||||
storePublishedDevices(metadata);
|
storePublishedDevices(metadata);
|
||||||
|
@ -130,6 +183,17 @@ public class OpenPgpContact {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the key identified by the {@code fingerprint}.
|
||||||
|
*
|
||||||
|
* @param fingerprint fingerprint of the key
|
||||||
|
* @throws InterruptedException
|
||||||
|
* @throws XMPPException.XMPPErrorException
|
||||||
|
* @throws SmackException
|
||||||
|
* @throws IOException
|
||||||
|
* @throws MissingUserIdOnKeyException
|
||||||
|
* @throws SmackOpenPgpException
|
||||||
|
*/
|
||||||
public void updateKey(OpenPgpV4Fingerprint fingerprint)
|
public void updateKey(OpenPgpV4Fingerprint fingerprint)
|
||||||
throws InterruptedException, XMPPException.XMPPErrorException, SmackException, IOException,
|
throws InterruptedException, XMPPException.XMPPErrorException, SmackException, IOException,
|
||||||
MissingUserIdOnKeyException, SmackOpenPgpException {
|
MissingUserIdOnKeyException, SmackOpenPgpException {
|
||||||
|
@ -148,6 +212,15 @@ public class OpenPgpContact {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Import a public key.
|
||||||
|
*
|
||||||
|
* @param data OpenPgp keys byte representation.
|
||||||
|
* @return the fingerprint of the imported key.
|
||||||
|
* @throws SmackOpenPgpException
|
||||||
|
* @throws MissingUserIdOnKeyException
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
private OpenPgpV4Fingerprint importPublicKey(byte[] data)
|
private OpenPgpV4Fingerprint importPublicKey(byte[] data)
|
||||||
throws SmackOpenPgpException, MissingUserIdOnKeyException, IOException {
|
throws SmackOpenPgpException, MissingUserIdOnKeyException, IOException {
|
||||||
OpenPgpV4Fingerprint fingerprint = cryptoProvider.importPublicKey(getJid(), data);
|
OpenPgpV4Fingerprint fingerprint = cryptoProvider.importPublicKey(getJid(), data);
|
||||||
|
@ -155,6 +228,13 @@ public class OpenPgpContact {
|
||||||
return fingerprint;
|
return fingerprint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process an incoming {@link PublicKeysListElement} and store the contained {@link OpenPgpV4Fingerprint}s and
|
||||||
|
* their publication dates in local storage.
|
||||||
|
*
|
||||||
|
* @param element publicKeysListElement
|
||||||
|
* @return {@link Map} with the contents of the element
|
||||||
|
*/
|
||||||
public Map<OpenPgpV4Fingerprint, Date> storePublishedDevices(PublicKeysListElement element) {
|
public Map<OpenPgpV4Fingerprint, Date> storePublishedDevices(PublicKeysListElement element) {
|
||||||
Map<OpenPgpV4Fingerprint, Date> announcedKeys = new HashMap<>();
|
Map<OpenPgpV4Fingerprint, Date> announcedKeys = new HashMap<>();
|
||||||
|
|
||||||
|
@ -170,6 +250,14 @@ public class OpenPgpContact {
|
||||||
return announcedKeys;
|
return announcedKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all keys to which a message sent to the contact would be encrypted to.
|
||||||
|
* Those are all active keys of the contact as well as our own active keys.
|
||||||
|
*
|
||||||
|
* @return encryption keys
|
||||||
|
* @throws SmackOpenPgpException if we cannot read the contacts keys or our own keys.
|
||||||
|
* @throws SmackException.NotLoggedInException if we are not logged in.
|
||||||
|
*/
|
||||||
private MultiMap<BareJid, OpenPgpV4Fingerprint> getEncryptionKeys()
|
private MultiMap<BareJid, OpenPgpV4Fingerprint> getEncryptionKeys()
|
||||||
throws SmackOpenPgpException, SmackException.NotLoggedInException {
|
throws SmackOpenPgpException, SmackException.NotLoggedInException {
|
||||||
OpenPgpSelf self = getSelf();
|
OpenPgpSelf self = getSelf();
|
||||||
|
@ -189,10 +277,28 @@ public class OpenPgpContact {
|
||||||
return recipientsKeys;
|
return recipientsKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get our OpenPgpSelf.
|
||||||
|
* The {@link OpenPgpSelf} contains all our fingerprints.
|
||||||
|
*
|
||||||
|
* @return openPgpSelf
|
||||||
|
* @throws SmackException.NotLoggedInException
|
||||||
|
*/
|
||||||
private OpenPgpSelf getSelf() throws SmackException.NotLoggedInException {
|
private OpenPgpSelf getSelf() throws SmackException.NotLoggedInException {
|
||||||
return OpenPgpManager.getInstanceFor(connection).getOpenPgpSelf();
|
return OpenPgpManager.getInstanceFor(connection).getOpenPgpSelf();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypt a message to a contact and ourselves and sign it with our signing key.
|
||||||
|
* The payload will be wrapped in a {@link SigncryptElement} before encryption.
|
||||||
|
*
|
||||||
|
* @param payload the payload we want to encrypt.
|
||||||
|
* @return {@link OpenPgpElement} containing the encrypted, signed {@link SigncryptElement}.
|
||||||
|
* @throws IOException IO is dangerous
|
||||||
|
* @throws SmackOpenPgpException OpenPgp is brittle
|
||||||
|
* @throws MissingOpenPgpKeyPairException if we cannot read our signing key pair
|
||||||
|
* @throws SmackException.NotLoggedInException if we are not logged in.
|
||||||
|
*/
|
||||||
public OpenPgpElement encryptAndSign(List<ExtensionElement> payload)
|
public OpenPgpElement encryptAndSign(List<ExtensionElement> payload)
|
||||||
throws IOException, SmackOpenPgpException, MissingOpenPgpKeyPairException,
|
throws IOException, SmackOpenPgpException, MissingOpenPgpKeyPairException,
|
||||||
SmackException.NotLoggedInException {
|
SmackException.NotLoggedInException {
|
||||||
|
@ -218,6 +324,17 @@ public class OpenPgpContact {
|
||||||
return new OpenPgpElement(Base64.encodeToString(encryptedBytes));
|
return new OpenPgpElement(Base64.encodeToString(encryptedBytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a signed and encrypted {@link SigncryptElement} containing the {@code payload} and append it as a
|
||||||
|
* {@link OpenPgpElement} to the {@code message}.
|
||||||
|
*
|
||||||
|
* @param message {@link Message} which will transport the payload.
|
||||||
|
* @param payload payload that will be encrypted and signed.
|
||||||
|
* @throws IOException IO is dangerous.
|
||||||
|
* @throws SmackOpenPgpException OpenPGP is brittle.
|
||||||
|
* @throws MissingOpenPgpKeyPairException if we cannot access our signing key.
|
||||||
|
* @throws SmackException.NotLoggedInException if we are not logged in.
|
||||||
|
*/
|
||||||
public void addSignedEncryptedPayloadTo(Message message, List<ExtensionElement> payload)
|
public void addSignedEncryptedPayloadTo(Message message, List<ExtensionElement> payload)
|
||||||
throws IOException, SmackOpenPgpException, MissingOpenPgpKeyPairException,
|
throws IOException, SmackOpenPgpException, MissingOpenPgpKeyPairException,
|
||||||
SmackException.NotLoggedInException {
|
SmackException.NotLoggedInException {
|
||||||
|
@ -237,36 +354,16 @@ public class OpenPgpContact {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(XMPPConnection connection, Message message, List<ExtensionElement> payload)
|
/**
|
||||||
throws MissingOpenPgpKeyPairException, SmackException.NotConnectedException, InterruptedException,
|
* Process an incoming {@link OpenPgpElement} and return the decrypted and verified {@link OpenPgpContentElement}.
|
||||||
SmackOpenPgpException, IOException, SmackException.NotLoggedInException {
|
*
|
||||||
MultiMap<BareJid, OpenPgpV4Fingerprint> fingerprints = getEncryptionKeys();
|
* @param element possibly encrypted, possibly signed {@link OpenPgpElement}.
|
||||||
|
* @return decrypted {@link OpenPgpContentElement}
|
||||||
SigncryptElement preparedPayload = new SigncryptElement(
|
* @throws XmlPullParserException if the decrypted message does not represent valid XML.
|
||||||
Collections.<Jid>singleton(getJid()),
|
* @throws MissingOpenPgpKeyPairException if we are missing the public key counterpart of the key that signed the message.
|
||||||
payload);
|
* @throws SmackOpenPgpException if the message cannot be decrypted and or verified.
|
||||||
|
* @throws IOException IO is dangerous
|
||||||
OpenPgpElement encryptedPayload;
|
*/
|
||||||
byte[] encryptedMessage;
|
|
||||||
|
|
||||||
// Encrypt the payload
|
|
||||||
try {
|
|
||||||
encryptedMessage = cryptoProvider.signAndEncrypt(
|
|
||||||
preparedPayload,
|
|
||||||
getSelf().getSigningKey(),
|
|
||||||
fingerprints);
|
|
||||||
} catch (MissingOpenPgpPublicKeyException e) {
|
|
||||||
throw new AssertionError("Missing OpenPGP public key, even though this should not happen here.", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
encryptedPayload = new OpenPgpElement(Base64.encodeToString(encryptedMessage));
|
|
||||||
|
|
||||||
// Add encrypted payload to message
|
|
||||||
message.addExtension(encryptedPayload);
|
|
||||||
|
|
||||||
ChatManager.getInstanceFor(connection).chatWith(getJid().asEntityBareJidIfPossible()).send(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public OpenPgpContentElement receive(OpenPgpElement element)
|
public OpenPgpContentElement receive(OpenPgpElement element)
|
||||||
throws XmlPullParserException, MissingOpenPgpKeyPairException, SmackOpenPgpException, IOException {
|
throws XmlPullParserException, MissingOpenPgpKeyPairException, SmackOpenPgpException, IOException {
|
||||||
byte[] decoded = Base64.decode(element.getEncryptedBase64MessageContent());
|
byte[] decoded = Base64.decode(element.getEncryptedBase64MessageContent());
|
|
@ -49,8 +49,6 @@ import org.jivesoftware.smackx.ox.callback.AskForBackupCodeCallback;
|
||||||
import org.jivesoftware.smackx.ox.callback.DisplayBackupCodeCallback;
|
import org.jivesoftware.smackx.ox.callback.DisplayBackupCodeCallback;
|
||||||
import org.jivesoftware.smackx.ox.callback.SecretKeyBackupSelectionCallback;
|
import org.jivesoftware.smackx.ox.callback.SecretKeyBackupSelectionCallback;
|
||||||
import org.jivesoftware.smackx.ox.callback.SecretKeyRestoreSelectionCallback;
|
import org.jivesoftware.smackx.ox.callback.SecretKeyRestoreSelectionCallback;
|
||||||
import org.jivesoftware.smackx.ox.chat.OpenPgpContact;
|
|
||||||
import org.jivesoftware.smackx.ox.chat.OpenPgpSelf;
|
|
||||||
import org.jivesoftware.smackx.ox.element.CryptElement;
|
import org.jivesoftware.smackx.ox.element.CryptElement;
|
||||||
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
|
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
|
||||||
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.ox.chat;
|
package org.jivesoftware.smackx.ox;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
@ -54,23 +54,24 @@ public class OpenPgpMessage {
|
||||||
|
|
||||||
private final String element;
|
private final String element;
|
||||||
private final State state;
|
private final State state;
|
||||||
|
private final Metadata metadata;
|
||||||
|
|
||||||
private OpenPgpContentElement openPgpContentElement;
|
private OpenPgpContentElement openPgpContentElement;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param state state of the content element.
|
* @param metadata Metadata about the encryption
|
||||||
* @param content XML representation of the decrypted {@link OpenPgpContentElement}.
|
* @param content XML representation of the decrypted {@link OpenPgpContentElement}.
|
||||||
*/
|
*/
|
||||||
public OpenPgpMessage(State state, String content) {
|
public OpenPgpMessage(String content, Metadata metadata) {
|
||||||
this.state = Objects.requireNonNull(state);
|
this.metadata = Objects.requireNonNull(metadata);
|
||||||
|
this.state = Objects.requireNonNull(metadata.getState());
|
||||||
this.element = Objects.requireNonNull(content);
|
this.element = Objects.requireNonNull(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public OpenPgpMessage(byte[] bytes, Metadata metadata) {
|
public OpenPgpMessage(byte[] bytes, Metadata metadata) {
|
||||||
this.element = new String(bytes, Charset.forName("UTF-8"));
|
this(new String(Objects.requireNonNull(bytes), Charset.forName("UTF-8")), metadata);
|
||||||
this.state = metadata.getState();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -126,6 +127,10 @@ public class OpenPgpMessage {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Metadata getMetadata() {
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
|
||||||
public static class Metadata {
|
public static class Metadata {
|
||||||
|
|
||||||
private final Long encryptionKeyId;
|
private final Long encryptionKeyId;
|
|
@ -1,8 +1,6 @@
|
||||||
package org.jivesoftware.smackx.ox.chat;
|
package org.jivesoftware.smackx.ox;
|
||||||
|
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smackx.ox.OpenPgpProvider;
|
|
||||||
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
|
|
||||||
|
|
||||||
import org.jxmpp.jid.BareJid;
|
import org.jxmpp.jid.BareJid;
|
||||||
|
|
|
@ -1,119 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
* 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.chat;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.jivesoftware.smack.util.Objects;
|
|
||||||
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
|
|
||||||
|
|
||||||
import org.jxmpp.jid.BareJid;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Metadata about a contacts OpenPGP fingerprints.
|
|
||||||
*/
|
|
||||||
public class OpenPgpFingerprints {
|
|
||||||
|
|
||||||
private final BareJid jid;
|
|
||||||
private final Set<OpenPgpV4Fingerprint> announcedKeys;
|
|
||||||
private final Set<OpenPgpV4Fingerprint> availableKeys;
|
|
||||||
private final Map<OpenPgpV4Fingerprint, Throwable> unfetchableKeys;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*
|
|
||||||
* @param announcedKeys keys the contact currently announces via their metadata node.
|
|
||||||
* @param availableKeys keys which contain the contacts {@link BareJid} as user ID.
|
|
||||||
* @param unfetchableKeys keys that are announced, but cannot be fetched fro PubSub.
|
|
||||||
*/
|
|
||||||
public OpenPgpFingerprints(BareJid jid,
|
|
||||||
Set<OpenPgpV4Fingerprint> announcedKeys,
|
|
||||||
Set<OpenPgpV4Fingerprint> availableKeys,
|
|
||||||
Map<OpenPgpV4Fingerprint, Throwable> unfetchableKeys) {
|
|
||||||
this.jid = jid;
|
|
||||||
this.announcedKeys = Collections.unmodifiableSet(Objects.requireNonNull(announcedKeys,
|
|
||||||
"Set of announced keys MUST NOT be null."));
|
|
||||||
this.availableKeys = Collections.unmodifiableSet(Objects.requireNonNull(availableKeys,
|
|
||||||
"Set of available keys MUST NOT be null."));
|
|
||||||
this.unfetchableKeys = Collections.unmodifiableMap(Objects.requireNonNull(unfetchableKeys,
|
|
||||||
"Map of unfetchable keys MUST NOT be null."));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the {@link BareJid} of the user.
|
|
||||||
* @return jid
|
|
||||||
*/
|
|
||||||
public BareJid getJid() {
|
|
||||||
return jid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a {@link Set} of {@link OpenPgpV4Fingerprint}s, which the contact in question announced via their
|
|
||||||
* metadata node.
|
|
||||||
*
|
|
||||||
* @see <a href="https://xmpp.org/extensions/xep-0373.html#announcing-pubkey-list">
|
|
||||||
* XEP-0373 §4.2 about the Public Key Metadata Node</a>
|
|
||||||
*
|
|
||||||
* @return announced keys.
|
|
||||||
*/
|
|
||||||
public Set<OpenPgpV4Fingerprint> getAnnouncedKeys() {
|
|
||||||
return announcedKeys;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a {@link Set} of {@link OpenPgpV4Fingerprint}s, which are available in the local key ring, which contain
|
|
||||||
* the users {@link BareJid} as user-id (eg. "xmpp:juliet@capulet.lit").
|
|
||||||
* <br>
|
|
||||||
* Note: As everybody can publish a key with an arbitrary user-id, the keys contained in the result set MAY be keys
|
|
||||||
* of an potential attacker. NEVER use these keys for encryption without collating them with the users announced
|
|
||||||
* keys.
|
|
||||||
*
|
|
||||||
* @return locally available keys with users user-id.
|
|
||||||
*/
|
|
||||||
public Set<OpenPgpV4Fingerprint> getAvailableKeys() {
|
|
||||||
return availableKeys;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a {@link Map}, which maps {@link OpenPgpV4Fingerprint}s which the user announced, but that cannot be fetched
|
|
||||||
* via PubSub to {@link Throwable}s which were thrown when we tried to fetch them.
|
|
||||||
*
|
|
||||||
* @return unfetchable keys.
|
|
||||||
*/
|
|
||||||
public Map<OpenPgpV4Fingerprint, Throwable> getUnfetchableKeys() {
|
|
||||||
return unfetchableKeys;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a {@link Set} of {@link OpenPgpV4Fingerprint}s, which are both announced, as well as available.
|
|
||||||
* It is recommended to use those keys for encryption.
|
|
||||||
*
|
|
||||||
* @return active keys.
|
|
||||||
*/
|
|
||||||
public Set<OpenPgpV4Fingerprint> getActiveKeys() {
|
|
||||||
Set<OpenPgpV4Fingerprint> active = new HashSet<>();
|
|
||||||
for (OpenPgpV4Fingerprint fingerprint : getAvailableKeys()) {
|
|
||||||
if (getAnnouncedKeys().contains(fingerprint)) {
|
|
||||||
active.add(fingerprint);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Collections.unmodifiableSet(active);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,20 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Chat related classes for XEP-0373: OpenPGP for XMPP.
|
|
||||||
*/
|
|
||||||
package org.jivesoftware.smackx.ox.chat;
|
|
|
@ -17,7 +17,7 @@
|
||||||
package org.jivesoftware.smackx.ox.listener;
|
package org.jivesoftware.smackx.ox.listener;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smackx.ox.chat.OpenPgpContact;
|
import org.jivesoftware.smackx.ox.OpenPgpContact;
|
||||||
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
||||||
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
package org.jivesoftware.smackx.ox.listener.internal;
|
package org.jivesoftware.smackx.ox.listener.internal;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smackx.ox.chat.OpenPgpContact;
|
import org.jivesoftware.smackx.ox.OpenPgpContact;
|
||||||
import org.jivesoftware.smackx.ox.element.CryptElement;
|
import org.jivesoftware.smackx.ox.element.CryptElement;
|
||||||
|
|
||||||
public interface CryptElementReceivedListener {
|
public interface CryptElementReceivedListener {
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
package org.jivesoftware.smackx.ox.listener.internal;
|
package org.jivesoftware.smackx.ox.listener.internal;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smackx.ox.chat.OpenPgpContact;
|
import org.jivesoftware.smackx.ox.OpenPgpContact;
|
||||||
import org.jivesoftware.smackx.ox.element.SignElement;
|
import org.jivesoftware.smackx.ox.element.SignElement;
|
||||||
|
|
||||||
public interface SignElementReceivedListener {
|
public interface SignElementReceivedListener {
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
package org.jivesoftware.smackx.ox.listener.internal;
|
package org.jivesoftware.smackx.ox.listener.internal;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smackx.ox.chat.OpenPgpContact;
|
import org.jivesoftware.smackx.ox.OpenPgpContact;
|
||||||
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
||||||
|
|
||||||
public interface SigncryptElementReceivedListener {
|
public interface SigncryptElementReceivedListener {
|
||||||
|
|
|
@ -1,63 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertEquals;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
|
||||||
import org.jivesoftware.smackx.ox.chat.OpenPgpFingerprints;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.jxmpp.jid.JidTestUtil;
|
|
||||||
|
|
||||||
public class OpenPgpFingerprintsTest extends SmackTestSuite {
|
|
||||||
|
|
||||||
private static final OpenPgpV4Fingerprint fingerprint1 = new OpenPgpV4Fingerprint("47696d6d6520323020636861726163746572730a");
|
|
||||||
private static final OpenPgpV4Fingerprint fingerprint2 = new OpenPgpV4Fingerprint("492e206e6565642e20656173746572656767730a");
|
|
||||||
private static final OpenPgpV4Fingerprint fingerprint3 = new OpenPgpV4Fingerprint("4d6f7265206f66207468656d21204d6f7265210a");
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void activeKeysTest() {
|
|
||||||
Set<OpenPgpV4Fingerprint> announced = new HashSet<>();
|
|
||||||
announced.add(fingerprint1);
|
|
||||||
announced.add(fingerprint2);
|
|
||||||
|
|
||||||
Set<OpenPgpV4Fingerprint> available = new HashSet<>();
|
|
||||||
available.add(fingerprint2);
|
|
||||||
available.add(fingerprint3);
|
|
||||||
|
|
||||||
OpenPgpFingerprints fingerprints = new OpenPgpFingerprints(
|
|
||||||
JidTestUtil.BARE_JID_1,
|
|
||||||
announced,
|
|
||||||
available,
|
|
||||||
new HashMap<OpenPgpV4Fingerprint, Throwable>());
|
|
||||||
|
|
||||||
assertEquals(announced, fingerprints.getAnnouncedKeys());
|
|
||||||
assertEquals(available, fingerprints.getAvailableKeys());
|
|
||||||
assertEquals(JidTestUtil.BARE_JID_1, fingerprints.getJid());
|
|
||||||
assertEquals(0, fingerprints.getUnfetchableKeys().size());
|
|
||||||
|
|
||||||
Set<OpenPgpV4Fingerprint> active = new HashSet<>();
|
|
||||||
active.add(fingerprint2);
|
|
||||||
|
|
||||||
assertEquals(active, fingerprints.getActiveKeys());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue