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