blob: 437afa4bcb8612093f851fcc6f5fa09d922674fd [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
33CertificateRequest
34CaMemory::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
44CaMemory::addRequest(const CertificateRequest& request)
45{
Zhiyi Zhang1bc23462017-04-12 14:16:09 -070046 for (auto& entry : m_requests) {
47 const auto& existingRequest = entry.second;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070048 if (existingRequest.m_cert.getKeyName() == request.m_cert.getKeyName()) {
49 BOOST_THROW_EXCEPTION(Error("Request for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang1bc23462017-04-12 14:16:09 -070050 return;
51 }
52 }
53 for (auto& entry : m_issuedCerts) {
54 const auto& cert = entry.second;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070055 if (cert.getKeyName() == request.m_cert.getKeyName()) {
56 BOOST_THROW_EXCEPTION(Error("Cert for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang1bc23462017-04-12 14:16:09 -070057 return;
58 }
59 }
60
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070061 auto search = m_requests.find(request.m_requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080062 if (search == m_requests.end()) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070063 m_requests[request.m_requestId] = request;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080064 }
65 else {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070066 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080067 }
68}
69
70void
71CaMemory::updateRequest(const CertificateRequest& request)
72{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070073 m_requests[request.m_requestId] = request;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080074}
75
76void
77CaMemory::deleteRequest(const std::string& requestId)
78{
79 auto search = m_requests.find(requestId);
80 if (search != m_requests.end()) {
81 m_requests.erase(search);
82 }
83}
84
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070085std::list<CertificateRequest>
86CaMemory::listAllRequests()
87{
88 std::list<CertificateRequest> result;
89 for (const auto& entry : m_requests) {
90 result.push_back(entry.second);
91 }
92 return result;
93}
94
95std::list<CertificateRequest>
96CaMemory::listAllRequests(const Name& caName)
97{
98 std::list<CertificateRequest> result;
99 for (const auto& entry : m_requests) {
Suyong Won256c9062020-05-11 02:45:56 -0700100 if (entry.second.m_caPrefix == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700101 result.push_back(entry.second);
102 }
103 }
104 return result;
105}
106
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800107// certificate related
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700108security::v2::Certificate
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800109CaMemory::getCertificate(const std::string& certId)
110{
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800111 auto search = m_issuedCerts.find(certId);
112 if (search != m_issuedCerts.end()) {
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400113 return search->second;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800114 }
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400115 BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " does not exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800116}
117
118void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700119CaMemory::addCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800120{
121 auto search = m_issuedCerts.find(certId);
122 if (search == m_issuedCerts.end()) {
123 m_issuedCerts[certId] = cert;
124 }
125 else {
Zhiyi Zhang1bc23462017-04-12 14:16:09 -0700126 BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800127 }
128}
129
130void
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700131CaMemory::updateCertificate(const std::string& certId, const security::v2::Certificate& cert)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800132{
Zhiyi Zhang1bc23462017-04-12 14:16:09 -0700133 m_issuedCerts[certId] = cert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800134}
135
136void
137CaMemory::deleteCertificate(const std::string& certId)
138{
139 auto search = m_issuedCerts.find(certId);
140 if (search != m_issuedCerts.end()) {
141 m_issuedCerts.erase(search);
142 }
143}
144
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700145std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700146CaMemory::listAllIssuedCertificates()
147{
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700148 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700149 for (const auto& entry : m_issuedCerts) {
150 result.push_back(entry.second);
151 }
152 return result;
153}
154
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700155std::list<security::v2::Certificate>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700156CaMemory::listAllIssuedCertificates(const Name& caName)
157{
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700158 std::list<security::v2::Certificate> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700159 for (const auto& entry : m_issuedCerts) {
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700160 const auto& klName = entry.second.getSignature().getKeyLocator().getName();
161 if (security::v2::extractIdentityFromKeyName(klName) == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700162 result.push_back(entry.second);
163 }
164 }
165 return result;
166}
167
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800168} // namespace ndncert
169} // namespace ndn