blob: 0a6ca60a293297c3e87af4f68427bed0a303f812 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -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_IDENTITY_HPP
23#define NDN_SECURITY_PIB_IDENTITY_HPP
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
25#include "key-container.hpp"
26
27namespace ndn {
28namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070029namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070030
31/**
32 * @brief represents an identity
33 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -070034 * Identity is at the top level in PIB's Identity-Key-Certificate hierarchy. An identity has a
35 * Name, and contains zero or more keys, at most one of which is set as the default key of this
36 * identity. Properties of a key can be accessed after obtaining a Key object.
Yingdi Yub8f8b342015-04-27 11:06:42 -070037 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -070038 * @throw Pib::Error when underlying implementation has non-semantic error.
Yingdi Yub8f8b342015-04-27 11:06:42 -070039 */
40class Identity
41{
42public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070043 /**
44 * @brief Default Constructor
45 *
46 * Identity created using this default constructor is just a place holder.
47 * It must obtain an actual instance from Pib::getIdentity(...). A typical
48 * usage would be for exception handling:
49 *
50 * Identity id;
51 * try {
52 * id = pib.getIdentity(...);
53 * }
Yingdi Yu6ee2d362015-07-16 21:48:05 -070054 * catch (const Pib::Error&) {
Yingdi Yub8f8b342015-04-27 11:06:42 -070055 * ...
56 * }
57 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -070058 * An Identity instance created using this constructor is invalid. Calling a
Yingdi Yub8f8b342015-04-27 11:06:42 -070059 * member method on an invalid Identity instance may cause an std::domain_error.
60 */
61 Identity();
62
Yingdi Yu6ee2d362015-07-16 21:48:05 -070063 /**
64 * @brief Create an Identity with @p identityName.
65 *
66 * @param identityName The name of the Identity.
67 * @param impl The backend implementation.
68 * @param needInit If true, create the identity in backend when the identity does not exist.
69 * Otherwise, throw Pib::Error when the identity does not exist.
70 */
71 Identity(const Name& identityName, shared_ptr<PibImpl> impl, bool needInit = false);
72
Yingdi Yub8f8b342015-04-27 11:06:42 -070073 /// @brief Get the name of the identity.
74 const Name&
75 getName() const;
76
77 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -070078 * @brief Get a key with @p keyName.
Davide Pesavento18cf81b2015-09-12 23:36:43 +020079 * @throw Pib::Error if the key does not exist.
Yingdi Yuc8209892015-06-19 17:47:56 -070080 */
81 Key
Yingdi Yu6ee2d362015-07-16 21:48:05 -070082 getKey(const Name& keyName) const;
Yingdi Yuc8209892015-06-19 17:47:56 -070083
84 /// @brief Get all the keys for this Identity.
85 const KeyContainer&
86 getKeys() const;
87
88 /**
89 * @brief Get the default key for this Identity.
90 *
91 * @throws Pib::Error if the default key does not exist.
92 */
93 Key&
94 getDefaultKey() const;
95
96 /// @brief Check if the Identity instance is valid
97 operator bool() const;
98
99 /// @brief Check if the Identity instance is invalid
100 bool
101 operator!() const;
102
103NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
Yingdi Yuc8209892015-06-19 17:47:56 -0700104 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700105 * @brief Add a @p key (in PKCS#8 format) with @p keyName.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700106 *
107 * If the key already exists, do nothing.
108 *
109 * If no default key is set before, the new key will be set as the default key of the identity.
110 *
Yingdi Yub8f8b342015-04-27 11:06:42 -0700111 * @return the added key or existing key with the same key id.
112 */
113 Key
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700114 addKey(const uint8_t* key, size_t keyLen, const Name& keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700115
116 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700117 * @brief Remove a key with @p keyName
Yingdi Yub8f8b342015-04-27 11:06:42 -0700118 */
119 void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700120 removeKey(const Name& keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700121
122 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700123 * @brief Set the key with id @p keyName.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700124 *
Yingdi Yub8f8b342015-04-27 11:06:42 -0700125 * @return The default key
126 * @throws Pib::Error if the key does not exist.
127 */
128 Key&
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700129 setDefaultKey(const Name& keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700130
131 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700132 * @brief Set the default key with @p keyName (in PKCS#8 format).
Yingdi Yub8f8b342015-04-27 11:06:42 -0700133 *
134 * If the key does not exist, add the key and set it as the default of the Identity.
135 * If the key exists, simply set it as the default key of the Identity.
136 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700137 * @param key The public key to add.
138 * @param keyLen The length of the key.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700139 * @return the default key
140 */
141 Key&
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700142 setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700143
Yingdi Yub8f8b342015-04-27 11:06:42 -0700144NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
145 /**
Yingdi Yub8f8b342015-04-27 11:06:42 -0700146 * @brief Check the validity of this instance
147 *
148 * @throws std::domain_error if the instance is invalid
149 */
150 void
151 validityCheck() const;
152
Yingdi Yub8f8b342015-04-27 11:06:42 -0700153private:
154 Name m_name;
155
Yingdi Yuc8209892015-06-19 17:47:56 -0700156 mutable bool m_hasDefaultKey;
157 mutable Key m_defaultKey;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700158
Yingdi Yuc8209892015-06-19 17:47:56 -0700159 mutable bool m_needRefreshKeys;
160 mutable KeyContainer m_keys;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700161
162 shared_ptr<PibImpl> m_impl;
163};
164
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700165} // namespace pib
166
167using pib::Identity;
168
Yingdi Yub8f8b342015-04-27 11:06:42 -0700169} // namespace security
170} // namespace ndn
171
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700172#endif // NDN_SECURITY_PIB_IDENTITY_HPP