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; 027import static org.xmlpull.v1.XmlPullParser.END_TAG; 028import static org.xmlpull.v1.XmlPullParser.START_TAG; 029 030import java.util.HashMap; 031 032import org.jivesoftware.smack.provider.ExtensionElementProvider; 033 034import org.jivesoftware.smackx.omemo.element.OmemoBundleVAxolotlElement; 035 036import org.xmlpull.v1.XmlPullParser; 037 038/** 039 * Smack ExtensionProvider that parses OMEMO bundle element into OmemoBundleElement objects. 040 * 041 * @author Paul Schaub 042 */ 043public class OmemoBundleVAxolotlProvider extends ExtensionElementProvider<OmemoBundleVAxolotlElement> { 044 @Override 045 public OmemoBundleVAxolotlElement parse(XmlPullParser parser, int initialDepth) throws Exception { 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 int tag = parser.next(); 057 String name = parser.getName(); 058 switch (tag) { 059 case START_TAG: 060 // <signedPreKeyPublic> 061 if (name.equals(SIGNED_PRE_KEY_PUB)) { 062 for (int i = 0; i < parser.getAttributeCount(); i++) { 063 if (parser.getAttributeName(i).equals(SIGNED_PRE_KEY_ID)) { 064 int id = Integer.parseInt(parser.getAttributeValue(i)); 065 signedPreKey = parser.nextText(); 066 signedPreKeyId = id; 067 } 068 } 069 } 070 // <bundleGetSignedPreKeySignature> 071 else if (name.equals(SIGNED_PRE_KEY_SIG)) { 072 signedPreKeySignature = parser.nextText(); 073 } 074 // <deserializeIdentityKey> 075 else if (name.equals(IDENTITY_KEY)) { 076 identityKey = parser.nextText(); 077 } 078 // <deserializeECPublicKeys> 079 else if (name.equals(PRE_KEYS)) { 080 inPreKeys = true; 081 } 082 // <preKeyPublic preKeyId='424242'> 083 else if (inPreKeys && name.equals(PRE_KEY_PUB)) { 084 for (int i = 0; i < parser.getAttributeCount(); i++) { 085 if (parser.getAttributeName(i).equals(PRE_KEY_ID)) { 086 preKeys.put(Integer.parseInt(parser.getAttributeValue(i)), 087 parser.nextText()); 088 } 089 } 090 } 091 break; 092 case END_TAG: 093 if (name.equals(BUNDLE)) { 094 stop = true; 095 } 096 break; 097 } 098 } 099 return new OmemoBundleVAxolotlElement(signedPreKeyId, signedPreKey, signedPreKeySignature, identityKey, preKeys); 100 } 101}