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

143 lines
6.0 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-08-16 16:42:11 +02:00
import org.jivesoftware.smackx.jet.JingleEnvelopeManager;
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-08-16 16:42:11 +02:00
private final String envelopeNamespace;
2017-07-30 15:40:04 +02:00
2017-07-30 22:31:29 +02:00
private AesGcmNoPadding aesKey;
2017-08-16 15:36:49 +02:00
private final ExtensionElement child;
private final String cipherName;
private final String contentName;
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();
2017-08-16 16:42:11 +02:00
this.envelopeNamespace = element.getEnvelopeNamespace();
2017-08-16 15:36:49 +02:00
this.contentName = element.getContentName();
2017-08-04 15:08:16 +02:00
this.cipherName = element.getCipherName();
2017-07-30 15:40:04 +02:00
}
2017-08-16 16:42:11 +02:00
public JetSecurity(JingleEnvelopeManager envelopeManager, FullJid recipient, String contentName, 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,
2017-08-16 16:42:11 +02:00
JingleEnvelopeManager.JingleEncryptionException, SmackException.NotConnectedException,
2017-07-30 22:31:29 +02:00
SmackException.NoResponseException {
2017-08-16 16:42:11 +02:00
this.envelopeNamespace = envelopeManager.getJingleEnvelopeNamespace();
2017-08-05 15:29:01 +02:00
this.aesKey = AesGcmNoPadding.createEncryptionKey(cipherName);
2017-08-16 16:42:11 +02:00
this.child = envelopeManager.encryptJingleTransfer(recipient, aesKey.getKeyAndIv());
2017-08-16 15:36:49 +02:00
this.contentName = contentName;
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-08-16 16:42:11 +02:00
private void decryptEncryptionKey(JingleEnvelopeManager method, FullJid sender)
throws InterruptedException, JingleEnvelopeManager.JingleEncryptionException, XMPPException.XMPPErrorException,
2017-07-30 22:31:29 +02:00
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-16 15:36:49 +02:00
return new JetSecurityElement(contentName, 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;
}
2017-08-16 16:42:11 +02:00
JingleEnvelopeManager method = JetManager.getInstanceFor(connection).getEnvelopeManager(getEnvelopeNamespace());
2017-07-31 12:08:18 +02:00
if (method == null) {
2017-08-16 16:42:11 +02:00
throw new AssertionError("No JingleEncryptionMethodManager found for " + getEnvelopeNamespace());
2017-07-31 12:08:18 +02:00
}
try {
decryptEncryptionKey(method, sender);
2017-08-16 16:42:11 +02:00
} catch (InterruptedException | NoSuchPaddingException | InvalidKeyException | NoSuchProviderException | InvalidAlgorithmParameterException | NoSuchAlgorithmException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException | JingleEnvelopeManager.JingleEncryptionException e) {
2017-07-31 12:08:18 +02:00
LOGGER.log(Level.SEVERE, "Could not decrypt security key: " + e, e);
}
}
2017-08-16 16:42:11 +02:00
public String getEnvelopeNamespace() {
return envelopeNamespace;
2017-07-30 22:31:29 +02:00
}
2017-07-22 01:01:50 +02:00
}