blob: 27d83da28819c721412d01e874620cf1b923f822 [file] [log] [blame]
Yingdi Yu151b5572015-04-27 11:07:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento3b101d02018-07-21 22:44:09 -04002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Yingdi Yu151b5572015-04-27 11:07:37 -07004 *
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
Alexander Afanasyev97709c02016-08-25 19:58:30 -070022#ifndef NDN_SECURITY_PIB_PIB_HPP
23#define NDN_SECURITY_PIB_PIB_HPP
Yingdi Yu151b5572015-04-27 11:07:37 -070024
Yingdi Yub8f8b342015-04-27 11:06:42 -070025#include "identity-container.hpp"
Yingdi Yu151b5572015-04-27 11:07:37 -070026
27namespace ndn {
28namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070029namespace pib {
30
Yingdi Yub8f8b342015-04-27 11:06:42 -070031class PibImpl;
32
Yingdi Yu151b5572015-04-27 11:07:37 -070033/**
34 * @brief represents the PIB
35 *
36 * The PIB (Public Information Base) stores the public portion of a user's cryptography keys.
37 * The format and location of stored information is indicated by the PibLocator.
38 * The PIB is designed to work with a TPM (Trusted Platform Module) which stores private keys.
39 * There is a one-to-one association between PIB and TPM, and therefore the TpmLocator is recorded
40 * by the PIB to enforce this association and prevent one from operating on mismatched PIB and TPM.
41 *
42 * Information in the PIB is organized in a hierarchy of Identity-Key-Certificate. At the top level,
43 * the Pib class provides access to identities, and allows setting a default identity. Properties of
44 * an identity can be accessed after obtaining an Identity object.
45 *
Yingdi Yufe4733a2015-10-22 14:24:12 -070046 * @note Pib instance is created and managed only by v2::KeyChain. v2::KeyChain::getPib()
47 * returns a const reference to the managed Pib instance, through which it is possible to
48 * retrieve information about identities, keys, and certificates.
49 *
Yingdi Yu151b5572015-04-27 11:07:37 -070050 * @throw PibImpl::Error when underlying implementation has non-semantic error.
51 */
52class Pib : noncopyable
53{
54public:
55 /// @brief represents a semantic error
56 class Error : public std::runtime_error
57 {
58 public:
59 explicit
60 Error(const std::string& what)
61 : std::runtime_error(what)
62 {
63 }
64 };
65
Yingdi Yub8f8b342015-04-27 11:06:42 -070066public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070067 ~Pib();
68
69 /**
Davide Pesavento3b101d02018-07-21 22:44:09 -040070 * @brief return the scheme of the PIB Locator
Yingdi Yub8f8b342015-04-27 11:06:42 -070071 */
72 std::string
73 getScheme() const
74 {
75 return m_scheme;
76 }
77
78 /**
79 * @brief Get PIB Locator
80 */
81 std::string
82 getPibLocator() const;
83
84 /**
85 * @brief Set the corresponding TPM information to @p tpmLocator.
86 *
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070087 * If the provided @p tpmLocator is different from the existing one, PIB will be reset.
88 * Otherwise, nothing will be changed.
Yingdi Yub8f8b342015-04-27 11:06:42 -070089 */
90 void
91 setTpmLocator(const std::string& tpmLocator);
92
93 /**
94 * @brief Get TPM Locator
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070095 * @throws Error if TPM locator is empty
Yingdi Yub8f8b342015-04-27 11:06:42 -070096 */
97 std::string
98 getTpmLocator() const;
99
Yingdi Yuc8209892015-06-19 17:47:56 -0700100 /**
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700101 * @brief Reset content in PIB, including reset of the TPM locator
102 */
103 void
104 reset();
105
106 /**
Yingdi Yuc8209892015-06-19 17:47:56 -0700107 * @brief Get an identity with name @p identityName.
Yingdi Yuc8209892015-06-19 17:47:56 -0700108 * @throw Pib::Error if the identity does not exist.
109 */
110 Identity
111 getIdentity(const Name& identityName) const;
112
Davide Pesavento3b101d02018-07-21 22:44:09 -0400113 /**
114 * @brief Get all the identities
115 */
Yingdi Yuc8209892015-06-19 17:47:56 -0700116 const IdentityContainer&
117 getIdentities() const;
118
119 /**
120 * @brief Get the default identity.
Davide Pesavento3b101d02018-07-21 22:44:09 -0400121 * @throw Pib::Error if no default identity exists.
Yingdi Yuc8209892015-06-19 17:47:56 -0700122 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800123 const Identity&
Yingdi Yuc8209892015-06-19 17:47:56 -0700124 getDefaultIdentity() const;
125
126NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
Davide Pesavento3b101d02018-07-21 22:44:09 -0400127 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800128 * @brief Create a Pib instance
129 *
130 * @param scheme The scheme for the Pib
131 * @param location The location for the Pib
132 * @param impl The backend implementation
133 */
134 Pib(const std::string& scheme, const std::string& location, shared_ptr<PibImpl> impl);
135
Yingdi Yub8f8b342015-04-27 11:06:42 -0700136 /**
Davide Pesavento3b101d02018-07-21 22:44:09 -0400137 * @brief Add an identity.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700138 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800139 * If no default identity is set before, the new identity will be set as the default identity
Yingdi Yub8f8b342015-04-27 11:06:42 -0700140 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800141 * @return handle of the added identity.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700142 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800143 Identity
144 addIdentity(const Name& identity);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700145
Davide Pesavento3b101d02018-07-21 22:44:09 -0400146 /**
147 * @brief Remove an identity.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700148 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800149 * If the default identity is being removed, no default identity will be selected.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700150 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800151 void
152 removeIdentity(const Name& identity);
153
154 /**
Davide Pesavento3b101d02018-07-21 22:44:09 -0400155 * @brief Set an identity as the default identity.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800156 *
157 * Create the identity if it does not exist.
158 *
159 * @return handle of the default identity
160 */
161 const Identity&
162 setDefaultIdentity(const Name& identity);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700163
164 shared_ptr<PibImpl>
165 getImpl()
166 {
167 return m_impl;
168 }
169
170protected:
171 std::string m_scheme;
172 std::string m_location;
Yingdi Yuc8209892015-06-19 17:47:56 -0700173
Yingdi Yucbe72b02015-11-25 17:35:37 -0800174 mutable bool m_isDefaultIdentityLoaded;
Yingdi Yuc8209892015-06-19 17:47:56 -0700175 mutable Identity m_defaultIdentity;
176
Yingdi Yucbe72b02015-11-25 17:35:37 -0800177 IdentityContainer m_identities;
Yingdi Yuc8209892015-06-19 17:47:56 -0700178
Yingdi Yub8f8b342015-04-27 11:06:42 -0700179 shared_ptr<PibImpl> m_impl;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800180
Yingdi Yufe4733a2015-10-22 14:24:12 -0700181 friend class v2::KeyChain;
Yingdi Yu151b5572015-04-27 11:07:37 -0700182};
183
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700184} // namespace pib
185
186using pib::Pib;
187
Yingdi Yu151b5572015-04-27 11:07:37 -0700188} // namespace security
189} // namespace ndn
190
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700191#endif // NDN_SECURITY_PIB_PIB_HPP