blob: 6341c7678fa08af83f75423e324edd0c8a10d31f [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
20#include <set>
21#include <string>
22#include <unordered_map>
23
24namespace keystore {
25
26/**
27 * Grant represents a mapping from an alias to a key file.
28 * Normally, key file names are derived from the alias chosen by the client
29 * and the clients UID, to generate a per client name space.
30 * Grants allow assotiating a key file with a new name, thereby making
31 * it visible in another client's - the grantee's - namespace.
32 */
33class Grant {
34public:
Janis Danisevskisd3024ed2017-09-01 13:24:23 -070035 Grant(const std::string& alias, const std::string& owner_dir_name, const uid_t owner_uid,
36 const uint64_t grant_no);
Janis Danisevskisf9f55452017-09-21 11:29:47 -070037 // the following three field are used to recover the key filename that the grant refers to
38 std::string alias_; ///< original/wrapped key alias
39 std::string owner_dir_name_; ///< key owner key directory
40 uid_t owner_uid_; ///< key owner uid
41
42 uint64_t grant_no_; ///< numeric grant identifier - randomly assigned
Janis Danisevskis6d449e82017-06-07 18:03:31 -070043
44 operator const uint64_t&() const { return grant_no_; }
45};
46
47/**
48 * The GrantStore holds a set of sets of Grants. One set of Grants for each grantee.
49 * The uid parameter to each of the GrantStore function determines the grantee's
50 * name space. The methods put, get, and removeByAlias/ByFileName create, lookup, and
51 * remove a Grant, respectively.
52 * put also returns a new alias for the newly granted key which has to be returned
53 * to the granter. The grantee, and only the grantee, can use the granted key
54 * by this new alias.
55 */
56class GrantStore {
57public:
58 GrantStore() : grants_() {}
Janis Danisevskisd3024ed2017-09-01 13:24:23 -070059 std::string put(const uid_t uid, const std::string& alias, const std::string& owner_dir_name,
60 const uid_t owner_uid);
Janis Danisevskis6d449e82017-06-07 18:03:31 -070061 const Grant* get(const uid_t uid, const std::string& alias) const;
Janis Danisevskisf9f55452017-09-21 11:29:47 -070062 bool removeByFileAlias(const uid_t granteeUid, const uid_t granterUid, const std::string& alias);
63 void removeAllGrantsToKey(const uid_t granterUid, const std::string& alias);
64 void removeAllGrantsToUid(const uid_t granteeUid);
Janis Danisevskis6d449e82017-06-07 18:03:31 -070065
66 // GrantStore is neither copyable nor movable.
67 GrantStore(const GrantStore&) = delete;
68 GrantStore& operator=(const GrantStore&) = delete;
69private:
70 std::unordered_map<uid_t, std::set<Grant, std::less<>>> grants_;
71};
72
73} // namespace keystore
74
75#endif // KEYSTORE_GRANT_STORE_H_