Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 23 | namespace ndn { |
| 24 | namespace ndncert { |
| 25 | |
| 26 | const std::string |
| 27 | CaMemory::STORAGE_TYPE = "ca-storage-memory"; |
| 28 | |
| 29 | NDNCERT_REGISTER_CA_STORAGE(CaMemory); |
| 30 | |
| 31 | CertificateRequest |
| 32 | CaMemory::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 | |
| 41 | void |
| 42 | CaMemory::addRequest(const CertificateRequest& request) |
| 43 | { |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 44 | 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 Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 59 | 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 | |
| 68 | void |
| 69 | CaMemory::updateRequest(const CertificateRequest& request) |
| 70 | { |
| 71 | m_requests[request.getRequestId()] = request; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | CaMemory::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 |
| 84 | security::v2::Certificate |
| 85 | CaMemory::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 Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 91 | return cert; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 92 | } |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 93 | else { |
| 94 | BOOST_THROW_EXCEPTION(Error("Certificate with ID " + certId + " does not exists")); |
| 95 | } |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | CaMemory::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 Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 106 | BOOST_THROW_EXCEPTION(Error("Certificate " + cert.getName().toUri() + " already exists")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | CaMemory::updateCertificate(const std::string& certId, const security::v2::Certificate& cert) |
| 112 | { |
Zhiyi Zhang | 1bc2346 | 2017-04-12 14:16:09 -0700 | [diff] [blame] | 113 | m_issuedCerts[certId] = cert; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void |
| 117 | CaMemory::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 |