001/**
002 *
003 * Copyright 2017 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.omemo.element;
018
019import static org.jivesoftware.smackx.omemo.util.OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL;
020
021import java.io.UnsupportedEncodingException;
022
023import org.jivesoftware.smack.util.StringUtils;
024import org.jivesoftware.smack.util.XmlStringBuilder;
025import org.jivesoftware.smack.util.stringencoder.Base64;
026
027/**
028 * An OMEMO (PreKey)WhisperMessage element.
029 *
030 * @author Paul Schaub
031 */
032public class OmemoVAxolotlElement extends OmemoElement {
033
034    /**
035     * Create a new OmemoMessageElement from a header and a payload.
036     *
037     * @param header  header of the message
038     * @param payload payload
039     */
040    public OmemoVAxolotlElement(OmemoHeader header, byte[] payload) {
041        super(header, payload);
042    }
043
044    @Override
045    public String getElementName() {
046        return ENCRYPTED;
047    }
048
049    @Override
050    public XmlStringBuilder toXML(String enclosingNamespace) {
051        XmlStringBuilder sb = new XmlStringBuilder(this).rightAngleBracket();
052
053        sb.element(header);
054
055        if (payload != null) {
056            sb.openElement(PAYLOAD).append(Base64.encodeToString(payload)).closeElement(PAYLOAD);
057        }
058
059        sb.closeElement(this);
060        return sb;
061    }
062
063    @Override
064    public String getNamespace() {
065        return OMEMO_NAMESPACE_V_AXOLOTL;
066    }
067
068    @Override
069    public String toString() {
070        try {
071            StringBuilder s = new StringBuilder("Encrypted:\n")
072                    .append("   header: sid: ").append(getHeader().getSid()).append('\n');
073            for (OmemoHeader.Key k : getHeader().getKeys()) {
074                s.append("      key: prekey: ").append(k.isPreKey()).append(" rid: ")
075                        .append(k.getId()).append(' ')
076                        .append(new String(k.getData(), StringUtils.UTF8)).append('\n');
077            }
078            s.append("      iv: ").append(new String(getHeader().getIv(), StringUtils.UTF8)).append('\n');
079            s.append("  payload: ").append(new String(getPayload(), StringUtils.UTF8));
080            return s.toString();
081        } catch (UnsupportedEncodingException e) {
082            // UTF-8 must be supported on all platforms claiming to be java compatible.
083            throw new AssertionError(e);
084        }
085    }
086}