NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
key-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 
26 
27 namespace ndn {
28 namespace security {
29 namespace pib {
30 namespace detail {
31 
32 KeyImpl::KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> pibImpl)
33  : m_identity(v2::extractIdentityFromKeyName(keyName))
34  , m_keyName(keyName)
35  , m_key(key, keyLen)
36  , m_pib(std::move(pibImpl))
37  , m_certificates(keyName, m_pib)
38  , m_isDefaultCertificateLoaded(false)
39 {
40  BOOST_ASSERT(m_pib != nullptr);
41 
42  transform::PublicKey publicKey;
43  try {
44  publicKey.loadPkcs8(key, keyLen);
45  }
46  catch (const transform::PublicKey::Error&) {
47  NDN_THROW_NESTED(std::invalid_argument("Invalid key bits"));
48  }
49  m_keyType = publicKey.getKeyType();
50 
51  m_pib->addKey(m_identity, m_keyName, key, keyLen);
52 }
53 
54 KeyImpl::KeyImpl(const Name& keyName, shared_ptr<PibImpl> pibImpl)
55  : m_identity(v2::extractIdentityFromKeyName(keyName))
56  , m_keyName(keyName)
57  , m_pib(std::move(pibImpl))
58  , m_certificates(keyName, m_pib)
59  , m_isDefaultCertificateLoaded(false)
60 {
61  BOOST_ASSERT(m_pib != nullptr);
62 
63  m_key = m_pib->getKeyBits(m_keyName);
64 
66  key.loadPkcs8(m_key.data(), m_key.size());
67  m_keyType = key.getKeyType();
68 }
69 
70 void
72 {
73  BOOST_ASSERT(m_certificates.isConsistent());
74  m_certificates.add(certificate);
75 }
76 
77 void
79 {
80  BOOST_ASSERT(m_certificates.isConsistent());
81 
82  if (m_isDefaultCertificateLoaded && m_defaultCertificate.getName() == certName)
83  m_isDefaultCertificateLoaded = false;
84 
85  m_certificates.remove(certName);
86 }
87 
89 KeyImpl::getCertificate(const Name& certName) const
90 {
91  BOOST_ASSERT(m_certificates.isConsistent());
92  return m_certificates.get(certName);
93 }
94 
97 {
98  BOOST_ASSERT(m_certificates.isConsistent());
99  return m_certificates;
100 }
101 
102 const v2::Certificate&
104 {
105  BOOST_ASSERT(m_certificates.isConsistent());
106 
107  m_defaultCertificate = m_certificates.get(certName);
108  m_pib->setDefaultCertificateOfKey(m_keyName, certName);
109  m_isDefaultCertificateLoaded = true;
110  return m_defaultCertificate;
111 }
112 
113 const v2::Certificate&
115 {
116  addCertificate(certificate);
117  return setDefaultCertificate(certificate.getName());
118 }
119 
120 const v2::Certificate&
122 {
123  BOOST_ASSERT(m_certificates.isConsistent());
124 
125  if (!m_isDefaultCertificateLoaded) {
126  m_defaultCertificate = m_pib->getDefaultCertificateOfKey(m_keyName);
127  m_isDefaultCertificateLoaded = true;
128  }
129  BOOST_ASSERT(m_pib->getDefaultCertificateOfKey(m_keyName).wireEncode() == m_defaultCertificate.wireEncode());
130 
131  return m_defaultCertificate;
132 }
133 
134 } // namespace detail
135 } // namespace pib
136 } // namespace security
137 } // namespace ndn
ndn::security::pib::CertificateContainer::add
void add(const v2::Certificate &certificate)
Add certificate into the container.
Definition: certificate-container.cpp:116
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::security::transform::PublicKey::loadPkcs8
void loadPkcs8(const uint8_t *buf, size_t size)
Load the public key in PKCS#8 format from a buffer buf.
Definition: public-key.cpp:88
ndn::security::pib::detail::KeyImpl::KeyImpl
KeyImpl(const Name &keyName, const uint8_t *key, size_t keyLen, shared_ptr< PibImpl > pibImpl)
Create a KeyImpl with keyName.
Definition: key-impl.cpp:32
ndn::security::pib::CertificateContainer
Container of certificates of a key.
Definition: certificate-container.hpp:48
public-key.hpp
ndn::Data::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder in NDN Packet Format v0.2.
Definition: data.cpp:48
ndn::Data::getName
const Name & getName() const
Get name.
Definition: data.hpp:124
ndn::security::v2::extractIdentityFromKeyName
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:160
ndn::security::pib::detail::KeyImpl::setDefaultCertificate
const v2::Certificate & setDefaultCertificate(const Name &certName)
Set an existing certificate with name certName as the default certificate.
Definition: key-impl.cpp:103
NDN_THROW_NESTED
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
ndn::security::pib::detail::KeyImpl::getCertificate
v2::Certificate getCertificate(const Name &certName) const
Get a certificate with certName.
Definition: key-impl.cpp:89
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::security::transform::PublicKey::getKeyType
KeyType getKeyType() const
Get the type of the public key.
Definition: public-key.cpp:72
ndn::security::v2::Certificate
The certificate following the certificate format naming convention.
Definition: certificate.hpp:82
ndn::security::pib::detail::KeyImpl::removeCertificate
void removeCertificate(const Name &certName)
Remove a certificate with certName.
Definition: key-impl.cpp:78
ndn::security::pib::detail::KeyImpl::getDefaultCertificate
const v2::Certificate & getDefaultCertificate() const
Get the default certificate for this key.
Definition: key-impl.cpp:121
ndn::security::pib::CertificateContainer::remove
void remove(const Name &certName)
Remove a certificate with certName from the container.
Definition: certificate-container.cpp:129
key-impl.hpp
ndn::security::pib::CertificateContainer::get
v2::Certificate get(const Name &certName) const
Get a certificate with certName from the container.
Definition: certificate-container.cpp:143
pib.hpp
ndn::security::pib::detail::KeyImpl::addCertificate
void addCertificate(const v2::Certificate &certificate)
Add certificate.
Definition: key-impl.cpp:71
ndn::security::transform::PublicKey::Error
Definition: public-key.hpp:39
ndn::security::transform::PublicKey
Abstraction of public key in crypto transformation.
Definition: public-key.hpp:36
ndn::security::pib::detail::KeyImpl::getCertificates
const CertificateContainer & getCertificates() const
Get all the certificates for this key.
Definition: key-impl.cpp:96
pib-impl.hpp
ndn::security::pib::CertificateContainer::isConsistent
bool isConsistent() const
Check if the container is consistent with the backend storage.
Definition: certificate-container.cpp:161
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34