2017-02-12 19:26:28 +01:00
|
|
|
/**
|
|
|
|
*
|
2018-05-08 12:53:36 +02:00
|
|
|
* Copyright 2017 Florian Schmaus, 2018 Paul Schaub.
|
2017-02-12 19:26:28 +01:00
|
|
|
*
|
|
|
|
* 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-05-21 12:44:27 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
2018-05-08 12:53:36 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
2018-05-21 15:42:04 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2017-02-12 19:26:28 +01:00
|
|
|
|
2018-05-08 12:53:36 +02:00
|
|
|
import org.jivesoftware.smack.Manager;
|
2018-05-21 12:44:27 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2018-05-08 12:53:36 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2018-05-21 12:44:27 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2018-05-21 15:42:04 +02:00
|
|
|
import org.jivesoftware.smack.util.Async;
|
2018-05-21 12:44:27 +02:00
|
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
|
|
|
import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
|
|
|
import org.jivesoftware.smackx.ox.element.PublicKeysListElement;
|
|
|
|
import org.jivesoftware.smackx.pep.PEPListener;
|
|
|
|
import org.jivesoftware.smackx.pep.PEPManager;
|
|
|
|
import org.jivesoftware.smackx.pubsub.EventElement;
|
|
|
|
import org.jivesoftware.smackx.pubsub.Item;
|
2018-05-21 15:42:04 +02:00
|
|
|
import org.jivesoftware.smackx.pubsub.ItemsExtension;
|
2018-05-21 12:44:27 +02:00
|
|
|
import org.jivesoftware.smackx.pubsub.LeafNode;
|
|
|
|
import org.jivesoftware.smackx.pubsub.PayloadItem;
|
|
|
|
import org.jivesoftware.smackx.pubsub.PubSubException;
|
|
|
|
import org.jivesoftware.smackx.pubsub.PubSubManager;
|
2018-05-08 12:53:36 +02:00
|
|
|
|
|
|
|
import org.jxmpp.jid.BareJid;
|
2018-05-21 12:44:27 +02:00
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
2018-05-08 12:53:36 +02:00
|
|
|
|
2018-05-21 15:42:04 +02:00
|
|
|
public final class OpenPgpManager extends Manager {
|
|
|
|
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(OpenPgpManager.class.getName());
|
2018-05-08 12:53:36 +02:00
|
|
|
|
|
|
|
public static final String PEP_NODE_PUBLIC_KEYS = "urn:xmpp:openpgp:0:public-keys";
|
2018-05-21 12:44:27 +02:00
|
|
|
public static final String PEP_NODE_PUBLIC_KEYS_NOTIFY = PEP_NODE_PUBLIC_KEYS + "+notify";
|
2018-05-08 12:53:36 +02:00
|
|
|
|
|
|
|
public static String PEP_NODE_PUBLIC_KEY(String id) {
|
|
|
|
return PEP_NODE_PUBLIC_KEYS + ":" + id;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final Map<XMPPConnection, OpenPgpManager> INSTANCES = new WeakHashMap<>();
|
2018-05-21 12:44:27 +02:00
|
|
|
private OpenPgpProvider provider;
|
2018-05-08 12:53:36 +02:00
|
|
|
|
|
|
|
private OpenPgpManager(XMPPConnection connection) {
|
|
|
|
super(connection);
|
2018-05-21 12:44:27 +02:00
|
|
|
|
|
|
|
// Subscribe to public key changes
|
2018-05-21 15:42:04 +02:00
|
|
|
PEPManager.getInstanceFor(connection()).addPEPListener(metadataListener);
|
2018-05-21 12:44:27 +02:00
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection())
|
|
|
|
.addFeature(PEP_NODE_PUBLIC_KEYS_NOTIFY);
|
2018-05-08 12:53:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static OpenPgpManager getInstanceFor(XMPPConnection connection) {
|
|
|
|
OpenPgpManager manager = INSTANCES.get(connection);
|
|
|
|
if (manager == null) {
|
|
|
|
manager = new OpenPgpManager(connection);
|
|
|
|
INSTANCES.put(connection, manager);
|
|
|
|
}
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:44:27 +02:00
|
|
|
public void setOpenPgpProvider(OpenPgpProvider provider) {
|
|
|
|
this.provider = provider;
|
2017-02-12 19:26:28 +01:00
|
|
|
}
|
2018-05-08 12:53:36 +02:00
|
|
|
|
2018-05-21 12:44:27 +02:00
|
|
|
public void publishPublicKey() throws Exception {
|
|
|
|
ensureProviderIsSet();
|
|
|
|
PubkeyElement pubkeyElement = provider.createPubkeyElement();
|
|
|
|
|
|
|
|
String fingerprint = provider.getFingerprint();
|
|
|
|
String keyNodeName = PEP_NODE_PUBLIC_KEY(fingerprint);
|
|
|
|
PubSubManager pm = PubSubManager.getInstance(connection(), connection().getUser().asBareJid());
|
|
|
|
|
2018-05-08 12:53:36 +02:00
|
|
|
// Check if key available at data node
|
|
|
|
// If not, publish key to data node
|
2018-05-21 12:44:27 +02:00
|
|
|
LeafNode keyNode = pm.getOrCreateLeafNode(keyNodeName);
|
|
|
|
List<Item> items = keyNode.getItems(1);
|
|
|
|
if (items.isEmpty()) {
|
2018-05-21 15:42:04 +02:00
|
|
|
LOGGER.log(Level.FINE, "Node " + keyNodeName + " is empty. Publish.");
|
2018-05-21 12:44:27 +02:00
|
|
|
keyNode.publish(new PayloadItem<>(pubkeyElement));
|
|
|
|
}
|
|
|
|
|
2018-05-08 12:53:36 +02:00
|
|
|
// Publish ID to metadata node
|
2018-05-21 12:44:27 +02:00
|
|
|
LeafNode metadataNode = pm.getOrCreateLeafNode(PEP_NODE_PUBLIC_KEYS);
|
|
|
|
List<PayloadItem<PublicKeysListElement>> metadataItems = metadataNode.getItems(1);
|
|
|
|
|
|
|
|
PublicKeysListElement.Builder builder = PublicKeysListElement.builder();
|
|
|
|
if (!metadataItems.isEmpty() && metadataItems.get(0).getPayload() != null) {
|
|
|
|
// Add old entries back to list.
|
|
|
|
PublicKeysListElement publishedList = metadataItems.get(0).getPayload();
|
|
|
|
for (PublicKeysListElement.PubkeyMetadataElement meta : publishedList.getMetadata().values()) {
|
|
|
|
builder.addMetadata(meta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
builder.addMetadata(new PublicKeysListElement.PubkeyMetadataElement(fingerprint, new Date()));
|
|
|
|
|
|
|
|
metadataNode.publish(new PayloadItem<>(builder.build()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public PublicKeysListElement fetchPubkeysList()
|
|
|
|
throws InterruptedException, PubSubException.NotALeafNodeException, SmackException.NoResponseException,
|
|
|
|
SmackException.NotConnectedException, XMPPException.XMPPErrorException,
|
|
|
|
PubSubException.NotAPubSubNodeException {
|
|
|
|
return fetchPubkeysList(connection().getUser().asBareJid());
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:42:04 +02:00
|
|
|
public void deletePubkeysListNode()
|
|
|
|
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
|
|
|
|
SmackException.NoResponseException {
|
|
|
|
PubSubManager pm = PubSubManager.getInstance(connection(), connection().getUser().asBareJid());
|
|
|
|
pm.deleteNode(PEP_NODE_PUBLIC_KEYS);
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:44:27 +02:00
|
|
|
public PublicKeysListElement fetchPubkeysList(BareJid jid)
|
|
|
|
throws InterruptedException, PubSubException.NotALeafNodeException, SmackException.NoResponseException,
|
|
|
|
SmackException.NotConnectedException, XMPPException.XMPPErrorException,
|
2018-05-21 15:42:04 +02:00
|
|
|
PubSubException.NotAPubSubNodeException {
|
2018-05-21 12:44:27 +02:00
|
|
|
PubSubManager pm = PubSubManager.getInstance(connection(), jid);
|
|
|
|
|
|
|
|
LeafNode node = pm.getLeafNode(PEP_NODE_PUBLIC_KEYS);
|
|
|
|
List<PayloadItem<PublicKeysListElement>> list = node.getItems(1);
|
|
|
|
|
|
|
|
if (list.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.get(0).getPayload();
|
|
|
|
}
|
|
|
|
|
|
|
|
public PubkeyElement fetchPubkey(BareJid jid, String v4_fingerprint)
|
|
|
|
throws InterruptedException, PubSubException.NotALeafNodeException, SmackException.NoResponseException,
|
|
|
|
SmackException.NotConnectedException, XMPPException.XMPPErrorException,
|
|
|
|
PubSubException.NotAPubSubNodeException {
|
|
|
|
PubSubManager pm = PubSubManager.getInstance(connection(), jid);
|
|
|
|
|
|
|
|
LeafNode node = pm.getLeafNode(PEP_NODE_PUBLIC_KEY(v4_fingerprint));
|
|
|
|
List<PayloadItem<PubkeyElement>> list = node.getItems(1);
|
|
|
|
|
|
|
|
if (list.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.get(0).getPayload();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void depositSecretKey() {
|
|
|
|
ensureProviderIsSet();
|
|
|
|
// Create key backup by appending serialized unencrypted secret keys.
|
|
|
|
// Encrypt the backup using a random generated password
|
|
|
|
// Publish the backup to the secret key node (whitelist protected)
|
|
|
|
// Display the backup key to the user
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getOurFingerprint() throws Exception {
|
|
|
|
ensureProviderIsSet();
|
|
|
|
return provider.getFingerprint();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw an {@link IllegalStateException} if no {@link OpenPgpProvider} is set.
|
|
|
|
*/
|
|
|
|
private void ensureProviderIsSet() {
|
|
|
|
if (provider == null) {
|
|
|
|
throw new IllegalStateException("No OpenPgpProvider set!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:42:04 +02:00
|
|
|
public boolean canSyncSecretKey()
|
|
|
|
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
|
|
|
|
SmackException.NoResponseException {
|
|
|
|
boolean pep = PEPManager.getInstanceFor(connection()).isSupported();
|
|
|
|
boolean whitelist = PubSubManager.getInstance(connection(), connection().getUser().asBareJid())
|
|
|
|
.getSupportedFeatures().containsFeature("http://jabber.org/protocol/pubsub#access-whitelist");
|
|
|
|
return pep && whitelist;
|
2018-05-08 12:53:36 +02:00
|
|
|
}
|
2018-05-21 15:42:04 +02:00
|
|
|
|
|
|
|
private final PEPListener metadataListener = new PEPListener() {
|
|
|
|
@Override
|
|
|
|
public void eventReceived(EntityBareJid from, final EventElement event, Message message) {
|
|
|
|
if (PEP_NODE_PUBLIC_KEYS.equals(event.getEvent().getNode())) {
|
|
|
|
LOGGER.log(Level.INFO, "Received OpenPGP metadata update from " + from);
|
|
|
|
Async.go(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
ItemsExtension items = (ItemsExtension) event.getExtensions().get(0);
|
|
|
|
PayloadItem<?> payload = (PayloadItem) items.getItems().get(0);
|
|
|
|
PublicKeysListElement listElement = (PublicKeysListElement) payload.getPayload();
|
|
|
|
|
|
|
|
}
|
|
|
|
}, "ProcessOXPublicKey");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-02-12 19:26:28 +01:00
|
|
|
}
|