Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
Zhiyi Zhang | b041d44 | 2020-10-22 21:57:11 -0700 | [diff] [blame] | 21 | #include "detail/ca-memory.hpp" |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 22 | #include <ndn-cxx/security/validation-policy.hpp> |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 23 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 24 | namespace ndn { |
| 25 | namespace ndncert { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 26 | namespace ca { |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 27 | |
| 28 | const std::string |
| 29 | CaMemory::STORAGE_TYPE = "ca-storage-memory"; |
| 30 | |
| 31 | NDNCERT_REGISTER_CA_STORAGE(CaMemory); |
| 32 | |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 33 | CaMemory::CaMemory(const Name& caName, const std::string& path) |
| 34 | : CaStorage() |
| 35 | { |
| 36 | } |
| 37 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 38 | RequestState |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 39 | CaMemory::getRequest(const RequestId& requestId) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 40 | { |
| 41 | auto search = m_requests.find(requestId); |
| 42 | if (search == m_requests.end()) { |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 43 | NDN_THROW(std::runtime_error("Request " + toHex(requestId.data(), requestId.size()) + " doest not exists")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 44 | } |
| 45 | return search->second; |
| 46 | } |
| 47 | |
| 48 | void |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 49 | CaMemory::addRequest(const RequestState& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 50 | { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 51 | auto search = m_requests.find(request.requestId); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 52 | if (search == m_requests.end()) { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 53 | m_requests.insert(std::make_pair(request.requestId, request)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 54 | } |
| 55 | else { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 56 | NDN_THROW(std::runtime_error("Request " + toHex(request.requestId.data(), request.requestId.size()) + " already exists")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | void |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 61 | CaMemory::updateRequest(const RequestState& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 62 | { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 63 | auto search = m_requests.find(request.requestId); |
Zhiyi Zhang | 84207c5 | 2020-10-28 21:45:44 -0700 | [diff] [blame] | 64 | if (search == m_requests.end()) { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 65 | m_requests.insert(std::make_pair(request.requestId, request)); |
Zhiyi Zhang | 84207c5 | 2020-10-28 21:45:44 -0700 | [diff] [blame] | 66 | } |
| 67 | else { |
| 68 | search->second = request; |
| 69 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void |
Zhiyi Zhang | c9ada1b | 2020-10-29 19:13:15 -0700 | [diff] [blame] | 73 | CaMemory::deleteRequest(const RequestId& requestId) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 74 | { |
| 75 | auto search = m_requests.find(requestId); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 76 | auto keyName = search->second.cert.getKeyName(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 77 | if (search != m_requests.end()) { |
| 78 | m_requests.erase(search); |
| 79 | } |
| 80 | } |
| 81 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 82 | std::list<RequestState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 83 | CaMemory::listAllRequests() |
| 84 | { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 85 | std::list<RequestState> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 86 | for (const auto& entry : m_requests) { |
| 87 | result.push_back(entry.second); |
| 88 | } |
| 89 | return result; |
| 90 | } |
| 91 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 92 | std::list<RequestState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 93 | CaMemory::listAllRequests(const Name& caName) |
| 94 | { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 95 | std::list<RequestState> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 96 | for (const auto& entry : m_requests) { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 97 | if (entry.second.caPrefix == caName) { |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 98 | result.push_back(entry.second); |
| 99 | } |
| 100 | } |
| 101 | return result; |
| 102 | } |
| 103 | |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 104 | } // namespace ca |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 105 | } // namespace ndncert |
| 106 | } // namespace ndn |