Janis Danisevskis | 6d449e8 | 2017-06-07 18:03:31 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 24 | namespace 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 | */ |
| 33 | class Grant { |
| 34 | public: |
| 35 | Grant(const std::string& alias, const std::string& key_file, const uint64_t grant_no); |
| 36 | std::string alias_; |
| 37 | std::string key_file_; |
| 38 | uint64_t grant_no_; |
| 39 | |
| 40 | operator const uint64_t&() const { return grant_no_; } |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * The GrantStore holds a set of sets of Grants. One set of Grants for each grantee. |
| 45 | * The uid parameter to each of the GrantStore function determines the grantee's |
| 46 | * name space. The methods put, get, and removeByAlias/ByFileName create, lookup, and |
| 47 | * remove a Grant, respectively. |
| 48 | * put also returns a new alias for the newly granted key which has to be returned |
| 49 | * to the granter. The grantee, and only the grantee, can use the granted key |
| 50 | * by this new alias. |
| 51 | */ |
| 52 | class GrantStore { |
| 53 | public: |
| 54 | GrantStore() : grants_() {} |
| 55 | std::string put(const uid_t uid, const std::string& alias, const std::string& key_file); |
| 56 | const Grant* get(const uid_t uid, const std::string& alias) const; |
| 57 | bool removeByFileName(const uid_t uid, const std::string& filename); |
| 58 | |
| 59 | // GrantStore is neither copyable nor movable. |
| 60 | GrantStore(const GrantStore&) = delete; |
| 61 | GrantStore& operator=(const GrantStore&) = delete; |
| 62 | private: |
| 63 | std::unordered_map<uid_t, std::set<Grant, std::less<>>> grants_; |
| 64 | }; |
| 65 | |
| 66 | } // namespace keystore |
| 67 | |
| 68 | #endif // KEYSTORE_GRANT_STORE_H_ |