NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
key.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 
30 Key::Key() = default;
31 
32 Key::Key(weak_ptr<detail::KeyImpl> impl)
33  : m_impl(impl)
34 {
35 }
36 
37 const Name&
38 Key::getName() const
39 {
40  return lock()->getName();
41 }
42 
43 const Name&
45 {
46  return lock()->getIdentity();
47 }
48 
49 KeyType
51 {
52  return lock()->getKeyType();
53 }
54 
55 const Buffer&
57 {
58  return lock()->getPublicKey();
59 }
60 
61 void
62 Key::addCertificate(const v2::Certificate& certificate) const
63 {
64  return lock()->addCertificate(certificate);
65 }
66 
67 void
68 Key::removeCertificate(const Name& certName) const
69 {
70  return lock()->removeCertificate(certName);
71 }
72 
74 Key::getCertificate(const Name& certName) const
75 {
76  return lock()->getCertificate(certName);
77 }
78 
81 {
82  return lock()->getCertificates();
83 }
84 
85 const v2::Certificate&
86 Key::setDefaultCertificate(const Name& certName) const
87 {
88  return lock()->setDefaultCertificate(certName);
89 }
90 
91 const v2::Certificate&
93 {
94  return lock()->setDefaultCertificate(certificate);
95 }
96 
97 const v2::Certificate&
99 {
100  return lock()->getDefaultCertificate();
101 }
102 
103 Key::operator bool() const
104 {
105  return !m_impl.expired();
106 }
107 
108 shared_ptr<detail::KeyImpl>
109 Key::lock() const
110 {
111  auto impl = m_impl.lock();
112 
113  if (impl == nullptr) {
114  NDN_THROW(std::domain_error("Invalid key instance"));
115  }
116 
117  return impl;
118 }
119 
120 bool
121 operator!=(const Key& lhs, const Key& rhs)
122 {
123  return lhs.m_impl.owner_before(rhs.m_impl) || rhs.m_impl.owner_before(lhs.m_impl);
124 }
125 
126 std::ostream&
127 operator<<(std::ostream& os, const Key& key)
128 {
129  if (key) {
130  os << key.getName();
131  }
132  else {
133  os << "(empty)";
134  }
135  return os;
136 }
137 
138 } // namespace pib
139 
140 namespace v2 {
141 
142 Name
143 constructKeyName(const Name& identity, const name::Component& keyId)
144 {
145  Name keyName = identity;
146  keyName
148  .append(keyId);
149  return keyName;
150 }
151 
152 bool
153 isValidKeyName(const Name& keyName)
154 {
155  return (keyName.size() >= Certificate::MIN_KEY_NAME_LENGTH &&
157 }
158 
159 Name
161 {
162  if (!isValidKeyName(keyName)) {
163  NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` "
164  "does not respect the naming conventions"));
165  }
166 
167  return keyName.getPrefix(-Certificate::MIN_KEY_NAME_LENGTH); // trim everything after and including "KEY"
168 }
169 
170 } // namespace v2
171 } // namespace security
172 } // namespace ndn
ndn::KeyType
KeyType
The type of a cryptographic key.
Definition: security-common.hpp:85
key.hpp
ndn::security::pib::Key::setDefaultCertificate
const v2::Certificate & setDefaultCertificate(const Name &certName) const
Set an existing certificate with certName as the default certificate.
Definition: key.cpp:86
certificate.hpp
ndn::security::pib::Key::getDefaultCertificate
const v2::Certificate & getDefaultCertificate() const
Get the default certificate for this Key.
Definition: key.cpp:98
ndn::security::v2::isValidKeyName
bool isValidKeyName(const Name &keyName)
Check if keyName follow the naming conventions for the key name.
Definition: key.cpp:153
ndn::Buffer
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:41
ndn::Name::size
size_t size() const
Returns the number of components.
Definition: name.hpp:153
ndn::security::pib::CertificateContainer
Container of certificates of a key.
Definition: certificate-container.hpp:48
ndn::security::v2::Certificate::KEY_COMPONENT
static const name::Component KEY_COMPONENT
Definition: certificate.hpp:172
ndn::security::v2::constructKeyName
Name constructKeyName(const Name &identity, const name::Component &keyId)
Construct key name based on the appropriate naming conventions.
Definition: key.cpp:143
ndn::security::pib::Key::getKeyType
KeyType getKeyType() const
Get key type.
Definition: key.cpp:50
ndn::security::v2::extractIdentityFromKeyName
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:160
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::security::v2::Certificate
The certificate following the certificate format naming convention.
Definition: certificate.hpp:82
ndn::security::pib::operator!=
bool operator!=(const Identity &lhs, const Identity &rhs)
Definition: identity.cpp:101
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::Name::append
Name & append(const Component &component)
Append a component.
Definition: name.hpp:277
ndn::Name::getPrefix
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:211
ndn::security::pib::Key::removeCertificate
void removeCertificate(const Name &certName) const
Remove a certificate with certName.
Definition: key.cpp:68
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
ndn::security::pib::operator<<
std::ostream & operator<<(std::ostream &os, const Identity &id)
Definition: identity.cpp:107
key-impl.hpp
ndn::security::pib::Key::getCertificate
v2::Certificate getCertificate(const Name &certName) const
Get a certificate with certName.
Definition: key.cpp:74
ndn::security::pib::Key::getCertificates
const CertificateContainer & getCertificates() const
Get all certificates for this key.
Definition: key.cpp:80
ndn::name::Component
Represents a name component.
Definition: name-component.hpp:94
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::Key::getIdentity
const Name & getIdentity() const
Get the name of the belonging identity.
Definition: key.cpp:44
ndn::security::v2::Certificate::MIN_KEY_NAME_LENGTH
static const size_t MIN_KEY_NAME_LENGTH
Definition: certificate.hpp:171
ndn::security::pib::Key::getPublicKey
const Buffer & getPublicKey() const
Get public key bits.
Definition: key.cpp:56
ndn::Name::get
const Component & get(ssize_t i) const
Returns an immutable reference to the component at the specified index.
Definition: name.hpp:164
ndn::security::pib::Key::Key
Key()
Default Constructor.
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34