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