blob: b4823a112024cad556e4adc7ff4bf7668564487e [file] [log] [blame]
Yingdi Yucbe72b02015-11-25 17:35:37 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 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_PIB_DETAIL_IDENTITY_IMPL_HPP
23#define NDN_SECURITY_PIB_DETAIL_IDENTITY_IMPL_HPP
24
25#include "../key-container.hpp"
26
27namespace ndn {
28namespace security {
29namespace pib {
30
31class PibImpl;
32
33namespace detail {
34
35/**
36 * @brief Backend instance of Identity
37 *
38 * An Identity has only one backend instance, but may have multiple frontend handles.
39 * Each frontend handle is associated with the only one backend IdentityImpl.
40 *
41 * @throw PibImpl::Error when underlying implementation has non-semantic error.
42 */
43class IdentityImpl : noncopyable
44{
45public:
46 /**
47 * @brief Create an Identity with @p identityName.
48 *
49 * @param identityName The name of the Identity.
Davide Pesavento50f66752017-05-15 20:57:12 -040050 * @param pibImpl The PIB backend implementation.
Yingdi Yucbe72b02015-11-25 17:35:37 -080051 * @param needInit If true, create the identity in backend when the identity does not exist.
52 * Otherwise, throw Pib::Error when the identity does not exist.
53 */
Davide Pesavento50f66752017-05-15 20:57:12 -040054 IdentityImpl(const Name& identityName, shared_ptr<PibImpl> pibImpl, bool needInit = false);
Yingdi Yucbe72b02015-11-25 17:35:37 -080055
Davide Pesavento50f66752017-05-15 20:57:12 -040056 /**
57 * @brief Get the name of the identity.
58 */
Yingdi Yucbe72b02015-11-25 17:35:37 -080059 const Name&
60 getName() const
61 {
62 return m_name;
63 }
64
65 /**
66 * @brief Add a @p key of @p keyLen bytes with @p keyName (in PKCS#8 format).
67 *
68 * If no default key is set before, the new key will be set as the default key of the identity.
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -080069 * If a key with the same name already exists, overwrite the key.
Yingdi Yucbe72b02015-11-25 17:35:37 -080070 *
71 * @return the added key.
72 * @throw std::invalid_argument key name does not match identity
Yingdi Yucbe72b02015-11-25 17:35:37 -080073 */
74 Key
75 addKey(const uint8_t* key, size_t keyLen, const Name& keyName);
76
77 /**
78 * @brief Remove a key with @p keyName
79 * @throw std::invalid_argument @p keyName does not match identity
80 */
81 void
82 removeKey(const Name& keyName);
83
84 /**
85 * @brief Get a key with id @p keyName.
86 *
87 * @throw std::invalid_argument @p keyName does not match identity
88 * @throw Pib::Error the key does not exist.
89 */
90 Key
91 getKey(const Name& keyName) const;
92
93 /**
94 * @brief Get all keys for this Identity.
95 */
96 const KeyContainer&
97 getKeys() const;
98
99 /**
100 * @brief Set the key with id @p keyName.
101 * @throw std::invalid_argument @p keyName does not match identity
102 * @throw Pib::Error the key does not exist.
103 * @return The default key
104 */
105 const Key&
106 setDefaultKey(const Name& keyName);
107
108 /**
109 * @brief Add @p key of @p keyLen bytes with @p keyName and set it as the default key
110 * @throw std::invalid_argument @p keyName does not match identity
111 * @throw Pib::Error the key with the same name already exists
112 * @return the default key
113 */
114 const Key&
115 setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName);
116
117 /**
118 * @brief Get the default key for this Identity.
119 * @throw Pib::Error the default key does not exist.
120 */
121 const Key&
122 getDefaultKey() const;
123
124private:
125 Name m_name;
126
Davide Pesavento50f66752017-05-15 20:57:12 -0400127 shared_ptr<PibImpl> m_pib;
128
129 KeyContainer m_keys;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800130 mutable bool m_isDefaultKeyLoaded;
131 mutable Key m_defaultKey;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800132};
133
134} // namespace detail
135} // namespace pib
136} // namespace security
137} // namespace ndn
138
139#endif // NDN_SECURITY_PIB_DETAIL_IDENTITY_IMPL_HPP