grant_store.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef KEYSTORE_GRANT_STORE_H_
  17. #define KEYSTORE_GRANT_STORE_H_
  18. #include <mutex>
  19. #include <set>
  20. #include <shared_mutex>
  21. #include <string>
  22. #include <unordered_map>
  23. #include <keystore/keystore_concurrency.h>
  24. #include "blob.h"
  25. namespace keystore {
  26. class Grant;
  27. using ReadLockedGrant =
  28. ProxyLock<MutexProxyLockHelper<const Grant, std::shared_mutex, std::shared_lock>>;
  29. /**
  30. * Grant represents a mapping from an alias to a key file.
  31. * Normally, key file names are derived from the alias chosen by the client
  32. * and the clients UID, to generate a per client name space.
  33. * Grants allow assotiating a key file with a new name, thereby making
  34. * it visible in another client's - the grantee's - namespace.
  35. */
  36. class Grant {
  37. public:
  38. Grant(const KeyBlobEntry& entry, const uint64_t grant_no);
  39. KeyBlobEntry entry_;
  40. uint64_t grant_no_; ///< numeric grant identifier - randomly assigned
  41. // NOLINTNEXTLINE(google-explicit-constructor)
  42. operator const uint64_t&() const { return grant_no_; }
  43. };
  44. /**
  45. * The GrantStore holds a set of sets of Grants. One set of Grants for each grantee.
  46. * The uid parameter to each of the GrantStore function determines the grantee's
  47. * name space. The methods put, get, and removeByAlias/ByFileName create, lookup, and
  48. * remove a Grant, respectively.
  49. * put also returns a new alias for the newly granted key which has to be returned
  50. * to the granter. The grantee, and only the grantee, can use the granted key
  51. * by this new alias.
  52. */
  53. class GrantStore {
  54. public:
  55. GrantStore() : grants_() {}
  56. std::string put(const uid_t uid, const LockedKeyBlobEntry& blobfile);
  57. ReadLockedGrant get(const uid_t uid, const std::string& alias) const;
  58. bool removeByFileAlias(const uid_t granteeUid, const LockedKeyBlobEntry& lockedEntry);
  59. void removeAllGrantsToKey(const uid_t granterUid, const std::string& alias);
  60. void removeAllGrantsToUid(const uid_t granteeUid);
  61. // GrantStore is neither copyable nor movable.
  62. GrantStore(const GrantStore&) = delete;
  63. GrantStore& operator=(const GrantStore&) = delete;
  64. private:
  65. std::unordered_map<uid_t, std::set<Grant, std::less<>>> grants_;
  66. mutable std::shared_mutex mutex_;
  67. };
  68. } // namespace keystore
  69. #endif // KEYSTORE_GRANT_STORE_H_