blob: 4d3961df6092852a2961ae747a585a8a85594fc2 [file] [log] [blame]
Omer Nebil Yaverogluee1db3f2019-12-10 12:18:03 +00001/*
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
17package com.android.server.integrity.serializer;
18
19import android.annotation.IntDef;
20import android.content.integrity.AtomicFormula;
21import android.content.integrity.CompoundFormula;
22import android.content.integrity.Formula;
23import android.content.integrity.Rule;
24
25import java.lang.annotation.Retention;
26import java.lang.annotation.RetentionPolicy;
27import java.util.List;
28import java.util.Set;
29import java.util.stream.Collectors;
30
31/** A helper class for identifying the indexing type of a given rule. */
32public class RuleIndexTypeIdentifier {
33
34 static final int NOT_INDEXED = 0;
35 static final int PACKAGE_NAME_INDEXED = 1;
36 static final int APP_CERTIFICATE_INDEXED = 2;
37
38 /** Represents which indexed file the rule should be located. */
39 @IntDef(
40 value = {
41 NOT_INDEXED,
42 PACKAGE_NAME_INDEXED,
43 APP_CERTIFICATE_INDEXED
44 })
45 @Retention(RetentionPolicy.SOURCE)
46 public @interface IndexType {
47 }
48
49 /** Determines the indexing file type that a given rule should be located at. */
50 public static int getIndexType(Rule rule) {
51 if (rule == null) {
52 throw new IllegalArgumentException("Indexing type cannot be determined for null rule.");
53 }
54 return getIndexType(rule.getFormula());
55 }
56
57 private static int getIndexType(Formula formula) {
58 if (formula == null) {
59 throw new IllegalArgumentException(
60 "Indexing type cannot be determined for null formula.");
61 }
62
63 switch (formula.getTag()) {
64 case Formula.COMPOUND_FORMULA_TAG:
65 return getIndexTypeForCompoundFormula((CompoundFormula) formula);
66 case Formula.STRING_ATOMIC_FORMULA_TAG:
67 return getIndexTypeForAtomicStringFormula((AtomicFormula) formula);
68 case Formula.INT_ATOMIC_FORMULA_TAG:
69 case Formula.BOOLEAN_ATOMIC_FORMULA_TAG:
70 // Package name and app certificate related formulas are string atomic formulas.
71 return NOT_INDEXED;
72 default:
73 throw new IllegalArgumentException(
74 String.format("Invalid formula tag type: %s", formula.getTag()));
75 }
76 }
77
78 private static int getIndexTypeForCompoundFormula(CompoundFormula compoundFormula) {
79 int connector = compoundFormula.getConnector();
80 List<Formula> formulas = compoundFormula.getFormulas();
81
82 switch (connector) {
83 case CompoundFormula.NOT:
84 // Having a NOT operator in the indexing messes up the indexing; e.g., deny
85 // installation if app certificate is NOT X (should not be indexed with app cert
86 // X). We will not keep these rules indexed.
87 return NOT_INDEXED;
88 case CompoundFormula.AND:
89 case CompoundFormula.OR:
90 Set<Integer> indexingTypesForAllFormulas =
91 formulas.stream()
92 .map(formula -> getIndexType(formula))
93 .collect(Collectors.toSet());
94 if (indexingTypesForAllFormulas.contains(PACKAGE_NAME_INDEXED)) {
95 return PACKAGE_NAME_INDEXED;
96 } else if (indexingTypesForAllFormulas.contains(APP_CERTIFICATE_INDEXED)) {
97 return APP_CERTIFICATE_INDEXED;
98 } else {
99 return NOT_INDEXED;
100 }
101 default:
102 return NOT_INDEXED;
103 }
104 }
105
106 private static int getIndexTypeForAtomicStringFormula(AtomicFormula atomicFormula) {
107 switch (atomicFormula.getKey()) {
108 case AtomicFormula.PACKAGE_NAME:
109 return PACKAGE_NAME_INDEXED;
110 case AtomicFormula.APP_CERTIFICATE:
111 return APP_CERTIFICATE_INDEXED;
112 default:
113 return NOT_INDEXED;
114 }
115 }
116}
117