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 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 33 | CaState |
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 |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 44 | CaMemory::addRequest(const CaState& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 45 | { |
tylerliu | 09a00fd | 2020-10-04 11:11:18 -0700 | [diff] [blame] | 46 | auto keyNameTLV = request.m_cert.getKeyName(); |
| 47 | if (request.m_requestType == RequestType::NEW) { |
| 48 | if (m_requestKeyIndex.find(keyNameTLV) != m_requestKeyIndex.end() |
| 49 | && !m_requestKeyIndex.find(keyNameTLV)->second.empty()){ |
| 50 | BOOST_THROW_EXCEPTION(Error("Request for " + keyNameTLV.toUri() + " already exists")); |
| 51 | return; |
| 52 | } |
| 53 | if (m_certsKeyIndex.find(keyNameTLV) != m_certsKeyIndex.end()) { |
| 54 | BOOST_THROW_EXCEPTION(Error("Cert for " + keyNameTLV.toUri() + " already exists")); |
| 55 | return; |
| 56 | } |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 57 | } |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 58 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 59 | auto search = m_requests.find(request.m_requestId); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 60 | if (search == m_requests.end()) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 61 | m_requests[request.m_requestId] = request; |
tylerliu | 09a00fd | 2020-10-04 11:11:18 -0700 | [diff] [blame] | 62 | m_requestKeyIndex[keyNameTLV].insert(request.m_requestId); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 63 | } |
| 64 | else { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 65 | BOOST_THROW_EXCEPTION(Error("Request " + request.m_requestId + " already exists")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | void |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 70 | CaMemory::updateRequest(const CaState& request) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 71 | { |
tylerliu | 09a00fd | 2020-10-04 11:11:18 -0700 | [diff] [blame] | 72 | m_requests[request.m_requestId].m_status = request.m_status; |
| 73 | m_requests[request.m_requestId].m_challengeState = request.m_challengeState; |
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); |
tylerliu | 09a00fd | 2020-10-04 11:11:18 -0700 | [diff] [blame] | 80 | auto keyName = search->second.m_cert.getKeyName(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 81 | if (search != m_requests.end()) { |
tylerliu | 09a00fd | 2020-10-04 11:11:18 -0700 | [diff] [blame] | 82 | m_requestKeyIndex.find(keyName)->second.erase(requestId); |
| 83 | if (m_requestKeyIndex.find(keyName)->second.empty()) m_requestKeyIndex.erase(keyName); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 84 | m_requests.erase(search); |
| 85 | } |
| 86 | } |
| 87 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 88 | std::list<CaState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 89 | CaMemory::listAllRequests() |
| 90 | { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 91 | std::list<CaState> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 92 | for (const auto& entry : m_requests) { |
| 93 | result.push_back(entry.second); |
| 94 | } |
| 95 | return result; |
| 96 | } |
| 97 | |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 98 | std::list<CaState> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 99 | CaMemory::listAllRequests(const Name& caName) |
| 100 | { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 101 | std::list<CaState> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 102 | for (const auto& entry : m_requests) { |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 103 | if (entry.second.m_caPrefix == caName) { |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 104 | result.push_back(entry.second); |
| 105 | } |
| 106 | } |
| 107 | return result; |
| 108 | } |
| 109 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 110 | // certificate related |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 111 | security::v2::Certificate |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 112 | CaMemory::getCertificate(const std::string& certId) |
| 113 | { |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 114 | auto search = m_issuedCerts.find(certId); |
| 115 | if (search != m_issuedCerts.end()) { |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 116 | return search->second; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 117 | } |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 118 | BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " does not exists")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 122 | CaMemory::addCertificate(const std::string& certId, const security::v2::Certificate& cert) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 123 | { |
| 124 | auto search = m_issuedCerts.find(certId); |
| 125 | if (search == m_issuedCerts.end()) { |
| 126 | m_issuedCerts[certId] = cert; |
tylerliu | 09a00fd | 2020-10-04 11:11:18 -0700 | [diff] [blame] | 127 | m_certsKeyIndex[cert.getKeyName()] = certId; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 128 | } |
| 129 | else { |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 130 | BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " already exists")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | void |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 135 | CaMemory::updateCertificate(const std::string& certId, const security::v2::Certificate& cert) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 136 | { |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 137 | m_issuedCerts[certId] = cert; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void |
| 141 | CaMemory::deleteCertificate(const std::string& certId) |
| 142 | { |
| 143 | auto search = m_issuedCerts.find(certId); |
| 144 | if (search != m_issuedCerts.end()) { |
tylerliu | 09a00fd | 2020-10-04 11:11:18 -0700 | [diff] [blame] | 145 | m_certsKeyIndex.erase(search->second.getKeyName()); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 146 | m_issuedCerts.erase(search); |
| 147 | } |
| 148 | } |
| 149 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 150 | std::list<security::v2::Certificate> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 151 | CaMemory::listAllIssuedCertificates() |
| 152 | { |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 153 | std::list<security::v2::Certificate> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 154 | for (const auto& entry : m_issuedCerts) { |
| 155 | result.push_back(entry.second); |
| 156 | } |
| 157 | return result; |
| 158 | } |
| 159 | |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 160 | std::list<security::v2::Certificate> |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 161 | CaMemory::listAllIssuedCertificates(const Name& caName) |
| 162 | { |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 163 | std::list<security::v2::Certificate> result; |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 164 | for (const auto& entry : m_issuedCerts) { |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 165 | const auto& klName = entry.second.getSignature().getKeyLocator().getName(); |
| 166 | if (security::v2::extractIdentityFromKeyName(klName) == caName) { |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 167 | result.push_back(entry.second); |
| 168 | } |
| 169 | } |
| 170 | return result; |
| 171 | } |
| 172 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 173 | } // namespace ndncert |
| 174 | } // namespace ndn |