key.hpp
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 
22 #ifndef NDN_CXX_SECURITY_PIB_KEY_HPP
23 #define NDN_CXX_SECURITY_PIB_KEY_HPP
24 
27 
28 namespace ndn::security {
29 
30 class KeyChain;
31 
32 namespace pib {
33 
34 class KeyContainer;
35 class KeyImpl;
36 
44 class Key : private boost::equality_comparable<Key>
45 {
46 public:
67  Key() noexcept;
68 
72  const Name&
73  getName() const;
74 
78  const Name&
79  getIdentity() const;
80 
84  KeyType
85  getKeyType() const;
86 
90  span<const uint8_t>
91  getPublicKey() const;
92 
99  getCertificate(const Name& certName) const;
100 
104  const CertificateContainer&
105  getCertificates() const;
106 
111  const Certificate&
112  getDefaultCertificate() const;
113 
117  explicit
118  operator bool() const noexcept;
119 
120 NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations are accessible only by KeyChain
131  void
132  addCertificate(const Certificate& certificate) const;
133 
138  void
139  removeCertificate(const Name& certName) const;
140 
147  const Certificate&
148  setDefaultCertificate(const Name& certName) const;
149 
154  void
155  setDefaultCertificate(const Certificate& certificate) const;
156 
157 NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // private interface for KeyContainer
158  explicit
159  Key(weak_ptr<KeyImpl> impl) noexcept;
160 
161 private:
167  shared_ptr<KeyImpl>
168  lock() const;
169 
170  bool
171  equals(const Key& other) const noexcept;
172 
173  // NOTE
174  // The following "hidden friend" non-member operators are available
175  // via argument-dependent lookup only and must be defined inline.
176  // boost::equality_comparable provides != operator.
177 
178  friend bool
179  operator==(const Key& lhs, const Key& rhs) noexcept
180  {
181  return lhs.equals(rhs);
182  }
183 
184  friend std::ostream&
185  operator<<(std::ostream& os, const Key& key)
186  {
187  if (key)
188  return os << key.getName();
189  else
190  return os << "(empty)";
191  }
192 
193 private:
194  weak_ptr<KeyImpl> m_impl;
195 
196  friend KeyChain;
197  friend KeyContainer;
198 };
199 
200 } // namespace pib
201 
202 using pib::Key;
203 
207 Name
208 constructKeyName(const Name& identity, const name::Component& keyId);
209 
213 bool
214 isValidKeyName(const Name& keyName);
215 
219 Name
220 extractIdentityFromKeyName(const Name& keyName);
221 
222 } // namespace ndn::security
223 
224 #endif // NDN_CXX_SECURITY_PIB_KEY_HPP
Represents an absolute name.
Definition: name.hpp:45
Represents a name component.
Represents an NDN certificate.
Definition: certificate.hpp:58
The main interface for signing key management.
Definition: key-chain.hpp:87
Container of certificates of a key.
Container of keys of an identity.
Frontend handle for a key in the PIB.
Definition: key.hpp:45
friend std::ostream & operator<<(std::ostream &os, const Key &key)
Definition: key.hpp:185
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_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:49
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
KeyType
The type of a cryptographic key.