All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
certificate-v2.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_CERTIFICATE_V2_HPP
24 #define NDN_CERTIFICATE_V2_HPP
25 
26 #include <ndn-cpp/security/validity-period.hpp>
27 #include <ndn-cpp/data.hpp>
28 
29 namespace ndn {
30 
81 class CertificateV2 : public Data {
82 public:
87  class Error : public std::runtime_error
88  {
89  public:
90  Error(const std::string& what)
91  : std::runtime_error(what)
92  {
93  }
94  };
95 
100  CertificateV2();
101 
107  CertificateV2(const Data& data);
108 
113  Name
114  getKeyName() const { return getName().getPrefix(KEY_ID_OFFSET + 1); }
115 
120  Name
121  getIdentity() const { return getName().getPrefix(KEY_COMPONENT_OFFSET); }
122 
128  getKeyId() const { return getName().get(KEY_ID_OFFSET); }
129 
135  getIssuerId() const { return getName().get(ISSUER_ID_OFFSET); }
136 
142  const Blob&
143  getPublicKey() const;
144 
152 
153  const ValidityPeriod&
154  getValidityPeriod() const
155  {
156  return const_cast<CertificateV2*>(this)->getValidityPeriod();
157  }
158 
167  bool
168  isValid(MillisecondsSince1970 time = -1.0) const
169  {
170  return getValidityPeriod().isValid(time);
171  }
172 
173  // TODO: getExtension
174 
179  void
180  printCertificate(std::ostream& output) const;
181 
185  void
186  printCertificate() const { printCertificate(std::cout); }
187 
195  virtual void
196  wireDecode
197  (const Blob& input,
199 
200  void
201  wireDecode
202  (const uint8_t* input, size_t inputLength,
204  {
205  wireDecode(Blob(input, inputLength), wireFormat);
206  }
207 
208  void
209  wireDecode
210  (const std::vector<uint8_t>& input,
211  WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
212  {
213  wireDecode(&input[0], input.size(), wireFormat);
214  }
215 
221  static bool
222  isValidName(const Name& certificateName);
223 
229  static Name
230  extractIdentityFromCertName(const Name& certificateName);
231 
237  static Name
238  extractKeyNameFromCertName(const Name& certificateName);
239 
245  static const Name::Component&
247 
248  static const int VERSION_OFFSET = -1;
249  static const int ISSUER_ID_OFFSET = -2;
250  static const int KEY_ID_OFFSET = -3;
251  static const int KEY_COMPONENT_OFFSET = -4;
252  static const int MIN_CERT_NAME_LENGTH = 4;
253  static const int MIN_KEY_NAME_LENGTH = 2;
254 
255 private:
256  static Name::Component* KEY_COMPONENT;
257 
258  void
259  checkFormat();
260 };
261 
262 inline std::ostream&
263 operator << (std::ostream& os, const CertificateV2& certificate)
264 {
265  certificate.printCertificate(os);
266  return os;
267 }
268 
269 }
270 
271 #endif
Name getIdentity() const
Get the identity name from the certificate name.
Definition: certificate-v2.hpp:121
Name::Component getIssuerId() const
Get issuer ID component from the certificate name.
Definition: certificate-v2.hpp:135
A CertificateV2::Error extends runtime_error and represents errors for not complying with the certifi...
Definition: certificate-v2.hpp:87
CertificateV2()
Create a CertificateV2 with content type KEY and default or unspecified values.
Definition: certificate-v2.cpp:36
Definition: data.hpp:37
bool isValid(MillisecondsSince1970 time=-1.0) const
Check if the time falls within the validity period.
Definition: validity-period.cpp:34
virtual void wireDecode(const Blob &input, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Override to call the base class wireDecode then check the certificate format.
Definition: certificate-v2.cpp:135
Name getKeyName() const
Get key name from the certificate name.
Definition: certificate-v2.hpp:114
static const Name::Component & getKEY_COMPONENT()
Get the name component for "KEY".
Definition: certificate-v2.cpp:175
bool isValid(MillisecondsSince1970 time=-1.0) const
Check if the time falls within the validity period.
Definition: certificate-v2.hpp:168
static Name extractKeyNameFromCertName(const Name &certificateName)
Extract key name from certificateName.
Definition: certificate-v2.cpp:162
CertificateV2 represents a certificate following the certificate format naming convention.
Definition: certificate-v2.hpp:81
A Name::Component holds a read-only name component value.
Definition: name.hpp:45
A ValidityPeriod is used in a Data packet's SignatureInfo and represents the begin and end times of a...
Definition: validity-period.hpp:37
Name::Component getKeyId() const
Get the key ID component from the certificate name.
Definition: certificate-v2.hpp:128
static bool isValidName(const Name &certificateName)
Check if certificateName follows the naming convention for a certificate.
Definition: certificate-v2.cpp:142
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.
Definition: blob.hpp:42
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:119
ValidityPeriod & getValidityPeriod()
Get the certificate validity period from the SignatureInfo.
Definition: certificate-v2.cpp:75
const Blob & getPublicKey() const
Get the public key DER encoding.
Definition: certificate-v2.cpp:66
void printCertificate() const
Print the certificate information to std::cout.
Definition: certificate-v2.hpp:186
static WireFormat * getDefaultWireFormat()
Return the default WireFormat used by default encoding and decoding methods which was set with setDef...
Definition: wire-format.cpp:34
void get(NameLite &nameLite) const
Set nameLite to point to the components in this name, without copying any memory. ...
Definition: name.cpp:463
Definition: wire-format.hpp:39
Name getPrefix(int nComponents) const
Return a new Name with the first nComponents components of this Name.
Definition: name.hpp:1102
static Name extractIdentityFromCertName(const Name &certificateName)
Extract the identity namespace from certificateName.
Definition: certificate-v2.cpp:150