mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-27 14:32:06 +01:00
Temp
This commit is contained in:
parent
520f77112d
commit
59e79ef668
5 changed files with 182 additions and 18 deletions
|
@ -16,26 +16,54 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.jet;
|
package org.jivesoftware.smackx.jet;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.CipherInputStream;
|
||||||
|
import javax.crypto.KeyGenerator;
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
|
||||||
import org.jivesoftware.smack.Manager;
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
|
import org.jivesoftware.smack.packet.Element;
|
||||||
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
|
import org.jivesoftware.smackx.jingle.JingleManager;
|
||||||
|
import org.jivesoftware.smackx.jingle.JingleUtil;
|
||||||
|
import org.jivesoftware.smackx.jingle.element.Jingle;
|
||||||
|
import org.jivesoftware.smackx.jingle.element.JingleContent;
|
||||||
|
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement;
|
||||||
|
import org.jivesoftware.smackx.jingle_filetransfer.OutgoingJingleFileOffer;
|
||||||
|
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
|
||||||
|
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild;
|
||||||
|
import org.jivesoftware.smackx.jingle_filetransfer.handler.FileTransferHandler;
|
||||||
|
|
||||||
|
import org.jxmpp.jid.FullJid;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manager for Jingle Encrypted Transfers (XEP-XXXX).
|
* Manager for Jingle Encrypted Transfers (XEP-XXXX).
|
||||||
*/
|
*/
|
||||||
public final class JetManager extends Manager {
|
public final class JetManager extends Manager {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(JetManager.class.getName());
|
||||||
|
|
||||||
public static final String NAMESPACE = "urn:xmpp:jingle:jet:0";
|
public static final String NAMESPACE = "urn:xmpp:jingle:jet:0";
|
||||||
|
|
||||||
private static final WeakHashMap<XMPPConnection, JetManager> INSTANCES = new WeakHashMap<>();
|
private static final WeakHashMap<XMPPConnection, JetManager> INSTANCES = new WeakHashMap<>();
|
||||||
|
private final JingleUtil jutil;
|
||||||
|
|
||||||
private static final Map<String, JingleEncryptionMethod> encryptionProviders = new HashMap<>();
|
private static final Map<String, JingleEncryptionMethod> encryptionMethods = new HashMap<>();
|
||||||
|
|
||||||
private JetManager(XMPPConnection connection) {
|
private JetManager(XMPPConnection connection) {
|
||||||
super(connection);
|
super(connection);
|
||||||
|
jutil = new JingleUtil(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JetManager getInstanceFor(XMPPConnection connection) {
|
public static JetManager getInstanceFor(XMPPConnection connection) {
|
||||||
|
@ -49,16 +77,91 @@ public final class JetManager extends Manager {
|
||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FileTransferHandler sendEncryptedFile(FullJid recipient, File file, String encryptionMethodNamespace) throws Exception {
|
||||||
|
|
||||||
public void registerEncryptionProvider(String namespace, JingleEncryptionMethod provider) {
|
JingleEncryptionMethod encryptionMethod = getEncryptionMethod(encryptionMethodNamespace);
|
||||||
encryptionProviders.put(namespace, provider);
|
if (encryptionMethod == null) {
|
||||||
|
throw new IllegalStateException("No encryption method with namespace " + encryptionMethodNamespace + " registered.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unregisterEncryptionProvider(String namespace) {
|
int keyLength = 256;
|
||||||
encryptionProviders.remove(namespace);
|
String keyType = "AES";
|
||||||
|
String cipherMode = "AES/GCM/NoPadding";
|
||||||
|
|
||||||
|
KeyGenerator keyGenerator = KeyGenerator.getInstance(keyType);
|
||||||
|
keyGenerator.init(keyLength);
|
||||||
|
byte[] key = keyGenerator.generateKey().getEncoded();
|
||||||
|
|
||||||
|
SecureRandom secureRandom = new SecureRandom();
|
||||||
|
byte[] iv = new byte[keyLength];
|
||||||
|
secureRandom.nextBytes(iv);
|
||||||
|
|
||||||
|
byte[] keyAndIv = new byte[2 * keyLength];
|
||||||
|
System.arraycopy(key, 0, keyAndIv, 0, keyLength);
|
||||||
|
System.arraycopy(iv, 0, keyAndIv, keyLength, keyLength);
|
||||||
|
|
||||||
|
SecretKey secretKey = new SecretKeySpec(key, keyType);
|
||||||
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
|
Cipher cipher = Cipher.getInstance(cipherMode, "BC");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
|
||||||
|
|
||||||
|
Exception ioe = null;
|
||||||
|
byte[] fileBuf = null;
|
||||||
|
|
||||||
|
FileInputStream fi = null;
|
||||||
|
CipherInputStream ci = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
fi = new FileInputStream(file);
|
||||||
|
ci = new CipherInputStream(fi, cipher);
|
||||||
|
|
||||||
|
fileBuf = new byte[(int) file.length()];
|
||||||
|
ci.read(fileBuf);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
ioe = e;
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
if (ci != null) {
|
||||||
|
ci.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public JingleEncryptionMethod getEncryptionProvider(String namespace) {
|
if (fi != null) {
|
||||||
return encryptionProviders.get(namespace);
|
fi.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ioe != null) {
|
||||||
|
throw ioe;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fileBuf == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ExtensionElement encryptionExtension = encryptionMethod.encryptJingleTransfer(recipient, keyAndIv);
|
||||||
|
|
||||||
|
OutgoingJingleFileOffer offer = new OutgoingJingleFileOffer(connection(), recipient);
|
||||||
|
|
||||||
|
JingleFileTransferChild fileTransferChild = JingleFileTransferChild.getBuilder().setFile(file).build();
|
||||||
|
JingleFileTransfer fileTransfer = new JingleFileTransfer(Collections.<JingleContentDescriptionChildElement>singletonList(fileTransferChild));
|
||||||
|
Jingle initiate = jutil.createSessionInitiateFileOffer(recipient, JingleManager.randomId(), JingleContent.Creator.initiator,
|
||||||
|
JingleManager.randomId(), fileTransfer, offer.getTransportSession().createTransport(), Collections.<Element>singletonList(encryptionExtension));
|
||||||
|
return offer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void registerEncryptionMethod(String namespace, JingleEncryptionMethod method) {
|
||||||
|
encryptionMethods.put(namespace, method);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unregisterEncryptionMethod(String namespace) {
|
||||||
|
encryptionMethods.remove(namespace);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JingleEncryptionMethod getEncryptionMethod(String namespace) {
|
||||||
|
return encryptionMethods.get(namespace);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package org.jivesoftware.smackx.jet;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
|
import org.jivesoftware.smackx.jingle_filetransfer.OutgoingJingleFileOffer;
|
||||||
|
|
||||||
|
import org.jxmpp.jid.FullJid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by vanitas on 14.07.17.
|
||||||
|
*/
|
||||||
|
public class OutgoingJetOffer extends OutgoingJingleFileOffer {
|
||||||
|
|
||||||
|
public OutgoingJetOffer(XMPPConnection connection, FullJid responder, String sid) {
|
||||||
|
super(connection, responder, sid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutgoingJetOffer(XMPPConnection connection, FullJid recipient) {
|
||||||
|
super(connection, recipient);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void send(File file) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -122,7 +122,7 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
|
||||||
|
|
||||||
state = State.pending;
|
state = State.pending;
|
||||||
|
|
||||||
Jingle initiate = jutil.createSessionInitiateFileOffer(getResponder(), getSessionId(), creator, name, file, transportSession.createTransport());
|
Jingle initiate = jutil.createSessionInitiateFileOffer(getResponder(), getSessionId(), creator, name, file, transportSession.createTransport(), null);
|
||||||
this.contents.addAll(initiate.getContents());
|
this.contents.addAll(initiate.getContents());
|
||||||
|
|
||||||
connection.sendStanza(initiate);
|
connection.sendStanza(initiate);
|
||||||
|
|
|
@ -16,9 +16,12 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.jingle;
|
package org.jivesoftware.smackx.jingle;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
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.packet.Element;
|
||||||
import org.jivesoftware.smack.packet.IQ;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
import org.jivesoftware.smack.packet.XMPPError;
|
import org.jivesoftware.smack.packet.XMPPError;
|
||||||
import org.jivesoftware.smackx.jingle.element.Jingle;
|
import org.jivesoftware.smackx.jingle.element.Jingle;
|
||||||
|
@ -60,7 +63,8 @@ public class JingleUtil {
|
||||||
String contentName,
|
String contentName,
|
||||||
JingleContent.Senders contentSenders,
|
JingleContent.Senders contentSenders,
|
||||||
JingleContentDescription description,
|
JingleContentDescription description,
|
||||||
JingleContentTransport transport) {
|
JingleContentTransport transport,
|
||||||
|
List<Element> additionalElements) {
|
||||||
|
|
||||||
Jingle.Builder jb = Jingle.getBuilder();
|
Jingle.Builder jb = Jingle.getBuilder();
|
||||||
jb.setAction(JingleAction.session_initiate)
|
jb.setAction(JingleAction.session_initiate)
|
||||||
|
@ -72,7 +76,8 @@ public class JingleUtil {
|
||||||
.setName(contentName)
|
.setName(contentName)
|
||||||
.setSenders(contentSenders)
|
.setSenders(contentSenders)
|
||||||
.setDescription(description)
|
.setDescription(description)
|
||||||
.setTransport(transport);
|
.setTransport(transport)
|
||||||
|
.addAdditionalElements(additionalElements);
|
||||||
|
|
||||||
Jingle jingle = jb.addJingleContent(cb.build()).build();
|
Jingle jingle = jb.addJingleContent(cb.build()).build();
|
||||||
jingle.setFrom(connection.getUser());
|
jingle.setFrom(connection.getUser());
|
||||||
|
@ -97,9 +102,10 @@ public class JingleUtil {
|
||||||
JingleContent.Creator contentCreator,
|
JingleContent.Creator contentCreator,
|
||||||
String contentName,
|
String contentName,
|
||||||
JingleContentDescription description,
|
JingleContentDescription description,
|
||||||
JingleContentTransport transport) {
|
JingleContentTransport transport,
|
||||||
|
List<Element> additionalElements) {
|
||||||
return createSessionInitiate(recipient, sessionId, contentCreator, contentName,
|
return createSessionInitiate(recipient, sessionId, contentCreator, contentName,
|
||||||
JingleContent.Senders.initiator, description, transport);
|
JingleContent.Senders.initiator, description, transport, additionalElements);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IQ sendSessionInitiateFileOffer(FullJid recipient,
|
public IQ sendSessionInitiateFileOffer(FullJid recipient,
|
||||||
|
@ -107,11 +113,12 @@ public class JingleUtil {
|
||||||
JingleContent.Creator contentCreator,
|
JingleContent.Creator contentCreator,
|
||||||
String contentName,
|
String contentName,
|
||||||
JingleContentDescription description,
|
JingleContentDescription description,
|
||||||
JingleContentTransport transport)
|
JingleContentTransport transport,
|
||||||
|
List<Element> additionalElements)
|
||||||
throws SmackException.NotConnectedException, InterruptedException,
|
throws SmackException.NotConnectedException, InterruptedException,
|
||||||
XMPPException.XMPPErrorException, SmackException.NoResponseException {
|
XMPPException.XMPPErrorException, SmackException.NoResponseException {
|
||||||
|
|
||||||
Jingle jingle = createSessionInitiateFileOffer(recipient, sessionId, contentCreator, contentName, description, transport);
|
Jingle jingle = createSessionInitiateFileOffer(recipient, sessionId, contentCreator, contentName, description, transport, additionalElements);
|
||||||
return connection.createStanzaCollectorAndSend(jingle).nextResultOrThrow();
|
return connection.createStanzaCollectorAndSend(jingle).nextResultOrThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,12 +128,13 @@ public class JingleUtil {
|
||||||
String contentName,
|
String contentName,
|
||||||
JingleContent.Senders contentSenders,
|
JingleContent.Senders contentSenders,
|
||||||
JingleContentDescription description,
|
JingleContentDescription description,
|
||||||
JingleContentTransport transport)
|
JingleContentTransport transport,
|
||||||
|
List<Element> additionalElements)
|
||||||
throws SmackException.NotConnectedException, InterruptedException,
|
throws SmackException.NotConnectedException, InterruptedException,
|
||||||
XMPPException.XMPPErrorException, SmackException.NoResponseException {
|
XMPPException.XMPPErrorException, SmackException.NoResponseException {
|
||||||
|
|
||||||
Jingle jingle = createSessionInitiate(recipient, sessionId, contentCreator, contentName, contentSenders,
|
Jingle jingle = createSessionInitiate(recipient, sessionId, contentCreator, contentName, contentSenders,
|
||||||
description, transport);
|
description, transport, additionalElements);
|
||||||
|
|
||||||
return connection.createStanzaCollectorAndSend(jingle).nextResult();
|
return connection.createStanzaCollectorAndSend(jingle).nextResult();
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,10 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.jingle.element;
|
package org.jivesoftware.smackx.jingle.element;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.packet.Element;
|
||||||
import org.jivesoftware.smack.packet.NamedElement;
|
import org.jivesoftware.smack.packet.NamedElement;
|
||||||
import org.jivesoftware.smack.util.Objects;
|
import org.jivesoftware.smack.util.Objects;
|
||||||
import org.jivesoftware.smack.util.StringUtils;
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
|
@ -68,17 +72,22 @@ public final class JingleContent implements NamedElement {
|
||||||
|
|
||||||
private final JingleContentTransport transport;
|
private final JingleContentTransport transport;
|
||||||
|
|
||||||
|
private final List<Element> additionalElements = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a content description..
|
* Creates a content description..
|
||||||
*/
|
*/
|
||||||
private JingleContent(Creator creator, String disposition, String name, Senders senders,
|
private JingleContent(Creator creator, String disposition, String name, Senders senders,
|
||||||
JingleContentDescription description, JingleContentTransport transport) {
|
JingleContentDescription description, JingleContentTransport transport, List<Element> additionalElements) {
|
||||||
this.creator = Objects.requireNonNull(creator, "Jingle content creator must not be null");
|
this.creator = Objects.requireNonNull(creator, "Jingle content creator must not be null");
|
||||||
this.disposition = disposition;
|
this.disposition = disposition;
|
||||||
this.name = StringUtils.requireNotNullOrEmpty(name, "Jingle content name must not be null or empty");
|
this.name = StringUtils.requireNotNullOrEmpty(name, "Jingle content name must not be null or empty");
|
||||||
this.senders = senders;
|
this.senders = senders;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.transport = transport;
|
this.transport = transport;
|
||||||
|
if (additionalElements != null) {
|
||||||
|
this.additionalElements.addAll(additionalElements);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Creator getCreator() {
|
public Creator getCreator() {
|
||||||
|
@ -115,6 +124,10 @@ public final class JingleContent implements NamedElement {
|
||||||
return transport;
|
return transport;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Element> getAdditionalElements() {
|
||||||
|
return additionalElements;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getElementName() {
|
public String getElementName() {
|
||||||
return ELEMENT;
|
return ELEMENT;
|
||||||
|
@ -132,6 +145,10 @@ public final class JingleContent implements NamedElement {
|
||||||
xml.optAppend(description);
|
xml.optAppend(description);
|
||||||
xml.optElement(transport);
|
xml.optElement(transport);
|
||||||
|
|
||||||
|
for (Element element : additionalElements) {
|
||||||
|
xml.element(element);
|
||||||
|
}
|
||||||
|
|
||||||
xml.closeElement(this);
|
xml.closeElement(this);
|
||||||
return xml;
|
return xml;
|
||||||
}
|
}
|
||||||
|
@ -153,6 +170,8 @@ public final class JingleContent implements NamedElement {
|
||||||
|
|
||||||
private JingleContentTransport transport;
|
private JingleContentTransport transport;
|
||||||
|
|
||||||
|
private List<Element> additionalElements = new ArrayList<>();
|
||||||
|
|
||||||
private Builder() {
|
private Builder() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,8 +208,15 @@ public final class JingleContent implements NamedElement {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder addAdditionalElements(List<Element> elements) {
|
||||||
|
if (elements != null) {
|
||||||
|
additionalElements.addAll(elements);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public JingleContent build() {
|
public JingleContent build() {
|
||||||
return new JingleContent(creator, disposition, name, senders, description, transport);
|
return new JingleContent(creator, disposition, name, senders, description, transport, additionalElements);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue