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-07-22 01:01:50 +02:00
|
|
|
package org.jivesoftware.smackx.jet.internal;
|
|
|
|
|
2017-07-30 15:40:04 +02:00
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.InvalidKeyException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchProviderException;
|
|
|
|
import javax.crypto.NoSuchPaddingException;
|
|
|
|
|
2017-07-30 22:31:29 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
|
|
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.Aes256GcmNoPadding;
|
|
|
|
import org.jivesoftware.smackx.ciphers.AesGcmNoPadding;
|
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-07-27 23:31:04 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.components.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> {
|
|
|
|
|
|
|
|
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-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-07-30 15:40:04 +02:00
|
|
|
}
|
|
|
|
|
2017-07-30 22:31:29 +02:00
|
|
|
public JetSecurity(JingleEncryptionMethod method, FullJid recipient, String name)
|
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();
|
|
|
|
this.aesKey = new Aes256GcmNoPadding();
|
|
|
|
this.child = method.encryptJingleTransfer(recipient, aesKey.getKeyAndIv());
|
|
|
|
this.name = name;
|
|
|
|
}
|
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);
|
|
|
|
aesKey = new Aes256GcmNoPadding(keyAndIv);
|
2017-07-30 15:40:04 +02:00
|
|
|
}
|
|
|
|
|
2017-07-22 01:01:50 +02:00
|
|
|
@Override
|
|
|
|
public JetSecurityElement getElement() {
|
|
|
|
return new JetSecurityElement(getParent().getName(), child);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
public String getMethodNamespace() {
|
|
|
|
return methodNamespace;
|
|
|
|
}
|
2017-07-22 01:01:50 +02:00
|
|
|
}
|