certificate.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  * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
22  * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
23  */
24 
25 #ifndef NDN_CXX_SECURITY_CERTIFICATE_HPP
26 #define NDN_CXX_SECURITY_CERTIFICATE_HPP
27 
28 #include "ndn-cxx/data.hpp"
29 
30 namespace ndn::security {
31 
57 class Certificate : public Data
58 {
59 public:
60  class Error : public Data::Error
61  {
62  public:
63  using Data::Error::Error;
64  };
65 
66  Certificate();
67 
72  explicit
73  Certificate(Data&& data);
74 
79  explicit
80  Certificate(const Data& data);
81 
86  explicit
87  Certificate(const Block& block);
88 
92  Name
93  getIdentity() const;
94 
98  Name
99  getKeyName() const;
100 
105  getKeyId() const;
106 
111  getIssuerId() const;
112 
117  span<const uint8_t>
118  getPublicKey() const noexcept
119  {
120  return getContent().value_bytes();
121  }
122 
127  getValidityPeriod() const;
128 
132  bool
134 
139  Block
140  getExtension(uint32_t type) const;
141 
142  // TODO: Implement extension enumeration (Issue #3907)
143 
147  static bool
148  isValidName(const Name& certName);
149 
150 public:
151  // Certificate name structure: /<IdentityName>/KEY/<KeyId>/<IssuerId>/<Version>
152  static constexpr ssize_t VERSION_OFFSET = -1;
153  static constexpr ssize_t ISSUER_ID_OFFSET = -2;
154  static constexpr ssize_t KEY_ID_OFFSET = -3;
155  static constexpr ssize_t KEY_COMPONENT_OFFSET = -4;
156  static constexpr size_t MIN_CERT_NAME_LENGTH = 4;
157  static constexpr size_t MIN_KEY_NAME_LENGTH = 2;
158  static inline const name::Component KEY_COMPONENT{"KEY"};
159  static inline const name::Component DEFAULT_ISSUER_ID{"NA"};
160 };
161 
162 std::ostream&
163 operator<<(std::ostream& os, const Certificate& cert);
164 
168 Name
169 extractIdentityFromCertName(const Name& certName);
170 
174 Name
175 extractKeyNameFromCertName(const Name& certName);
176 
177 } // namespace ndn::security
178 
179 #endif // NDN_CXX_SECURITY_CERTIFICATE_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
span< const uint8_t > value_bytes() const noexcept
Return a read-only view of TLV-VALUE as a contiguous range of bytes.
Definition: block.hpp:308
Represents a Data packet.
Definition: data.hpp:39
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:188
Represents an absolute name.
Definition: name.hpp:45
Represents a name component.
Represents an NDN certificate.
Definition: certificate.hpp:58
name::Component getIssuerId() const
Get issuer ID.
Definition: certificate.cpp:84
span< const uint8_t > getPublicKey() const noexcept
Return the public key as a DER-encoded SubjectPublicKeyInfo structure, i.e., exactly as it appears in...
Name getKeyName() const
Get key name.
Definition: certificate.cpp:72
Name getIdentity() const
Get identity name.
Definition: certificate.cpp:66
static constexpr ssize_t KEY_COMPONENT_OFFSET
name::Component getKeyId() const
Get key ID.
Definition: certificate.cpp:78
static constexpr ssize_t VERSION_OFFSET
static const name::Component DEFAULT_ISSUER_ID
bool isValid(const time::system_clock::time_point &ts=time::system_clock::now()) const
Check if the certificate is valid at ts.
Definition: certificate.cpp:96
ValidityPeriod getValidityPeriod() const
Get validity period of the certificate.
Definition: certificate.cpp:90
static constexpr ssize_t ISSUER_ID_OFFSET
static constexpr ssize_t KEY_ID_OFFSET
static const name::Component KEY_COMPONENT
static constexpr size_t MIN_KEY_NAME_LENGTH
static bool isValidName(const Name &certName)
Check if the specified name respects the naming conventions for certificates.
static constexpr size_t MIN_CERT_NAME_LENGTH
Block getExtension(uint32_t type) const
Get extension with TLV type.
Represents a ValidityPeriod TLV element.
static time_point now() noexcept
Definition: time.cpp:45
::boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:205
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:28
Contains the ndn-cxx security framework.
std::ostream & operator<<(std::ostream &os, const AdditionalDescription &desc)
Name extractIdentityFromCertName(const Name &certName)
Extract identity namespace from the certificate name certName.
Name extractKeyNameFromCertName(const Name &certName)
Extract key name from the certificate name certName.