blob: ab03630eb53a020e0471c7667dc5c090a371f1cb [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 Danisevskis6d449e82017-06-07 18:03:31 -070037 std::string alias_;
Janis Danisevskisd3024ed2017-09-01 13:24:23 -070038 std::string owner_dir_name_;
39 uid_t owner_uid_;
Janis Danisevskis6d449e82017-06-07 18:03:31 -070040 uint64_t grant_no_;
41
42 operator const uint64_t&() const { return grant_no_; }
43};
44
45/**
46 * The GrantStore holds a set of sets of Grants. One set of Grants for each grantee.
47 * The uid parameter to each of the GrantStore function determines the grantee's
48 * name space. The methods put, get, and removeByAlias/ByFileName create, lookup, and
49 * remove a Grant, respectively.
50 * put also returns a new alias for the newly granted key which has to be returned
51 * to the granter. The grantee, and only the grantee, can use the granted key
52 * by this new alias.
53 */
54class GrantStore {
55public:
56 GrantStore() : grants_() {}
Janis Danisevskisd3024ed2017-09-01 13:24:23 -070057 std::string put(const uid_t uid, const std::string& alias, const std::string& owner_dir_name,
58 const uid_t owner_uid);
Janis Danisevskis6d449e82017-06-07 18:03:31 -070059 const Grant* get(const uid_t uid, const std::string& alias) const;
Janis Danisevskisd3024ed2017-09-01 13:24:23 -070060 bool removeByFileAlias(const uid_t uid, const std::string& alias);
Janis Danisevskis6d449e82017-06-07 18:03:31 -070061
62 // GrantStore is neither copyable nor movable.
63 GrantStore(const GrantStore&) = delete;
64 GrantStore& operator=(const GrantStore&) = delete;
65private:
66 std::unordered_map<uid_t, std::set<Grant, std::less<>>> grants_;
67};
68
69} // namespace keystore
70
71#endif // KEYSTORE_GRANT_STORE_H_