NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
identity-impl.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "identity-impl.hpp"
23 #include "../pib-impl.hpp"
24 #include "../pib.hpp"
25 
26 namespace ndn {
27 namespace security {
28 namespace pib {
29 namespace detail {
30 
31 IdentityImpl::IdentityImpl(const Name& identityName, shared_ptr<PibImpl> pibImpl, bool needInit)
32  : m_name(identityName)
33  , m_pib(std::move(pibImpl))
34  , m_keys(identityName, m_pib)
35  , m_isDefaultKeyLoaded(false)
36 {
37  BOOST_ASSERT(m_pib != nullptr);
38 
39  if (needInit) {
40  m_pib->addIdentity(m_name);
41  }
42  else if (!m_pib->hasIdentity(m_name)) {
43  BOOST_THROW_EXCEPTION(Pib::Error("Identity " + m_name.toUri() + " does not exist"));
44  }
45 }
46 
47 Key
48 IdentityImpl::addKey(const uint8_t* key, size_t keyLen, const Name& keyName)
49 {
50  BOOST_ASSERT(m_keys.isConsistent());
51  return m_keys.add(key, keyLen, keyName);
52 }
53 
54 void
56 {
57  BOOST_ASSERT(m_keys.isConsistent());
58 
59  if (m_isDefaultKeyLoaded && m_defaultKey.getName() == keyName)
60  m_isDefaultKeyLoaded = false;
61 
62  m_keys.remove(keyName);
63 }
64 
65 Key
66 IdentityImpl::getKey(const Name& keyName) const
67 {
68  BOOST_ASSERT(m_keys.isConsistent());
69  return m_keys.get(keyName);
70 }
71 
72 const KeyContainer&
74 {
75  BOOST_ASSERT(m_keys.isConsistent());
76  return m_keys;
77 }
78 
79 const Key&
81 {
82  BOOST_ASSERT(m_keys.isConsistent());
83 
84  m_defaultKey = m_keys.get(keyName);
85  m_isDefaultKeyLoaded = true;
86  m_pib->setDefaultKeyOfIdentity(m_name, keyName);
87  return m_defaultKey;
88 }
89 
90 const Key&
91 IdentityImpl::setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName)
92 {
93  addKey(key, keyLen, keyName);
94  return setDefaultKey(keyName);
95 }
96 
97 const Key&
99 {
100  BOOST_ASSERT(m_keys.isConsistent());
101 
102  if (!m_isDefaultKeyLoaded) {
103  m_defaultKey = m_keys.get(m_pib->getDefaultKeyOfIdentity(m_name));
104  m_isDefaultKeyLoaded = true;
105  }
106  BOOST_ASSERT(m_pib->getDefaultKeyOfIdentity(m_name) == m_defaultKey.getName());
107 
108  return m_defaultKey;
109 }
110 
111 } // namespace detail
112 } // namespace pib
113 } // namespace security
114 } // namespace ndn
const Name & getName() const
Get key name.
Definition: key.cpp:38
Copyright (c) 2011-2015 Regents of the University of California.
represents a semantic error
Definition: pib.hpp:56
void remove(const Name &keyName)
Remove a key with keyName from the container.
STL namespace.
bool isConsistent() const
Check if the container is consistent with the backend storage.
const KeyContainer & getKeys() const
Get all keys for this Identity.
const Key & setDefaultKey(const Name &keyName)
Set the key with id keyName.
Catch-all error for security policy errors that don&#39;t fit in other categories.
Definition: base.hpp:79
std::string toUri() const
Get URI representation of the name.
Definition: name.cpp:122
Key getKey(const Name &keyName) const
Get a key with id keyName.
Container of keys of an identity.
A frontend handle of a key instance.
Definition: key.hpp:49
IdentityImpl(const Name &identityName, shared_ptr< PibImpl > pibImpl, bool needInit=false)
Create an Identity with identityName.
Key add(const uint8_t *key, size_t keyLen, const Name &keyName)
Add key of keyLen bytes with keyName into the container.
Represents an absolute name.
Definition: name.hpp:42
Key get(const Name &keyName) const
Get a key with keyName from the container.
void removeKey(const Name &keyName)
Remove a key with keyName.
Key addKey(const uint8_t *key, size_t keyLen, const Name &keyName)
Add a key of keyLen bytes with keyName (in PKCS#8 format).
const Key & getDefaultKey() const
Get the default key for this Identity.