Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library 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 Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_SECURITY_V2_CERTIFICATE_STORAGE_HPP |
| 23 | #define NDN_SECURITY_V2_CERTIFICATE_STORAGE_HPP |
| 24 | |
| 25 | #include "certificate.hpp" |
| 26 | #include "certificate-cache.hpp" |
| 27 | #include "trust-anchor-container.hpp" |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
| 31 | namespace v2 { |
| 32 | |
| 33 | /** |
| 34 | * @brief Storage for trusted anchors, verified certificate cache, and unverified certificate cache. |
| 35 | */ |
| 36 | class CertificateStorage : noncopyable |
| 37 | { |
| 38 | public: |
| 39 | CertificateStorage(); |
| 40 | |
| 41 | /** |
| 42 | * @brief Find a trusted certificate in trust anchor container or in verified cache |
| 43 | * @param interestForCert Interest for certificate |
| 44 | * @return found certificate, nullptr if not found. |
| 45 | * |
| 46 | * @note The returned pointer may get invalidated after next findTrustedCert or findCert calls. |
| 47 | */ |
| 48 | const Certificate* |
| 49 | findTrustedCert(const Interest& interestForCert) const; |
| 50 | |
| 51 | /** |
| 52 | * @brief Check if certificate exists in verified, unverified cache, or in the set of trust |
| 53 | * anchors |
| 54 | */ |
| 55 | bool |
| 56 | isCertKnown(const Name& certPrefix) const; |
| 57 | |
| 58 | /** |
| 59 | * @brief Cache unverified certificate for a period of time (5 minutes) |
| 60 | * @param cert The certificate packet |
| 61 | * |
| 62 | * @todo Add ability to customize time period |
| 63 | */ |
| 64 | void |
| 65 | cacheUnverifiedCert(Certificate&& cert); |
| 66 | |
| 67 | /** |
| 68 | * @return Trust anchor container |
| 69 | */ |
| 70 | const TrustAnchorContainer& |
| 71 | getTrustAnchors() const; |
| 72 | |
| 73 | /** |
| 74 | * @return Verified certificate cache |
| 75 | */ |
| 76 | const CertificateCache& |
| 77 | getVerifiedCertCache() const; |
| 78 | |
| 79 | /** |
| 80 | * @return Unverified certificate cache |
| 81 | */ |
| 82 | const CertificateCache& |
| 83 | getUnverifiedCertCache() const; |
| 84 | |
| 85 | protected: |
| 86 | /** |
| 87 | * @brief load static trust anchor. |
| 88 | * |
| 89 | * Static trust anchors are permanently associated with the validator and never expire. |
| 90 | * |
| 91 | * @param groupId Certificate group id. |
| 92 | * @param cert Certificate to load as a trust anchor. |
| 93 | */ |
| 94 | void |
| 95 | loadAnchor(const std::string& groupId, Certificate&& cert); |
| 96 | |
| 97 | /** |
| 98 | * @brief load dynamic trust anchors. |
| 99 | * |
| 100 | * Dynamic trust anchors are associated with the validator for as long as the underlying |
| 101 | * trust anchor file (set of files) exist(s). |
| 102 | * |
| 103 | * @param groupId Certificate group id, must not be empty. |
| 104 | * @param certfilePath Specifies the path to load the trust anchors. |
| 105 | * @param refreshPeriod Refresh period for the trust anchors, must be positive. |
| 106 | * @param isDir Tells whether the path is a directory or a single file. |
| 107 | */ |
| 108 | void |
| 109 | loadAnchor(const std::string& groupId, const std::string& certfilePath, |
| 110 | time::nanoseconds refreshPeriod, bool isDir = false); |
| 111 | |
| 112 | /** |
| 113 | * @brief Cache verified certificate a period of time (1 hour) |
| 114 | * @param cert The certificate packet |
| 115 | * |
| 116 | * @todo Add ability to customize time period |
| 117 | */ |
| 118 | void |
| 119 | cacheVerifiedCert(Certificate&& cert); |
| 120 | |
| 121 | protected: |
| 122 | TrustAnchorContainer m_trustAnchors; |
| 123 | CertificateCache m_verifiedCertCache; |
| 124 | CertificateCache m_unverifiedCertCache; |
| 125 | }; |
| 126 | |
| 127 | } // namespace v2 |
| 128 | } // namespace security |
| 129 | } // namespace ndn |
| 130 | |
| 131 | #endif // NDN_SECURITY_V2_CERTIFICATE_STORAGE_HPP |