Smack/smack-experimental/src/main/java/org/jivesoftware/smackx/jet/component/JetSecurity.java

143 lines
5.9 KiB
Java
Raw Normal View History

2017-07-27 23:31: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.
*/
2017-08-04 23:05:59 +02:00
package org.jivesoftware.smackx.jet.component;
2017-07-22 01:01:50 +02:00
2017-07-30 15:40:04 +02:00
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
2017-07-31 12:08:18 +02:00
import java.util.logging.Level;
import java.util.logging.Logger;
2017-07-30 15:40:04 +02:00
import javax.crypto.NoSuchPaddingException;
2017-07-30 22:31:29 +02:00
import org.jivesoftware.smack.SmackException;
2017-07-31 12:08:18 +02:00
import org.jivesoftware.smack.XMPPConnection;
2017-07-30 22:31:29 +02:00
import org.jivesoftware.smack.XMPPException;
2017-07-22 01:01:50 +02:00
import org.jivesoftware.smack.packet.ExtensionElement;
2017-07-30 15:40:04 +02:00
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
2017-07-30 22:31:29 +02:00
import org.jivesoftware.smackx.ciphers.AesGcmNoPadding;
2017-07-31 12:08:18 +02:00
import org.jivesoftware.smackx.jet.JetManager;
2017-07-30 15:40:04 +02:00
import org.jivesoftware.smackx.jet.JingleEncryptionMethod;
2017-07-22 01:01:50 +02:00
import org.jivesoftware.smackx.jet.element.JetSecurityElement;
2017-07-30 15:40:04 +02:00
import org.jivesoftware.smackx.jingle.callbacks.JingleSecurityCallback;
2017-08-04 23:05:59 +02:00
import org.jivesoftware.smackx.jingle.component.JingleSecurity;
2017-07-22 01:01:50 +02:00
import org.jivesoftware.smackx.jingle.element.JingleContentSecurityInfoElement;
import org.jivesoftware.smackx.jingle.element.JingleElement;
2017-07-30 22:31:29 +02:00
import org.jxmpp.jid.FullJid;
2017-07-22 01:01:50 +02:00
/**
* Created by vanitas on 22.07.17.
*/
public class JetSecurity extends JingleSecurity<JetSecurityElement> {
2017-07-31 12:08:18 +02:00
private static final Logger LOGGER = Logger.getLogger(JetSecurity.class.getName());
2017-07-22 01:01:50 +02:00
public static final String NAMESPACE_V0 = "urn:xmpp:jingle:jet:0";
public static final String NAMESPACE = NAMESPACE_V0;
2017-07-30 15:40:04 +02:00
private final String methodNamespace;
2017-07-30 22:31:29 +02:00
private AesGcmNoPadding aesKey;
2017-07-22 01:01:50 +02:00
private ExtensionElement child;
2017-08-04 15:08:16 +02:00
private String cipherName;
2017-07-30 22:31:29 +02:00
private String name;
2017-07-22 01:01:50 +02:00
2017-07-30 22:31:29 +02:00
public JetSecurity(JetSecurityElement element) {
2017-07-30 15:40:04 +02:00
super();
2017-07-30 22:31:29 +02:00
this.child = element.getChild();
this.methodNamespace = element.getMethodNamespace();
this.name = element.getName();
2017-08-04 15:08:16 +02:00
this.cipherName = element.getCipherName();
2017-07-30 15:40:04 +02:00
}
2017-08-04 15:08:16 +02:00
public JetSecurity(JingleEncryptionMethod method, FullJid recipient, String name, String cipherName)
2017-07-30 15:40:04 +02:00
throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,
2017-07-30 22:31:29 +02:00
InvalidAlgorithmParameterException, InvalidKeyException, InterruptedException,
JingleEncryptionMethod.JingleEncryptionException, SmackException.NotConnectedException,
SmackException.NoResponseException {
this.methodNamespace = method.getNamespace();
2017-08-05 15:29:01 +02:00
this.aesKey = AesGcmNoPadding.createEncryptionKey(cipherName);
2017-07-30 22:31:29 +02:00
this.child = method.encryptJingleTransfer(recipient, aesKey.getKeyAndIv());
this.name = name;
2017-08-04 15:08:16 +02:00
this.cipherName = cipherName;
2017-07-30 22:31:29 +02:00
}
2017-07-30 15:40:04 +02:00
2017-07-30 22:31:29 +02:00
public void decryptEncryptionKey(JingleEncryptionMethod method, FullJid sender)
throws InterruptedException, JingleEncryptionMethod.JingleEncryptionException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, NoSuchProviderException, InvalidKeyException, NoSuchPaddingException {
byte[] keyAndIv = method.decryptJingleTransfer(sender, child);
2017-08-05 15:29:01 +02:00
aesKey = AesGcmNoPadding.createDecryptionKey(cipherName, keyAndIv);
2017-07-30 15:40:04 +02:00
}
2017-07-22 01:01:50 +02:00
@Override
public JetSecurityElement getElement() {
2017-08-09 20:46:13 +02:00
return new JetSecurityElement(name, cipherName, child);
2017-07-22 01:01:50 +02:00
}
@Override
public JingleElement handleSecurityInfo(JingleContentSecurityInfoElement element, JingleElement wrapping) {
return null;
}
2017-07-30 15:40:04 +02:00
@Override
public void decryptIncomingBytestream(BytestreamSession bytestreamSession, JingleSecurityCallback callback) {
2017-07-30 22:31:29 +02:00
if (aesKey == null) {
throw new IllegalStateException("Encryption key has not yet been decrypted.");
}
JetSecurityBytestreamSession securityBytestreamSession = new JetSecurityBytestreamSession(bytestreamSession, aesKey.getCipher());
2017-07-30 15:40:04 +02:00
callback.onSecurityReady(securityBytestreamSession);
}
@Override
2017-07-30 22:31:29 +02:00
public void encryptOutgoingBytestream(BytestreamSession bytestreamSession, JingleSecurityCallback callback) {
JetSecurityBytestreamSession securityBytestreamSession = new JetSecurityBytestreamSession(bytestreamSession, aesKey.getCipher());
2017-07-30 15:40:04 +02:00
callback.onSecurityReady(securityBytestreamSession);
}
2017-07-30 22:31:29 +02:00
2017-07-31 12:08:18 +02:00
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override
public void prepare(XMPPConnection connection, FullJid sender) {
if (getParent().getParent().isInitiator()) {
return;
}
if (aesKey != null) {
return;
}
JingleEncryptionMethod method = JetManager.getInstanceFor(connection).getEncryptionMethod(getMethodNamespace());
if (method == null) {
throw new AssertionError("No JingleEncryptionMethodManager found for " + getMethodNamespace());
}
try {
decryptEncryptionKey(method, sender);
} catch (InterruptedException | NoSuchPaddingException | InvalidKeyException | NoSuchProviderException | InvalidAlgorithmParameterException | NoSuchAlgorithmException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException | JingleEncryptionMethod.JingleEncryptionException e) {
LOGGER.log(Level.SEVERE, "Could not decrypt security key: " + e, e);
}
}
2017-07-30 22:31:29 +02:00
public String getMethodNamespace() {
return methodNamespace;
}
2017-07-22 01:01:50 +02:00
}