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 | 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); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 38 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 39 | /** |
| 40 | * The virtual destructor. |
| 41 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 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); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 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 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 58 | void |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 59 | addSubjectDescription(const CertificateSubjectDescription& description) { subjectDescriptionList_.push_back(description); } |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 61 | const SubjectDescriptionList& |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 62 | getSubjectDescriptionList() const { return subjectDescriptionList_; } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 63 | |
| 64 | SubjectDescriptionList& |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 65 | getSubjectDescriptionList() { return subjectDescriptionList_; } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 66 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Add a certificate extension. |
| 69 | * @param extension the extension to be added |
| 70 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 71 | void |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 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_; } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 76 | |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 77 | ExtensionList& |
| 78 | getExtensionList() { return extensionList_; } |
| 79 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 80 | void |
| 81 | setNotBefore(const time::system_clock::TimePoint& notBefore) { notBefore_ = notBefore; } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 82 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 83 | time::system_clock::TimePoint& |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 84 | getNotBefore() { return notBefore_; } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 85 | |
| 86 | const time::system_clock::TimePoint& |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 87 | getNotBefore() const { return notBefore_; } |
| 88 | |
| 89 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 90 | setNotAfter(const time::system_clock::TimePoint& notAfter) { notAfter_ = notAfter; } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 92 | time::system_clock::TimePoint& |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 93 | getNotAfter() { return notAfter_; } |
| 94 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 95 | const time::system_clock::TimePoint& |
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; } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 100 | |
| 101 | PublicKey& |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 102 | getPublicKeyInfo() { return key_; } |
| 103 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 104 | const PublicKey& |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 105 | getPublicKeyInfo() const { return key_; } |
| 106 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 107 | // virtual Name |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 108 | // getPublicKeyName() const = 0; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 109 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Check if the certificate is valid. |
| 112 | * @return True if the current time is earlier than notBefore. |
| 113 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 114 | bool |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 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_; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 133 | time::system_clock::TimePoint notBefore_; |
| 134 | time::system_clock::TimePoint 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 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 156 | #endif //NDN_SECURITY_CERTIFICATE_HPP |