blob: bde895d27e88b76faa7c616f74c815001961ddb0 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017, Regents of the University of California.
4 *
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{
44 auto search = m_requests.find(request.getRequestId());
45 if (search == m_requests.end()) {
46 m_requests[request.getRequestId()] = request;
47 }
48 else {
49 BOOST_THROW_EXCEPTION(Error("Request " + request.getRequestId() + " already exists"));
50 }
51}
52
53void
54CaMemory::updateRequest(const CertificateRequest& request)
55{
56 m_requests[request.getRequestId()] = request;
57}
58
59void
60CaMemory::deleteRequest(const std::string& requestId)
61{
62 auto search = m_requests.find(requestId);
63 if (search != m_requests.end()) {
64 m_requests.erase(search);
65 }
66}
67
68// certificate related
69security::v2::Certificate
70CaMemory::getCertificate(const std::string& certId)
71{
72 security::v2::Certificate cert;
73 auto search = m_issuedCerts.find(certId);
74 if (search != m_issuedCerts.end()) {
75 cert = search->second;
76 }
77 return cert;
78}
79
80void
81CaMemory::addCertificate(const std::string& certId, const security::v2::Certificate& cert)
82{
83 auto search = m_issuedCerts.find(certId);
84 if (search == m_issuedCerts.end()) {
85 m_issuedCerts[certId] = cert;
86 }
87 else {
88 BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " does not exists"));
89 }
90}
91
92void
93CaMemory::updateCertificate(const std::string& certId, const security::v2::Certificate& cert)
94{
95 auto search = m_issuedCerts.find(certId);
96 if (search == m_issuedCerts.end()) {
97 m_issuedCerts[certId] = cert;
98 }
99 else {
100 BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " does not exists"));
101 }
102}
103
104void
105CaMemory::deleteCertificate(const std::string& certId)
106{
107 auto search = m_issuedCerts.find(certId);
108 if (search != m_issuedCerts.end()) {
109 m_issuedCerts.erase(search);
110 }
111}
112
113} // namespace ndncert
114} // namespace ndn