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-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 
22 #ifndef NDN_CXX_SECURITY_PIB_KEY_HPP
23 #define NDN_CXX_SECURITY_PIB_KEY_HPP
24 
27 
28 namespace ndn {
29 namespace security {
30 
31 inline namespace v2 {
32 class KeyChain;
33 } // inline namespace v2
34 
35 namespace pib {
36 
37 class KeyContainer;
38 
39 namespace detail {
40 class KeyImpl;
41 } // namespace detail
42 
50 class Key
51 {
52 public:
73  Key() noexcept;
74 
78  const Name&
79  getName() const;
80 
84  const Name&
85  getIdentity() const;
86 
90  KeyType
91  getKeyType() const;
92 
96  span<const uint8_t>
97  getPublicKey() const;
98 
105  getCertificate(const Name& certName) const;
106 
110  const CertificateContainer&
111  getCertificates() const;
112 
117  const Certificate&
118  getDefaultCertificate() const;
119 
123  explicit
124  operator bool() const noexcept;
125 
126 NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations are accessible only by KeyChain
137  void
138  addCertificate(const Certificate& certificate) const;
139 
144  void
145  removeCertificate(const Name& certName) const;
146 
153  const Certificate&
154  setDefaultCertificate(const Name& certName) const;
155 
160  void
161  setDefaultCertificate(const Certificate& certificate) const;
162 
163 NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // private interface for KeyContainer
164  explicit
165  Key(weak_ptr<detail::KeyImpl> impl) noexcept;
166 
167 private:
173  shared_ptr<detail::KeyImpl>
174  lock() const;
175 
176  bool
177  equals(const Key& other) const noexcept;
178 
179  // NOTE
180  // The following "hidden friend" non-member operators are available
181  // via argument-dependent lookup only and must be defined inline.
182 
183  friend bool
184  operator==(const Key& lhs, const Key& rhs)
185  {
186  return lhs.equals(rhs);
187  }
188 
189  friend bool
190  operator!=(const Key& lhs, const Key& rhs)
191  {
192  return !lhs.equals(rhs);
193  }
194 
195  friend std::ostream&
196  operator<<(std::ostream& os, const Key& key)
197  {
198  return os << (key ? key.getName() : "(empty)");
199  }
200 
201 private:
202  weak_ptr<detail::KeyImpl> m_impl;
203 
204  friend KeyChain;
205  friend KeyContainer;
206 };
207 
208 } // namespace pib
209 
210 using pib::Key;
211 
212 inline namespace v2 {
213 
217 Name
218 constructKeyName(const Name& identity, const name::Component& keyId);
219 
223 bool
224 isValidKeyName(const Name& keyName);
225 
229 Name
230 extractIdentityFromKeyName(const Name& keyName);
231 
232 } // inline namespace v2
233 } // namespace security
234 } // namespace ndn
235 
236 #endif // NDN_CXX_SECURITY_PIB_KEY_HPP
Represents an absolute name.
Definition: name.hpp:44
Represents a name component.
Container of certificates of a key.
Container of keys of an identity.
Frontend handle for a key in the PIB.
Definition: key.hpp:51
friend std::ostream & operator<<(std::ostream &os, const Key &key)
Definition: key.hpp:196
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
friend bool operator!=(const Key &lhs, const Key &rhs)
Definition: key.hpp:190
Represents an NDN certificate.
Definition: certificate.hpp:60
The main interface for signing key management.
Definition: key-chain.hpp:87
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
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
Definition: data.cpp:25
KeyType
The type of a cryptographic key.