OpenPgpElement.java

  1. /**
  2.  *
  3.  * Copyright 2017-2020 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.io.ByteArrayInputStream;
  19. import java.io.InputStream;

  20. import javax.xml.namespace.QName;

  21. import org.jivesoftware.smack.packet.ExtensionElement;
  22. import org.jivesoftware.smack.packet.Stanza;
  23. import org.jivesoftware.smack.util.StringUtils;
  24. import org.jivesoftware.smack.util.XmlStringBuilder;

  25. import org.jivesoftware.smack.util.stringencoder.Base64;
  26. import org.jivesoftware.smackx.ox.util.Util;

  27. /**
  28.  * Class that represents an OpenPGP message.
  29.  * The content of this elements text is an base64 encoded , OpenPGP encrypted/signed content element ({@link SignElement},
  30.  * {@link SigncryptElement}, {@link CryptElement}).
  31.  *
  32.  * @see <a href="https://xmpp.org/extensions/xep-0373.html#exchange">
  33.  *     XEP-0373: ยง3.1 Exchanging OpenPGP Encrypted and Signed Data</a>
  34.  */
  35. public class OpenPgpElement implements ExtensionElement {

  36.     public static final String ELEMENT = "openpgp";
  37.     public static final String NAMESPACE = "urn:xmpp:openpgp:0";
  38.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  39.     // Represents the OpenPGP message, but encoded using base64.
  40.     private final String base64EncodedOpenPgpMessage;

  41.     public OpenPgpElement(String base64EncodedOpenPgpMessage) {
  42.         this.base64EncodedOpenPgpMessage = StringUtils.requireNotNullNorEmpty(base64EncodedOpenPgpMessage,
  43.                 "base64 encoded message MUST NOT be null nor empty.");
  44.     }

  45.     public InputStream toInputStream() {
  46.         return new ByteArrayInputStream(
  47.                 Base64.decode(base64EncodedOpenPgpMessage.getBytes(Util.UTF8)));
  48.     }

  49.     /**
  50.      * Return the OpenPGP encrypted payload.
  51.      *
  52.      * @return OpenPGP encrypted payload.
  53.      */
  54.     public String getEncryptedBase64MessageContent() {
  55.         return base64EncodedOpenPgpMessage;
  56.     }

  57.     @Override
  58.     public String getElementName() {
  59.         return ELEMENT;
  60.     }

  61.     @Override
  62.     public String getNamespace() {
  63.         return NAMESPACE;
  64.     }

  65.     @Override
  66.     public QName getQName() {
  67.         return QNAME;
  68.     }

  69.     @Override
  70.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  71.         XmlStringBuilder xml = new XmlStringBuilder(this);
  72.         xml.rightAngleBracket().append(base64EncodedOpenPgpMessage).closeElement(this);
  73.         return xml;
  74.     }

  75.     public static OpenPgpElement fromStanza(Stanza stanza) {
  76.         return stanza.getExtension(OpenPgpElement.class);
  77.     }
  78. }