EncryptedOpenPgpContentElement.java

  1. /**
  2.  *
  3.  * Copyright 2017-2019 Florian Schmaus, 2018 Paul Schaub.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.ox.element;

  18. import java.util.Date;
  19. import java.util.List;
  20. import java.util.Set;

  21. import org.jivesoftware.smack.packet.ExtensionElement;
  22. import org.jivesoftware.smack.util.Objects;
  23. import org.jivesoftware.smack.util.RandomUtil;
  24. import org.jivesoftware.smack.util.StringUtils;
  25. import org.jivesoftware.smack.util.XmlStringBuilder;

  26. import org.jxmpp.jid.Jid;

  27. /**
  28.  * Abstract class that bundles functionality of encrypted OpenPGP content elements ({@link CryptElement},
  29.  * {@link SigncryptElement}) together.
  30.  */
  31. public abstract class EncryptedOpenPgpContentElement extends OpenPgpContentElement {

  32.     public static final String ELEM_RPAD = "rpad";

  33.     private final String rpad;

  34.     protected EncryptedOpenPgpContentElement(Set<? extends Jid> to, String rpad, Date timestamp, List<ExtensionElement> payload) {
  35.         super(Objects.requireNonNullNorEmpty(
  36.                 to, "Encrypted OpenPGP content elements must have at least one 'to' attribute."),
  37.                 timestamp, payload);
  38.         this.rpad = Objects.requireNonNull(rpad);
  39.     }

  40.     protected EncryptedOpenPgpContentElement(Set<? extends Jid> to, List<ExtensionElement> payload) {
  41.         super(Objects.requireNonNullNorEmpty(
  42.                 to, "Encrypted OpenPGP content elements must have at least one 'to' attribute."),
  43.                 new Date(), payload);
  44.         this.rpad = createRandomPadding();
  45.     }

  46.     private static String createRandomPadding() {
  47.         int len = RandomUtil.nextSecureRandomInt(256);
  48.         return StringUtils.randomString(len);
  49.     }

  50.     @Override
  51.     protected void addCommonXml(XmlStringBuilder xml) {
  52.         super.addCommonXml(xml);

  53.         xml.openElement("rpad").escape(rpad).closeElement("rpad");
  54.     }
  55. }