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 | |
| 21 | #include "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 { |
| 26 | |
| 27 | const std::string |
| 28 | CaMemory::STORAGE_TYPE = "ca-storage-memory"; |
| 29 | |
| 30 | NDNCERT_REGISTER_CA_STORAGE(CaMemory); |
| 31 | |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 32 | CaMemory::CaMemory(const Name& caName, const std::string& path) |
| 33 | : CaStorage() |
| 34 | { |
| 35 | } |
| 36 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 37 | CaState |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 38 | CaMemory::getRequest(const RequestID& requestId) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 39 | { |
| 40 | auto search = m_requests.find(requestId); |
| 41 | if (search == m_requests.end()) { |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 42 | 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] | 43 | } |
| 44 | return search->second; |
| 45 | } |
| 46 | |
| 47 | void |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 48 | CaMemory::addRequest(const CaState& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 49 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 50 | auto search = m_requests.find(request.m_requestId); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 51 | if (search == m_requests.end()) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 52 | m_requests[request.m_requestId] = request; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 53 | } |
| 54 | else { |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 55 | NDN_THROW(std::runtime_error("Request " + toHex(request.m_requestId.data(), request.m_requestId.size()) + " already exists")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | void |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 60 | CaMemory::updateRequest(const CaState& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 61 | { |
tylerliu | 09a00fd | 2020-10-04 11:11:18 -0700 | [diff] [blame] | 62 | m_requests[request.m_requestId].m_status = request.m_status; |
| 63 | m_requests[request.m_requestId].m_challengeState = request.m_challengeState; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
Zhiyi Zhang | 8fdb36b | 2020-10-18 11:58:51 -0700 | [diff] [blame] | 67 | CaMemory::deleteRequest(const RequestID& requestId) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 68 | { |
| 69 | auto search = m_requests.find(requestId); |
tylerliu | 09a00fd | 2020-10-04 11:11:18 -0700 | [diff] [blame] | 70 | auto keyName = search->second.m_cert.getKeyName(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 71 | if (search != m_requests.end()) { |
| 72 | m_requests.erase(search); |
| 73 | } |
| 74 | } |
| 75 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 76 | std::list<CaState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 77 | CaMemory::listAllRequests() |
| 78 | { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 79 | std::list<CaState> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 80 | for (const auto& entry : m_requests) { |
| 81 | result.push_back(entry.second); |
| 82 | } |
| 83 | return result; |
| 84 | } |
| 85 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 86 | std::list<CaState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 87 | CaMemory::listAllRequests(const Name& caName) |
| 88 | { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 89 | std::list<CaState> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 90 | for (const auto& entry : m_requests) { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 91 | if (entry.second.m_caPrefix == caName) { |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 92 | result.push_back(entry.second); |
| 93 | } |
| 94 | } |
| 95 | return result; |
| 96 | } |
| 97 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 98 | } // namespace ndncert |
| 99 | } // namespace ndn |