mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-23 20:42:06 +01:00
More progress on JET
This commit is contained in:
parent
c82b25ea09
commit
b0b46ad167
12 changed files with 339 additions and 65 deletions
|
@ -0,0 +1,43 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.ciphers;
|
||||||
|
|
||||||
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.NoSuchProviderException;
|
||||||
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
|
||||||
|
public class Aes128GcmNoPadding extends AesGcmNoPadding {
|
||||||
|
public static final String NAMESPACE = "urn:xmpp:ciphers:aes-128-gcm-nopadding:0";
|
||||||
|
|
||||||
|
public Aes128GcmNoPadding() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
|
||||||
|
NoSuchProviderException, InvalidAlgorithmParameterException {
|
||||||
|
super(128);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Aes128GcmNoPadding(byte[] keyAndIv) throws NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||||
|
NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException {
|
||||||
|
super(AesGcmNoPadding.copyOfRange(keyAndIv, 0, keyAndIv.length / 2), //Key
|
||||||
|
AesGcmNoPadding.copyOfRange(keyAndIv, keyAndIv.length / 2, keyAndIv.length / 2)); //IV
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNamespace() {
|
||||||
|
return NAMESPACE;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.ciphers;
|
||||||
|
|
||||||
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.NoSuchProviderException;
|
||||||
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
|
||||||
|
public class Aes256GcmNoPadding extends AesGcmNoPadding {
|
||||||
|
public static final String NAMESPACE = "urn:xmpp:ciphers:aes-256-gcm-nopadding:0";
|
||||||
|
|
||||||
|
public Aes256GcmNoPadding() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
|
||||||
|
NoSuchProviderException, InvalidAlgorithmParameterException {
|
||||||
|
super(256);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Aes256GcmNoPadding(byte[] keyAndIv) throws NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||||
|
NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException {
|
||||||
|
super(AesGcmNoPadding.copyOfRange(keyAndIv, 0, keyAndIv.length / 2), //Key
|
||||||
|
AesGcmNoPadding.copyOfRange(keyAndIv, keyAndIv.length / 2, keyAndIv.length / 2)); //IV
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNamespace() {
|
||||||
|
return NAMESPACE;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,105 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.ciphers;
|
||||||
|
|
||||||
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.NoSuchProviderException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.KeyGenerator;
|
||||||
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
|
||||||
|
public abstract class AesGcmNoPadding {
|
||||||
|
|
||||||
|
public static final String keyType = "AES";
|
||||||
|
public static final String cipherMode = "AES/GCM/NoPadding";
|
||||||
|
|
||||||
|
private final int length;
|
||||||
|
protected final Cipher cipher;
|
||||||
|
protected final byte[] key, iv, keyAndIv;
|
||||||
|
|
||||||
|
public AesGcmNoPadding(int length) throws NoSuchAlgorithmException, NoSuchProviderException,
|
||||||
|
NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException {
|
||||||
|
this.length = length;
|
||||||
|
|
||||||
|
KeyGenerator keyGenerator = KeyGenerator.getInstance(keyType);
|
||||||
|
keyGenerator.init(length);
|
||||||
|
key = keyGenerator.generateKey().getEncoded();
|
||||||
|
|
||||||
|
SecureRandom secureRandom = new SecureRandom();
|
||||||
|
iv = new byte[length];
|
||||||
|
secureRandom.nextBytes(iv);
|
||||||
|
|
||||||
|
keyAndIv = new byte[2 * length];
|
||||||
|
System.arraycopy(key, 0, keyAndIv, 0, length);
|
||||||
|
System.arraycopy(iv, 0, keyAndIv, length, length);
|
||||||
|
|
||||||
|
SecretKey secretKey = new SecretKeySpec(key, keyType);
|
||||||
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
|
cipher = Cipher.getInstance(cipherMode, "BC");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AesGcmNoPadding(byte[] key, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
|
||||||
|
NoSuchProviderException, InvalidAlgorithmParameterException, InvalidKeyException {
|
||||||
|
this.length = key.length;
|
||||||
|
this.key = key;
|
||||||
|
this.iv = iv;
|
||||||
|
|
||||||
|
keyAndIv = new byte[2 * length];
|
||||||
|
System.arraycopy(key, 0, keyAndIv, 0, length);
|
||||||
|
System.arraycopy(iv, 0, keyAndIv, length, length);
|
||||||
|
|
||||||
|
cipher = Cipher.getInstance(cipherMode, "BC");
|
||||||
|
SecretKeySpec keySpec = new SecretKeySpec(key, keyType);
|
||||||
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getKeyAndIv() {
|
||||||
|
return keyAndIv.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getKey() {
|
||||||
|
return key.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getIv() {
|
||||||
|
return iv.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLength() {
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Cipher getCipher() {
|
||||||
|
return cipher;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract String getNamespace();
|
||||||
|
|
||||||
|
public static byte[] copyOfRange(byte[] source, int start, int end) {
|
||||||
|
byte[] copy = new byte[end - start];
|
||||||
|
System.arraycopy(source, start, copy, 0, end - start);
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Smack's API for XEP-XXXX: Ciphers</a>.
|
||||||
|
* This contains some AES cipher utility functions.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smackx.ciphers;
|
|
@ -20,13 +20,14 @@ import java.io.File;
|
||||||
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 org.jivesoftware.smack.Manager;
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smackx.jet.internal.JetSecurity;
|
import org.jivesoftware.smackx.jet.internal.JetSecurity;
|
||||||
|
import org.jivesoftware.smackx.jft.JingleFileTransferManager;
|
||||||
import org.jivesoftware.smackx.jft.controller.OutgoingFileOfferController;
|
import org.jivesoftware.smackx.jft.controller.OutgoingFileOfferController;
|
||||||
import org.jivesoftware.smackx.jft.internal.JingleOutgoingFileOffer;
|
import org.jivesoftware.smackx.jft.internal.JingleOutgoingFileOffer;
|
||||||
|
import org.jivesoftware.smackx.jingle.JingleDescriptionManager;
|
||||||
import org.jivesoftware.smackx.jingle.JingleManager;
|
import org.jivesoftware.smackx.jingle.JingleManager;
|
||||||
import org.jivesoftware.smackx.jingle.JingleTransportManager;
|
import org.jivesoftware.smackx.jingle.JingleTransportManager;
|
||||||
import org.jivesoftware.smackx.jingle.components.JingleContent;
|
import org.jivesoftware.smackx.jingle.components.JingleContent;
|
||||||
|
@ -39,16 +40,17 @@ 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 implements JingleDescriptionManager {
|
||||||
|
|
||||||
private static final Logger LOGGER = Logger.getLogger(JetManager.class.getName());
|
|
||||||
|
|
||||||
private static final WeakHashMap<XMPPConnection, JetManager> INSTANCES = new WeakHashMap<>();
|
private static final WeakHashMap<XMPPConnection, JetManager> INSTANCES = new WeakHashMap<>();
|
||||||
|
|
||||||
private static final Map<String, JingleEncryptionMethod> encryptionMethods = new HashMap<>();
|
private static final Map<String, JingleEncryptionMethod> encryptionMethods = new HashMap<>();
|
||||||
|
|
||||||
private final JingleManager jingleManager;
|
private final JingleManager jingleManager;
|
||||||
|
|
||||||
|
static {
|
||||||
|
JingleManager.addJingleSecurityAdapter(new JetSecurityAdapter());
|
||||||
|
}
|
||||||
|
|
||||||
private JetManager(XMPPConnection connection) {
|
private JetManager(XMPPConnection connection) {
|
||||||
super(connection);
|
super(connection);
|
||||||
this.jingleManager = JingleManager.getInstanceFor(connection);
|
this.jingleManager = JingleManager.getInstanceFor(connection);
|
||||||
|
@ -65,7 +67,7 @@ public final class JetManager extends Manager {
|
||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OutgoingFileOfferController sendEncryptedFile(FullJid recipient, File file, String encryptionMethodNamespace) throws Exception {
|
public OutgoingFileOfferController sendEncryptedFile(FullJid recipient, File file, JingleEncryptionMethod method) throws Exception {
|
||||||
if (file == null || !file.exists()) {
|
if (file == null || !file.exists()) {
|
||||||
throw new IllegalArgumentException("File MUST NOT be null and MUST exist.");
|
throw new IllegalArgumentException("File MUST NOT be null and MUST exist.");
|
||||||
}
|
}
|
||||||
|
@ -81,16 +83,15 @@ public final class JetManager extends Manager {
|
||||||
JingleTransportManager transportManager = jingleManager.getBestAvailableTransportManager();
|
JingleTransportManager transportManager = jingleManager.getBestAvailableTransportManager();
|
||||||
content.setTransport(transportManager.createTransport(content));
|
content.setTransport(transportManager.createTransport(content));
|
||||||
|
|
||||||
JetSecurity security = new JetSecurity(encryptionMethodNamespace, connection());
|
JetSecurity security = new JetSecurity(method, recipient, content.getName());
|
||||||
content.setSecurity(security);
|
content.setSecurity(security);
|
||||||
session.initiate(connection());
|
session.initiate(connection());
|
||||||
|
|
||||||
return offer;
|
return offer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void registerEncryptionMethod(JingleEncryptionMethod method) {
|
||||||
public void registerEncryptionMethod(String namespace, JingleEncryptionMethod method) {
|
encryptionMethods.put(method.getNamespace(), method);
|
||||||
encryptionMethods.put(namespace, method);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unregisterEncryptionMethod(String namespace) {
|
public void unregisterEncryptionMethod(String namespace) {
|
||||||
|
@ -101,4 +102,18 @@ public final class JetManager extends Manager {
|
||||||
return encryptionMethods.get(namespace);
|
return encryptionMethods.get(namespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNamespace() {
|
||||||
|
return JetSecurity.NAMESPACE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void notifySessionInitiate(JingleSession session) {
|
||||||
|
JingleFileTransferManager.getInstanceFor(connection()).notifySessionInitiate(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void notifyContentAdd(JingleSession session, JingleContent content) {
|
||||||
|
JingleFileTransferManager.getInstanceFor(connection()).notifyContentAdd(session, content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import org.jivesoftware.smackx.jet.element.JetSecurityElement;
|
||||||
|
import org.jivesoftware.smackx.jet.internal.JetSecurity;
|
||||||
|
import org.jivesoftware.smackx.jingle.adapter.JingleSecurityAdapter;
|
||||||
|
import org.jivesoftware.smackx.jingle.element.JingleContentSecurityElement;
|
||||||
|
|
||||||
|
public class JetSecurityAdapter implements JingleSecurityAdapter<JetSecurity> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JetSecurity securityFromElement(JingleContentSecurityElement element) {
|
||||||
|
return new JetSecurity((JetSecurityElement) element);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNamespace() {
|
||||||
|
return JetSecurity.NAMESPACE;
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,6 +19,7 @@ package org.jivesoftware.smackx.jet;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
|
|
||||||
|
@ -40,4 +41,8 @@ public interface JingleEncryptionMethod {
|
||||||
super(throwable);
|
super(throwable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
XMPPConnection getConnection();
|
||||||
|
|
||||||
|
String getNamespace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,4 +57,16 @@ public class JetSecurityElement extends JingleContentSecurityElement {
|
||||||
public String getNamespace() {
|
public String getNamespace() {
|
||||||
return JetSecurity.NAMESPACE;
|
return JetSecurity.NAMESPACE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getMethodNamespace() {
|
||||||
|
return child.getNamespace();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExtensionElement getChild() {
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,18 +20,14 @@ import java.security.InvalidAlgorithmParameterException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.NoSuchProviderException;
|
import java.security.NoSuchProviderException;
|
||||||
import java.security.SecureRandom;
|
|
||||||
import javax.crypto.Cipher;
|
|
||||||
import javax.crypto.KeyGenerator;
|
|
||||||
import javax.crypto.NoSuchPaddingException;
|
import javax.crypto.NoSuchPaddingException;
|
||||||
import javax.crypto.SecretKey;
|
|
||||||
import javax.crypto.spec.IvParameterSpec;
|
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
|
||||||
|
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.SmackException;
|
||||||
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
||||||
import org.jivesoftware.smackx.jet.JetManager;
|
import org.jivesoftware.smackx.ciphers.Aes256GcmNoPadding;
|
||||||
|
import org.jivesoftware.smackx.ciphers.AesGcmNoPadding;
|
||||||
import org.jivesoftware.smackx.jet.JingleEncryptionMethod;
|
import org.jivesoftware.smackx.jet.JingleEncryptionMethod;
|
||||||
import org.jivesoftware.smackx.jet.element.JetSecurityElement;
|
import org.jivesoftware.smackx.jet.element.JetSecurityElement;
|
||||||
import org.jivesoftware.smackx.jingle.callbacks.JingleSecurityCallback;
|
import org.jivesoftware.smackx.jingle.callbacks.JingleSecurityCallback;
|
||||||
|
@ -39,6 +35,8 @@ import org.jivesoftware.smackx.jingle.components.JingleSecurity;
|
||||||
import org.jivesoftware.smackx.jingle.element.JingleContentSecurityInfoElement;
|
import org.jivesoftware.smackx.jingle.element.JingleContentSecurityInfoElement;
|
||||||
import org.jivesoftware.smackx.jingle.element.JingleElement;
|
import org.jivesoftware.smackx.jingle.element.JingleElement;
|
||||||
|
|
||||||
|
import org.jxmpp.jid.FullJid;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by vanitas on 22.07.17.
|
* Created by vanitas on 22.07.17.
|
||||||
*/
|
*/
|
||||||
|
@ -49,49 +47,35 @@ public class JetSecurity extends JingleSecurity<JetSecurityElement> {
|
||||||
|
|
||||||
private final String methodNamespace;
|
private final String methodNamespace;
|
||||||
|
|
||||||
private final Cipher cipher;
|
private AesGcmNoPadding aesKey;
|
||||||
|
|
||||||
private ExtensionElement child;
|
private ExtensionElement child;
|
||||||
|
private String name;
|
||||||
|
|
||||||
public JetSecurity(Cipher cipher, ExtensionElement child) {
|
public JetSecurity(JetSecurityElement element) {
|
||||||
super();
|
super();
|
||||||
this.cipher = cipher;
|
this.child = element.getChild();
|
||||||
this.child = child;
|
this.methodNamespace = element.getMethodNamespace();
|
||||||
this.methodNamespace = child.getNamespace();
|
this.name = element.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public JetSecurity(String methodNamespace, XMPPConnection connection)
|
public JetSecurity(JingleEncryptionMethod method, FullJid recipient, String name)
|
||||||
throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,
|
throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,
|
||||||
InvalidAlgorithmParameterException, InvalidKeyException {
|
InvalidAlgorithmParameterException, InvalidKeyException, InterruptedException,
|
||||||
this.methodNamespace = methodNamespace;
|
JingleEncryptionMethod.JingleEncryptionException, SmackException.NotConnectedException,
|
||||||
|
SmackException.NoResponseException {
|
||||||
|
|
||||||
JetManager jetManager = JetManager.getInstanceFor(connection);
|
this.methodNamespace = method.getNamespace();
|
||||||
JingleEncryptionMethod encryptionMethod = jetManager.getEncryptionMethod(methodNamespace);
|
this.aesKey = new Aes256GcmNoPadding();
|
||||||
if (encryptionMethod == null) {
|
this.child = method.encryptJingleTransfer(recipient, aesKey.getKeyAndIv());
|
||||||
throw new IllegalStateException("No encryption method with namespace " + methodNamespace + " registered.");
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Create key and cipher
|
public void decryptEncryptionKey(JingleEncryptionMethod method, FullJid sender)
|
||||||
int keyLength = 256;
|
throws InterruptedException, JingleEncryptionMethod.JingleEncryptionException, XMPPException.XMPPErrorException,
|
||||||
String keyType = "AES";
|
SmackException.NotConnectedException, SmackException.NoResponseException, NoSuchAlgorithmException,
|
||||||
String cipherMode = "AES/GCM/NoPadding";
|
InvalidAlgorithmParameterException, NoSuchProviderException, InvalidKeyException, NoSuchPaddingException {
|
||||||
|
byte[] keyAndIv = method.decryptJingleTransfer(sender, child);
|
||||||
KeyGenerator keyGenerator = KeyGenerator.getInstance(keyType);
|
aesKey = new Aes256GcmNoPadding(keyAndIv);
|
||||||
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.getInstance(cipherMode, "BC");
|
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -106,13 +90,20 @@ public class JetSecurity extends JingleSecurity<JetSecurityElement> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void decryptIncomingBytestream(BytestreamSession bytestreamSession, JingleSecurityCallback callback) {
|
public void decryptIncomingBytestream(BytestreamSession bytestreamSession, JingleSecurityCallback callback) {
|
||||||
JetSecurityBytestreamSession securityBytestreamSession = new JetSecurityBytestreamSession(bytestreamSession, cipher);
|
if (aesKey == null) {
|
||||||
|
throw new IllegalStateException("Encryption key has not yet been decrypted.");
|
||||||
|
}
|
||||||
|
JetSecurityBytestreamSession securityBytestreamSession = new JetSecurityBytestreamSession(bytestreamSession, aesKey.getCipher());
|
||||||
callback.onSecurityReady(securityBytestreamSession);
|
callback.onSecurityReady(securityBytestreamSession);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void encryptIncomingBytestream(BytestreamSession bytestreamSession, JingleSecurityCallback callback) {
|
public void encryptOutgoingBytestream(BytestreamSession bytestreamSession, JingleSecurityCallback callback) {
|
||||||
JetSecurityBytestreamSession securityBytestreamSession = new JetSecurityBytestreamSession(bytestreamSession, cipher);
|
JetSecurityBytestreamSession securityBytestreamSession = new JetSecurityBytestreamSession(bytestreamSession, aesKey.getCipher());
|
||||||
callback.onSecurityReady(securityBytestreamSession);
|
callback.onSecurityReady(securityBytestreamSession);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getMethodNamespace() {
|
||||||
|
return methodNamespace;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,8 +102,7 @@ public class JingleContent implements JingleTransportCallback, JingleSecurityCal
|
||||||
if (descriptionAdapter != null) {
|
if (descriptionAdapter != null) {
|
||||||
description = descriptionAdapter.descriptionFromElement(content.getCreator(), content.getSenders(), content.getName(), content.getDisposition(), descriptionElement);
|
description = descriptionAdapter.descriptionFromElement(content.getCreator(), content.getSenders(), content.getName(), content.getDisposition(), descriptionElement);
|
||||||
} else {
|
} else {
|
||||||
throw new AssertionError("DescriptionProvider for " + descriptionElement.getNamespace() +
|
throw new AssertionError("Unsupported Description: " + descriptionElement.getNamespace());
|
||||||
" seems to be registered, but no corresponding JingleDescriptionAdapter was found.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,8 +112,7 @@ public class JingleContent implements JingleTransportCallback, JingleSecurityCal
|
||||||
if (transportAdapter != null) {
|
if (transportAdapter != null) {
|
||||||
transport = transportAdapter.transportFromElement(transportElement);
|
transport = transportAdapter.transportFromElement(transportElement);
|
||||||
} else {
|
} else {
|
||||||
throw new AssertionError("TransportProvider for " + transportElement.getNamespace() +
|
throw new AssertionError("Unsupported Transport: " + transportElement.getNamespace());
|
||||||
" seems to be registered, but no corresponding JingleTransportAdapter was found.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,8 +122,7 @@ public class JingleContent implements JingleTransportCallback, JingleSecurityCal
|
||||||
if (securityAdapter != null) {
|
if (securityAdapter != null) {
|
||||||
security = securityAdapter.securityFromElement(securityElement);
|
security = securityAdapter.securityFromElement(securityElement);
|
||||||
} else {
|
} else {
|
||||||
throw new AssertionError("SecurityProvider for " + securityElement.getNamespace() +
|
throw new AssertionError("Unsupported Security: " + securityElement.getNamespace());
|
||||||
" seems to be registered, but no corresponding JingleSecurityAdapter was found.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -341,7 +338,7 @@ public class JingleContent implements JingleTransportCallback, JingleSecurityCal
|
||||||
getSecurity().decryptIncomingBytestream(bytestreamSession, this);
|
getSecurity().decryptIncomingBytestream(bytestreamSession, this);
|
||||||
} else if (isSending()) {
|
} else if (isSending()) {
|
||||||
LOGGER.log(Level.INFO, "Encrypt outgoing Bytestream.");
|
LOGGER.log(Level.INFO, "Encrypt outgoing Bytestream.");
|
||||||
getSecurity().encryptIncomingBytestream(bytestreamSession, this);
|
getSecurity().encryptOutgoingBytestream(bytestreamSession, this);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
description.onBytestreamReady(bytestreamSession);
|
description.onBytestreamReady(bytestreamSession);
|
||||||
|
|
|
@ -45,5 +45,5 @@ public abstract class JingleSecurity<D extends JingleContentSecurityElement> {
|
||||||
|
|
||||||
public abstract void decryptIncomingBytestream(BytestreamSession bytestreamSession, JingleSecurityCallback callback);
|
public abstract void decryptIncomingBytestream(BytestreamSession bytestreamSession, JingleSecurityCallback callback);
|
||||||
|
|
||||||
public abstract void encryptIncomingBytestream(BytestreamSession bytestreamSession, JingleSecurityCallback callbacks);
|
public abstract void encryptOutgoingBytestream(BytestreamSession bytestreamSession, JingleSecurityCallback callbacks);
|
||||||
}
|
}
|
||||||
|
|
|
@ -799,10 +799,16 @@ public final class OmemoManager extends Manager implements JingleEncryptionMetho
|
||||||
*
|
*
|
||||||
* @return the connection of this manager
|
* @return the connection of this manager
|
||||||
*/
|
*/
|
||||||
XMPPConnection getConnection() {
|
@Override
|
||||||
|
public XMPPConnection getConnection() {
|
||||||
return connection();
|
return connection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNamespace() {
|
||||||
|
return OMEMO_NAMESPACE_V_AXOLOTL;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the OMEMO service object.
|
* Return the OMEMO service object.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue