Khaled Abdelmohsen | 7c56df6 | 2019-10-31 16:59:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.integrity.serializer; |
| 18 | |
Omer Nebil Yaveroglu | a00ba07 | 2019-12-20 17:25:40 +0000 | [diff] [blame] | 19 | import static com.android.server.integrity.serializer.RuleIndexingDetails.APP_CERTIFICATE_INDEXED; |
| 20 | import static com.android.server.integrity.serializer.RuleIndexingDetails.NOT_INDEXED; |
| 21 | import static com.android.server.integrity.serializer.RuleIndexingDetails.PACKAGE_NAME_INDEXED; |
| 22 | |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 23 | import android.content.integrity.AtomicFormula; |
| 24 | import android.content.integrity.CompoundFormula; |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 25 | import android.content.integrity.IntegrityFormula; |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 26 | import android.content.integrity.Rule; |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 27 | import android.util.Xml; |
| 28 | |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 29 | import org.xmlpull.v1.XmlSerializer; |
Khaled Abdelmohsen | 7c56df6 | 2019-10-31 16:59:43 +0000 | [diff] [blame] | 30 | |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 31 | import java.io.IOException; |
| 32 | import java.io.OutputStream; |
| 33 | import java.io.StringWriter; |
| 34 | import java.nio.charset.StandardCharsets; |
| 35 | import java.util.List; |
Omer Nebil Yaveroglu | a00ba07 | 2019-12-20 17:25:40 +0000 | [diff] [blame] | 36 | import java.util.Map; |
Khaled Abdelmohsen | 47483d5 | 2019-11-28 12:28:44 +0000 | [diff] [blame] | 37 | import java.util.Optional; |
Omer Nebil Yaveroglu | 8d46f34 | 2020-01-13 15:28:11 +0000 | [diff] [blame] | 38 | import java.util.stream.Collectors; |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 39 | |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 40 | /** A helper class to serialize rules from the {@link Rule} model to Xml representation. */ |
Khaled Abdelmohsen | 7c56df6 | 2019-10-31 16:59:43 +0000 | [diff] [blame] | 41 | public class RuleXmlSerializer implements RuleSerializer { |
| 42 | |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 43 | public static final String TAG = "RuleXmlSerializer"; |
| 44 | private static final String NAMESPACE = ""; |
| 45 | |
Khaled Abdelmohsen | 6d77f94 | 2019-11-12 16:47:01 +0000 | [diff] [blame] | 46 | private static final String RULE_LIST_TAG = "RL"; |
| 47 | private static final String RULE_TAG = "R"; |
Khaled Abdelmohsen | 1fa4de5 | 2019-11-13 15:13:19 +0000 | [diff] [blame] | 48 | private static final String COMPOUND_FORMULA_TAG = "OF"; |
Khaled Abdelmohsen | 6d77f94 | 2019-11-12 16:47:01 +0000 | [diff] [blame] | 49 | private static final String ATOMIC_FORMULA_TAG = "AF"; |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 50 | private static final String EFFECT_ATTRIBUTE = "E"; |
| 51 | private static final String KEY_ATTRIBUTE = "K"; |
| 52 | private static final String OPERATOR_ATTRIBUTE = "O"; |
| 53 | private static final String VALUE_ATTRIBUTE = "V"; |
| 54 | private static final String CONNECTOR_ATTRIBUTE = "C"; |
Khaled Abdelmohsen | 1efff87 | 2019-11-25 16:44:20 +0000 | [diff] [blame] | 55 | private static final String IS_HASHED_VALUE_ATTRIBUTE = "H"; |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 56 | |
Khaled Abdelmohsen | 7c56df6 | 2019-10-31 16:59:43 +0000 | [diff] [blame] | 57 | @Override |
Khaled Abdelmohsen | 47483d5 | 2019-11-28 12:28:44 +0000 | [diff] [blame] | 58 | public void serialize( |
Omer Nebil Yaveroglu | 2935a21 | 2020-01-03 14:33:51 +0000 | [diff] [blame] | 59 | List<Rule> rules, |
| 60 | Optional<Integer> formatVersion, |
| 61 | OutputStream outputStream, |
| 62 | OutputStream indexingOutputStream) |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 63 | throws RuleSerializeException { |
| 64 | try { |
| 65 | XmlSerializer xmlSerializer = Xml.newSerializer(); |
| 66 | xmlSerializer.setOutput(outputStream, StandardCharsets.UTF_8.name()); |
| 67 | serializeRules(rules, xmlSerializer); |
Omer Nebil Yaveroglu | 2935a21 | 2020-01-03 14:33:51 +0000 | [diff] [blame] | 68 | |
| 69 | // TODO(b/145493956): Implement the indexing logic. |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 70 | } catch (Exception e) { |
| 71 | throw new RuleSerializeException(e.getMessage(), e); |
| 72 | } |
Khaled Abdelmohsen | 7c56df6 | 2019-10-31 16:59:43 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
Khaled Abdelmohsen | 52d9341 | 2019-12-04 16:19:28 +0000 | [diff] [blame] | 76 | public byte[] serialize(List<Rule> rules, Optional<Integer> formatVersion) |
Khaled Abdelmohsen | 47483d5 | 2019-11-28 12:28:44 +0000 | [diff] [blame] | 77 | throws RuleSerializeException { |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 78 | try { |
| 79 | XmlSerializer xmlSerializer = Xml.newSerializer(); |
| 80 | StringWriter writer = new StringWriter(); |
| 81 | xmlSerializer.setOutput(writer); |
| 82 | serializeRules(rules, xmlSerializer); |
Khaled Abdelmohsen | 52d9341 | 2019-12-04 16:19:28 +0000 | [diff] [blame] | 83 | return writer.toString().getBytes(StandardCharsets.UTF_8); |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 84 | } catch (Exception e) { |
| 85 | throw new RuleSerializeException(e.getMessage(), e); |
| 86 | } |
| 87 | } |
| 88 | |
Omer Nebil Yaveroglu | a00ba07 | 2019-12-20 17:25:40 +0000 | [diff] [blame] | 89 | private void serializeRules(List<Rule> rules, XmlSerializer xmlSerializer) |
| 90 | throws RuleSerializeException { |
| 91 | try { |
| 92 | // Determine the indexing groups and the order of the rules within each indexed group. |
Omer Nebil Yaveroglu | 8d46f34 | 2020-01-13 15:28:11 +0000 | [diff] [blame] | 93 | Map<Integer, Map<String, List<Rule>>> indexedRules = |
Omer Nebil Yaveroglu | a00ba07 | 2019-12-20 17:25:40 +0000 | [diff] [blame] | 94 | RuleIndexingDetailsIdentifier.splitRulesIntoIndexBuckets(rules); |
| 95 | |
| 96 | // Write the XML formatted rules in order. |
| 97 | xmlSerializer.startTag(NAMESPACE, RULE_LIST_TAG); |
| 98 | |
| 99 | serializeRuleList(indexedRules.get(PACKAGE_NAME_INDEXED), xmlSerializer); |
| 100 | serializeRuleList(indexedRules.get(APP_CERTIFICATE_INDEXED), xmlSerializer); |
| 101 | serializeRuleList(indexedRules.get(NOT_INDEXED), xmlSerializer); |
| 102 | |
| 103 | xmlSerializer.endTag(NAMESPACE, RULE_LIST_TAG); |
| 104 | xmlSerializer.endDocument(); |
| 105 | } catch (Exception e) { |
| 106 | throw new RuleSerializeException(e.getMessage(), e); |
| 107 | } |
| 108 | } |
| 109 | |
Omer Nebil Yaveroglu | 8d46f34 | 2020-01-13 15:28:11 +0000 | [diff] [blame] | 110 | private void serializeRuleList(Map<String, List<Rule>> rulesMap, XmlSerializer xmlSerializer) |
Omer Nebil Yaveroglu | a00ba07 | 2019-12-20 17:25:40 +0000 | [diff] [blame] | 111 | throws IOException { |
Omer Nebil Yaveroglu | 8d46f34 | 2020-01-13 15:28:11 +0000 | [diff] [blame] | 112 | List<String> sortedKeyList = |
| 113 | rulesMap.keySet().stream().sorted().collect(Collectors.toList()); |
| 114 | for (String key : sortedKeyList) { |
| 115 | for (Rule rule : rulesMap.get(key)) { |
Omer Nebil Yaveroglu | d562ee0 | 2020-01-02 16:20:02 +0000 | [diff] [blame] | 116 | serializeRule(rule, xmlSerializer); |
| 117 | } |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 118 | } |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Khaled Abdelmohsen | 1fa4de5 | 2019-11-13 15:13:19 +0000 | [diff] [blame] | 121 | private void serializeRule(Rule rule, XmlSerializer xmlSerializer) throws IOException { |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 122 | if (rule == null) { |
| 123 | return; |
| 124 | } |
| 125 | xmlSerializer.startTag(NAMESPACE, RULE_TAG); |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 126 | serializeAttributeValue(EFFECT_ATTRIBUTE, String.valueOf(rule.getEffect()), xmlSerializer); |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 127 | serializeFormula(rule.getFormula(), xmlSerializer); |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 128 | xmlSerializer.endTag(NAMESPACE, RULE_TAG); |
| 129 | } |
| 130 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 131 | private void serializeFormula(IntegrityFormula formula, XmlSerializer xmlSerializer) |
| 132 | throws IOException { |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 133 | if (formula instanceof AtomicFormula) { |
| 134 | serializeAtomicFormula((AtomicFormula) formula, xmlSerializer); |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 135 | } else if (formula instanceof CompoundFormula) { |
Khaled Abdelmohsen | 1fa4de5 | 2019-11-13 15:13:19 +0000 | [diff] [blame] | 136 | serializeCompoundFormula((CompoundFormula) formula, xmlSerializer); |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 137 | } else { |
| 138 | throw new IllegalArgumentException( |
| 139 | String.format("Invalid formula type: %s", formula.getClass())); |
| 140 | } |
| 141 | } |
| 142 | |
Khaled Abdelmohsen | 1fa4de5 | 2019-11-13 15:13:19 +0000 | [diff] [blame] | 143 | private void serializeCompoundFormula( |
| 144 | CompoundFormula compoundFormula, XmlSerializer xmlSerializer) throws IOException { |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 145 | if (compoundFormula == null) { |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 146 | return; |
| 147 | } |
Khaled Abdelmohsen | 1fa4de5 | 2019-11-13 15:13:19 +0000 | [diff] [blame] | 148 | xmlSerializer.startTag(NAMESPACE, COMPOUND_FORMULA_TAG); |
Khaled Abdelmohsen | 47483d5 | 2019-11-28 12:28:44 +0000 | [diff] [blame] | 149 | serializeAttributeValue( |
| 150 | CONNECTOR_ATTRIBUTE, String.valueOf(compoundFormula.getConnector()), xmlSerializer); |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 151 | for (IntegrityFormula formula : compoundFormula.getFormulas()) { |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 152 | serializeFormula(formula, xmlSerializer); |
| 153 | } |
Khaled Abdelmohsen | 1fa4de5 | 2019-11-13 15:13:19 +0000 | [diff] [blame] | 154 | xmlSerializer.endTag(NAMESPACE, COMPOUND_FORMULA_TAG); |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private void serializeAtomicFormula(AtomicFormula atomicFormula, XmlSerializer xmlSerializer) |
| 158 | throws IOException { |
| 159 | if (atomicFormula == null) { |
| 160 | return; |
| 161 | } |
| 162 | xmlSerializer.startTag(NAMESPACE, ATOMIC_FORMULA_TAG); |
Khaled Abdelmohsen | 47483d5 | 2019-11-28 12:28:44 +0000 | [diff] [blame] | 163 | serializeAttributeValue( |
| 164 | KEY_ATTRIBUTE, String.valueOf(atomicFormula.getKey()), xmlSerializer); |
Khaled Abdelmohsen | 8bc377c | 2019-12-09 13:08:37 +0000 | [diff] [blame] | 165 | if (atomicFormula.getTag() == AtomicFormula.STRING_ATOMIC_FORMULA_TAG) { |
Khaled Abdelmohsen | 47483d5 | 2019-11-28 12:28:44 +0000 | [diff] [blame] | 166 | serializeAttributeValue( |
| 167 | VALUE_ATTRIBUTE, |
| 168 | ((AtomicFormula.StringAtomicFormula) atomicFormula).getValue(), |
| 169 | xmlSerializer); |
| 170 | serializeAttributeValue( |
| 171 | IS_HASHED_VALUE_ATTRIBUTE, |
Khaled Abdelmohsen | 1efff87 | 2019-11-25 16:44:20 +0000 | [diff] [blame] | 172 | String.valueOf( |
| 173 | ((AtomicFormula.StringAtomicFormula) atomicFormula).getIsHashedValue()), |
| 174 | xmlSerializer); |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 175 | } else if (atomicFormula.getTag() == AtomicFormula.LONG_ATOMIC_FORMULA_TAG) { |
Khaled Abdelmohsen | 47483d5 | 2019-11-28 12:28:44 +0000 | [diff] [blame] | 176 | serializeAttributeValue( |
| 177 | OPERATOR_ATTRIBUTE, |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 178 | String.valueOf(((AtomicFormula.LongAtomicFormula) atomicFormula).getOperator()), |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 179 | xmlSerializer); |
Khaled Abdelmohsen | 47483d5 | 2019-11-28 12:28:44 +0000 | [diff] [blame] | 180 | serializeAttributeValue( |
| 181 | VALUE_ATTRIBUTE, |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 182 | String.valueOf(((AtomicFormula.LongAtomicFormula) atomicFormula).getValue()), |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 183 | xmlSerializer); |
Khaled Abdelmohsen | 8bc377c | 2019-12-09 13:08:37 +0000 | [diff] [blame] | 184 | } else if (atomicFormula.getTag() == AtomicFormula.BOOLEAN_ATOMIC_FORMULA_TAG) { |
Khaled Abdelmohsen | 47483d5 | 2019-11-28 12:28:44 +0000 | [diff] [blame] | 185 | serializeAttributeValue( |
| 186 | VALUE_ATTRIBUTE, |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 187 | String.valueOf(((AtomicFormula.BooleanAtomicFormula) atomicFormula).getValue()), |
| 188 | xmlSerializer); |
| 189 | } else { |
| 190 | throw new IllegalArgumentException( |
| 191 | String.format("Invalid atomic formula type: %s", atomicFormula.getClass())); |
| 192 | } |
| 193 | xmlSerializer.endTag(NAMESPACE, ATOMIC_FORMULA_TAG); |
| 194 | } |
| 195 | |
Khaled Abdelmohsen | 47483d5 | 2019-11-28 12:28:44 +0000 | [diff] [blame] | 196 | private void serializeAttributeValue( |
| 197 | String attribute, String value, XmlSerializer xmlSerializer) throws IOException { |
Khaled Abdelmohsen | 0efe7cb | 2019-11-08 22:18:54 +0000 | [diff] [blame] | 198 | if (value == null) { |
| 199 | return; |
| 200 | } |
Khaled Abdelmohsen | b05d706 | 2019-11-19 01:22:23 +0000 | [diff] [blame] | 201 | xmlSerializer.attribute(NAMESPACE, attribute, value); |
Khaled Abdelmohsen | 7c56df6 | 2019-10-31 16:59:43 +0000 | [diff] [blame] | 202 | } |
| 203 | } |