ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
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
25namespace ndn::security {
26namespace pib {
27
28Key::Key() noexcept = default;
29
30Key::Key(weak_ptr<KeyImpl> impl) noexcept
31 : m_impl(std::move(impl))
32{
33}
34
35const Name&
37{
38 return lock()->getName();
39}
40
41const Name&
43{
44 return lock()->getIdentity();
45}
46
49{
50 return lock()->getKeyType();
51}
52
53span<const uint8_t>
55{
56 return lock()->getPublicKey();
57}
58
59void
60Key::addCertificate(const Certificate& certificate) const
61{
62 lock()->addCertificate(certificate);
63}
64
65void
66Key::removeCertificate(const Name& certName) const
67{
68 lock()->removeCertificate(certName);
69}
70
71Certificate
72Key::getCertificate(const Name& certName) const
73{
74 return lock()->getCertificate(certName);
75}
76
79{
80 return lock()->getCertificates();
81}
82
83const Certificate&
84Key::setDefaultCertificate(const Name& certName) const
85{
86 return lock()->setDefaultCertificate(certName);
87}
88
89void
90Key::setDefaultCertificate(const Certificate& certificate) const
91{
92 return lock()->setDefaultCertificate(certificate);
93}
94
95const Certificate&
97{
98 return lock()->getDefaultCertificate();
99}
100
101Key::operator bool() const noexcept
102{
103 return !m_impl.expired();
104}
105
106shared_ptr<KeyImpl>
107Key::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
116bool
117Key::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
125Name
126constructKeyName(const Name& identity, const name::Component& keyId)
127{
128 return Name(identity)
130 .append(keyId);
131}
132
133bool
139
140Name
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
const Component & get(ssize_t i) const noexcept
Returns an immutable reference to the component at the specified index.
Definition name.hpp:192
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition name.hpp:241
Name & append(const Component &component)
Append a name component.
Definition name.hpp:308
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
Represents a name component.
Represents an NDN certificate.
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.
STL namespace.