blob: 1baf32c33057e75c6bee83ba12de5a2937948560 [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#ifndef KEYSTORE_GRANT_STORE_H_
18#define KEYSTORE_GRANT_STORE_H_
19
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070020#include <mutex>
Janis Danisevskis6d449e82017-06-07 18:03:31 -070021#include <set>
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070022#include <shared_mutex>
Janis Danisevskis6d449e82017-06-07 18:03:31 -070023#include <string>
24#include <unordered_map>
25
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070026#include <keystore/keystore_concurrency.h>
27
28#include "blob.h"
29
Janis Danisevskis6d449e82017-06-07 18:03:31 -070030namespace keystore {
31
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070032class Grant;
33
34using ReadLockedGrant =
35 ProxyLock<MutexProxyLockHelper<const Grant, std::shared_mutex, std::shared_lock>>;
36
Janis Danisevskis6d449e82017-06-07 18:03:31 -070037/**
38 * Grant represents a mapping from an alias to a key file.
39 * Normally, key file names are derived from the alias chosen by the client
40 * and the clients UID, to generate a per client name space.
41 * Grants allow assotiating a key file with a new name, thereby making
42 * it visible in another client's - the grantee's - namespace.
43 */
44class Grant {
45public:
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070046 Grant(const KeyBlobEntry& entry, const uint64_t grant_no);
47 KeyBlobEntry entry_;
Janis Danisevskisf9f55452017-09-21 11:29:47 -070048
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070049 uint64_t grant_no_; ///< numeric grant identifier - randomly assigned
Janis Danisevskis6d449e82017-06-07 18:03:31 -070050
Chih-Hung Hsieh4fa39ef2019-01-04 13:34:17 -080051 // NOLINTNEXTLINE(google-explicit-constructor)
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070052 operator const uint64_t&() const { return grant_no_; }
Janis Danisevskis6d449e82017-06-07 18:03:31 -070053};
54
55/**
56 * The GrantStore holds a set of sets of Grants. One set of Grants for each grantee.
57 * The uid parameter to each of the GrantStore function determines the grantee's
58 * name space. The methods put, get, and removeByAlias/ByFileName create, lookup, and
59 * remove a Grant, respectively.
60 * put also returns a new alias for the newly granted key which has to be returned
61 * to the granter. The grantee, and only the grantee, can use the granted key
62 * by this new alias.
63 */
64class GrantStore {
65public:
66 GrantStore() : grants_() {}
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070067 std::string put(const uid_t uid, const LockedKeyBlobEntry& blobfile);
68 ReadLockedGrant get(const uid_t uid, const std::string& alias) const;
69 bool removeByFileAlias(const uid_t granteeUid, const LockedKeyBlobEntry& lockedEntry);
Janis Danisevskisf9f55452017-09-21 11:29:47 -070070 void removeAllGrantsToKey(const uid_t granterUid, const std::string& alias);
71 void removeAllGrantsToUid(const uid_t granteeUid);
Janis Danisevskis6d449e82017-06-07 18:03:31 -070072
73 // GrantStore is neither copyable nor movable.
74 GrantStore(const GrantStore&) = delete;
75 GrantStore& operator=(const GrantStore&) = delete;
76private:
77 std::unordered_map<uid_t, std::set<Grant, std::less<>>> grants_;
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070078 mutable std::shared_mutex mutex_;
Janis Danisevskis6d449e82017-06-07 18:03:31 -070079};
80
81} // namespace keystore
82
83#endif // KEYSTORE_GRANT_STORE_H_