001/**
002 *
003 * Copyright 2017-2020 Florian Schmaus, 2018 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.ox.element;
018
019import java.io.ByteArrayInputStream;
020import java.io.InputStream;
021
022import javax.xml.namespace.QName;
023
024import org.jivesoftware.smack.packet.ExtensionElement;
025import org.jivesoftware.smack.packet.Stanza;
026import org.jivesoftware.smack.util.StringUtils;
027import org.jivesoftware.smack.util.XmlStringBuilder;
028
029import org.jivesoftware.smackx.ox.util.Util;
030
031/**
032 * Class that represents an OpenPGP message.
033 * The content of this elements text is an base64 encoded , OpenPGP encrypted/signed content element ({@link SignElement},
034 * {@link SigncryptElement}, {@link CryptElement}).
035 *
036 * @see <a href="https://xmpp.org/extensions/xep-0373.html#exchange">
037 *     XEP-0373: ยง3.1 Exchanging OpenPGP Encrypted and Signed Data</a>
038 */
039public class OpenPgpElement implements ExtensionElement {
040
041    public static final String ELEMENT = "openpgp";
042    public static final String NAMESPACE = "urn:xmpp:openpgp:0";
043    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
044
045    // Represents the OpenPGP message, but encoded using base64.
046    private final String base64EncodedOpenPgpMessage;
047
048    public OpenPgpElement(String base64EncodedOpenPgpMessage) {
049        this.base64EncodedOpenPgpMessage = StringUtils.requireNotNullNorEmpty(base64EncodedOpenPgpMessage,
050                "base64 encoded message MUST NOT be null nor empty.");
051    }
052
053    public InputStream toInputStream() {
054        return new ByteArrayInputStream(base64EncodedOpenPgpMessage.getBytes(Util.UTF8));
055    }
056
057    /**
058     * Return the OpenPGP encrypted payload.
059     *
060     * @return OpenPGP encrypted payload.
061     */
062    public String getEncryptedBase64MessageContent() {
063        return base64EncodedOpenPgpMessage;
064    }
065
066    @Override
067    public String getElementName() {
068        return ELEMENT;
069    }
070
071    @Override
072    public String getNamespace() {
073        return NAMESPACE;
074    }
075
076    @Override
077    public QName getQName() {
078        return QNAME;
079    }
080
081    @Override
082    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
083        XmlStringBuilder xml = new XmlStringBuilder(this);
084        xml.rightAngleBracket().append(base64EncodedOpenPgpMessage).closeElement(this);
085        return xml;
086    }
087
088    public static OpenPgpElement fromStanza(Stanza stanza) {
089        return stanza.getExtension(OpenPgpElement.class);
090    }
091}