blob: 228ef1678c71204c95737dda09eba06e9068eb56 [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
Yingdi Yucbe72b02015-11-25 17:35:37 -0800100 /*
101 * @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 Yucbe72b02015-11-25 17:35:37 -0800106 /**
107 * @return True if the identity instance is invalid
108 */
Yingdi Yuc8209892015-06-19 17:47:56 -0700109 bool
110 operator!() const;
111
112NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
Yingdi Yuc8209892015-06-19 17:47:56 -0700113 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800114 * @brief Add a @p key of @p keyLen bytes (in PKCS#8 format) with @p keyName.
115 * @return the handle of added key
116 * @throw std::invalid_argument key name does not match identity
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800117 *
118 * If a key with the same name already exists, overwrite the key.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700119 */
120 Key
Yingdi Yufe4733a2015-10-22 14:24:12 -0700121 addKey(const uint8_t* key, size_t keyLen, const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700122
123 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700124 * @brief Remove a key with @p keyName
Yingdi Yucbe72b02015-11-25 17:35:37 -0800125 * @throw std::invalid_argument @p keyName does not match identity
Yingdi Yub8f8b342015-04-27 11:06:42 -0700126 */
127 void
Yingdi Yufe4733a2015-10-22 14:24:12 -0700128 removeKey(const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700129
130 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800131 * @brief Set an existing key with @p keyName as the default key.
132 * @throw std::invalid_argument @p keyName does not match identity
133 * @throw Pib::Error the key does not exist.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700134 * @return The default key
Yingdi Yub8f8b342015-04-27 11:06:42 -0700135 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800136 const Key&
Yingdi Yufe4733a2015-10-22 14:24:12 -0700137 setDefaultKey(const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700138
139 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800140 * @brief Add a @p key of @p keyLen bytes with @p keyName and set it as the default key
141 * @throw std::invalid_argument @p keyName does not match identity
142 * @throw Pib::Error the key with the same name already exists.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700143 * @return the default key
144 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800145 const Key&
Yingdi Yufe4733a2015-10-22 14:24:12 -0700146 setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName) const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700147
Yingdi Yucbe72b02015-11-25 17:35:37 -0800148private:
Yingdi Yub8f8b342015-04-27 11:06:42 -0700149 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800150 * @brief Check the validity of the instance
151 * @return a shared_ptr when the instance is valid
152 * @throw std::domain_error the instance is invalid
Yingdi Yub8f8b342015-04-27 11:06:42 -0700153 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800154 shared_ptr<detail::IdentityImpl>
155 lock() const;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700156
Yingdi Yub8f8b342015-04-27 11:06:42 -0700157private:
Yingdi Yucbe72b02015-11-25 17:35:37 -0800158 weak_ptr<detail::IdentityImpl> m_impl;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700159
160 friend class v2::KeyChain;
Junxiao Shi5759be32017-10-15 00:00:52 +0000161 friend bool operator!=(const Identity&, const Identity&);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700162};
163
Junxiao Shi5759be32017-10-15 00:00:52 +0000164bool
165operator!=(const Identity& lhs, const Identity& rhs);
166
167inline bool
168operator==(const Identity& lhs, const Identity& rhs)
169{
170 return !(lhs != rhs);
171}
172
173std::ostream&
174operator<<(std::ostream& os, const Identity& id);
175
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700176} // namespace pib
177
178using pib::Identity;
179
Yingdi Yub8f8b342015-04-27 11:06:42 -0700180} // namespace security
181} // namespace ndn
182
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700183#endif // NDN_SECURITY_PIB_IDENTITY_HPP