blob: f06e70c7517705c171b603422b710cf5bf9a0dda [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#ifndef NDNCERT_CA_STORAGE_HPP
22#define NDNCERT_CA_STORAGE_HPP
23
24#include "certificate-request.hpp"
25
26namespace ndn {
27namespace ndncert {
28
29class CaStorage : noncopyable
30{
31public:
32 /**
33 * @brief Error that can be thrown from CaStorage
34 */
35 class Error : public std::runtime_error
36 {
37 public:
38 using std::runtime_error::runtime_error;
39 };
40
41public:
42 // certificate request related
43 virtual CertificateRequest
44 getRequest(const std::string& requestId) = 0;
45
46 virtual void
47 addRequest(const CertificateRequest& request) = 0;
48
49 virtual void
50 updateRequest(const CertificateRequest& request) = 0;
51
52 virtual void
53 deleteRequest(const std::string& requestId) = 0;
54
55 // certificate related
56 virtual security::v2::Certificate
57 getCertificate(const std::string& certId) = 0;
58
59 virtual void
60 addCertificate(const std::string& certId, const security::v2::Certificate& cert) = 0;
61
62 virtual void
63 updateCertificate(const std::string& certId, const security::v2::Certificate& cert) = 0;
64
65 virtual void
66 deleteCertificate(const std::string& certId) = 0;
67
68public:
69 template<class CaStorageType>
70 static void
71 registerCaStorage(const std::string& caStorageType = CaStorageType::STORAGE_TYPE)
72 {
73 CaStorageFactory& factory = getFactory();
74 BOOST_ASSERT(factory.count(caStorageType) == 0);
75 factory[caStorageType] = [] {
76 return make_unique<CaStorageType>();
77 };
78 }
79
80 static unique_ptr<CaStorage>
81 createCaStorage(const std::string& caStorageType);
82
83private:
84 using CaStorageCreateFunc = function<unique_ptr<CaStorage> ()>;
85 using CaStorageFactory = std::map<std::string, CaStorageCreateFunc>;
86
87 static CaStorageFactory&
88 getFactory();
89};
90
91#define NDNCERT_REGISTER_CA_STORAGE(C) \
92static class NdnCert ## C ## CaStorageRegistrationClass \
93{ \
94public: \
95 NdnCert ## C ## CaStorageRegistrationClass() \
96 { \
97 ::ndn::ndncert::CaStorage::registerCaStorage<C>(); \
98 } \
99} g_NdnCert ## C ## CaStorageRegistrationVariable
100
101} // namespace ndncert
102} // namespace ndn
103
104#endif // NDNCERT_CA_STORAGE_HPP