Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
| 9 | #ifndef NDN_CERTIFICATE_HPP |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 10 | #define NDN_CERTIFICATE_HPP |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 11 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 12 | #include "../common.hpp" |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 13 | #include "../data.hpp" |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 14 | |
| 15 | #include "certificate-subject-description.hpp" |
| 16 | #include "certificate-extension.hpp" |
| 17 | #include "public-key.hpp" |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 18 | |
| 19 | namespace ndn { |
| 20 | |
| 21 | class Certificate : public Data { |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 22 | public: |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 23 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 24 | |
| 25 | typedef std::vector<CertificateSubjectDescription> SubjectDescriptionList; |
| 26 | typedef std::vector<CertificateExtension> ExtensionList; |
| 27 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 28 | /** |
| 29 | * The default constructor. |
| 30 | */ |
| 31 | Certificate(); |
| 32 | |
| 33 | /** |
| 34 | * Create a Certificate from the content in the data packet. |
| 35 | * @param data The data packet with the content to decode. |
| 36 | */ |
| 37 | Certificate(const Data& data); |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * The virtual destructor. |
| 41 | */ |
| 42 | virtual |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 43 | ~Certificate(); |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 44 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 45 | inline void |
| 46 | wireDecode(const Block &wire); |
| 47 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 48 | /** |
| 49 | * encode certificate info into content |
| 50 | */ |
| 51 | void |
| 52 | encode(); |
| 53 | |
| 54 | /** |
| 55 | * Add a subject description. |
| 56 | * @param description The description to be added. |
| 57 | */ |
| 58 | void |
| 59 | addSubjectDescription(const CertificateSubjectDescription& description) { subjectDescriptionList_.push_back(description); } |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 60 | |
| 61 | const SubjectDescriptionList& |
| 62 | getSubjectDescriptionList() const { return subjectDescriptionList_; } |
| 63 | |
| 64 | SubjectDescriptionList& |
| 65 | getSubjectDescriptionList() { return subjectDescriptionList_; } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Add a certificate extension. |
| 69 | * @param extension the extension to be added |
| 70 | */ |
| 71 | void |
| 72 | addExtension(const CertificateExtension& extension) { extensionList_.push_back(extension); } |
| 73 | |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 74 | const ExtensionList& |
| 75 | getExtensionList() const { return extensionList_; } |
| 76 | |
| 77 | ExtensionList& |
| 78 | getExtensionList() { return extensionList_; } |
| 79 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 80 | void |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 81 | setNotBefore(const MillisecondsSince1970& notBefore) { notBefore_ = notBefore; } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 82 | |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 83 | MillisecondsSince1970& |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 84 | getNotBefore() { return notBefore_; } |
| 85 | |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 86 | const MillisecondsSince1970& |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 87 | getNotBefore() const { return notBefore_; } |
| 88 | |
| 89 | void |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 90 | setNotAfter(const MillisecondsSince1970& notAfter) { notAfter_ = notAfter; } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 91 | |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 92 | MillisecondsSince1970& |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 93 | getNotAfter() { return notAfter_; } |
| 94 | |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 95 | const MillisecondsSince1970& |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 96 | getNotAfter() const { return notAfter_; } |
| 97 | |
| 98 | void |
| 99 | setPublicKeyInfo(const PublicKey& key) { key_ = key; } |
| 100 | |
| 101 | PublicKey& |
| 102 | getPublicKeyInfo() { return key_; } |
| 103 | |
| 104 | const PublicKey& |
| 105 | getPublicKeyInfo() const { return key_; } |
| 106 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 107 | // virtual Name |
| 108 | // getPublicKeyName() const = 0; |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Check if the certificate is valid. |
| 112 | * @return True if the current time is earlier than notBefore. |
| 113 | */ |
| 114 | bool |
| 115 | isTooEarly(); |
| 116 | |
| 117 | /** |
| 118 | * Check if the certificate is valid. |
| 119 | * @return True if the current time is later than notAfter. |
| 120 | */ |
| 121 | bool |
| 122 | isTooLate(); |
| 123 | |
| 124 | void |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 125 | printCertificate(std::ostream &os) const; |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 126 | |
| 127 | protected: |
| 128 | void |
| 129 | decode(); |
| 130 | |
| 131 | protected: |
| 132 | SubjectDescriptionList subjectDescriptionList_; |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 133 | MillisecondsSince1970 notBefore_; |
| 134 | MillisecondsSince1970 notAfter_; |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 135 | PublicKey key_; |
| 136 | ExtensionList extensionList_; |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 139 | inline void |
| 140 | Certificate::wireDecode(const Block &wire) |
| 141 | { |
| 142 | Data::wireDecode(wire); |
| 143 | decode(); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 146 | |
| 147 | inline std::ostream& |
| 148 | operator <<(std::ostream &os, const Certificate &cert) |
| 149 | { |
| 150 | cert.printCertificate(os); |
| 151 | return os; |
| 152 | } |
| 153 | |
| 154 | } // namespace ndn |
| 155 | |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 156 | #endif |