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