blob: 7c9f714d48ee3cab2600b843142facdea5fe2925 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5759be32017-10-15 00:00:52 +00002/*
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
Yingdi Yucbe72b02015-11-25 17:35:37 -080031namespace detail {
32class IdentityImpl;
33} // namespace detail
34
Yingdi Yub8f8b342015-04-27 11:06:42 -070035/**
Yingdi Yucbe72b02015-11-25 17:35:37 -080036 * @brief A frontend handle of an Identity
Yingdi Yub8f8b342015-04-27 11:06:42 -070037 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -070038 * Identity is at the top level in PIB's Identity-Key-Certificate hierarchy. An identity has a
39 * Name, and contains zero or more keys, at most one of which is set as the default key of this
40 * identity. Properties of a key can be accessed after obtaining a Key object.
Yingdi Yub8f8b342015-04-27 11:06:42 -070041 */
42class Identity
43{
44public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070045 /**
46 * @brief Default Constructor
47 *
48 * Identity created using this default constructor is just a place holder.
Yingdi Yucbe72b02015-11-25 17:35:37 -080049 * It can obtain an actual instance from Pib::getIdentity(...). A typical
Yingdi Yub8f8b342015-04-27 11:06:42 -070050 * usage would be for exception handling:
51 *
52 * Identity id;
53 * try {
54 * id = pib.getIdentity(...);
55 * }
Yingdi Yu6ee2d362015-07-16 21:48:05 -070056 * catch (const Pib::Error&) {
Yingdi Yub8f8b342015-04-27 11:06:42 -070057 * ...
58 * }
59 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -070060 * An Identity instance created using this constructor is invalid. Calling a
Yingdi Yub8f8b342015-04-27 11:06:42 -070061 * member method on an invalid Identity instance may cause an std::domain_error.
62 */
63 Identity();
64
Yingdi Yu6ee2d362015-07-16 21:48:05 -070065 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -080066 * @brief Create an Identity with a backend implementation @p impl.
Yingdi Yu6ee2d362015-07-16 21:48:05 -070067 *
Yingdi Yucbe72b02015-11-25 17:35:37 -080068 * This method should only be used by IdentityContainer.
Yingdi Yu6ee2d362015-07-16 21:48:05 -070069 */
Yingdi Yucbe72b02015-11-25 17:35:37 -080070 explicit
71 Identity(weak_ptr<detail::IdentityImpl> impl);
Yingdi Yu6ee2d362015-07-16 21:48:05 -070072
Yingdi Yucbe72b02015-11-25 17:35:37 -080073 /**
74 * @brief Get the name of the identity.
75 */
Yingdi Yub8f8b342015-04-27 11:06:42 -070076 const Name&
77 getName() const;
78
79 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -080080 * @brief Get a key with id @p keyName.
81 * @throw std::invalid_argument @p keyName does not match identity
82 * @throw Pib::Error the key does not exist.
Yingdi Yuc8209892015-06-19 17:47:56 -070083 */
84 Key
Yingdi Yu6ee2d362015-07-16 21:48:05 -070085 getKey(const Name& keyName) const;
Yingdi Yuc8209892015-06-19 17:47:56 -070086
Yingdi Yucbe72b02015-11-25 17:35:37 -080087 /**
88 * @brief Get all keys for this identity.
89 */
Yingdi Yuc8209892015-06-19 17:47:56 -070090 const KeyContainer&
91 getKeys() const;
92
93 /**
94 * @brief Get the default key for this Identity.
Yingdi Yucbe72b02015-11-25 17:35:37 -080095 * @throw Pib::Error the default key does not exist.
Yingdi Yuc8209892015-06-19 17:47:56 -070096 */
Yingdi Yucbe72b02015-11-25 17:35:37 -080097 const Key&
Yingdi Yuc8209892015-06-19 17:47:56 -070098 getDefaultKey() const;
99
Davide Pesaventobdcedf42017-10-15 14:56:28 -0400100 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800101 * @return True if the identity instance is valid
102 */
Junxiao Shi5759be32017-10-15 00:00:52 +0000103 explicit
Yingdi Yuc8209892015-06-19 17:47:56 -0700104 operator bool() const;
105
Yingdi Yuc8209892015-06-19 17:47:56 -0700106NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
Yingdi Yuc8209892015-06-19 17:47:56 -0700107 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800108 * @brief Add a @p key of @p keyLen bytes (in PKCS#8 format) with @p keyName.
109 * @return the handle of added key
110 * @throw std::invalid_argument key name does not match identity
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800111 *
112 * If a key with the same name already exists, overwrite the key.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700113 */
114 Key
Yingdi Yufe4733a2015-10-22 14:24:12 -0700115 addKey(const uint8_t* key, size_t keyLen, const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700116
117 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700118 * @brief Remove a key with @p keyName
Yingdi Yucbe72b02015-11-25 17:35:37 -0800119 * @throw std::invalid_argument @p keyName does not match identity
Yingdi Yub8f8b342015-04-27 11:06:42 -0700120 */
121 void
Yingdi Yufe4733a2015-10-22 14:24:12 -0700122 removeKey(const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700123
124 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800125 * @brief Set an existing key with @p keyName as the default key.
126 * @throw std::invalid_argument @p keyName does not match identity
127 * @throw Pib::Error the key does not exist.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700128 * @return The default key
Yingdi Yub8f8b342015-04-27 11:06:42 -0700129 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800130 const Key&
Yingdi Yufe4733a2015-10-22 14:24:12 -0700131 setDefaultKey(const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700132
133 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800134 * @brief Add a @p key of @p keyLen bytes with @p keyName and set it as the default key
135 * @throw std::invalid_argument @p keyName does not match identity
136 * @throw Pib::Error the key with the same name already exists.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700137 * @return the default key
138 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800139 const Key&
Yingdi Yufe4733a2015-10-22 14:24:12 -0700140 setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700141
Yingdi Yucbe72b02015-11-25 17:35:37 -0800142private:
Yingdi Yub8f8b342015-04-27 11:06:42 -0700143 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800144 * @brief Check the validity of the instance
145 * @return a shared_ptr when the instance is valid
146 * @throw std::domain_error the instance is invalid
Yingdi Yub8f8b342015-04-27 11:06:42 -0700147 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800148 shared_ptr<detail::IdentityImpl>
149 lock() const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700150
Yingdi Yub8f8b342015-04-27 11:06:42 -0700151private:
Yingdi Yucbe72b02015-11-25 17:35:37 -0800152 weak_ptr<detail::IdentityImpl> m_impl;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700153
154 friend class v2::KeyChain;
Junxiao Shi5759be32017-10-15 00:00:52 +0000155 friend bool operator!=(const Identity&, const Identity&);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700156};
157
Junxiao Shi5759be32017-10-15 00:00:52 +0000158bool
159operator!=(const Identity& lhs, const Identity& rhs);
160
161inline bool
162operator==(const Identity& lhs, const Identity& rhs)
163{
164 return !(lhs != rhs);
165}
166
167std::ostream&
168operator<<(std::ostream& os, const Identity& id);
169
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700170} // namespace pib
171
172using pib::Identity;
173
Yingdi Yub8f8b342015-04-27 11:06:42 -0700174} // namespace security
175} // namespace ndn
176
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700177#endif // NDN_SECURITY_PIB_IDENTITY_HPP