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 037import org.jxmpp.JxmppContext; 038 039/** 040 * Smack ExtensionProvider that parses OMEMO bundle element into OmemoBundleElement objects. 041 * 042 * @author Paul Schaub 043 */ 044public class OmemoBundleVAxolotlProvider extends ExtensionElementProvider<OmemoBundleElement_VAxolotl> { 045 @Override 046 public OmemoBundleElement_VAxolotl parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException { 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 outerloop: while (true) { 056 XmlPullParser.Event tag = parser.next(); 057 switch (tag) { 058 case START_ELEMENT: 059 String name = parser.getName(); 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 (parser.getDepth() == initialDepth) { 095 break outerloop; 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}