Search in sources :

Example 26 with Element

use of org.kxml2.kdom.Element in project briefcase by opendatakit.

the class FileSystemUtils method decryptAndValidateSubmission.

public static DecryptOutcome decryptAndValidateSubmission(Document doc, PrivateKey rsaPrivateKey, File instanceDir, File unEncryptedDir) throws ParsingException, FileSystemException, CryptoException {
    Element rootElement = doc.getRootElement();
    String base64EncryptedSymmetricKey;
    String instanceIdMetadata = null;
    List<String> mediaNames = new ArrayList<>();
    String encryptedSubmissionFile;
    String base64EncryptedElementSignature;
    {
        Element base64Key = null;
        Element base64Signature = null;
        Element encryptedXml = null;
        for (int i = 0; i < rootElement.getChildCount(); ++i) {
            if (rootElement.getType(i) == Node.ELEMENT) {
                Element child = rootElement.getElement(i);
                String name = child.getName();
                if (name.equals("base64EncryptedKey")) {
                    base64Key = child;
                } else if (name.equals("base64EncryptedElementSignature")) {
                    base64Signature = child;
                } else if (name.equals("encryptedXmlFile")) {
                    encryptedXml = child;
                } else if (name.equals("media")) {
                    Element media = child;
                    for (int j = 0; j < media.getChildCount(); ++j) {
                        if (media.getType(j) == Node.ELEMENT) {
                            Element mediaChild = media.getElement(j);
                            String mediaFileElementName = mediaChild.getName();
                            if (mediaFileElementName.equals("file")) {
                                String mediaName = XFormParser.getXMLText(mediaChild, true);
                                if (mediaName == null || mediaName.length() == 0) {
                                    mediaNames.add(null);
                                } else {
                                    mediaNames.add(mediaName);
                                }
                            }
                        }
                    }
                }
            }
        }
        // verify base64Key
        if (base64Key == null) {
            throw new ParsingException("Missing base64EncryptedKey element in encrypted form.");
        }
        base64EncryptedSymmetricKey = XFormParser.getXMLText(base64Key, true);
        // get instanceID out of OpenRosa meta block
        instanceIdMetadata = XmlManipulationUtils.getOpenRosaInstanceId(rootElement);
        if (instanceIdMetadata == null) {
            throw new ParsingException("Missing instanceID within meta block of encrypted form.");
        }
        // get submission filename
        if (encryptedXml == null) {
            throw new ParsingException("Missing encryptedXmlFile element in encrypted form.");
        }
        encryptedSubmissionFile = XFormParser.getXMLText(encryptedXml, true);
        if (base64Signature == null) {
            throw new ParsingException("Missing base64EncryptedElementSignature element in encrypted form.");
        }
        base64EncryptedElementSignature = XFormParser.getXMLText(base64Signature, true);
    }
    if (instanceIdMetadata == null || base64EncryptedSymmetricKey == null || base64EncryptedElementSignature == null || encryptedSubmissionFile == null) {
        throw new ParsingException("Missing one or more required elements of encrypted form.");
    }
    FormInstanceMetadata fim;
    try {
        fim = XmlManipulationUtils.getFormInstanceMetadata(rootElement);
    } catch (ParsingException e) {
        String msg = "Unable to extract form instance metadata from submission manifest";
        log.error(msg, e);
        throw new ParsingException(msg + ". Cause: " + e.toString());
    }
    if (!instanceIdMetadata.equals(fim.instanceId)) {
        throw new ParsingException("InstanceID within metadata does not match that on top level element.");
    }
    boolean isValidated = FileSystemUtils.decryptSubmissionFiles(base64EncryptedSymmetricKey, fim, mediaNames, encryptedSubmissionFile, base64EncryptedElementSignature, rsaPrivateKey, instanceDir, unEncryptedDir);
    // and change doc to be the decrypted submission document
    File decryptedSubmission = new File(unEncryptedDir, "submission.xml");
    doc = XmlManipulationUtils.parseXml(decryptedSubmission);
    if (doc == null) {
        return null;
    }
    // verify that the metadata matches between the manifest and the submission
    rootElement = doc.getRootElement();
    FormInstanceMetadata sim = XmlManipulationUtils.getFormInstanceMetadata(rootElement);
    if (!fim.xparam.equals(sim.xparam)) {
        throw new ParsingException("FormId or version in decrypted submission does not match that in manifest!");
    }
    if (!fim.instanceId.equals(sim.instanceId)) {
        throw new ParsingException("InstanceId in decrypted submission does not match that in manifest!");
    }
    return new DecryptOutcome(doc, isValidated);
}
Also used : FormInstanceMetadata(org.opendatakit.briefcase.util.XmlManipulationUtils.FormInstanceMetadata) Element(org.kxml2.kdom.Element) ParsingException(org.opendatakit.briefcase.model.ParsingException) ArrayList(java.util.ArrayList) File(java.io.File)

Example 27 with Element

use of org.kxml2.kdom.Element in project briefcase by opendatakit.

the class XmlManipulationUtils method getBase64EncryptedFieldKey.

/**
 * Encrypted field-level encryption key.
 *
 * @param root
 * @return
 */
public static String getBase64EncryptedFieldKey(Element root) {
    String rootUri = root.getNamespace();
    Element meta = findMetaTag(root, rootUri);
    if (meta != null) {
        for (int i = 0; i < meta.getChildCount(); ++i) {
            if (meta.getType(i) == Node.ELEMENT) {
                Element child = meta.getElement(i);
                String cnUri = child.getNamespace();
                String cnName = child.getName();
                if (cnName.equals(BASE64_ENCRYPTED_FIELD_KEY) && (cnUri == null || cnUri.equals(EMPTY_STRING) || cnUri.equals(rootUri) || cnUri.equalsIgnoreCase(OPEN_ROSA_NAMESPACE) || cnUri.equalsIgnoreCase(OPEN_ROSA_NAMESPACE_SLASH))) {
                    return XFormParser.getXMLText(child, true);
                }
            }
        }
    }
    return null;
}
Also used : Element(org.kxml2.kdom.Element)

Example 28 with Element

use of org.kxml2.kdom.Element in project briefcase by opendatakit.

the class XmlManipulationUtils method parseSubmissionDownloadListResponse.

public static final SubmissionChunk parseSubmissionDownloadListResponse(Document doc) throws ParsingException {
    List<String> uriList = new ArrayList<>();
    String websafeCursorString = "";
    // Attempt parsing
    Element idChunkElement = doc.getRootElement();
    if (!idChunkElement.getName().equals("idChunk")) {
        String msg = "Parsing submissionList reply -- root element is not <idChunk> :" + idChunkElement.getName();
        log.error(msg);
        throw new ParsingException(msg);
    }
    String namespace = idChunkElement.getNamespace();
    if (!namespace.equalsIgnoreCase(NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS)) {
        String msg = "Parsing submissionList reply -- root element namespace is incorrect:" + namespace;
        log.error(msg);
        throw new ParsingException(msg);
    }
    int nElements = idChunkElement.getChildCount();
    for (int i = 0; i < nElements; ++i) {
        if (idChunkElement.getType(i) != Element.ELEMENT) {
            // e.g., whitespace (text)
            continue;
        }
        Element subElement = (Element) idChunkElement.getElement(i);
        namespace = subElement.getNamespace();
        if (!namespace.equalsIgnoreCase(NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS)) {
            // someone else's extension?
            continue;
        }
        String name = subElement.getName();
        if (name.equalsIgnoreCase("idList")) {
            // parse the idList
            int nIdElements = subElement.getChildCount();
            for (int j = 0; j < nIdElements; ++j) {
                if (subElement.getType(j) != Element.ELEMENT) {
                    // e.g., whitespace (text)
                    continue;
                }
                Element idElement = (Element) subElement.getElement(j);
                namespace = idElement.getNamespace();
                if (!namespace.equalsIgnoreCase(NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS)) {
                    // someone else's extension?
                    continue;
                }
                name = idElement.getName();
                if (name.equalsIgnoreCase("id")) {
                    // gather the uri
                    String uri = XFormParser.getXMLText(idElement, true);
                    if (uri != null) {
                        uriList.add(uri);
                    }
                } else {
                    log.warn("Unrecognized tag inside idList: " + name);
                }
            }
        } else if (name.equalsIgnoreCase("resumptionCursor")) {
            // gather the resumptionCursor
            websafeCursorString = XFormParser.getXMLText(subElement, true);
            if (websafeCursorString == null) {
                websafeCursorString = "";
            }
        } else {
            log.warn("Unrecognized tag inside idChunk: " + name);
        }
    }
    return new SubmissionChunk(uriList, websafeCursorString);
}
Also used : Element(org.kxml2.kdom.Element) ParsingException(org.opendatakit.briefcase.model.ParsingException) ArrayList(java.util.ArrayList) SubmissionChunk(org.opendatakit.briefcase.util.ServerFetcher.SubmissionChunk)

Example 29 with Element

use of org.kxml2.kdom.Element in project briefcase by opendatakit.

the class XmlManipulationUtils method getOpenRosaInstanceId.

/**
 * Find the OpenRosa instanceID defined for this record, if any.
 *
 * @return
 */
public static String getOpenRosaInstanceId(Element root) {
    String rootUri = root.getNamespace();
    Element meta = findMetaTag(root, rootUri);
    if (meta != null) {
        for (int i = 0; i < meta.getChildCount(); ++i) {
            if (meta.getType(i) == Node.ELEMENT) {
                Element child = meta.getElement(i);
                String cnUri = child.getNamespace();
                String cnName = child.getName();
                if (cnName.equals(OPEN_ROSA_INSTANCE_ID) && (cnUri == null || cnUri.equals(EMPTY_STRING) || cnUri.equals(rootUri) || cnUri.equalsIgnoreCase(OPEN_ROSA_NAMESPACE) || cnUri.equalsIgnoreCase(OPEN_ROSA_NAMESPACE_SLASH) || cnUri.equalsIgnoreCase(OPEN_ROSA_NAMESPACE_PRELIM))) {
                    return XFormParser.getXMLText(child, true);
                }
            }
        }
    }
    return null;
}
Also used : Element(org.kxml2.kdom.Element)

Example 30 with Element

use of org.kxml2.kdom.Element in project ETSMobile-Android2 by ApplETS.

the class Helper method convertToHeader.

public static Element convertToHeader(Object obj, String namespace, String name) {
    Element parentElement = new org.kxml2.kdom.Element().createElement(namespace, name);
    if (obj == null) {
        return parentElement;
    }
    if (obj instanceof KvmSerializable) {
        KvmSerializable soapObject = (KvmSerializable) obj;
        for (int i = 0; i < soapObject.getPropertyCount(); i++) {
            PropertyInfo info = new PropertyInfo();
            soapObject.getPropertyInfo(i, new Hashtable(), info);
            info.setValue(soapObject.getProperty(i));
            Element el1 = convertToHeader(info.getValue(), info.getNamespace(), info.getName());
            parentElement.addChild(Node.ELEMENT, el1);
        }
    } else {
        String value = obj.toString();
        parentElement.addChild(org.kxml2.kdom.Node.TEXT, value);
    }
    return parentElement;
}
Also used : Hashtable(java.util.Hashtable) Element(org.kxml2.kdom.Element) KvmSerializable(org.ksoap2.serialization.KvmSerializable) PropertyInfo(org.ksoap2.serialization.PropertyInfo) Element(org.kxml2.kdom.Element)

Aggregations

Element (org.kxml2.kdom.Element)50 TreeElement (org.javarosa.core.model.instance.TreeElement)25 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)23 ArrayList (java.util.ArrayList)21 IOException (java.io.IOException)17 IFormElement (org.javarosa.core.model.IFormElement)17 KXmlParser (org.kxml2.io.KXmlParser)12 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)12 File (java.io.File)8 Test (org.junit.Test)7 ParsingException (org.opendatakit.briefcase.model.ParsingException)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 Document (org.kxml2.kdom.Document)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 HashMap (java.util.HashMap)3 KXmlSerializer (org.kxml2.io.KXmlSerializer)3