blob: 14a6f87c17a70984c6fd65a63a253ab187701418 [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{
Zhiyi Zhang1bc23462017-04-12 14:16:09 -070044 for (auto& entry : m_requests) {
45 const auto& existingRequest = entry.second;
46 if (existingRequest.getCert().getKeyName() == request.getCert().getKeyName()) {
47 BOOST_THROW_EXCEPTION(Error("Request for " + request.getCert().getKeyName().toUri() + " already exists"));
48 return;
49 }
50 }
51 for (auto& entry : m_issuedCerts) {
52 const auto& cert = entry.second;
53 if (cert.getKeyName() == request.getCert().getKeyName()) {
54 BOOST_THROW_EXCEPTION(Error("Cert for " + request.getCert().getKeyName().toUri() + " already exists"));
55 return;
56 }
57 }
58
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080059 auto search = m_requests.find(request.getRequestId());
60 if (search == m_requests.end()) {
61 m_requests[request.getRequestId()] = request;
62 }
63 else {
64 BOOST_THROW_EXCEPTION(Error("Request " + request.getRequestId() + " already exists"));
65 }
66}
67
68void
69CaMemory::updateRequest(const CertificateRequest& request)
70{
71 m_requests[request.getRequestId()] = request;
72}
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
83// certificate related
84security::v2::Certificate
85CaMemory::getCertificate(const std::string& certId)
86{
87 security::v2::Certificate cert;
88 auto search = m_issuedCerts.find(certId);
89 if (search != m_issuedCerts.end()) {
90 cert = search->second;
Zhiyi Zhang1bc23462017-04-12 14:16:09 -070091 return cert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080092 }
Zhiyi Zhang1bc23462017-04-12 14:16:09 -070093 else {
94 BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " does not exists"));
95 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080096}
97
98void
99CaMemory::addCertificate(const std::string& certId, const security::v2::Certificate& cert)
100{
101 auto search = m_issuedCerts.find(certId);
102 if (search == m_issuedCerts.end()) {
103 m_issuedCerts[certId] = cert;
104 }
105 else {
Zhiyi Zhang1bc23462017-04-12 14:16:09 -0700106 BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800107 }
108}
109
110void
111CaMemory::updateCertificate(const std::string& certId, const security::v2::Certificate& cert)
112{
Zhiyi Zhang1bc23462017-04-12 14:16:09 -0700113 m_issuedCerts[certId] = cert;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800114}
115
116void
117CaMemory::deleteCertificate(const std::string& certId)
118{
119 auto search = m_issuedCerts.find(certId);
120 if (search != m_issuedCerts.end()) {
121 m_issuedCerts.erase(search);
122 }
123}
124
125} // namespace ndncert
126} // namespace ndn