PubkeyElement.java

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

  18. import java.util.Date;

  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.util.Objects;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;
  22. import org.jivesoftware.smack.util.stringencoder.Base64;

  23. /**
  24.  * Class representing a pubkey element which is used to transport OpenPGP public keys.
  25.  *
  26.  * @see <a href="https://xmpp.org/extensions/xep-0373.html#announcing-pubkey">
  27.  *     XEP-0373: ยง4.1 The OpenPGP Public-Key Data Node</a>
  28.  */
  29. public class PubkeyElement implements ExtensionElement {

  30.     public static final String NAMESPACE = OpenPgpElement.NAMESPACE;
  31.     public static final String ELEMENT = "pubkey";
  32.     public static final String ATTR_DATE = "date";

  33.     private final PubkeyDataElement dataElement;
  34.     private final Date date;

  35.     public PubkeyElement(PubkeyDataElement dataElement, Date date) {
  36.         this.dataElement = Objects.requireNonNull(dataElement);
  37.         this.date = date;
  38.     }

  39.     /**
  40.      * Return the &lt;data&gt; element containing the base64 encoded public key.
  41.      *
  42.      * @return data element
  43.      */
  44.     public PubkeyDataElement getDataElement() {
  45.         return dataElement;
  46.     }

  47.     /**
  48.      * Date on which the key was last modified.
  49.      *
  50.      * @return last modification date
  51.      */
  52.     public Date getDate() {
  53.         return date;
  54.     }

  55.     @Override
  56.     public String getNamespace() {
  57.         return NAMESPACE;
  58.     }

  59.     @Override
  60.     public String getElementName() {
  61.         return ELEMENT;
  62.     }

  63.     @Override
  64.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  65.         XmlStringBuilder xml = new XmlStringBuilder(this)
  66.                 .optAttribute(ATTR_DATE, date)
  67.                 .rightAngleBracket()
  68.                 .append(getDataElement())
  69.                 .closeElement(this);
  70.         return xml;
  71.     }

  72.     /**
  73.      * Element that contains the base64 encoded public key.
  74.      */
  75.     public static class PubkeyDataElement implements ExtensionElement {

  76.         public static final String ELEMENT = "data";

  77.         private final String b64Data;

  78.         public PubkeyDataElement(String b64Data) {
  79.             this.b64Data = Objects.requireNonNull(b64Data);
  80.         }

  81.         /**
  82.          * Base64 encoded public key.
  83.          *
  84.          * @return the base64 encoded version of the public key.
  85.          */
  86.         public String getB64Data() {
  87.             return b64Data;
  88.         }

  89.         private transient byte[] pubKeyBytesCache;

  90.         public byte[] getPubKeyBytes() {
  91.             if (pubKeyBytesCache == null) {
  92.                 pubKeyBytesCache = Base64.decode(b64Data);
  93.             }
  94.             return pubKeyBytesCache.clone();
  95.         }

  96.         @Override
  97.         public String getElementName() {
  98.             return ELEMENT;
  99.         }

  100.         @Override
  101.         public String getNamespace() {
  102.             return NAMESPACE;
  103.         }

  104.         @Override
  105.         public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  106.             XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace)
  107.                     .rightAngleBracket()
  108.                     .append(b64Data)
  109.                     .closeElement(this);
  110.             return xml;
  111.         }
  112.     }
  113. }