blob: 9e627ce4efa8653f2deb96a568a0b067ae1700d5 [file] [log] [blame]
Janis Danisevskis6d449e82017-06-07 18:03:31 -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 "grant_store.h"
18
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070019#include "blob.h"
Janis Danisevskis6d449e82017-06-07 18:03:31 -070020#include <algorithm>
21#include <sstream>
22
23namespace keystore {
24
25static constexpr uint64_t kInvalidGrantNo = std::numeric_limits<uint64_t>::max();
26static const char* kKeystoreGrantInfix = "_KEYSTOREGRANT_";
27static constexpr size_t kKeystoreGrantInfixLength = 15;
28
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070029Grant::Grant(const KeyBlobEntry& entry, const uint64_t grant_no)
30 : entry_(entry), grant_no_(grant_no) {}
Janis Danisevskis6d449e82017-06-07 18:03:31 -070031
32static std::pair<uint64_t, std::string> parseGrantAlias(const std::string& grantAlias) {
33 auto pos = grantAlias.rfind(kKeystoreGrantInfix);
34 if (pos == std::string::npos) return {kInvalidGrantNo, ""};
35 std::stringstream s(grantAlias.substr(pos + kKeystoreGrantInfixLength));
36 std::string wrapped_alias = grantAlias.substr(0, pos);
37 uint64_t grant_no = kInvalidGrantNo;
38 s >> grant_no;
39 if (s.fail() || grant_no == kInvalidGrantNo) return {kInvalidGrantNo, ""};
40 return {grant_no, wrapped_alias};
41}
42
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070043std::string GrantStore::put(const uid_t uid, const LockedKeyBlobEntry& lockedEntry) {
44 std::unique_lock<std::shared_mutex> lock(mutex_);
Janis Danisevskis6d449e82017-06-07 18:03:31 -070045 std::stringstream s;
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070046 KeyBlobEntry blobEntry = *lockedEntry;
47 s << blobEntry.alias() << kKeystoreGrantInfix;
48
49 std::set<Grant, std::less<>>& uid_grant_list = grants_[uid];
Janis Danisevskis6d449e82017-06-07 18:03:31 -070050
51 bool success = false;
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070052 auto iterator =
53 std::find_if(uid_grant_list.begin(), uid_grant_list.end(),
54 [&](const Grant& entry) { return success = entry.entry_ == blobEntry; });
Janis Danisevskis6d449e82017-06-07 18:03:31 -070055 while (!success) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070056 std::tie(iterator, success) = uid_grant_list.emplace(blobEntry, std::rand());
Janis Danisevskis6d449e82017-06-07 18:03:31 -070057 }
58 s << iterator->grant_no_;
59 return s.str();
60}
61
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070062ReadLockedGrant GrantStore::get(const uid_t uid, const std::string& alias) const {
63 std::shared_lock<std::shared_mutex> lock(mutex_);
Janis Danisevskis6d449e82017-06-07 18:03:31 -070064 uint64_t grant_no;
65 std::string wrappedAlias;
66 std::tie(grant_no, wrappedAlias) = parseGrantAlias(alias);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070067 if (grant_no == kInvalidGrantNo) return {};
Janis Danisevskis6d449e82017-06-07 18:03:31 -070068 auto uid_set_iter = grants_.find(uid);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070069 if (uid_set_iter == grants_.end()) return {};
Janis Danisevskis6d449e82017-06-07 18:03:31 -070070 auto& uid_grant_list = uid_set_iter->second;
71 auto grant = uid_grant_list.find(grant_no);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070072 if (grant == uid_grant_list.end()) return {};
73 if (grant->entry_.alias() != wrappedAlias) return {};
74 return {&(*grant), std::move(lock)};
Janis Danisevskis6d449e82017-06-07 18:03:31 -070075}
76
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070077bool GrantStore::removeByFileAlias(const uid_t granteeUid, const LockedKeyBlobEntry& lockedEntry) {
78 std::unique_lock<std::shared_mutex> lock(mutex_);
Janis Danisevskisf9f55452017-09-21 11:29:47 -070079 auto& uid_grant_list = grants_[granteeUid];
Janis Danisevskis6d449e82017-06-07 18:03:31 -070080 for (auto i = uid_grant_list.begin(); i != uid_grant_list.end(); ++i) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070081 if (i->entry_ == *lockedEntry) {
Janis Danisevskis6d449e82017-06-07 18:03:31 -070082 uid_grant_list.erase(i);
83 return true;
84 }
85 }
86 return false;
87}
88
Janis Danisevskisf9f55452017-09-21 11:29:47 -070089void GrantStore::removeAllGrantsToKey(const uid_t granterUid, const std::string& alias) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070090 std::unique_lock<std::shared_mutex> lock(mutex_);
Janis Danisevskisf9f55452017-09-21 11:29:47 -070091 for (auto& uid_grant_list : grants_) {
92 for (auto i = uid_grant_list.second.begin(); i != uid_grant_list.second.end(); ++i) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070093 if (i->entry_.alias() == alias && i->entry_.uid() == granterUid) {
Janis Danisevskisf9f55452017-09-21 11:29:47 -070094 uid_grant_list.second.erase(i);
95 break;
96 }
97 }
98 }
99}
100
101void GrantStore::removeAllGrantsToUid(const uid_t granteeUid) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700102 std::unique_lock<std::shared_mutex> lock(mutex_);
Janis Danisevskisf9f55452017-09-21 11:29:47 -0700103 grants_.erase(granteeUid);
104}
105
Janis Danisevskis6d449e82017-06-07 18:03:31 -0700106} // namespace keystore