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-2021 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(std::move(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 span<const uint8_t>
57 {
58  return lock()->getPublicKey();
59 }
60 
61 void
62 Key::addCertificate(const Certificate& certificate) const
63 {
64  lock()->addCertificate(certificate);
65 }
66 
67 void
68 Key::removeCertificate(const Name& certName) const
69 {
70  lock()->removeCertificate(certName);
71 }
72 
73 Certificate
74 Key::getCertificate(const Name& certName) const
75 {
76  return lock()->getCertificate(certName);
77 }
78 
81 {
82  return lock()->getCertificates();
83 }
84 
85 const Certificate&
86 Key::setDefaultCertificate(const Name& certName) const
87 {
88  return lock()->setDefaultCertificate(certName);
89 }
90 
91 const Certificate&
92 Key::setDefaultCertificate(const Certificate& certificate) const
93 {
94  return lock()->setDefaultCertificate(certificate);
95 }
96 
97 const 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 inline namespace v2 {
141 
142 Name
143 constructKeyName(const Name& identity, const name::Component& keyId)
144 {
145  Name keyName = identity;
146  keyName
147  .append(Certificate::KEY_COMPONENT)
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 &&
156  keyName.get(-Certificate::MIN_KEY_NAME_LENGTH) == Certificate::KEY_COMPONENT);
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 } // inline namespace v2
171 } // namespace security
172 } // namespace ndn
std::ostream & operator<<(std::ostream &os, const Identity &id)
Definition: identity.cpp:107
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:209
Copyright (c) 2011-2015 Regents of the University of California.
span< const uint8_t > getPublicKey() const
Get public key bits.
Definition: key.cpp:56
const Component & get(ssize_t i) const
Returns an immutable reference to the component at the specified index.
Definition: name.hpp:162
KeyType getKeyType() const
Get key type.
Definition: key.cpp:50
STL namespace.
Name & append(const Component &component)
Append a component.
Definition: name.hpp:275
bool isValidKeyName(const Name &keyName)
Check if keyName follow the naming conventions for the key name.
Definition: key.cpp:153
#define NDN_THROW(e)
Definition: exception.hpp:61
const Certificate & setDefaultCertificate(const Name &certName) const
Set an existing certificate with certName as the default certificate.
Definition: key.cpp:86
KeyType
The type of a cryptographic key.
Certificate getCertificate(const Name &certName) const
Get a certificate with certName.
Definition: key.cpp:74
Container of certificates of a key.
A frontend handle of a key instance.
Definition: key.hpp:49
Represents an absolute name.
Definition: name.hpp:41
const Name & getName() const
Get key name.
Definition: key.cpp:38
size_t size() const
Returns the number of components.
Definition: name.hpp:151
Represents a name component.
Name constructKeyName(const Name &identity, const name::Component &keyId)
Construct key name based on the appropriate naming conventions.
Definition: key.cpp:143
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:349
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:160
void removeCertificate(const Name &certName) const
Remove a certificate with certName.
Definition: key.cpp:68
Key()
Default Constructor.
friend bool operator!=(const Key &, const Key &)
Definition: key.cpp:121
const Name & getIdentity() const
Get the name of the belonging identity.
Definition: key.cpp:44
const Certificate & getDefaultCertificate() const
Get the default certificate for this Key.
Definition: key.cpp:98
const CertificateContainer & getCertificates() const
Get all certificates for this key.
Definition: key.cpp:80