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