mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-08 20:11:08 +01:00
Add support for XEP-0420: Stanza Content Encryption
This commit is contained in:
parent
00acdfcb9e
commit
78f37a909e
19 changed files with 1220 additions and 0 deletions
|
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
*
|
||||
* 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.Element;
|
||||
|
||||
/**
|
||||
* Interface that marks elements that may be used as affix elements inside a {@link ContentElement}.
|
||||
*
|
||||
* @see <a href="https://xmpp.org/extensions/xep-0420.html#affix_elements">
|
||||
* XEP-0420: Stanza Content Encryption - §4. Affix Elements</a>
|
||||
*/
|
||||
public interface AffixElement extends Element {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
*
|
||||
* 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.ExtensionElement;
|
||||
|
||||
/**
|
||||
* Affix element that is identified by element name and namespace.
|
||||
* You should extend this interface with your custom affix extension elements
|
||||
* and also provide a {@link org.jivesoftware.smackx.stanza_content_encryption.provider.AffixExtensionElementProvider}
|
||||
* for them.
|
||||
*
|
||||
* @see <a href="https://xmpp.org/extensions/xep-0420.html#affix_elements">
|
||||
* XEP-0420: Stanza Content Encryption - §4. Affix Elements</a>
|
||||
*/
|
||||
public interface AffixExtensionElement extends ExtensionElement, AffixElement {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,288 @@
|
|||
/**
|
||||
*
|
||||
* 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.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
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.jivesoftware.smackx.address.packet.MultipleAddresses;
|
||||
import org.jivesoftware.smackx.hints.element.MessageProcessingHint;
|
||||
import org.jivesoftware.smackx.sid.element.StanzaIdElement;
|
||||
|
||||
import org.jxmpp.jid.Jid;
|
||||
|
||||
/**
|
||||
* Extension element that holds the payload element, as well as a list of affix elements.
|
||||
* In SCE, the XML representation of this element is what will be encrypted using the encryption mechanism of choice.
|
||||
*/
|
||||
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";
|
||||
public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
|
||||
|
||||
private final PayloadElement payload;
|
||||
private final List<AffixElement> affixElements;
|
||||
|
||||
ContentElement(PayloadElement payload, List<AffixElement> affixElements) {
|
||||
this.payload = payload;
|
||||
this.affixElements = Collections.unmodifiableList(affixElements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link PayloadElement} which holds the sensitive payload extensions.
|
||||
*
|
||||
* @return payload element
|
||||
*/
|
||||
public PayloadElement getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of affix elements.
|
||||
* Those are elements that need to be verified upon reception by the encryption mechanisms implementation.
|
||||
*
|
||||
* @see <a href="https://xmpp.org/extensions/xep-0420.html#affix_elements">
|
||||
* XEP-0420: Stanza Content Encryption - §4. Affix Elements</a>
|
||||
*
|
||||
* @return list of affix elements
|
||||
*/
|
||||
public List<AffixElement> getAffixElements() {
|
||||
return affixElements;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this).rightAngleBracket();
|
||||
xml.append(affixElements);
|
||||
xml.append(payload);
|
||||
return xml.closeElement(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QName getQName() {
|
||||
return QNAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a {@link Builder} that can be used to build the {@link ContentElement}.
|
||||
* @return builder
|
||||
*/
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
private static final Set<String> BLACKLISTED_NAMESPACES = Collections.singleton(MessageProcessingHint.NAMESPACE);
|
||||
private static final Set<QName> BLACKLISTED_QNAMES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
|
||||
StanzaIdElement.QNAME,
|
||||
MultipleAddresses.QNAME
|
||||
)));
|
||||
|
||||
private FromAffixElement from = null;
|
||||
private TimestampAffixElement timestamp = null;
|
||||
private RandomPaddingAffixElement rpad = null;
|
||||
|
||||
private final List<AffixElement> otherAffixElements = new ArrayList<>();
|
||||
private final List<ExtensionElement> payloadItems = new ArrayList<>();
|
||||
|
||||
private Builder() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an affix element of type 'to' which addresses one recipient.
|
||||
* The jid in the 'to' element SHOULD be a bare jid.
|
||||
*
|
||||
* @param jid jid
|
||||
* @return builder
|
||||
*/
|
||||
public Builder addTo(Jid jid) {
|
||||
return addTo(new ToAffixElement(jid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an affix element of type 'to' which addresses one recipient.
|
||||
*
|
||||
* @param to affix element
|
||||
* @return builder
|
||||
*/
|
||||
public Builder addTo(ToAffixElement to) {
|
||||
this.otherAffixElements.add(Objects.requireNonNull(to, "'to' affix element MUST NOT be null."));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the senders jid as a 'from' affix element.
|
||||
*
|
||||
* @param jid jid of the sender
|
||||
* @return builder
|
||||
*/
|
||||
public Builder setFrom(Jid jid) {
|
||||
return setFrom(new FromAffixElement(jid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the senders jid as a 'from' affix element.
|
||||
*
|
||||
* @param from affix element
|
||||
* @return builder
|
||||
*/
|
||||
public Builder setFrom(FromAffixElement from) {
|
||||
this.from = Objects.requireNonNull(from, "'form' affix element MUST NOT be null.");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the given date as a 'time' affix element.
|
||||
*
|
||||
* @param date timestamp as date
|
||||
* @return builder
|
||||
*/
|
||||
public Builder setTimestamp(Date date) {
|
||||
return setTimestamp(new TimestampAffixElement(date));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the timestamp of the message as a 'time' affix element.
|
||||
*
|
||||
* @param timestamp timestamp affix element
|
||||
* @return builder
|
||||
*/
|
||||
public Builder setTimestamp(TimestampAffixElement timestamp) {
|
||||
this.timestamp = Objects.requireNonNull(timestamp, "'time' affix element MUST NOT be null.");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set some random length random content padding.
|
||||
*
|
||||
* @return builder
|
||||
*/
|
||||
public Builder setRandomPadding() {
|
||||
this.rpad = new RandomPaddingAffixElement();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the given string as padding.
|
||||
* The padding should be of length between 1 and 200 characters.
|
||||
*
|
||||
* @param padding padding string
|
||||
* @return builder
|
||||
*/
|
||||
public Builder setRandomPadding(String padding) {
|
||||
return setRandomPadding(new RandomPaddingAffixElement(padding));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a padding affix element.
|
||||
*
|
||||
* @param padding affix element
|
||||
* @return builder
|
||||
*/
|
||||
public Builder setRandomPadding(RandomPaddingAffixElement padding) {
|
||||
this.rpad = Objects.requireNonNull(padding, "'rpad' affix element MUST NOT be empty.");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an additional, SCE profile specific affix element.
|
||||
*
|
||||
* @param customAffixElement additional affix element
|
||||
* @return builder
|
||||
*/
|
||||
public Builder addFurtherAffixElement(AffixElement customAffixElement) {
|
||||
this.otherAffixElements.add(Objects.requireNonNull(customAffixElement,
|
||||
"Custom affix element MUST NOT be null."));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a payload item as child element of the payload element.
|
||||
* There are some items that are not allowed as payload.
|
||||
* Adding those will throw an exception.
|
||||
*
|
||||
* @see <a href="https://xmpp.org/extensions/xep-0420.html#server-processed">
|
||||
* XEP-0420: Stanza Content Encryption - §9. Server-processed Elements</a>
|
||||
*
|
||||
* @param payloadItem extension element
|
||||
* @return builder
|
||||
* @throws IllegalArgumentException in case an extension element from the blacklist is added.
|
||||
*/
|
||||
public Builder addPayloadItem(ExtensionElement payloadItem) {
|
||||
Objects.requireNonNull(payloadItem, "Payload item MUST NOT be null.");
|
||||
this.payloadItems.add(checkForIllegalPayloadsAndPossiblyThrow(payloadItem));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a content element from this builder.
|
||||
*
|
||||
* @return content element
|
||||
*/
|
||||
public ContentElement build() {
|
||||
List<AffixElement> allAffixElements = collectAffixElements();
|
||||
PayloadElement payloadElement = new PayloadElement(payloadItems);
|
||||
return new ContentElement(payloadElement, allAffixElements);
|
||||
}
|
||||
|
||||
private static ExtensionElement checkForIllegalPayloadsAndPossiblyThrow(ExtensionElement payloadItem) {
|
||||
QName qName = payloadItem.getQName();
|
||||
if (BLACKLISTED_QNAMES.contains(qName)) {
|
||||
throw new IllegalArgumentException("Element identified by " + qName +
|
||||
" is not allowed as payload item. See https://xmpp.org/extensions/xep-0420.html#server-processed");
|
||||
}
|
||||
|
||||
String namespace = payloadItem.getNamespace();
|
||||
if (BLACKLISTED_NAMESPACES.contains(namespace)) {
|
||||
throw new IllegalArgumentException("Elements of namespace '" + namespace +
|
||||
"' are not allowed as payload items. See https://xmpp.org/extensions/xep-0420.html#server-processed");
|
||||
}
|
||||
|
||||
return payloadItem;
|
||||
}
|
||||
|
||||
private List<AffixElement> collectAffixElements() {
|
||||
List<AffixElement> allAffixElements = new ArrayList<>(Arrays.asList(rpad, from, timestamp));
|
||||
allAffixElements.addAll(otherAffixElements);
|
||||
return allAffixElements;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
*
|
||||
* 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.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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
/**
|
||||
*
|
||||
* 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.EqualsUtil;
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
import org.jxmpp.jid.Jid;
|
||||
|
||||
public abstract class JidAffixElement implements NamedElement, AffixElement {
|
||||
|
||||
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 final boolean equals(Object obj) {
|
||||
return EqualsUtil.equals(this, obj, (e, o) -> e.append(getJid(), o.getJid()).append(getElementName(), o.getElementName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return (getElementName() + getJid().toString()).hashCode();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
*
|
||||
* 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();
|
||||
xml.append(payloadElements);
|
||||
return xml.closeElement(this);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
/**
|
||||
*
|
||||
* 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.EqualsUtil;
|
||||
import org.jivesoftware.smack.util.RandomUtil;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
public class RandomPaddingAffixElement implements NamedElement, AffixElement {
|
||||
|
||||
private static final int minPaddingLength = 1;
|
||||
private static final int maxPaddingLength = 200;
|
||||
public static final String ELEMENT = "rpad";
|
||||
|
||||
private final String padding;
|
||||
|
||||
public RandomPaddingAffixElement(String padding) {
|
||||
this.padding = StringUtils.escapeForXmlText(
|
||||
StringUtils.requireNotNullNorEmpty(padding, "Value of 'rpad' MUST NOT be null nor empty."))
|
||||
.toString();
|
||||
}
|
||||
|
||||
public RandomPaddingAffixElement() {
|
||||
this(StringUtils.randomString(randomPaddingLength(), new SecureRandom()));
|
||||
}
|
||||
|
||||
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) {
|
||||
return EqualsUtil.equals(this, obj, (e, o) -> e.append(getPadding(), o.getPadding()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getPadding().hashCode();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
/**
|
||||
*
|
||||
* 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.EqualsUtil;
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
public class TimestampAffixElement implements NamedElement, AffixElement {
|
||||
|
||||
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) {
|
||||
return EqualsUtil.equals(this, obj, (e, o) -> e.append(getTimestamp(), o.getTimestamp()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return timestamp.hashCode();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/**
|
||||
*
|
||||
* 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.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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
*
|
||||
* 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 org.jivesoftware.smack.provider.ExtensionElementProvider;
|
||||
import org.jivesoftware.smackx.stanza_content_encryption.element.AffixExtensionElement;
|
||||
|
||||
/**
|
||||
* Abstract class that needs to be extended by provider classes that parse out affix extension elements.
|
||||
*
|
||||
* @param <AE> affix extension element.
|
||||
*/
|
||||
public abstract class AffixExtensionElementProvider<AE extends AffixExtensionElement> extends ExtensionElementProvider<AE> {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,137 @@
|
|||
/**
|
||||
*
|
||||
* 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.ExtensionElement;
|
||||
import org.jivesoftware.smack.packet.XmlEnvironment;
|
||||
import org.jivesoftware.smack.parsing.SmackParsingException;
|
||||
import org.jivesoftware.smack.provider.ExtensionElementProvider;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
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.AffixElement;
|
||||
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();
|
||||
|
||||
while (true) {
|
||||
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, xmlEnvironment, builder);
|
||||
break;
|
||||
|
||||
default:
|
||||
parseCustomAffix(parser, xmlEnvironment, builder);
|
||||
break;
|
||||
}
|
||||
} else if (tag == XmlPullParser.Event.END_ELEMENT) {
|
||||
if (parser.getDepth() == initialDepth) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private static void parseCustomAffix(XmlPullParser parser, XmlEnvironment outerXmlEnvironment, ContentElement.Builder builder)
|
||||
throws XmlPullParserException, IOException, SmackParsingException {
|
||||
String name = parser.getName();
|
||||
String namespace = parser.getNamespace();
|
||||
|
||||
AffixElement element = (AffixElement) PacketParserUtils.parseExtensionElement(name, namespace, parser, outerXmlEnvironment);
|
||||
builder.addFurtherAffixElement(element);
|
||||
}
|
||||
|
||||
private static void parsePayload(XmlPullParser parser, XmlEnvironment outerXmlEnvironment, ContentElement.Builder builder)
|
||||
throws IOException, XmlPullParserException, SmackParsingException {
|
||||
final int initialDepth = parser.getDepth();
|
||||
while (true) {
|
||||
XmlPullParser.Event tag = parser.next();
|
||||
|
||||
if (tag == XmlPullParser.Event.START_ELEMENT) {
|
||||
String name = parser.getName();
|
||||
String namespace = parser.getNamespace();
|
||||
ExtensionElement element = PacketParserUtils.parseExtensionElement(name, namespace, parser, outerXmlEnvironment);
|
||||
builder.addPayloadItem(element);
|
||||
}
|
||||
|
||||
if (tag == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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.addTo(JidCreate.from(jidString));
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
@ -292,6 +292,13 @@
|
|||
<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-0422: Message Fastening -->
|
||||
<extensionProvider>
|
||||
<elementName>apply-to</elementName>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue