2018-05-30 22:06:09 +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.
|
|
|
|
*/
|
2018-06-13 18:39:09 +02:00
|
|
|
package org.jivesoftware.smackx.ox.chat;
|
2018-05-30 22:06:09 +02:00
|
|
|
|
2018-06-13 18:39:09 +02:00
|
|
|
import java.io.IOException;
|
2018-05-30 22:06:09 +02:00
|
|
|
import java.util.Collections;
|
2018-07-03 18:07:54 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
2018-05-30 22:06:09 +02:00
|
|
|
import java.util.List;
|
2018-07-03 18:07:54 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2018-05-30 22:06:09 +02:00
|
|
|
|
2018-06-01 15:36:50 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2018-06-20 12:45:05 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2018-07-03 18:07:54 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2018-06-20 12:45:05 +02:00
|
|
|
import org.jivesoftware.smack.chat2.ChatManager;
|
2018-05-30 22:06:09 +02:00
|
|
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2018-06-13 18:39:09 +02:00
|
|
|
import org.jivesoftware.smack.util.MultiMap;
|
|
|
|
import org.jivesoftware.smack.util.stringencoder.Base64;
|
2018-05-30 22:06:09 +02:00
|
|
|
import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement;
|
|
|
|
import org.jivesoftware.smackx.hints.element.StoreHint;
|
2018-07-03 18:07:54 +02:00
|
|
|
import org.jivesoftware.smackx.ox.OpenPgpManager;
|
2018-06-13 18:39:09 +02:00
|
|
|
import org.jivesoftware.smackx.ox.OpenPgpProvider;
|
|
|
|
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
|
2018-06-20 11:02:30 +02:00
|
|
|
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
|
2018-05-30 22:06:09 +02:00
|
|
|
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
2018-07-03 18:07:54 +02:00
|
|
|
import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
|
|
|
import org.jivesoftware.smackx.ox.element.PublicKeysListElement;
|
2018-05-30 22:06:09 +02:00
|
|
|
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
|
|
|
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyPairException;
|
|
|
|
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpPublicKeyException;
|
2018-07-03 18:07:54 +02:00
|
|
|
import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException;
|
2018-06-13 18:39:09 +02:00
|
|
|
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
|
2018-06-20 11:02:30 +02:00
|
|
|
import org.jivesoftware.smackx.ox.util.DecryptedBytesAndMetadata;
|
2018-07-03 18:07:54 +02:00
|
|
|
import org.jivesoftware.smackx.ox.util.PubSubDelegate;
|
2018-05-30 22:06:09 +02:00
|
|
|
|
2018-06-13 18:39:09 +02:00
|
|
|
import org.jxmpp.jid.BareJid;
|
2018-05-30 22:06:09 +02:00
|
|
|
import org.jxmpp.jid.Jid;
|
2018-06-20 11:02:30 +02:00
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
2018-05-30 22:06:09 +02:00
|
|
|
|
2018-07-03 18:07:54 +02:00
|
|
|
public class OpenPgpContact {
|
|
|
|
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(OpenPgpContact.class.getName());
|
2018-05-30 22:06:09 +02:00
|
|
|
|
2018-06-20 12:45:05 +02:00
|
|
|
private final BareJid jid;
|
2018-07-03 18:07:54 +02:00
|
|
|
protected final OpenPgpProvider cryptoProvider;
|
|
|
|
private final XMPPConnection connection;
|
|
|
|
|
|
|
|
private Map<OpenPgpV4Fingerprint, Date> announcedKeys = null;
|
|
|
|
private Map<OpenPgpV4Fingerprint, Date> availableKeys = null;
|
|
|
|
private final Map<OpenPgpV4Fingerprint, Throwable> unfetchableKeys = new HashMap<>();
|
2018-05-30 22:06:09 +02:00
|
|
|
|
2018-06-20 11:02:30 +02:00
|
|
|
public OpenPgpContact(OpenPgpProvider cryptoProvider,
|
2018-06-20 12:45:05 +02:00
|
|
|
BareJid jid,
|
2018-07-03 18:07:54 +02:00
|
|
|
XMPPConnection connection) {
|
2018-06-20 12:45:05 +02:00
|
|
|
this.jid = jid;
|
2018-07-03 18:07:54 +02:00
|
|
|
this.cryptoProvider = cryptoProvider;
|
|
|
|
this.connection = connection;
|
2018-05-30 22:06:09 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 12:45:05 +02:00
|
|
|
public BareJid getJid() {
|
|
|
|
return jid;
|
2018-06-20 11:02:30 +02:00
|
|
|
}
|
|
|
|
|
2018-07-03 18:07:54 +02:00
|
|
|
public Map<OpenPgpV4Fingerprint, Date> getAnnouncedKeys() {
|
|
|
|
if (announcedKeys == null) {
|
|
|
|
announcedKeys = cryptoProvider.getStore().getAnnouncedKeysFingerprints(getJid());
|
|
|
|
}
|
|
|
|
return announcedKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<OpenPgpV4Fingerprint, Date> getAvailableKeys() throws SmackOpenPgpException {
|
|
|
|
if (availableKeys == null) {
|
|
|
|
availableKeys = cryptoProvider.getStore().getAvailableKeysFingerprints(getJid());
|
|
|
|
}
|
|
|
|
return availableKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<OpenPgpV4Fingerprint, Throwable> getUnfetchableKeys() {
|
|
|
|
return unfetchableKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<OpenPgpV4Fingerprint> getActiveKeys() throws SmackOpenPgpException {
|
|
|
|
Set<OpenPgpV4Fingerprint> fingerprints = getAvailableKeys().keySet();
|
|
|
|
fingerprints.retainAll(getAnnouncedKeys().keySet());
|
|
|
|
return fingerprints;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateKeys()
|
|
|
|
throws InterruptedException, XMPPException.XMPPErrorException, SmackException, SmackOpenPgpException {
|
|
|
|
updateKeys(PubSubDelegate.fetchPubkeysList(connection, getJid()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateKeys(PublicKeysListElement metadata)
|
|
|
|
throws SmackOpenPgpException {
|
|
|
|
storePublishedDevices(metadata);
|
|
|
|
this.availableKeys = getAvailableKeys();
|
|
|
|
|
|
|
|
for (OpenPgpV4Fingerprint fingerprint : announcedKeys.keySet()) {
|
|
|
|
Date announcedDate = announcedKeys.get(fingerprint);
|
|
|
|
Date availableDate = availableKeys.get(fingerprint);
|
|
|
|
|
|
|
|
if (availableDate == null || availableDate.before(announcedDate)) {
|
|
|
|
try {
|
|
|
|
updateKey(fingerprint);
|
|
|
|
unfetchableKeys.remove(fingerprint);
|
|
|
|
} catch (IOException | XMPPException.XMPPErrorException | SmackException | InterruptedException |
|
|
|
|
SmackOpenPgpException | MissingUserIdOnKeyException | NullPointerException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Could not update key " + fingerprint + " of " +getJid());
|
|
|
|
unfetchableKeys.put(fingerprint, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateKey(OpenPgpV4Fingerprint fingerprint)
|
|
|
|
throws InterruptedException, XMPPException.XMPPErrorException, SmackException, IOException,
|
|
|
|
MissingUserIdOnKeyException, SmackOpenPgpException {
|
|
|
|
PubkeyElement pubkeyElement = PubSubDelegate.fetchPubkey(connection, getJid(), fingerprint);
|
|
|
|
if (pubkeyElement == null) {
|
|
|
|
throw new NullPointerException("Fetched pubkeyElement for key " + fingerprint + " of " + getJid() + " is null.");
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] base64 = pubkeyElement.getDataElement().getB64Data();
|
|
|
|
OpenPgpV4Fingerprint imported = importPublicKey(Base64.decode(base64));
|
|
|
|
|
|
|
|
if (!fingerprint.equals(imported)) {
|
|
|
|
// Not sure, if this can/should happen. Lets be safe and throw, even if its too late at this point.
|
|
|
|
throw new AssertionError("Fingerprint of imported key differs from expected fingerprint. " +
|
|
|
|
"Expected: " + fingerprint + " Imported: " + imported);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private OpenPgpV4Fingerprint importPublicKey(byte[] data)
|
|
|
|
throws SmackOpenPgpException, MissingUserIdOnKeyException, IOException {
|
|
|
|
OpenPgpV4Fingerprint fingerprint = cryptoProvider.importPublicKey(getJid(), data);
|
|
|
|
availableKeys.put(fingerprint, new Date());
|
|
|
|
return fingerprint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<OpenPgpV4Fingerprint, Date> storePublishedDevices(PublicKeysListElement element) {
|
|
|
|
Map<OpenPgpV4Fingerprint, Date> announcedKeys = new HashMap<>();
|
|
|
|
|
|
|
|
for (OpenPgpV4Fingerprint f : element.getMetadata().keySet()) {
|
|
|
|
PublicKeysListElement.PubkeyMetadataElement meta = element.getMetadata().get(f);
|
|
|
|
announcedKeys.put(meta.getV4Fingerprint(), meta.getDate());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!announcedKeys.equals(this.announcedKeys)) {
|
|
|
|
cryptoProvider.getStore().setAnnouncedKeysFingerprints(getJid(), announcedKeys);
|
|
|
|
this.announcedKeys = announcedKeys;
|
|
|
|
}
|
|
|
|
return announcedKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
private MultiMap<BareJid, OpenPgpV4Fingerprint> getEncryptionKeys()
|
|
|
|
throws SmackOpenPgpException, SmackException.NotLoggedInException {
|
|
|
|
OpenPgpSelf self = getSelf();
|
|
|
|
|
|
|
|
Set<OpenPgpV4Fingerprint> contactsKeys = getActiveKeys();
|
|
|
|
Set<OpenPgpV4Fingerprint> ourKeys = self.getActiveKeys();
|
|
|
|
|
|
|
|
MultiMap<BareJid, OpenPgpV4Fingerprint> recipientsKeys = new MultiMap<>();
|
|
|
|
for (OpenPgpV4Fingerprint fingerprint : contactsKeys) {
|
|
|
|
recipientsKeys.put(getJid(), fingerprint);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (OpenPgpV4Fingerprint fingerprint : ourKeys) {
|
|
|
|
recipientsKeys.put(self.getJid(), fingerprint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return recipientsKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
private OpenPgpSelf getSelf() throws SmackException.NotLoggedInException {
|
|
|
|
return OpenPgpManager.getInstanceFor(connection).getOpenPgpSelf();
|
2018-06-20 11:02:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 15:20:04 +02:00
|
|
|
public OpenPgpElement encryptAndSign(List<ExtensionElement> payload)
|
2018-07-03 18:07:54 +02:00
|
|
|
throws IOException, SmackOpenPgpException, MissingOpenPgpKeyPairException,
|
|
|
|
SmackException.NotLoggedInException {
|
|
|
|
|
|
|
|
OpenPgpSelf self = OpenPgpManager.getInstanceFor(connection).getOpenPgpSelf();
|
2018-06-20 12:45:05 +02:00
|
|
|
|
|
|
|
SigncryptElement preparedPayload = new SigncryptElement(
|
|
|
|
Collections.<Jid>singleton(getJid()),
|
|
|
|
payload);
|
|
|
|
|
|
|
|
byte[] encryptedBytes;
|
|
|
|
|
|
|
|
// Encrypt the payload
|
|
|
|
try {
|
|
|
|
encryptedBytes = cryptoProvider.signAndEncrypt(
|
|
|
|
preparedPayload,
|
2018-07-03 18:07:54 +02:00
|
|
|
self.getSigningKey(),
|
|
|
|
getEncryptionKeys());
|
2018-06-20 12:45:05 +02:00
|
|
|
} catch (MissingOpenPgpPublicKeyException e) {
|
|
|
|
throw new AssertionError("Missing OpenPGP public key, even though this should not happen here.", e);
|
|
|
|
}
|
|
|
|
|
2018-06-21 15:20:04 +02:00
|
|
|
return new OpenPgpElement(Base64.encodeToString(encryptedBytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addSignedEncryptedPayloadTo(Message message, List<ExtensionElement> payload)
|
2018-07-03 18:07:54 +02:00
|
|
|
throws IOException, SmackOpenPgpException, MissingOpenPgpKeyPairException,
|
|
|
|
SmackException.NotLoggedInException {
|
2018-06-20 12:45:05 +02:00
|
|
|
|
|
|
|
// Add encrypted payload to message
|
2018-06-21 15:20:04 +02:00
|
|
|
OpenPgpElement encryptedPayload = encryptAndSign(payload);
|
2018-06-20 12:45:05 +02:00
|
|
|
message.addExtension(encryptedPayload);
|
|
|
|
|
|
|
|
// Add additional information to the message
|
2018-07-03 11:40:02 +02:00
|
|
|
if (!ExplicitMessageEncryptionElement.hasProtocol(message,
|
|
|
|
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0)) {
|
|
|
|
message.addExtension(new ExplicitMessageEncryptionElement(
|
|
|
|
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));
|
|
|
|
}
|
2018-06-20 12:45:05 +02:00
|
|
|
StoreHint.set(message);
|
|
|
|
message.setBody("This message is encrypted using XEP-0374: OpenPGP for XMPP: Instant Messaging.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void send(XMPPConnection connection, Message message, List<ExtensionElement> payload)
|
2018-06-13 18:39:09 +02:00
|
|
|
throws MissingOpenPgpKeyPairException, SmackException.NotConnectedException, InterruptedException,
|
2018-07-03 18:07:54 +02:00
|
|
|
SmackOpenPgpException, IOException, SmackException.NotLoggedInException {
|
|
|
|
MultiMap<BareJid, OpenPgpV4Fingerprint> fingerprints = getEncryptionKeys();
|
2018-05-30 22:06:09 +02:00
|
|
|
|
|
|
|
SigncryptElement preparedPayload = new SigncryptElement(
|
2018-06-20 12:45:05 +02:00
|
|
|
Collections.<Jid>singleton(getJid()),
|
2018-05-30 22:06:09 +02:00
|
|
|
payload);
|
2018-06-13 18:39:09 +02:00
|
|
|
|
2018-05-30 22:06:09 +02:00
|
|
|
OpenPgpElement encryptedPayload;
|
2018-06-13 18:39:09 +02:00
|
|
|
byte[] encryptedMessage;
|
2018-05-30 22:06:09 +02:00
|
|
|
|
|
|
|
// Encrypt the payload
|
|
|
|
try {
|
2018-06-13 18:39:09 +02:00
|
|
|
encryptedMessage = cryptoProvider.signAndEncrypt(
|
2018-05-30 22:06:09 +02:00
|
|
|
preparedPayload,
|
2018-07-03 18:07:54 +02:00
|
|
|
getSelf().getSigningKey(),
|
2018-06-13 18:39:09 +02:00
|
|
|
fingerprints);
|
2018-05-30 22:06:09 +02:00
|
|
|
} catch (MissingOpenPgpPublicKeyException e) {
|
|
|
|
throw new AssertionError("Missing OpenPGP public key, even though this should not happen here.", e);
|
|
|
|
}
|
|
|
|
|
2018-06-13 18:39:09 +02:00
|
|
|
encryptedPayload = new OpenPgpElement(Base64.encodeToString(encryptedMessage));
|
|
|
|
|
2018-05-30 22:06:09 +02:00
|
|
|
// Add encrypted payload to message
|
|
|
|
message.addExtension(encryptedPayload);
|
|
|
|
|
2018-06-20 12:45:05 +02:00
|
|
|
ChatManager.getInstanceFor(connection).chatWith(getJid().asEntityBareJidIfPossible()).send(message);
|
2018-05-30 22:06:09 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 11:02:30 +02:00
|
|
|
public OpenPgpContentElement receive(OpenPgpElement element)
|
|
|
|
throws XmlPullParserException, MissingOpenPgpKeyPairException, SmackOpenPgpException, IOException {
|
|
|
|
byte[] decoded = Base64.decode(element.getEncryptedBase64MessageContent());
|
|
|
|
|
2018-06-20 12:45:05 +02:00
|
|
|
DecryptedBytesAndMetadata decryptedBytes = cryptoProvider.decrypt(decoded, getJid(), null);
|
2018-06-20 11:02:30 +02:00
|
|
|
|
|
|
|
OpenPgpMessage openPgpMessage = new OpenPgpMessage(decryptedBytes.getBytes(),
|
|
|
|
new OpenPgpMessage.Metadata(decryptedBytes.getDecryptionKey(),
|
|
|
|
decryptedBytes.getVerifiedSignatures()));
|
|
|
|
|
|
|
|
return openPgpMessage.getOpenPgpContentElement();
|
|
|
|
}
|
2018-05-30 22:06:09 +02:00
|
|
|
}
|