OmemoHeaderElement.java

  1. /**
  2.  *
  3.  * Copyright 2017 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.omemo.element;

  18. import java.util.ArrayList;
  19. import java.util.List;

  20. import org.jivesoftware.smack.packet.XmlElement;
  21. import org.jivesoftware.smack.packet.XmlEnvironment;
  22. import org.jivesoftware.smack.util.XmlStringBuilder;
  23. import org.jivesoftware.smack.util.stringencoder.Base64;

  24. import org.jivesoftware.smackx.omemo.util.OmemoConstants;

  25. /**
  26.  * Header element of the message. The header contains information about the sender and the encrypted keys for
  27.  * the recipients, as well as the iv element for AES.
  28.  */
  29. public abstract class OmemoHeaderElement implements XmlElement {

  30.     public static final String ELEMENT = "header";
  31.     public static final String NAMESPACE = OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL;

  32.     public static final String ATTR_SID = "sid";
  33.     public static final String ATTR_IV = "iv";

  34.     private final int sid;
  35.     private final List<OmemoKeyElement> keys;
  36.     private final byte[] iv;

  37.     public OmemoHeaderElement(int sid, List<OmemoKeyElement> keys, byte[] iv) {
  38.         this.sid = sid;
  39.         this.keys = keys;
  40.         this.iv = iv;
  41.     }

  42.     /**
  43.      * Return the deviceId of the sender of the message.
  44.      *
  45.      * @return senders id
  46.      */
  47.     public int getSid() {
  48.         return sid;
  49.     }

  50.     public ArrayList<OmemoKeyElement> getKeys() {
  51.         return new ArrayList<>(keys);
  52.     }

  53.     public byte[] getIv() {
  54.         return iv != null ? iv.clone() : null;
  55.     }

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

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

  64.     @Override
  65.     public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
  66.         XmlStringBuilder sb = new XmlStringBuilder(this, enclosingXmlEnvironment);
  67.         sb.attribute(ATTR_SID, getSid()).rightAngleBracket();

  68.         for (OmemoKeyElement k : getKeys()) {
  69.             sb.append(k);
  70.         }

  71.         sb.openElement(ATTR_IV).append(Base64.encodeToString(getIv())).closeElement(ATTR_IV);

  72.         return sb.closeElement(this);
  73.     }


  74. }