2018-05-28 00:58:13 +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;
|
|
|
|
|
2018-06-01 13:11:43 +02:00
|
|
|
import java.io.IOException;
|
2018-06-13 18:39:09 +02:00
|
|
|
import java.util.HashMap;
|
2018-06-01 13:11:43 +02:00
|
|
|
import java.util.HashSet;
|
2018-05-28 00:58:13 +02:00
|
|
|
import java.util.Map;
|
2018-06-01 13:11:43 +02:00
|
|
|
import java.util.Set;
|
2018-05-28 00:58:13 +02:00
|
|
|
import java.util.WeakHashMap;
|
2018-06-01 13:11:43 +02:00
|
|
|
import java.util.logging.Level;
|
2018-05-30 22:06:09 +02:00
|
|
|
import java.util.logging.Logger;
|
2018-05-28 00:58:13 +02:00
|
|
|
|
|
|
|
import org.jivesoftware.smack.Manager;
|
2018-05-30 22:06:09 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2018-05-28 00:58:13 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2018-05-30 22:06:09 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
|
import org.jivesoftware.smack.chat2.Chat;
|
|
|
|
import org.jivesoftware.smack.chat2.ChatManager;
|
2018-06-01 13:11:43 +02:00
|
|
|
import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
|
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2018-06-13 18:39:09 +02:00
|
|
|
import org.jivesoftware.smack.util.stringencoder.Base64;
|
2018-05-30 22:06:09 +02:00
|
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
2018-06-13 18:39:09 +02:00
|
|
|
import org.jivesoftware.smackx.ox.chat.OpenPgpEncryptedChat;
|
|
|
|
import org.jivesoftware.smackx.ox.chat.OpenPgpFingerprints;
|
|
|
|
import org.jivesoftware.smackx.ox.chat.OpenPgpMessage;
|
2018-06-01 13:11:43 +02:00
|
|
|
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
|
|
|
|
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
|
|
|
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
|
|
|
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyPairException;
|
2018-05-30 22:06:09 +02:00
|
|
|
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
|
2018-06-01 13:11:43 +02:00
|
|
|
import org.jivesoftware.smackx.ox.listener.OpenPgpEncryptedMessageListener;
|
2018-06-13 18:39:09 +02:00
|
|
|
import org.jivesoftware.smackx.ox.util.DecryptedBytesAndMetadata;
|
2018-05-28 00:58:13 +02:00
|
|
|
|
|
|
|
import org.jxmpp.jid.BareJid;
|
2018-05-30 22:06:09 +02:00
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
2018-06-01 13:11:43 +02:00
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
2018-05-28 00:58:13 +02:00
|
|
|
|
2018-05-30 22:06:09 +02:00
|
|
|
/**
|
|
|
|
* Entry point of Smacks API for XEP-0374: OpenPGP for XMPP: Instant Messaging.
|
|
|
|
*
|
|
|
|
* @see <a href="https://xmpp.org/extensions/xep-0374.html">
|
|
|
|
* XEP-0374: OpenPGP for XMPP: Instant Messaging</a>
|
|
|
|
*/
|
|
|
|
public final class OXInstantMessagingManager extends Manager {
|
|
|
|
|
|
|
|
public static final String NAMESPACE_0 = "urn:xmpp:openpgp:im:0";
|
|
|
|
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(OXInstantMessagingManager.class.getName());
|
2018-05-28 00:58:13 +02:00
|
|
|
|
|
|
|
private static final Map<XMPPConnection, OXInstantMessagingManager> INSTANCES = new WeakHashMap<>();
|
|
|
|
private final OpenPgpManager openPgpManager;
|
2018-05-30 22:06:09 +02:00
|
|
|
private final ChatManager chatManager;
|
2018-05-28 00:58:13 +02:00
|
|
|
|
2018-06-01 13:11:43 +02:00
|
|
|
private final Set<OpenPgpEncryptedMessageListener> chatMessageListeners = new HashSet<>();
|
2018-06-13 18:39:09 +02:00
|
|
|
private final Map<BareJid, OpenPgpEncryptedChat> chats = new HashMap<>();
|
2018-06-01 13:11:43 +02:00
|
|
|
|
|
|
|
private OXInstantMessagingManager(final XMPPConnection connection) {
|
2018-05-28 00:58:13 +02:00
|
|
|
super(connection);
|
|
|
|
this.openPgpManager = OpenPgpManager.getInstanceFor(connection);
|
2018-05-30 22:06:09 +02:00
|
|
|
this.chatManager = ChatManager.getInstanceFor(connection);
|
2018-06-01 13:11:43 +02:00
|
|
|
chatManager.addIncomingListener(incomingChatMessageListener);
|
2018-05-28 00:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static OXInstantMessagingManager getInstanceFor(XMPPConnection connection) {
|
|
|
|
OXInstantMessagingManager manager = INSTANCES.get(connection);
|
2018-06-19 16:41:02 +02:00
|
|
|
|
2018-05-28 00:58:13 +02:00
|
|
|
if (manager == null) {
|
|
|
|
manager = new OXInstantMessagingManager(connection);
|
|
|
|
INSTANCES.put(connection, manager);
|
|
|
|
}
|
2018-06-19 16:41:02 +02:00
|
|
|
|
2018-05-28 00:58:13 +02:00
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
2018-06-01 15:36:50 +02:00
|
|
|
public void announceSupportForOxInstantMessaging() {
|
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection())
|
|
|
|
.addFeature(NAMESPACE_0);
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:06:09 +02:00
|
|
|
/**
|
|
|
|
* Determine, whether a contact announces support for XEP-0374: OpenPGP for XMPP: Instant Messaging.
|
|
|
|
*
|
|
|
|
* @param jid {@link BareJid} of the contact in question.
|
|
|
|
* @return true if contact announces support, otherwise false.
|
|
|
|
* @throws XMPPException.XMPPErrorException
|
|
|
|
* @throws SmackException.NotConnectedException
|
|
|
|
* @throws InterruptedException
|
|
|
|
* @throws SmackException.NoResponseException
|
|
|
|
*/
|
|
|
|
public boolean contactSupportsOxInstantMessaging(BareJid jid)
|
|
|
|
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
|
|
|
|
SmackException.NoResponseException {
|
|
|
|
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start an encrypted chat with {@code jid}.
|
|
|
|
* The chat is encrypted with OpenPGP for XMPP: Instant Messaging (XEP-0374).
|
|
|
|
*
|
|
|
|
* @see <a href="https://xmpp.org/extensions/xep-0374.html">XEP-0374: OpenPGP for XMPP: Instant Messaging</a>
|
|
|
|
* @param jid {@link BareJid} of the contact.
|
|
|
|
* @return {@link OpenPgpEncryptedChat} with the contact.
|
|
|
|
* @throws SmackOpenPgpException if something happens while gathering fingerprints.
|
|
|
|
* @throws InterruptedException
|
|
|
|
* @throws XMPPException.XMPPErrorException
|
|
|
|
* @throws SmackException.NotConnectedException
|
|
|
|
* @throws SmackException.NoResponseException
|
|
|
|
*/
|
|
|
|
public OpenPgpEncryptedChat chatWith(EntityBareJid jid)
|
|
|
|
throws SmackOpenPgpException, InterruptedException, XMPPException.XMPPErrorException,
|
2018-06-01 13:11:43 +02:00
|
|
|
SmackException.NotConnectedException, SmackException.NoResponseException {
|
2018-05-30 22:06:09 +02:00
|
|
|
|
2018-06-19 16:41:02 +02:00
|
|
|
OpenPgpEncryptedChat encryptedChat = chats.get(jid);
|
|
|
|
|
|
|
|
if (encryptedChat == null) {
|
|
|
|
OpenPgpFingerprints theirKeys = openPgpManager.determineContactsKeys(jid);
|
|
|
|
OpenPgpFingerprints ourKeys = openPgpManager.determineContactsKeys(connection().getUser().asBareJid());
|
|
|
|
Chat chat = chatManager.chatWith(jid);
|
|
|
|
encryptedChat = new OpenPgpEncryptedChat(openPgpManager.getOpenPgpProvider(), chat, ourKeys, theirKeys);
|
|
|
|
chats.put(jid, encryptedChat);
|
|
|
|
}
|
|
|
|
|
|
|
|
return encryptedChat;
|
2018-05-30 22:06:09 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 13:11:43 +02:00
|
|
|
public boolean addOpenPgpEncryptedMessageListener(OpenPgpEncryptedMessageListener listener) {
|
|
|
|
return chatMessageListeners.add(listener);
|
|
|
|
}
|
2018-05-28 00:58:13 +02:00
|
|
|
|
2018-06-01 13:11:43 +02:00
|
|
|
public boolean removeOpenPgpEncryptedMessageListener(OpenPgpEncryptedMessageListener listener) {
|
|
|
|
return chatMessageListeners.remove(listener);
|
2018-05-28 00:58:13 +02:00
|
|
|
}
|
2018-06-01 13:11:43 +02:00
|
|
|
|
|
|
|
private final IncomingChatMessageListener incomingChatMessageListener =
|
|
|
|
new IncomingChatMessageListener() {
|
|
|
|
@Override
|
|
|
|
public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
|
|
|
|
OpenPgpElement element = message.getExtension(OpenPgpElement.ELEMENT, OpenPgpElement.NAMESPACE);
|
|
|
|
if (element == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenPgpProvider provider = openPgpManager.getOpenPgpProvider();
|
2018-06-19 15:06:25 +02:00
|
|
|
byte[] decoded = Base64.decode(element.getEncryptedBase64MessageContent());
|
2018-06-01 13:11:43 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
OpenPgpEncryptedChat encryptedChat = chatWith(from);
|
2018-06-19 15:06:25 +02:00
|
|
|
DecryptedBytesAndMetadata decryptedBytes = provider.decrypt(decoded, from.asBareJid(), null);
|
2018-06-13 18:39:09 +02:00
|
|
|
|
|
|
|
OpenPgpMessage openPgpMessage = new OpenPgpMessage(decryptedBytes.getBytes(),
|
|
|
|
new OpenPgpMessage.Metadata(decryptedBytes.getDecryptionKey(),
|
|
|
|
decryptedBytes.getVerifiedSignatures()));
|
|
|
|
|
|
|
|
OpenPgpContentElement contentElement = openPgpMessage.getOpenPgpContentElement();
|
|
|
|
if (openPgpMessage.getState() != OpenPgpMessage.State.signcrypt) {
|
2018-06-01 13:11:43 +02:00
|
|
|
LOGGER.log(Level.WARNING, "Decrypted content is not a signcrypt element. Ignore it.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SigncryptElement signcryptElement = (SigncryptElement) contentElement;
|
|
|
|
for (OpenPgpEncryptedMessageListener l : chatMessageListeners) {
|
2018-06-13 18:39:09 +02:00
|
|
|
l.newIncomingOxMessage(from, message, signcryptElement, encryptedChat);
|
2018-06-01 13:11:43 +02:00
|
|
|
}
|
|
|
|
} catch (SmackOpenPgpException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Could not start chat with " + from, e);
|
|
|
|
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Something went wrong.", e);
|
|
|
|
} catch (MissingOpenPgpKeyPairException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Could not decrypt message " + message.getStanzaId() + ": Missing secret key", e);
|
|
|
|
} catch (XmlPullParserException | IOException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Could not parse decrypted content element", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2018-05-28 00:58:13 +02:00
|
|
|
}
|