blob: 1fe27e14f7fd0b467bd36216e3ef989e24abf0e1 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob48bbda2020-07-27 19:41:37 -04002/*
3 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08004 *
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"
22
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -070023#include <ndn-cxx/security/v2/validation-policy.hpp>
Davide Pesaventob48bbda2020-07-27 19:41:37 -040024
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080025namespace ndn {
26namespace ndncert {
27
28const std::string
29CaMemory::STORAGE_TYPE = "ca-storage-memory";
30
31NDNCERT_REGISTER_CA_STORAGE(CaMemory);
32
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070033CaMemory::CaMemory(const Name& caName, const std::string& path)
34 : CaStorage()
35{
36}
37
tylerliu8704d032020-06-23 10:18:15 -070038CaState
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080039CaMemory::getRequest(const std::string& requestId)
40{
41 auto search = m_requests.find(requestId);
42 if (search == m_requests.end()) {
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070043 BOOST_THROW_EXCEPTION(std::runtime_error("Request " + requestId + " doest not exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080044 }
45 return search->second;
46}
47
48void
tylerliu8704d032020-06-23 10:18:15 -070049CaMemory::addRequest(const CaState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080050{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070051 auto search = m_requests.find(request.m_requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080052 if (search == m_requests.end()) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070053 m_requests[request.m_requestId] = request;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080054 }
55 else {
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070056 BOOST_THROW_EXCEPTION(std::runtime_error("Request " + request.m_requestId + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080057 }
58}
59
60void
tylerliu8704d032020-06-23 10:18:15 -070061CaMemory::updateRequest(const CaState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080062{
tylerliu09a00fd2020-10-04 11:11:18 -070063 m_requests[request.m_requestId].m_status = request.m_status;
64 m_requests[request.m_requestId].m_challengeState = request.m_challengeState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080065}
66
67void
68CaMemory::deleteRequest(const std::string& requestId)
69{
70 auto search = m_requests.find(requestId);
tylerliu09a00fd2020-10-04 11:11:18 -070071 auto keyName = search->second.m_cert.getKeyName();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080072 if (search != m_requests.end()) {
73 m_requests.erase(search);
74 }
75}
76
tylerliu8704d032020-06-23 10:18:15 -070077std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070078CaMemory::listAllRequests()
79{
tylerliu8704d032020-06-23 10:18:15 -070080 std::list<CaState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070081 for (const auto& entry : m_requests) {
82 result.push_back(entry.second);
83 }
84 return result;
85}
86
tylerliu8704d032020-06-23 10:18:15 -070087std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070088CaMemory::listAllRequests(const Name& caName)
89{
tylerliu8704d032020-06-23 10:18:15 -070090 std::list<CaState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070091 for (const auto& entry : m_requests) {
Suyong Won256c9062020-05-11 02:45:56 -070092 if (entry.second.m_caPrefix == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070093 result.push_back(entry.second);
94 }
95 }
96 return result;
97}
98
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080099} // namespace ndncert
100} // namespace ndn