OmemoBundleVAxolotlProvider.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.provider;

  18. import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.IDENTITY_KEY;
  19. import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.PRE_KEYS;
  20. import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.PRE_KEY_ID;
  21. import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.PRE_KEY_PUB;
  22. import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.SIGNED_PRE_KEY_ID;
  23. import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.SIGNED_PRE_KEY_PUB;
  24. import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.SIGNED_PRE_KEY_SIG;

  25. import java.io.IOException;
  26. import java.util.HashMap;

  27. import org.jivesoftware.smack.packet.XmlEnvironment;
  28. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  29. import org.jivesoftware.smack.xml.XmlPullParser;
  30. import org.jivesoftware.smack.xml.XmlPullParserException;

  31. import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VAxolotl;

  32. /**
  33.  * Smack ExtensionProvider that parses OMEMO bundle element into OmemoBundleElement objects.
  34.  *
  35.  * @author Paul Schaub
  36.  */
  37. public class OmemoBundleVAxolotlProvider extends ExtensionElementProvider<OmemoBundleElement_VAxolotl> {
  38.     @Override
  39.     public OmemoBundleElement_VAxolotl parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  40.         boolean inPreKeys = false;

  41.         int signedPreKeyId = -1;
  42.         String signedPreKey = null;
  43.         String signedPreKeySignature = null;
  44.         String identityKey = null;
  45.         HashMap<Integer, String> preKeys = new HashMap<>();

  46.         outerloop: while (true) {
  47.             XmlPullParser.Event tag = parser.next();
  48.             switch (tag) {
  49.                 case START_ELEMENT:
  50.                     String name = parser.getName();
  51.                     final int attributeCount = parser.getAttributeCount();
  52.                     // <signedPreKeyPublic>
  53.                     if (name.equals(SIGNED_PRE_KEY_PUB)) {
  54.                         for (int i = 0; i < attributeCount; i++) {
  55.                             if (parser.getAttributeName(i).equals(SIGNED_PRE_KEY_ID)) {
  56.                                 int id = Integer.parseInt(parser.getAttributeValue(i));
  57.                                 signedPreKey = parser.nextText();
  58.                                 signedPreKeyId = id;
  59.                             }
  60.                         }
  61.                     }
  62.                     // <bundleGetSignedPreKeySignature>
  63.                     else if (name.equals(SIGNED_PRE_KEY_SIG)) {
  64.                         signedPreKeySignature = parser.nextText();
  65.                     }
  66.                     // <deserializeIdentityKey>
  67.                     else if (name.equals(IDENTITY_KEY)) {
  68.                         identityKey = parser.nextText();
  69.                     }
  70.                     // <deserializeECPublicKeys>
  71.                     else if (name.equals(PRE_KEYS)) {
  72.                         inPreKeys = true;
  73.                     }
  74.                     // <preKeyPublic preKeyId='424242'>
  75.                     else if (inPreKeys && name.equals(PRE_KEY_PUB)) {
  76.                         for (int i = 0; i < attributeCount; i++) {
  77.                             if (parser.getAttributeName(i).equals(PRE_KEY_ID)) {
  78.                                 preKeys.put(Integer.parseInt(parser.getAttributeValue(i)),
  79.                                         parser.nextText());
  80.                             }
  81.                         }
  82.                     }
  83.                     break;
  84.                 case END_ELEMENT:
  85.                     if (parser.getDepth() == initialDepth) {
  86.                         break outerloop;
  87.                     }
  88.                     break;
  89.                 default:
  90.                     // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
  91.                     break;
  92.             }
  93.         }
  94.         return new OmemoBundleElement_VAxolotl(signedPreKeyId, signedPreKey, signedPreKeySignature, identityKey, preKeys);
  95.     }
  96. }