blob: f8e4f918d2e696e55451e4ad54c35cc4257b1283 [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
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070051 operator const uint64_t&() const { return grant_no_; }
Janis Danisevskis6d449e82017-06-07 18:03:31 -070052};
53
54/**
55 * The GrantStore holds a set of sets of Grants. One set of Grants for each grantee.
56 * The uid parameter to each of the GrantStore function determines the grantee's
57 * name space. The methods put, get, and removeByAlias/ByFileName create, lookup, and
58 * remove a Grant, respectively.
59 * put also returns a new alias for the newly granted key which has to be returned
60 * to the granter. The grantee, and only the grantee, can use the granted key
61 * by this new alias.
62 */
63class GrantStore {
64public:
65 GrantStore() : grants_() {}
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070066 std::string put(const uid_t uid, const LockedKeyBlobEntry& blobfile);
67 ReadLockedGrant get(const uid_t uid, const std::string& alias) const;
68 bool removeByFileAlias(const uid_t granteeUid, const LockedKeyBlobEntry& lockedEntry);
Janis Danisevskisf9f55452017-09-21 11:29:47 -070069 void removeAllGrantsToKey(const uid_t granterUid, const std::string& alias);
70 void removeAllGrantsToUid(const uid_t granteeUid);
Janis Danisevskis6d449e82017-06-07 18:03:31 -070071
72 // GrantStore is neither copyable nor movable.
73 GrantStore(const GrantStore&) = delete;
74 GrantStore& operator=(const GrantStore&) = delete;
75private:
76 std::unordered_map<uid_t, std::set<Grant, std::less<>>> grants_;
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070077 mutable std::shared_mutex mutex_;
Janis Danisevskis6d449e82017-06-07 18:03:31 -070078};
79
80} // namespace keystore
81
82#endif // KEYSTORE_GRANT_STORE_H_