NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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; -*- */
2 /*
3  * Copyright (c) 2013-2019 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 
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  NDN_THROW(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
ndn::security::pib::KeyContainer::add
Key add(const uint8_t *key, size_t keyLen, const Name &keyName)
Add key of keyLen bytes with keyName into the container.
Definition: key-container.cpp:116
identity-impl.hpp
ndn::security::pib::detail::IdentityImpl::getKeys
const KeyContainer & getKeys() const
Get all keys for this Identity.
Definition: identity-impl.cpp:73
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::security::pib::detail::IdentityImpl::IdentityImpl
IdentityImpl(const Name &identityName, shared_ptr< PibImpl > pibImpl, bool needInit=false)
Create an Identity with identityName.
Definition: identity-impl.cpp:31
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::security::pib::KeyContainer::isConsistent
bool isConsistent() const
Check if the container is consistent with the backend storage.
Definition: key-container.cpp:165
ndn::security::pib::detail::IdentityImpl::getKey
Key getKey(const Name &keyName) const
Get a key with id keyName.
Definition: identity-impl.cpp:66
ndn::security::pib::KeyContainer::remove
void remove(const Name &keyName)
Remove a key with keyName from the container.
Definition: key-container.cpp:130
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::security::pib::Pib::Error
represents a semantic error
Definition: pib.hpp:57
ndn::security::pib::detail::IdentityImpl::removeKey
void removeKey(const Name &keyName)
Remove a key with keyName.
Definition: identity-impl.cpp:55
ndn::security::pib::detail::IdentityImpl::addKey
Key addKey(const uint8_t *key, size_t keyLen, const Name &keyName)
Add a key of keyLen bytes with keyName (in PKCS#8 format).
Definition: identity-impl.cpp:48
ndn::Name::toUri
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Definition: name.cpp:348
pib.hpp
ndn::security::pib::Key::getName
const Name & getName() const
Get key name.
Definition: key.cpp:38
ndn::security::pib::Key
A frontend handle of a key instance.
Definition: key.hpp:50
ndn::security::pib::detail::IdentityImpl::setDefaultKey
const Key & setDefaultKey(const Name &keyName)
Set the key with id keyName.
Definition: identity-impl.cpp:80
ndn::security::pib::KeyContainer
Container of keys of an identity.
Definition: key-container.hpp:49
ndn::security::pib::detail::IdentityImpl::getDefaultKey
const Key & getDefaultKey() const
Get the default key for this Identity.
Definition: identity-impl.cpp:98
pib-impl.hpp
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::security::pib::KeyContainer::get
Key get(const Name &keyName) const
Get a key with keyName from the container.
Definition: key-container.cpp:143