blob: 893801e85ec4d6edd314239911f4da68634b9325 [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
Zhiyi Zhangb041d442020-10-22 21:57:11 -070021#include "detail/ca-memory.hpp"
tylerliua7bea662020-10-08 18:51:02 -070022#include <ndn-cxx/security/validation-policy.hpp>
Davide Pesaventob48bbda2020-07-27 19:41:37 -040023
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080024namespace ndn {
25namespace ndncert {
26
27const std::string
28CaMemory::STORAGE_TYPE = "ca-storage-memory";
29
30NDNCERT_REGISTER_CA_STORAGE(CaMemory);
31
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070032CaMemory::CaMemory(const Name& caName, const std::string& path)
33 : CaStorage()
34{
35}
36
tylerliu8704d032020-06-23 10:18:15 -070037CaState
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070038CaMemory::getRequest(const RequestID& requestId)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080039{
40 auto search = m_requests.find(requestId);
41 if (search == m_requests.end()) {
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070042 NDN_THROW(std::runtime_error("Request " + toHex(requestId.data(), requestId.size()) + " doest not exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080043 }
44 return search->second;
45}
46
47void
tylerliu8704d032020-06-23 10:18:15 -070048CaMemory::addRequest(const CaState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080049{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070050 auto search = m_requests.find(request.m_requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080051 if (search == m_requests.end()) {
Zhiyi Zhang84207c52020-10-28 21:45:44 -070052 m_requests.insert(std::make_pair(request.m_requestId, request));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080053 }
54 else {
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070055 NDN_THROW(std::runtime_error("Request " + toHex(request.m_requestId.data(), request.m_requestId.size()) + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080056 }
57}
58
59void
tylerliu8704d032020-06-23 10:18:15 -070060CaMemory::updateRequest(const CaState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080061{
Zhiyi Zhang84207c52020-10-28 21:45:44 -070062 auto search = m_requests.find(request.m_requestId);
63 if (search == m_requests.end()) {
64 m_requests.insert(std::make_pair(request.m_requestId, request));
65 }
66 else {
67 search->second = request;
68 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080069}
70
71void
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070072CaMemory::deleteRequest(const RequestID& requestId)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080073{
74 auto search = m_requests.find(requestId);
tylerliu09a00fd2020-10-04 11:11:18 -070075 auto keyName = search->second.m_cert.getKeyName();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080076 if (search != m_requests.end()) {
77 m_requests.erase(search);
78 }
79}
80
tylerliu8704d032020-06-23 10:18:15 -070081std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070082CaMemory::listAllRequests()
83{
tylerliu8704d032020-06-23 10:18:15 -070084 std::list<CaState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070085 for (const auto& entry : m_requests) {
86 result.push_back(entry.second);
87 }
88 return result;
89}
90
tylerliu8704d032020-06-23 10:18:15 -070091std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070092CaMemory::listAllRequests(const Name& caName)
93{
tylerliu8704d032020-06-23 10:18:15 -070094 std::list<CaState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070095 for (const auto& entry : m_requests) {
Suyong Won256c9062020-05-11 02:45:56 -070096 if (entry.second.m_caPrefix == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070097 result.push_back(entry.second);
98 }
99 }
100 return result;
101}
102
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800103} // namespace ndncert
104} // namespace ndn