blob: 533bfc9fb03e53370f02dfa6a4c5a9ccc65d96ac [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 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_IDENTITY_HPP
23#define NDN_SECURITY_IDENTITY_HPP
24
25#include "key-container.hpp"
26
27namespace ndn {
28namespace security {
29
30class PibImpl;
31class Pib;
32class IdentityContainer;
33
34/**
35 * @brief represents an identity
36 *
37 * Identity is at the top level in PIB's Identity-Key-Certificate hierarchy.
38 * An identity has a Name, and contains one or more keys, one of which is set
39 * as the default key of this identity. Properties of a key can be accessed
40 * after obtaining a Key object.
41 *
42 * @throw PibImpl::Error when underlying implementation has non-semantic error.
43 */
44class Identity
45{
46public:
47 friend class Pib;
48 friend class IdentityContainer;
Yingdi Yuc8209892015-06-19 17:47:56 -070049 friend class KeyChain;
Yingdi Yub8f8b342015-04-27 11:06:42 -070050
51public:
52 /**
53 * @brief Default Constructor
54 *
55 * Identity created using this default constructor is just a place holder.
56 * It must obtain an actual instance from Pib::getIdentity(...). A typical
57 * usage would be for exception handling:
58 *
59 * Identity id;
60 * try {
61 * id = pib.getIdentity(...);
62 * }
63 * catch (Pib::Error&) {
64 * ...
65 * }
66 *
67 * An Identity instance created using the constructor is invalid. Calling a
68 * member method on an invalid Identity instance may cause an std::domain_error.
69 */
70 Identity();
71
72 /// @brief Get the name of the identity.
73 const Name&
74 getName() const;
75
76 /**
Yingdi Yuc8209892015-06-19 17:47:56 -070077 * @brief Get a key with id @keyId.
78 *
79 * @param identityName The name for the identity to get.
80 * @throw Pib::Error if the identity does not exist.
81 */
82 Key
83 getKey(const name::Component& keyId) const;
84
85 /// @brief Get all the keys for this Identity.
86 const KeyContainer&
87 getKeys() const;
88
89 /**
90 * @brief Get the default key for this Identity.
91 *
92 * @throws Pib::Error if the default key does not exist.
93 */
94 Key&
95 getDefaultKey() const;
96
97 /// @brief Check if the Identity instance is valid
98 operator bool() const;
99
100 /// @brief Check if the Identity instance is invalid
101 bool
102 operator!() const;
103
104NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
105
106 /**
Yingdi Yub8f8b342015-04-27 11:06:42 -0700107 * @brief Add a key.
108 *
109 * If the key already exists, do nothing.
110 *
111 * If no default key is set before, the new key will be set as the default key of the identity.
112 *
113 * @param publicKey The public key to add.
114 * @param keyId The key id component of the new key to add.
115 * By default, the keyId will be set to the hash of the public key bits.
116 * @return the added key or existing key with the same key id.
117 */
118 Key
119 addKey(const PublicKey& publicKey, const name::Component& keyId = EMPTY_KEY_ID);
120
121 /**
122 * @brief Remove a key.
123 *
124 * @param keyId The key id component of the key to delete.
125 */
126 void
127 removeKey(const name::Component& keyId);
128
129 /**
Yingdi Yub8f8b342015-04-27 11:06:42 -0700130 * @brief Set the key with id @p keyId as the default key.
131 *
132 * @param keyId The key id component of the default key.
133 * @return The default key
134 * @throws Pib::Error if the key does not exist.
135 */
136 Key&
137 setDefaultKey(const name::Component& keyId);
138
139 /**
140 * @brief Set the default key.
141 *
142 * If the key does not exist, add the key and set it as the default of the Identity.
143 * If the key exists, simply set it as the default key of the Identity.
144 *
145 * @param publicKey The public key to add.
146 * @param keyId The key id component of the default key.
147 * @return the default key
148 */
149 Key&
150 setDefaultKey(const PublicKey& publicKey, const name::Component& keyId = EMPTY_KEY_ID);
151
Yingdi Yub8f8b342015-04-27 11:06:42 -0700152NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
153 /**
154 * @brief Create an Identity with @p identityName.
155 *
156 * @param identityName The name of the Identity.
157 * @param impl The backend implementation.
158 * @param needInit If true, create the identity in backend when the identity does not exist.
159 * Otherwise, throw Pib::Error when the identity does not exist.
160 */
161 Identity(const Name& identityName, shared_ptr<PibImpl> impl, bool needInit = false);
162
163 /**
164 * @brief Check the validity of this instance
165 *
166 * @throws std::domain_error if the instance is invalid
167 */
168 void
169 validityCheck() const;
170
171public:
172 /**
173 * @brief The default value of keyId when add a new key.
174 *
175 * An empty keyId implies that the key digest should be used as the actual keyId.
176 */
177 static const name::Component EMPTY_KEY_ID;
178
179private:
180 Name m_name;
181
Yingdi Yuc8209892015-06-19 17:47:56 -0700182 mutable bool m_hasDefaultKey;
183 mutable Key m_defaultKey;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700184
Yingdi Yuc8209892015-06-19 17:47:56 -0700185 mutable bool m_needRefreshKeys;
186 mutable KeyContainer m_keys;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700187
188 shared_ptr<PibImpl> m_impl;
189};
190
191} // namespace security
192} // namespace ndn
193
194#endif // NDN_SECURITY_IDENTITY_HPP