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-2018 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  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
Copyright (c) 2011-2015 Regents of the University of California.
std::string toUri() const
Get URI representation of the name.
Definition: name.cpp:116
represents a semantic error
Definition: pib.hpp:56
void remove(const Name &keyName)
Remove a key with keyName from the container.
const Key & setDefaultKey(const Name &keyName)
Set the key with id keyName.
const KeyContainer & getKeys() const
Get all keys for this Identity.
Container of keys of an identity.
A frontend handle of a key instance.
Definition: key.hpp:49
Key get(const Name &keyName) const
Get a key with keyName from the container.
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.
bool isConsistent() const
Check if the container is consistent with the backend storage.
Represents an absolute name.
Definition: name.hpp:43
const Name & getName() const
Get key name.
Definition: key.cpp:38
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).
Key getKey(const Name &keyName) const
Get a key with id keyName.
const Key & getDefaultKey() const
Get the default key for this Identity.