blob: ceff37d1604e87bb8d1c3284dc4591c7c2f29f7a [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.
50 * @param impl The PIB backend implementation.
51 * @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 */
54 IdentityImpl(const Name& identityName, shared_ptr<PibImpl> impl, bool needInit = false);
55
56 /// @brief Get the name of the identity.
57 const Name&
58 getName() const
59 {
60 return m_name;
61 }
62
63 /**
64 * @brief Add a @p key of @p keyLen bytes with @p keyName (in PKCS#8 format).
65 *
66 * 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 -080067 * If a key with the same name already exists, overwrite the key.
Yingdi Yucbe72b02015-11-25 17:35:37 -080068 *
69 * @return the added key.
70 * @throw std::invalid_argument key name does not match identity
Yingdi Yucbe72b02015-11-25 17:35:37 -080071 */
72 Key
73 addKey(const uint8_t* key, size_t keyLen, const Name& keyName);
74
75 /**
76 * @brief Remove a key with @p keyName
77 * @throw std::invalid_argument @p keyName does not match identity
78 */
79 void
80 removeKey(const Name& keyName);
81
82 /**
83 * @brief Get a key with id @p keyName.
84 *
85 * @throw std::invalid_argument @p keyName does not match identity
86 * @throw Pib::Error the key does not exist.
87 */
88 Key
89 getKey(const Name& keyName) const;
90
91 /**
92 * @brief Get all keys for this Identity.
93 */
94 const KeyContainer&
95 getKeys() const;
96
97 /**
98 * @brief Set the key with id @p keyName.
99 * @throw std::invalid_argument @p keyName does not match identity
100 * @throw Pib::Error the key does not exist.
101 * @return The default key
102 */
103 const Key&
104 setDefaultKey(const Name& keyName);
105
106 /**
107 * @brief Add @p key of @p keyLen bytes with @p keyName and set it as the default key
108 * @throw std::invalid_argument @p keyName does not match identity
109 * @throw Pib::Error the key with the same name already exists
110 * @return the default key
111 */
112 const Key&
113 setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName);
114
115 /**
116 * @brief Get the default key for this Identity.
117 * @throw Pib::Error the default key does not exist.
118 */
119 const Key&
120 getDefaultKey() const;
121
122private:
123 Name m_name;
124
125 mutable bool m_isDefaultKeyLoaded;
126 mutable Key m_defaultKey;
127
128 mutable KeyContainer m_keys;
129
130 shared_ptr<PibImpl> m_impl;
131};
132
133} // namespace detail
134} // namespace pib
135} // namespace security
136} // namespace ndn
137
138#endif // NDN_SECURITY_PIB_DETAIL_IDENTITY_IMPL_HPP