blob: ecc42ebd63fe1a81609b857efeb405462c1667ad [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;
49
50public:
51 /**
52 * @brief Default Constructor
53 *
54 * Identity created using this default constructor is just a place holder.
55 * It must obtain an actual instance from Pib::getIdentity(...). A typical
56 * usage would be for exception handling:
57 *
58 * Identity id;
59 * try {
60 * id = pib.getIdentity(...);
61 * }
62 * catch (Pib::Error&) {
63 * ...
64 * }
65 *
66 * An Identity instance created using the constructor is invalid. Calling a
67 * member method on an invalid Identity instance may cause an std::domain_error.
68 */
69 Identity();
70
71 /// @brief Get the name of the identity.
72 const Name&
73 getName() const;
74
75 /**
76 * @brief Add a key.
77 *
78 * If the key already exists, do nothing.
79 *
80 * If no default key is set before, the new key will be set as the default key of the identity.
81 *
82 * @param publicKey The public key to add.
83 * @param keyId The key id component of the new key to add.
84 * By default, the keyId will be set to the hash of the public key bits.
85 * @return the added key or existing key with the same key id.
86 */
87 Key
88 addKey(const PublicKey& publicKey, const name::Component& keyId = EMPTY_KEY_ID);
89
90 /**
91 * @brief Remove a key.
92 *
93 * @param keyId The key id component of the key to delete.
94 */
95 void
96 removeKey(const name::Component& keyId);
97
98 /**
99 * @brief Get a key with id @keyId.
100 *
101 * @param identityName The name for the identity to get.
102 * @throw Pib::Error if the identity does not exist.
103 */
104 Key
105 getKey(const name::Component& keyId);
106
107 /// @brief Get all the keys for this Identity.
108 const KeyContainer&
109 getKeys();
110
111 /**
112 * @brief Set the key with id @p keyId as the default key.
113 *
114 * @param keyId The key id component of the default key.
115 * @return The default key
116 * @throws Pib::Error if the key does not exist.
117 */
118 Key&
119 setDefaultKey(const name::Component& keyId);
120
121 /**
122 * @brief Set the default key.
123 *
124 * If the key does not exist, add the key and set it as the default of the Identity.
125 * If the key exists, simply set it as the default key of the Identity.
126 *
127 * @param publicKey The public key to add.
128 * @param keyId The key id component of the default key.
129 * @return the default key
130 */
131 Key&
132 setDefaultKey(const PublicKey& publicKey, const name::Component& keyId = EMPTY_KEY_ID);
133
134 /**
135 * @brief Get the default key for this Identity.
136 *
137 * @throws Pib::Error if the default key does not exist.
138 */
139 Key&
140 getDefaultKey();
141
142 /// @brief Check if the Identity instance is valid
143 operator bool() const;
144
145 /// @brief Check if the Identity instance is invalid
146 bool
147 operator!() const;
148
149NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
150 /**
151 * @brief Create an Identity with @p identityName.
152 *
153 * @param identityName The name of the Identity.
154 * @param impl The backend implementation.
155 * @param needInit If true, create the identity in backend when the identity does not exist.
156 * Otherwise, throw Pib::Error when the identity does not exist.
157 */
158 Identity(const Name& identityName, shared_ptr<PibImpl> impl, bool needInit = false);
159
160 /**
161 * @brief Check the validity of this instance
162 *
163 * @throws std::domain_error if the instance is invalid
164 */
165 void
166 validityCheck() const;
167
168public:
169 /**
170 * @brief The default value of keyId when add a new key.
171 *
172 * An empty keyId implies that the key digest should be used as the actual keyId.
173 */
174 static const name::Component EMPTY_KEY_ID;
175
176private:
177 Name m_name;
178
179 bool m_hasDefaultKey;
180 Key m_defaultKey;
181
182 bool m_needRefreshKeys;
183 KeyContainer m_keys;
184
185 shared_ptr<PibImpl> m_impl;
186};
187
188} // namespace security
189} // namespace ndn
190
191#endif // NDN_SECURITY_IDENTITY_HPP