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 java.util.ArrayList; 020import java.util.List; 021 022import org.jivesoftware.smack.packet.NamedElement; 023import org.jivesoftware.smack.util.XmlStringBuilder; 024import org.jivesoftware.smack.util.stringencoder.Base64; 025 026/** 027 * Header element of the message. The header contains information about the sender and the encrypted keys for 028 * the recipients, as well as the iv element for AES. 029 */ 030public abstract class OmemoHeaderElement implements NamedElement { 031 032 public static final String NAME_HEADER = "header"; 033 public static final String ATTR_SID = "sid"; 034 public static final String ATTR_IV = "iv"; 035 036 private final int sid; 037 private final List<OmemoKeyElement> keys; 038 private final byte[] iv; 039 040 public OmemoHeaderElement(int sid, List<OmemoKeyElement> keys, byte[] iv) { 041 this.sid = sid; 042 this.keys = keys; 043 this.iv = iv; 044 } 045 046 /** 047 * Return the deviceId of the sender of the message. 048 * 049 * @return senders id 050 */ 051 public int getSid() { 052 return sid; 053 } 054 055 public ArrayList<OmemoKeyElement> getKeys() { 056 return new ArrayList<>(keys); 057 } 058 059 public byte[] getIv() { 060 return iv != null ? iv.clone() : null; 061 } 062 063 @Override 064 public String getElementName() { 065 return NAME_HEADER; 066 } 067 068 @Override 069 public CharSequence toXML(String enclosingNamespace) { 070 XmlStringBuilder sb = new XmlStringBuilder(this); 071 sb.attribute(ATTR_SID, getSid()).rightAngleBracket(); 072 073 for (OmemoKeyElement k : getKeys()) { 074 sb.element(k); 075 } 076 077 sb.openElement(ATTR_IV).append(Base64.encodeToString(getIv())).closeElement(ATTR_IV); 078 079 return sb.closeElement(this); 080 } 081 082 083}