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.provider; 018 019import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.BUNDLE; 020import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.IDENTITY_KEY; 021import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.PRE_KEYS; 022import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.PRE_KEY_ID; 023import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.PRE_KEY_PUB; 024import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.SIGNED_PRE_KEY_ID; 025import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.SIGNED_PRE_KEY_PUB; 026import static org.jivesoftware.smackx.omemo.element.OmemoBundleElement.SIGNED_PRE_KEY_SIG; 027 028import java.io.IOException; 029import java.util.HashMap; 030 031import org.jivesoftware.smack.packet.XmlEnvironment; 032import org.jivesoftware.smack.provider.ExtensionElementProvider; 033import org.jivesoftware.smack.xml.XmlPullParser; 034import org.jivesoftware.smack.xml.XmlPullParserException; 035 036import org.jivesoftware.smackx.omemo.element.OmemoBundleElement_VAxolotl; 037 038/** 039 * Smack ExtensionProvider that parses OMEMO bundle element into OmemoBundleElement objects. 040 * 041 * @author Paul Schaub 042 */ 043public class OmemoBundleVAxolotlProvider extends ExtensionElementProvider<OmemoBundleElement_VAxolotl> { 044 @Override 045 public OmemoBundleElement_VAxolotl parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { 046 boolean stop = false; 047 boolean inPreKeys = false; 048 049 int signedPreKeyId = -1; 050 String signedPreKey = null; 051 String signedPreKeySignature = null; 052 String identityKey = null; 053 HashMap<Integer, String> preKeys = new HashMap<>(); 054 055 while (!stop) { 056 XmlPullParser.Event tag = parser.next(); 057 String name = parser.getName(); 058 switch (tag) { 059 case START_ELEMENT: 060 final int attributeCount = parser.getAttributeCount(); 061 // <signedPreKeyPublic> 062 if (name.equals(SIGNED_PRE_KEY_PUB)) { 063 for (int i = 0; i < attributeCount; i++) { 064 if (parser.getAttributeName(i).equals(SIGNED_PRE_KEY_ID)) { 065 int id = Integer.parseInt(parser.getAttributeValue(i)); 066 signedPreKey = parser.nextText(); 067 signedPreKeyId = id; 068 } 069 } 070 } 071 // <bundleGetSignedPreKeySignature> 072 else if (name.equals(SIGNED_PRE_KEY_SIG)) { 073 signedPreKeySignature = parser.nextText(); 074 } 075 // <deserializeIdentityKey> 076 else if (name.equals(IDENTITY_KEY)) { 077 identityKey = parser.nextText(); 078 } 079 // <deserializeECPublicKeys> 080 else if (name.equals(PRE_KEYS)) { 081 inPreKeys = true; 082 } 083 // <preKeyPublic preKeyId='424242'> 084 else if (inPreKeys && name.equals(PRE_KEY_PUB)) { 085 for (int i = 0; i < attributeCount; i++) { 086 if (parser.getAttributeName(i).equals(PRE_KEY_ID)) { 087 preKeys.put(Integer.parseInt(parser.getAttributeValue(i)), 088 parser.nextText()); 089 } 090 } 091 } 092 break; 093 case END_ELEMENT: 094 if (name.equals(BUNDLE)) { 095 stop = true; 096 } 097 break; 098 default: 099 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement. 100 break; 101 } 102 } 103 return new OmemoBundleElement_VAxolotl(signedPreKeyId, signedPreKey, signedPreKeySignature, identityKey, preKeys); 104 } 105}