blob: 4fe74c4459212cefd3a7f9fc5455f79500eb2937 [file] [log] [blame]
Yi Jin99c248f2017-08-25 18:11:58 -07001/*
2 * Copyright (C) 2017 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#include "Privacy.h"
18
Yi Jinbdf58942017-11-14 17:58:19 -080019#include <android/os/IncidentReportArgs.h>
Yi Jin7e0b4e52017-09-12 20:00:25 -070020#include <stdlib.h>
21
Yi Jin6cacbcb2018-03-30 14:04:52 -070022namespace android {
23namespace os {
24namespace incidentd {
25
Joe Onorato99598ee2019-02-11 15:55:13 +000026using namespace android::os;
27
Yao Chen35be60e2019-04-25 11:52:10 -070028static const bool kEncryptionEnabled = false;
29
Yi Jinbdf58942017-11-14 17:58:19 -080030uint64_t encode_field_id(const Privacy* p) { return (uint64_t)p->type << 32 | p->field_id; }
Yi Jin99c248f2017-08-25 18:11:58 -070031
Yi Jinb592e3b2018-02-01 15:17:04 -080032const Privacy* lookup(const Privacy* p, uint32_t fieldId) {
Yi Jinbdf58942017-11-14 17:58:19 -080033 if (p->children == NULL) return NULL;
Yi Jinb592e3b2018-02-01 15:17:04 -080034 for (int i = 0; p->children[i] != NULL; i++) { // NULL-terminated.
Yi Jinbdf58942017-11-14 17:58:19 -080035 if (p->children[i]->field_id == fieldId) return p->children[i];
36 // Incident section gen tool guarantees field ids in ascending order.
37 if (p->children[i]->field_id > fieldId) return NULL;
Yi Jin99c248f2017-08-25 18:11:58 -070038 }
39 return NULL;
40}
41
Yao Chen35be60e2019-04-25 11:52:10 -070042bool sectionEncryption(int section_id) {
43 return kEncryptionEnabled ? (section_id == 3025) /*restricted image section*/ : false;
44}
Yao Chen43706b42019-04-21 14:34:30 -070045
Joe Onorato99598ee2019-02-11 15:55:13 +000046static bool isAllowed(const uint8_t policy, const uint8_t check) {
47 switch (check) {
48 case PRIVACY_POLICY_LOCAL:
49 return policy == PRIVACY_POLICY_LOCAL;
50 case PRIVACY_POLICY_EXPLICIT:
51 case PRIVACY_POLICY_UNSET:
52 return policy == PRIVACY_POLICY_LOCAL
53 || policy == PRIVACY_POLICY_EXPLICIT
54 || policy == PRIVACY_POLICY_UNSET;
55 case PRIVACY_POLICY_AUTOMATIC:
Yi Jinb592e3b2018-02-01 15:17:04 -080056 return true;
57 default:
58 return false;
Yi Jin99c248f2017-08-25 18:11:58 -070059 }
60}
61
Joe Onorato99598ee2019-02-11 15:55:13 +000062PrivacySpec::PrivacySpec(uint8_t argPolicy) {
63 // TODO: Why on earth do we have two definitions of policy. Maybe
64 // it's not too late to clean this up.
65 switch (argPolicy) {
66 case android::os::PRIVACY_POLICY_AUTOMATIC:
67 case android::os::PRIVACY_POLICY_EXPLICIT:
68 case android::os::PRIVACY_POLICY_LOCAL:
69 mPolicy = argPolicy;
70 break;
71 default:
72 mPolicy = android::os::PRIVACY_POLICY_AUTOMATIC;
73 break;
74 }
Yi Jin99c248f2017-08-25 18:11:58 -070075}
76
Joe Onorato99598ee2019-02-11 15:55:13 +000077bool PrivacySpec::operator<(const PrivacySpec& that) const {
78 return mPolicy < that.mPolicy;
79}
Yi Jin99c248f2017-08-25 18:11:58 -070080
Joe Onorato99598ee2019-02-11 15:55:13 +000081bool PrivacySpec::CheckPremission(const Privacy* privacy, const uint8_t defaultDest) const {
82 uint8_t check = privacy != NULL ? privacy->policy : defaultDest;
83 return isAllowed(mPolicy, check);
84}
85
86bool PrivacySpec::RequireAll() const {
87 return mPolicy == android::os::PRIVACY_POLICY_LOCAL;
Yi Jin0f047162017-09-05 13:44:22 -070088}
Yi Jin6cacbcb2018-03-30 14:04:52 -070089
90} // namespace incidentd
91} // namespace os
92} // namespace android