blob: 5adaa21b85e3e352f42a358fe8d46837b30daa05 [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
tylerliu8704d032020-06-23 10:18:15 -070033CaState
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080034CaMemory::getRequest(const std::string& requestId)
35{
36 auto search = m_requests.find(requestId);
37 if (search == m_requests.end()) {
38 BOOST_THROW_EXCEPTION(Error("Request " + requestId + " doest not exists"));
39 }
40 return search->second;
41}
42
43void
tylerliu8704d032020-06-23 10:18:15 -070044CaMemory::addRequest(const CaState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080045{
tylerliu09a00fd2020-10-04 11:11:18 -070046 auto keyNameTLV = request.m_cert.getKeyName();
47 if (request.m_requestType == RequestType::NEW) {
48 if (m_requestKeyIndex.find(keyNameTLV) != m_requestKeyIndex.end()
49 && !m_requestKeyIndex.find(keyNameTLV)->second.empty()){
50 BOOST_THROW_EXCEPTION(Error("Request for " + keyNameTLV.toUri() + " already exists"));
51 return;
52 }
53 if (m_certsKeyIndex.find(keyNameTLV) != m_certsKeyIndex.end()) {
54 BOOST_THROW_EXCEPTION(Error("Cert for " + keyNameTLV.toUri() + " already exists"));
55 return;
56 }
Zhiyi Zhang1bc23462017-04-12 14:16:09 -070057 }
Zhiyi Zhang1bc23462017-04-12 14:16:09 -070058
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070059 auto search = m_requests.find(request.m_requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080060 if (search == m_requests.end()) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070061 m_requests[request.m_requestId] = request;
tylerliu09a00fd2020-10-04 11:11:18 -070062 m_requestKeyIndex[keyNameTLV].insert(request.m_requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080063 }
64 else {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070065 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080066 }
67}
68
69void
tylerliu8704d032020-06-23 10:18:15 -070070CaMemory::updateRequest(const CaState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080071{
tylerliu09a00fd2020-10-04 11:11:18 -070072 m_requests[request.m_requestId].m_status = request.m_status;
73 m_requests[request.m_requestId].m_challengeState = request.m_challengeState;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080074}
75
76void
77CaMemory::deleteRequest(const std::string& requestId)
78{
79 auto search = m_requests.find(requestId);
tylerliu09a00fd2020-10-04 11:11:18 -070080 auto keyName = search->second.m_cert.getKeyName();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080081 if (search != m_requests.end()) {
tylerliu09a00fd2020-10-04 11:11:18 -070082 m_requestKeyIndex.find(keyName)->second.erase(requestId);
83 if (m_requestKeyIndex.find(keyName)->second.empty()) m_requestKeyIndex.erase(keyName);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080084 m_requests.erase(search);
85 }
86}
87
tylerliu8704d032020-06-23 10:18:15 -070088std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070089CaMemory::listAllRequests()
90{
tylerliu8704d032020-06-23 10:18:15 -070091 std::list<CaState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070092 for (const auto& entry : m_requests) {
93 result.push_back(entry.second);
94 }
95 return result;
96}
97
tylerliu8704d032020-06-23 10:18:15 -070098std::list<CaState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070099CaMemory::listAllRequests(const Name& caName)
100{
tylerliu8704d032020-06-23 10:18:15 -0700101 std::list<CaState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700102 for (const auto& entry : m_requests) {
Suyong Won256c9062020-05-11 02:45:56 -0700103 if (entry.second.m_caPrefix == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700104 result.push_back(entry.second);
105 }
106 }
107 return result;
108}
109
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800110// certificate related
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700111security::v2::Certificate
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800112CaMemory::getCertificate(const std::string& certId)
113{
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800114 auto search = m_issuedCerts.find(certId);
115 if (search != m_issuedCerts.end()) {
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400116 return search->second;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800117 }
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400118 BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " does not exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800119}
120
121void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700122CaMemory::addCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800123{
124 auto search = m_issuedCerts.find(certId);
125 if (search == m_issuedCerts.end()) {
126 m_issuedCerts[certId] = cert;
tylerliu09a00fd2020-10-04 11:11:18 -0700127 m_certsKeyIndex[cert.getKeyName()] = certId;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800128 }
129 else {
Zhiyi Zhang1bc23462017-04-12 14:16:09 -0700130 BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800131 }
132}
133
134void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700135CaMemory::updateCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800136{
Zhiyi Zhang1bc23462017-04-12 14:16:09 -0700137 m_issuedCerts[certId] = cert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800138}
139
140void
141CaMemory::deleteCertificate(const std::string& certId)
142{
143 auto search = m_issuedCerts.find(certId);
144 if (search != m_issuedCerts.end()) {
tylerliu09a00fd2020-10-04 11:11:18 -0700145 m_certsKeyIndex.erase(search->second.getKeyName());
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800146 m_issuedCerts.erase(search);
147 }
148}
149
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700150std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700151CaMemory::listAllIssuedCertificates()
152{
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700153 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700154 for (const auto& entry : m_issuedCerts) {
155 result.push_back(entry.second);
156 }
157 return result;
158}
159
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700160std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700161CaMemory::listAllIssuedCertificates(const Name& caName)
162{
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700163 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700164 for (const auto& entry : m_issuedCerts) {
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700165 const auto& klName = entry.second.getSignature().getKeyLocator().getName();
166 if (security::v2::extractIdentityFromKeyName(klName) == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700167 result.push_back(entry.second);
168 }
169 }
170 return result;
171}
172
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800173} // namespace ndncert
174} // namespace ndn