NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
identity.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "identity.hpp"
23 #include "pib-impl.hpp"
24 #include "pib.hpp"
25 
26 namespace ndn {
27 namespace security {
28 
30 
32  : m_hasDefaultKey(false)
33  , m_needRefreshKeys(false)
34  , m_impl(nullptr)
35 {
36 }
37 
38 Identity::Identity(const Name& identityName, shared_ptr<PibImpl> impl, bool needInit)
39  : m_name(identityName)
40  , m_hasDefaultKey(false)
41  , m_needRefreshKeys(true)
42  , m_impl(impl)
43 {
44  validityCheck();
45 
46  if (needInit)
47  m_impl->addIdentity(m_name);
48  else if (!m_impl->hasIdentity(m_name))
49  BOOST_THROW_EXCEPTION(Pib::Error("Identity: " + m_name.toUri() + " does not exist"));
50 }
51 
52 const Name&
54 {
55  validityCheck();
56 
57  return m_name;
58 }
59 
60 Key
61 Identity::addKey(const PublicKey& publicKey, const name::Component& keyId)
62 {
63  validityCheck();
64 
65  name::Component actualKeyId = keyId;
66  if (actualKeyId == EMPTY_KEY_ID) {
67  const Block& digest = publicKey.computeDigest();
68  actualKeyId = name::Component(digest.wire(), digest.size());
69  }
70 
71  if (!m_needRefreshKeys && m_keys.find(actualKeyId) == m_keys.end()) {
72  // if we have already loaded all the keys, but the new key is not one of them
73  // the KeyContainer should be refreshed
74  m_needRefreshKeys = true;
75  }
76 
77  return Key(m_name, actualKeyId, publicKey, m_impl);
78 }
79 
80 void
82 {
83  validityCheck();
84 
85  if (m_hasDefaultKey && m_defaultKey.getKeyId() == keyId)
86  m_hasDefaultKey = false;
87 
88  m_impl->removeKey(m_name, keyId);
89  m_needRefreshKeys = true;
90 }
91 
92 Key
93 Identity::getKey(const name::Component& keyId) const
94 {
95  validityCheck();
96 
97  return Key(m_name, keyId, m_impl);
98 }
99 
100 const KeyContainer&
102 {
103  validityCheck();
104 
105  if (m_needRefreshKeys) {
106  m_keys = KeyContainer(m_name, m_impl->getKeysOfIdentity(m_name), m_impl);
107  m_needRefreshKeys = false;
108  }
109 
110  return m_keys;
111 }
112 
113 Key&
115 {
116  validityCheck();
117 
118  m_defaultKey = Key(m_name, keyId, m_impl);
119  m_hasDefaultKey = true;
120 
121  m_impl->setDefaultKeyOfIdentity(m_name, keyId);
122  return m_defaultKey;
123 }
124 
125 Key&
126 Identity::setDefaultKey(const PublicKey& publicKey, const name::Component& keyId)
127 {
128  const Key& keyEntry = addKey(publicKey, keyId);
129  return setDefaultKey(keyEntry.getKeyId());
130 }
131 
132 Key&
134 {
135  validityCheck();
136 
137  if (!m_hasDefaultKey) {
138  m_defaultKey = Key(m_name, m_impl->getDefaultKeyOfIdentity(m_name), m_impl);
139  m_hasDefaultKey = true;
140  }
141 
142  return m_defaultKey;
143 }
144 
145 Identity::operator bool() const
146 {
147  return !(this->operator!());
148 }
149 
150 bool
152 {
153  return (m_impl == nullptr);
154 }
155 
156 void
158 {
159  if (m_impl == nullptr)
160  BOOST_THROW_EXCEPTION(std::domain_error("Invalid Identity instance"));
161 }
162 
163 } // namespace security
164 } // namespace ndn
Key & getDefaultKey() const
Get the default key for this Identity.
Definition: identity.cpp:133
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE const name::Component & keyId
Definition: identity.hpp:119
Copyright (c) 2011-2015 Regents of the University of California.
std::string toUri() const
Encode this name as a URI.
Definition: name.cpp:183
const name::Component & getKeyId() const
Get the key id of the key.
Definition: key.cpp:87
bool operator!() const
Check if the Identity instance is invalid.
Definition: identity.cpp:151
const KeyContainer & getKeys() const
Get all the keys for this Identity.
Definition: identity.cpp:101
const uint8_t * wire() const
Definition: block.cpp:495
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
represents a key
Definition: key.hpp:45
Key & setDefaultKey(const name::Component &keyId)
Set the key with id keyId as the default key.
Definition: identity.cpp:114
represents a semantic error
Definition: pib.hpp:55
A handler to search or enumerate keys of an identity.
size_t size() const
Definition: block.cpp:504
const_iterator end() const
Identity()
Default Constructor.
Definition: identity.cpp:31
void validityCheck() const
Check the validity of this instance.
Definition: identity.cpp:157
const Block & computeDigest() const
Definition: public-key.cpp:46
const Name & getName() const
Get the name of the identity.
Definition: identity.cpp:53
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE shared_ptr< PibImpl > impl
Definition: identity.hpp:161
void removeKey(const name::Component &keyId)
Remove a key.
Definition: identity.cpp:81
Name abstraction to represent an absolute name.
Definition: name.hpp:46
static const name::Component EMPTY_KEY_ID
The default value of keyId when add a new key.
Definition: identity.hpp:177
const_iterator find(const name::Component &keyId) const
Component holds a read-only name component value.
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE shared_ptr< PibImpl > bool needInit
Definition: identity.hpp:161
Key getKey(const name::Component &keyId) const
Get a key with id keyId.
Definition: identity.cpp:93