2017-07-13 21:45:04 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright 2017 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.jet;
|
2017-07-13 21:36:37 +02:00
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.util.Collections;
|
2017-07-13 21:36:37 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
2017-07-14 21:19:03 +02:00
|
|
|
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;
|
2017-07-13 21:36:37 +02:00
|
|
|
|
|
|
|
import org.jivesoftware.smack.Manager;
|
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2017-07-14 21:19:03 +02:00
|
|
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
2017-07-17 20:18:08 +02:00
|
|
|
import org.jivesoftware.smackx.jet.element.JetSecurityElement;
|
2017-07-21 23:05:46 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.transport.legacy.JingleUtil;
|
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleContentElement;
|
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleElement;
|
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement;
|
2017-07-14 21:19:03 +02:00
|
|
|
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;
|
2017-07-13 21:36:37 +02:00
|
|
|
|
|
|
|
/**
|
2017-07-13 21:58:49 +02:00
|
|
|
* Manager for Jingle Encrypted Transfers (XEP-XXXX).
|
2017-07-13 21:36:37 +02:00
|
|
|
*/
|
2017-07-13 21:58:49 +02:00
|
|
|
public final class JetManager extends Manager {
|
2017-07-13 21:36:37 +02:00
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(JetManager.class.getName());
|
|
|
|
|
2017-07-13 21:36:37 +02:00
|
|
|
public static final String NAMESPACE = "urn:xmpp:jingle:jet:0";
|
|
|
|
|
2017-07-13 21:58:49 +02:00
|
|
|
private static final WeakHashMap<XMPPConnection, JetManager> INSTANCES = new WeakHashMap<>();
|
2017-07-14 21:19:03 +02:00
|
|
|
private final JingleUtil jutil;
|
2017-07-13 21:36:37 +02:00
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
private static final Map<String, JingleEncryptionMethod> encryptionMethods = new HashMap<>();
|
2017-07-13 21:36:37 +02:00
|
|
|
|
2017-07-13 21:58:49 +02:00
|
|
|
private JetManager(XMPPConnection connection) {
|
2017-07-13 21:36:37 +02:00
|
|
|
super(connection);
|
2017-07-14 21:19:03 +02:00
|
|
|
jutil = new JingleUtil(connection);
|
2017-07-13 21:36:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 21:58:49 +02:00
|
|
|
public static JetManager getInstanceFor(XMPPConnection connection) {
|
|
|
|
JetManager manager = INSTANCES.get(connection);
|
2017-07-13 21:36:37 +02:00
|
|
|
|
|
|
|
if (manager == null) {
|
2017-07-13 21:58:49 +02:00
|
|
|
manager = new JetManager(connection);
|
2017-07-13 21:36:37 +02:00
|
|
|
INSTANCES.put(connection, manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
public FileTransferHandler sendEncryptedFile(FullJid recipient, File file, String encryptionMethodNamespace) throws Exception {
|
|
|
|
|
|
|
|
JingleEncryptionMethod encryptionMethod = getEncryptionMethod(encryptionMethodNamespace);
|
|
|
|
if (encryptionMethod == null) {
|
|
|
|
throw new IllegalStateException("No encryption method with namespace " + encryptionMethodNamespace + " registered.");
|
|
|
|
}
|
|
|
|
|
|
|
|
int keyLength = 256;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fi != null) {
|
|
|
|
fi.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioe != null) {
|
|
|
|
throw ioe;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileBuf == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-17 20:18:08 +02:00
|
|
|
String contentName = JingleManager.randomId();
|
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
ExtensionElement encryptionExtension = encryptionMethod.encryptJingleTransfer(recipient, keyAndIv);
|
2017-07-17 20:18:08 +02:00
|
|
|
JetSecurityElement securityElement = new JetSecurityElement(contentName, encryptionExtension);
|
2017-07-14 21:19:03 +02:00
|
|
|
|
|
|
|
OutgoingJingleFileOffer offer = new OutgoingJingleFileOffer(connection(), recipient);
|
|
|
|
|
|
|
|
JingleFileTransferChild fileTransferChild = JingleFileTransferChild.getBuilder().setFile(file).build();
|
|
|
|
JingleFileTransfer fileTransfer = new JingleFileTransfer(Collections.<JingleContentDescriptionChildElement>singletonList(fileTransferChild));
|
2017-07-17 20:18:08 +02:00
|
|
|
|
2017-07-19 15:17:12 +02:00
|
|
|
JingleContentElement content = JingleContentElement.getBuilder()
|
|
|
|
.setCreator(JingleContentElement.Creator.initiator)
|
2017-07-17 20:18:08 +02:00
|
|
|
.setName(contentName)
|
|
|
|
.setTransport(offer.getTransportSession().createTransport())
|
|
|
|
.setSecurity(securityElement)
|
|
|
|
.setDescription(fileTransfer)
|
|
|
|
.build();
|
|
|
|
|
2017-07-19 15:17:12 +02:00
|
|
|
JingleElement initiate = jutil.createSessionInitiate(recipient, JingleManager.randomId(), content);
|
2017-07-14 21:19:03 +02:00
|
|
|
return offer;
|
|
|
|
}
|
|
|
|
|
2017-07-13 21:36:37 +02:00
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
public void registerEncryptionMethod(String namespace, JingleEncryptionMethod method) {
|
|
|
|
encryptionMethods.put(namespace, method);
|
2017-07-13 21:36:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
public void unregisterEncryptionMethod(String namespace) {
|
|
|
|
encryptionMethods.remove(namespace);
|
2017-07-13 21:36:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
public JingleEncryptionMethod getEncryptionMethod(String namespace) {
|
|
|
|
return encryptionMethods.get(namespace);
|
2017-07-13 21:36:37 +02:00
|
|
|
}
|
2017-07-14 21:19:03 +02:00
|
|
|
|
2017-07-13 21:36:37 +02:00
|
|
|
}
|