Browse Source

Add support for XEP-0420: Stanza Content Encryption

undefined
Paul Schaub 3 months ago
parent
commit
4c89e76e88
No known key found for this signature in database GPG Key ID: 62BEE9264BF17311
16 changed files with 1050 additions and 0 deletions
  1. +1
    -0
      documentation/extensions/index.md
  2. +163
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/ContentElement.java
  3. +50
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/FromAffixElement.java
  4. +51
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/JidAffixElement.java
  5. +54
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/PayloadElement.java
  6. +85
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/RandomPaddingAffixElement.java
  7. +72
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/TimestampAffixElement.java
  8. +50
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/ToAffixElement.java
  9. +20
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/package-info.java
  10. +22
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/package-info.java
  11. +145
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/provider/ContentElementProvider.java
  12. +20
    -0
      smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/provider/package-info.java
  13. +6
    -0
      smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers
  14. +156
    -0
      smack-experimental/src/test/java/org/jivesoftware/smackx/stanza_content_encryption/element/AffixElementsTest.java
  15. +66
    -0
      smack-experimental/src/test/java/org/jivesoftware/smackx/stanza_content_encryption/element/ContentElementTest.java
  16. +89
    -0
      smack-experimental/src/test/java/org/jivesoftware/smackx/stanza_content_encryption/provider/ContentElementProviderTest.java

+ 1
- 0
documentation/extensions/index.md View File

@@ -120,6 +120,7 @@ Experimental Smack Extensions and currently supported XEPs of smack-experimental
| [Consistent Color Generation](consistent_colors.md) | [XEP-0392](https://xmpp.org/extensions/xep-0392.html) | 0.6.0 | Generate consistent colors for identifiers like usernames to provide a consistent user experience. |
| [Message Markup](messagemarkup.md) | [XEP-0394](https://xmpp.org/extensions/xep-0394.html) | 0.1.0 | Style message bodies while keeping body and markup information separated. |
| DNS Queries over XMPP (DoX) | [XEP-0418](https://xmpp.org/extensions/xep-0418.html) | 0.1.0 | Send DNS queries and responses over XMPP. |
| Stanza Content Encryption | [XEP-0420](https://xmpp.org/extensions/xep-0420.html) | 0.2.0 | End-to-end encryption of arbitrary extension elements. Smack provides elements and providers to be used by encryption mechanisms. |

Unofficial XMPP Extensions
--------------------------


+ 163
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/ContentElement.java View File

@@ -0,0 +1,163 @@
/**
*
* Copyright 2020 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.stanza_content_encryption.element;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.jivesoftware.smack.packet.Element;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder;

import org.jxmpp.jid.Jid;

public class ContentElement implements ExtensionElement {

private static final String NAMESPACE_UNVERSIONED = "urn:xmpp:sce";
public static final String NAMESPACE_0 = NAMESPACE_UNVERSIONED + ":0";
public static final String NAMESPACE = NAMESPACE_0;
public static final String ELEMENT = "content";

private final PayloadElement payload;
private final List<Element> affixElements;

ContentElement(PayloadElement payload, List<Element> affixElements) {
this.payload = payload;
this.affixElements = Collections.unmodifiableList(affixElements);
}

public PayloadElement getPayload() {
return payload;
}

public List<Element> getAffixElements() {
return affixElements;
}

@Override
public String getNamespace() {
return NAMESPACE;
}

@Override
public String getElementName() {
return ELEMENT;
}

@Override
public CharSequence toXML(XmlEnvironment xmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(this).rightAngleBracket();
for (Element affix : affixElements) {
xml.append(affix);
}
xml.append(payload);
return xml.closeElement(this);
}

public static Builder builder() {
return new Builder();
}

public static final class Builder {

private ToAffixElement to = null;
private FromAffixElement from = null;
private TimestampAffixElement timestamp = null;
private RandomPaddingAffixElement rpad = null;

private List<Element> customAffixElements = new ArrayList<>();
private List<ExtensionElement> payloadItems = new ArrayList<>();

private Builder() {

}

public Builder setTo(Jid jid) {
return setTo(new ToAffixElement(jid));
}

public Builder setTo(ToAffixElement to) {
this.to = Objects.requireNonNull(to, "'to' affix element MUST NOT be null.");
return this;
}

public Builder setFrom(Jid jid) {
return setFrom(new FromAffixElement(jid));
}

public Builder setFrom(FromAffixElement from) {
this.from = Objects.requireNonNull(from, "'form' affix element MUST NOT be null.");
return this;
}

public Builder setTimestamp(Date date) {
return setTimestamp(new TimestampAffixElement(date));
}

public Builder setTimestamp(TimestampAffixElement timestamp) {
this.timestamp = Objects.requireNonNull(timestamp, "'time' affix element MUST NOT be null.");
return this;
}

public Builder setRandomPadding() {
this.rpad = new RandomPaddingAffixElement();
return this;
}

public Builder setRandomPadding(String padding) {
return setRandomPadding(new RandomPaddingAffixElement(padding));
}

public Builder setRandomPadding(RandomPaddingAffixElement padding) {
this.rpad = Objects.requireNonNull(padding, "'rpad' affix element MUST NOT be empty.");
return this;
}

public Builder addCustomAffixElement(Element customAffixElement) {
this.customAffixElements.add(Objects.requireNonNull(customAffixElement,
"Custom affix element MUST NOT be null."));
return this;
}

public Builder addPayloadItem(ExtensionElement payloadItem) {
this.payloadItems.add(Objects.requireNonNull(payloadItem,
"Payload item MUST NOT be null."));
return this;
}

public ContentElement build() {
List<Element> allAffixElements = collectAffixElements();
PayloadElement payloadElement = new PayloadElement(payloadItems);
return new ContentElement(payloadElement, allAffixElements);
}

private List<Element> collectAffixElements() {
List<Element> allAffixElements = new ArrayList<>(this.customAffixElements);
for (Element affixElement : Arrays.asList(to, from, timestamp, rpad)) {
if (affixElement != null) {
allAffixElements.add(affixElement);
}
}
return allAffixElements;
}
}
}

+ 50
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/FromAffixElement.java View File

@@ -0,0 +1,50 @@
/**
*
* Copyright 2020 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.stanza_content_encryption.element;

import org.jivesoftware.smack.util.Objects;

import org.jxmpp.jid.Jid;

public class FromAffixElement extends JidAffixElement {

public static final String ELEMENT = "from";

public FromAffixElement(Jid jid) {
super(jid);
}

@Override
public String getElementName() {
return ELEMENT;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof FromAffixElement)) {
return false;
}
if (this == obj) {
return true;
}

return Objects.equals(getJid(), ((FromAffixElement) obj).getJid());
}
}

+ 51
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/JidAffixElement.java View File

@@ -0,0 +1,51 @@
/**
*
* Copyright 2020 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.stanza_content_encryption.element;

import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder;

import org.jxmpp.jid.Jid;

public abstract class JidAffixElement implements NamedElement {

public static final String ATTR_JID = "jid";

private final Jid jid;

public JidAffixElement(Jid jid) {
this.jid = Objects.requireNonNull(jid, "Value of 'jid' MUST NOT be null.");
}

public Jid getJid() {
return jid;
}

@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
return new XmlStringBuilder(this)
.attribute(ATTR_JID, getJid())
.closeEmptyElement();
}

@Override
public int hashCode() {
return (getElementName() + getJid().toString()).hashCode();
}
}

+ 54
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/PayloadElement.java View File

@@ -0,0 +1,54 @@
/**
*
* Copyright 2020 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.stanza_content_encryption.element;

import java.util.Collections;
import java.util.List;

import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.XmlStringBuilder;

public class PayloadElement implements NamedElement {

public static final String ELEMENT = "payload";

private final List<ExtensionElement> payloadElements;

public PayloadElement(List<ExtensionElement> payloadElements) {
this.payloadElements = Collections.unmodifiableList(payloadElements);
}

public List<ExtensionElement> getItems() {
return payloadElements;
}

@Override
public String getElementName() {
return ELEMENT;
}

@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
XmlStringBuilder xml = new XmlStringBuilder(this).rightAngleBracket();
for (ExtensionElement element : payloadElements) {
xml.append(element);
}
return xml.closeElement(this);
}
}

+ 85
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/RandomPaddingAffixElement.java View File

@@ -0,0 +1,85 @@
/**
*
* Copyright 2020 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.stanza_content_encryption.element;

import java.security.SecureRandom;

import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.RandomUtil;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;

public class RandomPaddingAffixElement implements NamedElement {

private static final int minPaddingLength = 1;
private static final int maxPaddingLength = 200;
public static final String ELEMENT = "rpad";

private static final char[] BASE64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();

private final String padding;

public RandomPaddingAffixElement(String padding) {
this.padding = StringUtils.requireNotNullNorEmpty(padding, "Value of 'rpad' MUST NOT be null nor empty.");
}

public RandomPaddingAffixElement() {
this(StringUtils.randomString(new SecureRandom(), BASE64_CHARACTERS, randomPaddingLength()));
}

private static int randomPaddingLength() {
return minPaddingLength + RandomUtil.nextSecureRandomInt(maxPaddingLength - minPaddingLength);
}

public String getPadding() {
return padding;
}

@Override
public String getElementName() {
return ELEMENT;
}

@Override
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
return new XmlStringBuilder(this).rightAngleBracket()
.append(getPadding())
.closeElement(this);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof RandomPaddingAffixElement)) {
return false;
}
if (this == obj) {
return true;
}

return Objects.equals(getPadding(), ((RandomPaddingAffixElement) obj).getPadding());
}

@Override
public int hashCode() {
return getPadding().hashCode();
}
}

+ 72
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/TimestampAffixElement.java View File

@@ -0,0 +1,72 @@
/**
*
* Copyright 2020 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.stanza_content_encryption.element;

import java.util.Date;

import org.jivesoftware.smack.packet.NamedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder;

public class TimestampAffixElement implements NamedElement {

public static final String ELEMENT = "time";
public static final String ATTR_STAMP = "stamp";

private final Date timestamp;

public TimestampAffixElement(Date timestamp) {
this.timestamp = Objects.requireNonNull(timestamp, "Date must not be null.");
}

public Date getTimestamp() {
return timestamp;
}

@Override
public String getElementName() {
return ELEMENT;
}

@Override
public CharSequence toXML(XmlEnvironment xmlEnvironment) {
return new XmlStringBuilder(this)
.attribute(ATTR_STAMP, getTimestamp())
.closeEmptyElement();
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof TimestampAffixElement)) {
return false;
}
if (this == obj) {
return true;
}

return Objects.equals(getTimestamp(), ((TimestampAffixElement) obj).getTimestamp());
}

@Override
public int hashCode() {
return timestamp.hashCode();
}
}

+ 50
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/ToAffixElement.java View File

@@ -0,0 +1,50 @@
/**
*
* Copyright 2020 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.stanza_content_encryption.element;

import org.jivesoftware.smack.util.Objects;

import org.jxmpp.jid.Jid;

public class ToAffixElement extends JidAffixElement {

public static final String ELEMENT = "to";

public ToAffixElement(Jid jid) {
super(jid);
}

@Override
public String getElementName() {
return ELEMENT;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof ToAffixElement)) {
return false;
}
if (this == obj) {
return true;
}

return Objects.equals(getJid(), ((ToAffixElement) obj).getJid());
}
}

+ 20
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/element/package-info.java View File

@@ -0,0 +1,20 @@
/**
*
* Copyright 2018 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-0420: Stanza Content Encryption: Element classes.
*/
package org.jivesoftware.smackx.stanza_content_encryption.element;

+ 22
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/package-info.java View File

@@ -0,0 +1,22 @@
/**
*
* Copyright 2018 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-0420: Stanza Content Encryption.
*
* @see <a href="https://xmpp.org/extensions/xep-0420.html">XEP-0420: Stanza Content Encryption</a>
*/
package org.jivesoftware.smackx.stanza_content_encryption;

+ 145
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/provider/ContentElementProvider.java View File

@@ -0,0 +1,145 @@
/**
*
* Copyright 2020 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.stanza_content_encryption.provider;

import java.io.IOException;
import java.util.Date;

import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.parsing.StandardExtensionElementProvider;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.stanza_content_encryption.element.ContentElement;
import org.jivesoftware.smackx.stanza_content_encryption.element.FromAffixElement;
import org.jivesoftware.smackx.stanza_content_encryption.element.PayloadElement;
import org.jivesoftware.smackx.stanza_content_encryption.element.RandomPaddingAffixElement;
import org.jivesoftware.smackx.stanza_content_encryption.element.TimestampAffixElement;
import org.jivesoftware.smackx.stanza_content_encryption.element.ToAffixElement;

import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;

public class ContentElementProvider extends ExtensionElementProvider<ContentElement> {

@Override
public ContentElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException {
ContentElement.Builder builder = ContentElement.builder();

boolean inContentElement = true;
while (inContentElement) {

XmlPullParser.Event tag = parser.next();
if (tag == XmlPullParser.Event.START_ELEMENT) {
String name = parser.getName();
switch (name) {
case ToAffixElement.ELEMENT:
parseToAffix(parser, builder);
break;

case FromAffixElement.ELEMENT:
parseFromAffix(parser, builder);
break;

case TimestampAffixElement.ELEMENT:
parseTimestampAffix(parser, builder);
break;

case RandomPaddingAffixElement.ELEMENT:
parseRPadAffix(parser, builder);
break;

case PayloadElement.ELEMENT:
parsePayload(parser, builder);
break;

default:
parseCustomAffix(parser, builder);
break;
}
} else if (tag == XmlPullParser.Event.END_ELEMENT) {
String name = parser.getName();
if (name.equals(ContentElement.ELEMENT) && ContentElement.NAMESPACE.equals(parser.getNamespace())) {
inContentElement = false;
}
}
}
return builder.build();
}

private static void parseCustomAffix(XmlPullParser parser, ContentElement.Builder builder) throws XmlPullParserException, IOException, SmackParsingException {
String name = parser.getName();
String namespace = parser.getNamespace();

ExtensionElementProvider<?> provider = getProvider(name, namespace);
builder.addCustomAffixElement(provider.parse(parser));
}

private static void parsePayload(XmlPullParser parser, ContentElement.Builder builder) throws IOException, XmlPullParserException, SmackParsingException {
boolean inPayload = true;
while (inPayload) {
XmlPullParser.Event tag = parser.next();

if (tag == XmlPullParser.Event.START_ELEMENT) {
String name = parser.getName();
String namespace = parser.getNamespace();
ExtensionElementProvider<?> provider = getProvider(name, namespace);
builder.addPayloadItem(provider.parse(parser));
}

if (tag == XmlPullParser.Event.END_ELEMENT) {
String name = parser.getName();
String namespace = parser.getNamespace();
if (name.equals(PayloadElement.ELEMENT) && namespace.equals(ContentElement.NAMESPACE)) {
inPayload = false;
}
}
}
}

private static void parseRPadAffix(XmlPullParser parser, ContentElement.Builder builder) throws IOException, XmlPullParserException {
builder.setRandomPadding(parser.nextText());
}

private static void parseTimestampAffix(XmlPullParser parser, ContentElement.Builder builder) throws SmackParsingException.SmackTextParseException {
Date timestamp = ParserUtils.getDateFromXep82String(
parser.getAttributeValue("", TimestampAffixElement.ATTR_STAMP));
builder.setTimestamp(timestamp);
}

private static void parseFromAffix(XmlPullParser parser, ContentElement.Builder builder) throws XmppStringprepException {
String jidString = parser.getAttributeValue("", FromAffixElement.ATTR_JID);
builder.setFrom(JidCreate.from(jidString));
}

private static void parseToAffix(XmlPullParser parser, ContentElement.Builder builder) throws XmppStringprepException {
String jidString = parser.getAttributeValue("", ToAffixElement.ATTR_JID);
builder.setTo(JidCreate.from(jidString));
}

private static ExtensionElementProvider<?> getProvider(String elementName, String namespace) {
ExtensionElementProvider<?> provider = ProviderManager.getExtensionProvider(elementName, namespace);
if (provider == null) {
provider = new StandardExtensionElementProvider();
}
return provider;
}
}

+ 20
- 0
smack-experimental/src/main/java/org/jivesoftware/smackx/stanza_content_encryption/provider/package-info.java View File

@@ -0,0 +1,20 @@
/**
*
* Copyright 2018 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-0420: Stanza Content Encryption: Provider classes.
*/
package org.jivesoftware.smackx.stanza_content_encryption.provider;

+ 6
- 0
smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers View File

@@ -292,6 +292,12 @@
<className>org.jivesoftware.smackx.dox.provider.DnsIqProvider</className>
</iqProvider>

<!-- XEP-0420: Stanza Content Encryption (SCE) -->
<extensionProvider>
<elementName>content</elementName>
<namespace>urn:xmpp:sce:0</namespace>
<className>org.jivesoftware.smackx.stanza_content_encryption.provider.ContentElementProvider</className>
</extensionProvider>

<!-- XEP-xxxx: Multi-User Chat Light -->
<iqProvider>


+ 156
- 0
smack-experimental/src/test/java/org/jivesoftware/smackx/stanza_content_encryption/element/AffixElementsTest.java View File

@@ -0,0 +1,156 @@
/**
*
* Copyright 2020 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.stanza_content_encryption.element;

import static org.jivesoftware.smack.test.util.XmlUnitUtils.assertXmlSimilar;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.text.ParseException;
import java.util.Date;

import org.junit.jupiter.api.Test;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.util.XmppDateTime;

public class AffixElementsTest {

public static final EntityBareJid JID_HOUSTON = JidCreate.entityBareFromOrThrowUnchecked("missioncontrol@houston.nasa.gov");
public static final EntityBareJid JID_OPPORTUNITY = JidCreate.entityBareFromOrThrowUnchecked("opportunity@mars.planet");

/**
* Test serialization of 'to' affix element.
*
* @see <a href="https://xmpp.org/extensions/xep-0420.html#example-1">XEP-420 Example 1</a>
*/
@Test
public void testToAffixElement() {
ToAffixElement to = new ToAffixElement(JID_HOUSTON);
String expectedXml = "<to jid='missioncontrol@houston.nasa.gov'/>";

assertXmlSimilar(expectedXml, to.toXML());
assertEquals(JID_HOUSTON, to.getJid());
}

@Test
public void testToAffixElementEquals() {
ToAffixElement to1 = new ToAffixElement(JID_HOUSTON);
ToAffixElement to2 = new ToAffixElement(JID_HOUSTON);

assertEquals(to1, to2);
assertEquals(to1, to1);
assertEquals(to1.hashCode(), to2.hashCode());
assertFalse(to1.equals(null));
}

@Test
public void toElementNullArgThrows() {
assertThrows(IllegalArgumentException.class, () -> new ToAffixElement(null));
}

/**
* Test serialization of 'from' affix element.
*
* @see <a href="https://xmpp.org/extensions/xep-0420.html#example-1">XEP-420 Example 1</a>
*/
@Test
public void testFromAffixElement() {
FromAffixElement from = new FromAffixElement(JID_OPPORTUNITY);
String expectedXml = "<from jid='opportunity@mars.planet'/>";

assertXmlSimilar(expectedXml, from.toXML());
assertEquals(JID_OPPORTUNITY, from.getJid());
}

@Test
public void testFromAffixElementEquals() {
FromAffixElement from1 = new FromAffixElement(JID_HOUSTON);
FromAffixElement from2 = new FromAffixElement(JID_HOUSTON);

assertEquals(from1, from2);
assertEquals(from1, from1);
assertEquals(from1.hashCode(), from2.hashCode());
assertFalse(from1.equals(null));
}

@Test
public void fromElementNullArgThrows() {
assertThrows(IllegalArgumentException.class, () -> new FromAffixElement(null));
}

@Test
public void testTimestampAffixElement() throws ParseException {
Date date = XmppDateTime.parseDate("2004-01-25T05:05:00.000+00:00");
TimestampAffixElement timestamp = new TimestampAffixElement(date);
String expectedXml = "<time stamp='2004-01-25T05:05:00.000+00:00'/>";

assertXmlSimilar(expectedXml, timestamp.toXML());
assertEquals(date, timestamp.getTimestamp());
}

@Test
public void timestampElementNullArgThrows() {
assertThrows(IllegalArgumentException.class, () -> new TimestampAffixElement(null));
}

@Test
public void testTimestampElementEquals() throws ParseException {
TimestampAffixElement t1 = new TimestampAffixElement(XmppDateTime.parseDate("2004-01-25T05:05:00.000+00:00"));
TimestampAffixElement t2 = new TimestampAffixElement(t1.getTimestamp());

assertEquals(t1, t2);
assertEquals(t1, t1);
assertEquals(t1.hashCode(), t2.hashCode());
assertFalse(t1.equals(null));
}

@Test
public void testRandomPaddingElement() {
RandomPaddingAffixElement rpad = new RandomPaddingAffixElement();

assertNotNull(rpad.getPadding());
assertTrue(rpad.getPadding().length() < 200);
}

@Test
public void testRandomPaddingEquals() {
RandomPaddingAffixElement rpad1 = new RandomPaddingAffixElement();
RandomPaddingAffixElement rpad2 = new RandomPaddingAffixElement(rpad1.getPadding());

assertEquals(rpad1, rpad2);
assertEquals(rpad1, rpad1);
assertEquals(rpad1.hashCode(), rpad2.hashCode());
assertFalse(rpad1.equals(null));
}

@Test
public void testRandomPaddingSerialization() {
RandomPaddingAffixElement rpad = new RandomPaddingAffixElement();
String expectedXml = "<rpad>" + rpad.getPadding() + "</rpad>";

assertXmlSimilar(expectedXml, rpad.toXML());
}

@Test
public void rpadElementNullArgThrows() {
assertThrows(IllegalArgumentException.class, () -> new RandomPaddingAffixElement(null));
}
}

+ 66
- 0
smack-experimental/src/test/java/org/jivesoftware/smackx/stanza_content_encryption/element/ContentElementTest.java View File

@@ -0,0 +1,66 @@
/**
*
* Copyright 2020 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.stanza_content_encryption.element;

import static org.jivesoftware.smack.test.util.XmlUnitUtils.assertXmlSimilar;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.text.ParseException;
import java.util.Collections;

import org.jivesoftware.smack.packet.Message;

import org.junit.jupiter.api.Test;
import org.jxmpp.util.XmppDateTime;

public class ContentElementTest {

@Test
public void testContentElement() throws ParseException {
Message.Body body = new Message.Body("en", "My battery is low and it’s getting dark"); // :'(

ContentElement contentElement = ContentElement.builder()
.addPayloadItem(body)
.setFrom(AffixElementsTest.JID_OPPORTUNITY)
.setTo(AffixElementsTest.JID_HOUSTON)
.setTimestamp(XmppDateTime.parseXEP0082Date("2018-06-10T00:00:00.000+00:00"))
.setRandomPadding("RANDOMPADDING")
.build();

String expectedXml = "" +
"<content xmlns='urn:xmpp:sce:0'>" +
" <to jid='missioncontrol@houston.nasa.gov'/>" +
" <from jid='opportunity@mars.planet'/>" +
" <time stamp='2018-06-10T00:00:00.000+00:00'/>" +
" <rpad>RANDOMPADDING</rpad>" +
" <payload>" +
" <body xmlns='jabber:client' xml:lang='en'>My battery is low and it’s getting dark</body>" +
" </payload>" +
"</content>";

assertXmlSimilar(expectedXml, contentElement.toXML());
assertEquals(Collections.singletonList(body), contentElement.getPayload().getItems());

assertEquals(4, contentElement.getAffixElements().size());
assertTrue(contentElement.getAffixElements().contains(new ToAffixElement(AffixElementsTest.JID_HOUSTON)));
assertTrue(contentElement.getAffixElements().contains(new FromAffixElement(AffixElementsTest.JID_OPPORTUNITY)));
assertTrue(contentElement.getAffixElements().contains(
new TimestampAffixElement(XmppDateTime.parseXEP0082Date("2018-06-10T00:00:00.000+00:00"))));
assertTrue(contentElement.getAffixElements().contains(new RandomPaddingAffixElement("RANDOMPADDING")));
}
}

+ 89
- 0
smack-experimental/src/test/java/org/jivesoftware/smackx/stanza_content_encryption/provider/ContentElementProviderTest.java View File

@@ -0,0 +1,89 @@
/**
*
* Copyright 2020 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.stanza_content_encryption.provider;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.StandardExtensionElement;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.stanza_content_encryption.element.ContentElement;
import org.jivesoftware.smackx.stanza_content_encryption.element.FromAffixElement;
import org.jivesoftware.smackx.stanza_content_encryption.element.RandomPaddingAffixElement;
import org.jivesoftware.smackx.stanza_content_encryption.element.TimestampAffixElement;
import org.jivesoftware.smackx.stanza_content_encryption.element.ToAffixElement;

import org.junit.jupiter.api.Test;
import org.jxmpp.jid.impl.JidCreate;

public class ContentElementProviderTest {

@Test
public void testParsing() throws XmlPullParserException, IOException, SmackParsingException {
String xml = "" +
"<content xmlns='urn:xmpp:sce:0'>\n" +
" <payload>\n" +
" <body xmlns='jabber:client'>Have you seen that new movie?</body>\n" +
" <x xmlns='jabber:x:oob'>\n" +
" <url>https://en.wikipedia.org/wiki/Fight_Club#Plot</url>\n" +
" </x>\n" +
" </payload>\n" +
" <affix xmlns='urn:xmpp:foo:sce' custom='foo'/>\n" +
" <from jid='ladymacbeth@shakespear.lit/castle'/>\n" +
" <to jid='doctor@shakespeare.lit/pda'/>\n" +
" <time stamp='1993-10-12T03:13:10.000+00:00'/>\n" +
" <rpad>A98D7KJF1ASDVG232sdff341</rpad>\n" +
"</content>";

ContentElementProvider provider = new ContentElementProvider();
ContentElement contentElement = provider.parse(TestUtils.getParser(xml));

assertNotNull(contentElement);

assertEquals(5, contentElement.getAffixElements().size());
assertTrue(contentElement.getAffixElements().contains(
new FromAffixElement(JidCreate.from("ladymacbeth@shakespear.lit/castle"))));
assertTrue(contentElement.getAffixElements().contains(
new ToAffixElement(JidCreate.from("doctor@shakespeare.lit/pda"))));
assertTrue(contentElement.getAffixElements().contains(
new TimestampAffixElement(ParserUtils.getDateFromXep82String("1993-10-12T03:13:10.000+00:00"))));
assertTrue(contentElement.getAffixElements().contains(
new RandomPaddingAffixElement("A98D7KJF1ASDVG232sdff341")));
StandardExtensionElement customAffix = (StandardExtensionElement) contentElement.getAffixElements().get(0);
assertEquals("affix", customAffix.getElementName());
assertEquals("urn:xmpp:foo:sce", customAffix.getNamespace());
assertEquals("foo", customAffix.getAttributeValue("custom"));

assertEquals(2, contentElement.getPayload().getItems().size());

assertTrue(contentElement.getPayload().getItems().get(0) instanceof Message.Body);
Message.Body body = (Message.Body) contentElement.getPayload().getItems().get(0);
assertEquals("Have you seen that new movie?", body.getMessage());

StandardExtensionElement oob = (StandardExtensionElement) contentElement.getPayload().getItems().get(1);
assertEquals("x", oob.getElementName());
assertEquals("jabber:x:oob", oob.getNamespace());
assertEquals("https://en.wikipedia.org/wiki/Fight_Club#Plot", oob.getFirstElement("url").getText());
}
}

Loading…
Cancel
Save