001/**
002 *
003 * Copyright 2020 Paul Schaub
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.stanza_content_encryption.element;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.Collections;
022import java.util.Date;
023import java.util.HashSet;
024import java.util.List;
025import java.util.Set;
026
027import javax.xml.namespace.QName;
028
029import org.jivesoftware.smack.packet.ExtensionElement;
030import org.jivesoftware.smack.packet.XmlElement;
031import org.jivesoftware.smack.packet.XmlEnvironment;
032import org.jivesoftware.smack.util.Objects;
033import org.jivesoftware.smack.util.XmlStringBuilder;
034
035import org.jivesoftware.smackx.address.packet.MultipleAddresses;
036import org.jivesoftware.smackx.hints.element.MessageProcessingHint;
037import org.jivesoftware.smackx.sid.element.StanzaIdElement;
038
039import org.jxmpp.jid.Jid;
040
041/**
042 * Extension element that holds the payload element, as well as a list of affix elements.
043 * In SCE, the XML representation of this element is what will be encrypted using the encryption mechanism of choice.
044 */
045public class ContentElement implements ExtensionElement {
046
047    private static final String NAMESPACE_UNVERSIONED = "urn:xmpp:sce";
048    public static final String NAMESPACE_0 = NAMESPACE_UNVERSIONED + ":0";
049    public static final String NAMESPACE = NAMESPACE_0;
050    public static final String ELEMENT = "content";
051    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
052
053    private final PayloadElement payload;
054    private final List<AffixElement> affixElements;
055
056    ContentElement(PayloadElement payload, List<AffixElement> affixElements) {
057        this.payload = payload;
058        this.affixElements = Collections.unmodifiableList(affixElements);
059    }
060
061    /**
062     * Return the {@link PayloadElement} which holds the sensitive payload extensions.
063     *
064     * @return payload element
065     */
066    public PayloadElement getPayload() {
067        return payload;
068    }
069
070    /**
071     * Return a list of affix elements.
072     * Those are elements that need to be verified upon reception by the encryption mechanisms implementation.
073     *
074     * @see <a href="https://xmpp.org/extensions/xep-0420.html#affix_elements">
075     *     XEP-0420: Stanza Content Encryption - §4. Affix Elements</a>
076     *
077     * @return list of affix elements
078     */
079    public List<AffixElement> getAffixElements() {
080        return affixElements;
081    }
082
083    @Override
084    public String getNamespace() {
085        return NAMESPACE;
086    }
087
088    @Override
089    public String getElementName() {
090        return ELEMENT;
091    }
092
093    @Override
094    public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
095        XmlStringBuilder xml = new XmlStringBuilder(this).rightAngleBracket();
096        xml.append(affixElements);
097        xml.append(payload);
098        return xml.closeElement(this);
099    }
100
101    @Override
102    public QName getQName() {
103        return QNAME;
104    }
105
106    /**
107     * Return a {@link Builder} that can be used to build the {@link ContentElement}.
108     * @return builder
109     */
110    public static Builder builder() {
111        return new Builder();
112    }
113
114    public static final class Builder {
115        private static final Set<String> BLACKLISTED_NAMESPACES = Collections.singleton(MessageProcessingHint.NAMESPACE);
116        private static final Set<QName> BLACKLISTED_QNAMES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
117                StanzaIdElement.QNAME,
118                MultipleAddresses.QNAME
119        )));
120
121        private FromAffixElement from = null;
122        private TimestampAffixElement timestamp = null;
123        private RandomPaddingAffixElement rpad = null;
124
125        private final List<AffixElement> otherAffixElements = new ArrayList<>();
126        private final List<XmlElement> payloadItems = new ArrayList<>();
127
128        private Builder() {
129
130        }
131
132        /**
133         * Add an affix element of type 'to' which addresses one recipient.
134         * The jid in the 'to' element SHOULD be a bare jid.
135         *
136         * @param jid jid
137         * @return builder
138         */
139        public Builder addTo(Jid jid) {
140            return addTo(new ToAffixElement(jid));
141        }
142
143        /**
144         * Add an affix element of type 'to' which addresses one recipient.
145         *
146         * @param to affix element
147         * @return builder
148         */
149        public Builder addTo(ToAffixElement to) {
150            this.otherAffixElements.add(Objects.requireNonNull(to, "'to' affix element MUST NOT be null."));
151            return this;
152        }
153
154        /**
155         * Set the senders jid as a 'from' affix element.
156         *
157         * @param jid jid of the sender
158         * @return builder
159         */
160        public Builder setFrom(Jid jid) {
161            return setFrom(new FromAffixElement(jid));
162        }
163
164        /**
165         * Set the senders jid as a 'from' affix element.
166         *
167         * @param from affix element
168         * @return builder
169         */
170        public Builder setFrom(FromAffixElement from) {
171            this.from = Objects.requireNonNull(from, "'form' affix element MUST NOT be null.");
172            return this;
173        }
174
175        /**
176         * Set the given date as a 'time' affix element.
177         *
178         * @param date timestamp as date
179         * @return builder
180         */
181        public Builder setTimestamp(Date date) {
182            return setTimestamp(new TimestampAffixElement(date));
183        }
184
185        /**
186         * Set the timestamp of the message as a 'time' affix element.
187         *
188         * @param timestamp timestamp affix element
189         * @return builder
190         */
191        public Builder setTimestamp(TimestampAffixElement timestamp) {
192            this.timestamp = Objects.requireNonNull(timestamp, "'time' affix element MUST NOT be null.");
193            return this;
194        }
195
196        /**
197         * Set some random length random content padding.
198         *
199         * @return builder
200         */
201        public Builder setRandomPadding() {
202            this.rpad = new RandomPaddingAffixElement();
203            return this;
204        }
205
206        /**
207         * Set the given string as padding.
208         * The padding should be of length between 1 and 200 characters.
209         *
210         * @param padding padding string
211         * @return builder
212         */
213        public Builder setRandomPadding(String padding) {
214            return setRandomPadding(new RandomPaddingAffixElement(padding));
215        }
216
217        /**
218         * Set a padding affix element.
219         *
220         * @param padding affix element
221         * @return builder
222         */
223        public Builder setRandomPadding(RandomPaddingAffixElement padding) {
224            this.rpad = Objects.requireNonNull(padding, "'rpad' affix element MUST NOT be empty.");
225            return this;
226        }
227
228        /**
229         * Add an additional, SCE profile specific affix element.
230         *
231         * @param customAffixElement additional affix element
232         * @return builder
233         */
234        public Builder addFurtherAffixElement(AffixElement customAffixElement) {
235            this.otherAffixElements.add(Objects.requireNonNull(customAffixElement,
236                    "Custom affix element MUST NOT be null."));
237            return this;
238        }
239
240        /**
241         * Add a payload item as child element of the payload element.
242         * There are some items that are not allowed as payload.
243         * Adding those will throw an exception.
244         *
245         * @see <a href="https://xmpp.org/extensions/xep-0420.html#server-processed">
246         *     XEP-0420: Stanza Content Encryption - §9. Server-processed Elements</a>
247         *
248         * @param payloadItem extension element
249         * @return builder
250         * @throws IllegalArgumentException in case an extension element from the blacklist is added.
251         */
252        public Builder addPayloadItem(XmlElement payloadItem) {
253            Objects.requireNonNull(payloadItem, "Payload item MUST NOT be null.");
254            this.payloadItems.add(checkForIllegalPayloadsAndPossiblyThrow(payloadItem));
255            return this;
256        }
257
258        /**
259         * Construct a content element from this builder.
260         *
261         * @return content element
262         */
263        public ContentElement build() {
264            List<AffixElement> allAffixElements = collectAffixElements();
265            PayloadElement payloadElement = new PayloadElement(payloadItems);
266            return new ContentElement(payloadElement, allAffixElements);
267        }
268
269        private static XmlElement checkForIllegalPayloadsAndPossiblyThrow(XmlElement payloadItem) {
270            QName qName = payloadItem.getQName();
271            if (BLACKLISTED_QNAMES.contains(qName)) {
272                throw new IllegalArgumentException("Element identified by " + qName +
273                        " is not allowed as payload item. See https://xmpp.org/extensions/xep-0420.html#server-processed");
274            }
275
276            String namespace = payloadItem.getNamespace();
277            if (BLACKLISTED_NAMESPACES.contains(namespace)) {
278                throw new IllegalArgumentException("Elements of namespace '" + namespace +
279                        "' are not allowed as payload items. See https://xmpp.org/extensions/xep-0420.html#server-processed");
280            }
281
282            return payloadItem;
283        }
284
285        private List<AffixElement> collectAffixElements() {
286            List<AffixElement> allAffixElements = new ArrayList<>(Arrays.asList(rpad, from, timestamp));
287            allAffixElements.addAll(otherAffixElements);
288            return allAffixElements;
289        }
290    }
291}