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-2023 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 
23 #include "ndn-cxx/security/pib/impl/key-impl.hpp"
24 
25 namespace ndn::security {
26 namespace pib {
27 
28 Key::Key() noexcept = default;
29 
30 Key::Key(weak_ptr<KeyImpl> impl) noexcept
31  : m_impl(std::move(impl))
32 {
33 }
34 
35 const Name&
36 Key::getName() const
37 {
38  return lock()->getName();
39 }
40 
41 const Name&
43 {
44  return lock()->getIdentity();
45 }
46 
47 KeyType
49 {
50  return lock()->getKeyType();
51 }
52 
53 span<const uint8_t>
55 {
56  return lock()->getPublicKey();
57 }
58 
59 void
60 Key::addCertificate(const Certificate& certificate) const
61 {
62  lock()->addCertificate(certificate);
63 }
64 
65 void
66 Key::removeCertificate(const Name& certName) const
67 {
68  lock()->removeCertificate(certName);
69 }
70 
71 Certificate
72 Key::getCertificate(const Name& certName) const
73 {
74  return lock()->getCertificate(certName);
75 }
76 
79 {
80  return lock()->getCertificates();
81 }
82 
83 const Certificate&
84 Key::setDefaultCertificate(const Name& certName) const
85 {
86  return lock()->setDefaultCertificate(certName);
87 }
88 
89 void
90 Key::setDefaultCertificate(const Certificate& certificate) const
91 {
92  return lock()->setDefaultCertificate(certificate);
93 }
94 
95 const Certificate&
97 {
98  return lock()->getDefaultCertificate();
99 }
100 
101 Key::operator bool() const noexcept
102 {
103  return !m_impl.expired();
104 }
105 
106 shared_ptr<KeyImpl>
107 Key::lock() const
108 {
109  auto impl = m_impl.lock();
110  if (impl == nullptr) {
111  NDN_THROW(std::domain_error("Invalid PIB key instance"));
112  }
113  return impl;
114 }
115 
116 bool
117 Key::equals(const Key& other) const noexcept
118 {
119  return !this->m_impl.owner_before(other.m_impl) &&
120  !other.m_impl.owner_before(this->m_impl);
121 }
122 
123 } // namespace pib
124 
125 Name
126 constructKeyName(const Name& identity, const name::Component& keyId)
127 {
128  return Name(identity)
130  .append(keyId);
131 }
132 
133 bool
134 isValidKeyName(const Name& keyName)
135 {
136  return keyName.size() >= Certificate::MIN_KEY_NAME_LENGTH &&
138 }
139 
140 Name
142 {
143  if (!isValidKeyName(keyName)) {
144  NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` "
145  "does not respect the naming conventions"));
146  }
147 
148  return keyName.getPrefix(-Certificate::MIN_KEY_NAME_LENGTH); // trim everything after and including "KEY"
149 }
150 
151 } // namespace ndn::security
Represents an absolute name.
Definition: name.hpp:45
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:241
size_t size() const noexcept
Returns the number of components.
Definition: name.hpp:180
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:324
const Component & get(ssize_t i) const noexcept
Returns an immutable reference to the component at the specified index.
Definition: name.hpp:192
Represents a name component.
Represents an NDN certificate.
Definition: certificate.hpp:58
static const name::Component KEY_COMPONENT
static constexpr size_t MIN_KEY_NAME_LENGTH
Container of certificates of a key.
KeyType getKeyType() const
Return the key type.
Definition: key.cpp:48
const Certificate & getDefaultCertificate() const
Return the default certificate for this key.
Definition: key.cpp:96
const Name & getIdentity() const
Return the name of the owning identity.
Definition: key.cpp:42
Key() noexcept
Default constructor.
span< const uint8_t > getPublicKey() const
Return the raw public key bits.
Definition: key.cpp:54
Certificate getCertificate(const Name &certName) const
Return the certificate with the given name.
Definition: key.cpp:72
const Name & getName() const
Return the name of the key.
Definition: key.cpp:36
const CertificateContainer & getCertificates() const
Return all the certificates of this key.
Definition: key.cpp:78
#define NDN_THROW(e)
Definition: exception.hpp:56
Contains the ndn-cxx security framework.
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:141
bool isValidKeyName(const Name &keyName)
Check if keyName follow the naming conventions for the key name.
Definition: key.cpp:134
Name constructKeyName(const Name &identity, const name::Component &keyId)
Construct key name based on the appropriate naming conventions.
Definition: key.cpp:126
@ Name
Definition: tlv.hpp:71
KeyType
The type of a cryptographic key.