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 | #include "grant_store.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <sstream> |
| 21 | |
| 22 | namespace keystore { |
| 23 | |
| 24 | static constexpr uint64_t kInvalidGrantNo = std::numeric_limits<uint64_t>::max(); |
| 25 | static const char* kKeystoreGrantInfix = "_KEYSTOREGRANT_"; |
| 26 | static constexpr size_t kKeystoreGrantInfixLength = 15; |
| 27 | |
| 28 | Grant::Grant(const std::string& alias, const std::string& key_file, const uint64_t grant_no) |
| 29 | : alias_(alias), key_file_(key_file), grant_no_(grant_no) {} |
| 30 | |
| 31 | static std::pair<uint64_t, std::string> parseGrantAlias(const std::string& grantAlias) { |
| 32 | auto pos = grantAlias.rfind(kKeystoreGrantInfix); |
| 33 | if (pos == std::string::npos) return {kInvalidGrantNo, ""}; |
| 34 | std::stringstream s(grantAlias.substr(pos + kKeystoreGrantInfixLength)); |
| 35 | std::string wrapped_alias = grantAlias.substr(0, pos); |
| 36 | uint64_t grant_no = kInvalidGrantNo; |
| 37 | s >> grant_no; |
| 38 | if (s.fail() || grant_no == kInvalidGrantNo) return {kInvalidGrantNo, ""}; |
| 39 | return {grant_no, wrapped_alias}; |
| 40 | } |
| 41 | |
| 42 | std::string GrantStore::put(const uid_t uid, const std::string& alias, const std::string& key_file) { |
| 43 | std::stringstream s; |
| 44 | s << alias << kKeystoreGrantInfix; |
| 45 | auto& uid_grant_list = grants_[uid]; |
| 46 | |
| 47 | bool success = false; |
| 48 | auto iterator = std::find_if(uid_grant_list.begin(), uid_grant_list.end(), |
| 49 | [&](auto& entry) { |
| 50 | return success = entry.alias_ == alias && entry.key_file_ == key_file; |
| 51 | }); |
| 52 | while (!success) { |
| 53 | std::tie(iterator, success) = uid_grant_list.emplace(alias, key_file, std::rand()); |
| 54 | } |
| 55 | s << iterator->grant_no_; |
| 56 | return s.str(); |
| 57 | } |
| 58 | |
| 59 | const Grant* GrantStore::get(const uid_t uid, const std::string& alias) const { |
| 60 | uint64_t grant_no; |
| 61 | std::string wrappedAlias; |
| 62 | std::tie(grant_no, wrappedAlias) = parseGrantAlias(alias); |
| 63 | if (grant_no == kInvalidGrantNo) return nullptr; |
| 64 | auto uid_set_iter = grants_.find(uid); |
| 65 | if (uid_set_iter == grants_.end()) return nullptr; |
| 66 | auto& uid_grant_list = uid_set_iter->second; |
| 67 | auto grant = uid_grant_list.find(grant_no); |
| 68 | if (grant == uid_grant_list.end()) return nullptr; |
| 69 | if (grant->alias_ != wrappedAlias) return nullptr; |
| 70 | return &(*grant); |
| 71 | } |
| 72 | |
| 73 | bool GrantStore::removeByFileName(const uid_t uid, const std::string& fileName) { |
| 74 | auto& uid_grant_list = grants_.operator[](uid); |
| 75 | for (auto i = uid_grant_list.begin(); i != uid_grant_list.end(); ++i) { |
| 76 | if (i->key_file_ == fileName) { |
| 77 | uid_grant_list.erase(i); |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | } // namespace keystore |