blob: ce5bd98ff9daed23ed75accb5d052277bda84436 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, 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
23namespace ndn {
24namespace ndncert {
25
26const std::string
27CaMemory::STORAGE_TYPE = "ca-storage-memory";
28
29NDNCERT_REGISTER_CA_STORAGE(CaMemory);
30
31CertificateRequest
32CaMemory::getRequest(const std::string& requestId)
33{
34 auto search = m_requests.find(requestId);
35 if (search == m_requests.end()) {
36 BOOST_THROW_EXCEPTION(Error("Request " + requestId + " doest not exists"));
37 }
38 return search->second;
39}
40
41void
42CaMemory::addRequest(const CertificateRequest& request)
43{
Zhiyi Zhang1bc23462017-04-12 14:16:09 -070044 for (auto& entry : m_requests) {
45 const auto& existingRequest = entry.second;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070046 if (existingRequest.m_cert.getKeyName() == request.m_cert.getKeyName()) {
47 BOOST_THROW_EXCEPTION(Error("Request for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang1bc23462017-04-12 14:16:09 -070048 return;
49 }
50 }
51 for (auto& entry : m_issuedCerts) {
52 const auto& cert = entry.second;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070053 if (cert.getKeyName() == request.m_cert.getKeyName()) {
54 BOOST_THROW_EXCEPTION(Error("Cert for " + request.m_cert.getKeyName().toUri() + " already exists"));
Zhiyi Zhang1bc23462017-04-12 14:16:09 -070055 return;
56 }
57 }
58
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;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080062 }
63 else {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070064 BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080065 }
66}
67
68void
69CaMemory::updateRequest(const CertificateRequest& request)
70{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070071 m_requests[request.m_requestId] = request;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080072}
73
74void
75CaMemory::deleteRequest(const std::string& requestId)
76{
77 auto search = m_requests.find(requestId);
78 if (search != m_requests.end()) {
79 m_requests.erase(search);
80 }
81}
82
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070083std::list<CertificateRequest>
84CaMemory::listAllRequests()
85{
86 std::list<CertificateRequest> result;
87 for (const auto& entry : m_requests) {
88 result.push_back(entry.second);
89 }
90 return result;
91}
92
93std::list<CertificateRequest>
94CaMemory::listAllRequests(const Name& caName)
95{
96 std::list<CertificateRequest> result;
97 for (const auto& entry : m_requests) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070098 if (entry.second.m_caName == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070099 result.push_back(entry.second);
100 }
101 }
102 return result;
103}
104
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800105// certificate related
106security::v2::Certificate
107CaMemory::getCertificate(const std::string& certId)
108{
109 security::v2::Certificate cert;
110 auto search = m_issuedCerts.find(certId);
111 if (search != m_issuedCerts.end()) {
112 cert = search->second;
Zhiyi Zhang1bc23462017-04-12 14:16:09 -0700113 return cert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800114 }
Zhiyi Zhang1bc23462017-04-12 14:16:09 -0700115 else {
116 BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " does not exists"));
117 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800118}
119
120void
121CaMemory::addCertificate(const std::string& certId, const security::v2::Certificate& cert)
122{
123 auto search = m_issuedCerts.find(certId);
124 if (search == m_issuedCerts.end()) {
125 m_issuedCerts[certId] = cert;
126 }
127 else {
Zhiyi Zhang1bc23462017-04-12 14:16:09 -0700128 BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800129 }
130}
131
132void
133CaMemory::updateCertificate(const std::string& certId, const security::v2::Certificate& cert)
134{
Zhiyi Zhang1bc23462017-04-12 14:16:09 -0700135 m_issuedCerts[certId] = cert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800136}
137
138void
139CaMemory::deleteCertificate(const std::string& certId)
140{
141 auto search = m_issuedCerts.find(certId);
142 if (search != m_issuedCerts.end()) {
143 m_issuedCerts.erase(search);
144 }
145}
146
Zhiyi Zhangae123bf2017-04-14 12:24:53 -0700147std::list<security::v2::Certificate>
148CaMemory::listAllIssuedCertificates()
149{
150 std::list<security::v2::Certificate> result;
151 for (const auto& entry : m_issuedCerts) {
152 result.push_back(entry.second);
153 }
154 return result;
155}
156
157std::list<security::v2::Certificate>
158CaMemory::listAllIssuedCertificates(const Name& caName)
159{
160 std::list<security::v2::Certificate> result;
161 for (const auto& entry : m_issuedCerts) {
162 if (entry.second.getSignature().getKeyLocator().getName().getPrefix(-2) == caName) {
163 result.push_back(entry.second);
164 }
165 }
166 return result;
167}
168
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800169} // namespace ndncert
170} // namespace ndn