Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 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 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 23 | #include <ndn-cxx/security/v2/validation-policy.hpp> |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 24 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 25 | namespace ndn { |
| 26 | namespace ndncert { |
| 27 | |
| 28 | const std::string |
| 29 | CaMemory::STORAGE_TYPE = "ca-storage-memory"; |
| 30 | |
| 31 | NDNCERT_REGISTER_CA_STORAGE(CaMemory); |
| 32 | |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame^] | 33 | RequestState |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 34 | CaMemory::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 | |
| 43 | void |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame^] | 44 | CaMemory::addRequest(const RequestState& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 45 | { |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 46 | for (auto& entry : m_requests) { |
| 47 | const auto& existingRequest = entry.second; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 48 | if (existingRequest.m_cert.getKeyName() == request.m_cert.getKeyName()) { |
| 49 | BOOST_THROW_EXCEPTION(Error("Request for " + request.m_cert.getKeyName().toUri() + " already exists")); |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | } |
| 53 | for (auto& entry : m_issuedCerts) { |
| 54 | const auto& cert = entry.second; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 55 | if (cert.getKeyName() == request.m_cert.getKeyName()) { |
| 56 | BOOST_THROW_EXCEPTION(Error("Cert for " + request.m_cert.getKeyName().toUri() + " already exists")); |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | } |
| 60 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 61 | auto search = m_requests.find(request.m_requestId); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 62 | if (search == m_requests.end()) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 63 | m_requests[request.m_requestId] = request; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 64 | } |
| 65 | else { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 66 | BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " already exists")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | void |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame^] | 71 | CaMemory::updateRequest(const RequestState& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 72 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 73 | m_requests[request.m_requestId] = request; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
| 77 | CaMemory::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 Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame^] | 85 | std::list<RequestState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 86 | CaMemory::listAllRequests() |
| 87 | { |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame^] | 88 | std::list<RequestState> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 89 | for (const auto& entry : m_requests) { |
| 90 | result.push_back(entry.second); |
| 91 | } |
| 92 | return result; |
| 93 | } |
| 94 | |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame^] | 95 | std::list<RequestState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 96 | CaMemory::listAllRequests(const Name& caName) |
| 97 | { |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame^] | 98 | std::list<RequestState> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 99 | for (const auto& entry : m_requests) { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 100 | if (entry.second.m_caPrefix == caName) { |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 101 | result.push_back(entry.second); |
| 102 | } |
| 103 | } |
| 104 | return result; |
| 105 | } |
| 106 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 107 | // certificate related |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 108 | security::v2::Certificate |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 109 | CaMemory::getCertificate(const std::string& certId) |
| 110 | { |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 111 | auto search = m_issuedCerts.find(certId); |
| 112 | if (search != m_issuedCerts.end()) { |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 113 | return search->second; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 114 | } |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 115 | BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " does not exists")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 119 | CaMemory::addCertificate(const std::string& certId, const security::v2::Certificate& cert) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 120 | { |
| 121 | auto search = m_issuedCerts.find(certId); |
| 122 | if (search == m_issuedCerts.end()) { |
| 123 | m_issuedCerts[certId] = cert; |
| 124 | } |
| 125 | else { |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 126 | BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " already exists")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | void |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 131 | CaMemory::updateCertificate(const std::string& certId, const security::v2::Certificate& cert) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 132 | { |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 133 | m_issuedCerts[certId] = cert; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void |
| 137 | CaMemory::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 Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 145 | std::list<security::v2::Certificate> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 146 | CaMemory::listAllIssuedCertificates() |
| 147 | { |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 148 | std::list<security::v2::Certificate> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 149 | for (const auto& entry : m_issuedCerts) { |
| 150 | result.push_back(entry.second); |
| 151 | } |
| 152 | return result; |
| 153 | } |
| 154 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 155 | std::list<security::v2::Certificate> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 156 | CaMemory::listAllIssuedCertificates(const Name& caName) |
| 157 | { |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 158 | std::list<security::v2::Certificate> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 159 | for (const auto& entry : m_issuedCerts) { |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 160 | const auto& klName = entry.second.getSignature().getKeyLocator().getName(); |
| 161 | if (security::v2::extractIdentityFromKeyName(klName) == caName) { |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 162 | result.push_back(entry.second); |
| 163 | } |
| 164 | } |
| 165 | return result; |
| 166 | } |
| 167 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 168 | } // namespace ndncert |
| 169 | } // namespace ndn |