blob: 48a09037accbccb802e06688369a123953f62205 [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 {
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070026namespace ca {
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080027
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
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070038RequestState
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070039CaMemory::getRequest(const RequestId& requestId)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080040{
41 auto search = m_requests.find(requestId);
42 if (search == m_requests.end()) {
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070043 NDN_THROW(std::runtime_error("Request " + toHex(requestId.data(), requestId.size()) + " doest not exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080044 }
45 return search->second;
46}
47
48void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070049CaMemory::addRequest(const RequestState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080050{
tylerliu7b9185c2020-11-24 12:15:18 -080051 auto search = m_requests.find(request.requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080052 if (search == m_requests.end()) {
tylerliu7b9185c2020-11-24 12:15:18 -080053 m_requests.insert(std::make_pair(request.requestId, request));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080054 }
55 else {
tylerliu7b9185c2020-11-24 12:15:18 -080056 NDN_THROW(std::runtime_error("Request " + toHex(request.requestId.data(), request.requestId.size()) + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080057 }
58}
59
60void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070061CaMemory::updateRequest(const RequestState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080062{
tylerliu7b9185c2020-11-24 12:15:18 -080063 auto search = m_requests.find(request.requestId);
Zhiyi Zhang84207c52020-10-28 21:45:44 -070064 if (search == m_requests.end()) {
tylerliu7b9185c2020-11-24 12:15:18 -080065 m_requests.insert(std::make_pair(request.requestId, request));
Zhiyi Zhang84207c52020-10-28 21:45:44 -070066 }
67 else {
68 search->second = request;
69 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080070}
71
72void
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070073CaMemory::deleteRequest(const RequestId& requestId)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080074{
75 auto search = m_requests.find(requestId);
tylerliu7b9185c2020-11-24 12:15:18 -080076 auto keyName = search->second.cert.getKeyName();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080077 if (search != m_requests.end()) {
78 m_requests.erase(search);
79 }
80}
81
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070082std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070083CaMemory::listAllRequests()
84{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070085 std::list<RequestState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070086 for (const auto& entry : m_requests) {
87 result.push_back(entry.second);
88 }
89 return result;
90}
91
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070092std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070093CaMemory::listAllRequests(const Name& caName)
94{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070095 std::list<RequestState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070096 for (const auto& entry : m_requests) {
tylerliu7b9185c2020-11-24 12:15:18 -080097 if (entry.second.caPrefix == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070098 result.push_back(entry.second);
99 }
100 }
101 return result;
102}
103
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700104} // namespace ca
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800105} // namespace ndncert
106} // namespace ndn