Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 13 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
| 14 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 17 | #ifndef NDN_SECURITY_CERTIFICATE_HPP |
| 18 | #define NDN_SECURITY_CERTIFICATE_HPP |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 20 | #include "../common.hpp" |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 21 | #include "../data.hpp" |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 22 | #include "certificate-subject-description.hpp" |
| 23 | #include "certificate-extension.hpp" |
| 24 | #include "public-key.hpp" |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 28 | class Certificate : public Data |
| 29 | { |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 30 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 31 | class Error : public std::runtime_error |
| 32 | { |
| 33 | public: |
| 34 | explicit |
| 35 | Error(const std::string& what) |
| 36 | : std::runtime_error(what) |
| 37 | { |
| 38 | } |
| 39 | }; |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 40 | |
| 41 | typedef std::vector<CertificateSubjectDescription> SubjectDescriptionList; |
| 42 | typedef std::vector<CertificateExtension> ExtensionList; |
| 43 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 44 | /** |
| 45 | * The default constructor. |
| 46 | */ |
| 47 | Certificate(); |
| 48 | |
| 49 | /** |
| 50 | * Create a Certificate from the content in the data packet. |
| 51 | * @param data The data packet with the content to decode. |
| 52 | */ |
| 53 | Certificate(const Data& data); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 54 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 55 | /** |
| 56 | * The virtual destructor. |
| 57 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 58 | virtual |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 59 | ~Certificate(); |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 61 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 62 | wireDecode(const Block& wire); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 63 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 64 | /** |
| 65 | * encode certificate info into content |
| 66 | */ |
| 67 | void |
| 68 | encode(); |
| 69 | |
| 70 | /** |
| 71 | * Add a subject description. |
| 72 | * @param description The description to be added. |
| 73 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 74 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 75 | addSubjectDescription(const CertificateSubjectDescription& description) |
| 76 | { |
| 77 | m_subjectDescriptionList.push_back(description); |
| 78 | } |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 80 | const SubjectDescriptionList& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 81 | getSubjectDescriptionList() const |
| 82 | { |
| 83 | return m_subjectDescriptionList; |
| 84 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 85 | |
| 86 | SubjectDescriptionList& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 87 | getSubjectDescriptionList() |
| 88 | { |
| 89 | return m_subjectDescriptionList; |
| 90 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 91 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Add a certificate extension. |
| 94 | * @param extension the extension to be added |
| 95 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 96 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 97 | addExtension(const CertificateExtension& extension) |
| 98 | { |
| 99 | m_extensionList.push_back(extension); |
| 100 | } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 101 | |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 102 | const ExtensionList& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 103 | getExtensionList() const |
| 104 | { |
| 105 | return m_extensionList; |
| 106 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 107 | |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 108 | ExtensionList& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 109 | getExtensionList() |
| 110 | { |
| 111 | return m_extensionList; |
| 112 | } |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 114 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 115 | setNotBefore(const time::system_clock::TimePoint& notBefore) |
| 116 | { |
| 117 | m_notBefore = notBefore; |
| 118 | } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 120 | time::system_clock::TimePoint& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 121 | getNotBefore() |
| 122 | { |
| 123 | return m_notBefore; |
| 124 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 125 | |
| 126 | const time::system_clock::TimePoint& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 127 | getNotBefore() const |
| 128 | { |
| 129 | return m_notBefore; |
| 130 | } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 131 | |
| 132 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 133 | setNotAfter(const time::system_clock::TimePoint& notAfter) |
| 134 | { |
| 135 | m_notAfter = notAfter; |
| 136 | } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 138 | time::system_clock::TimePoint& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 139 | getNotAfter() |
| 140 | { |
| 141 | return m_notAfter; |
| 142 | } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 143 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 144 | const time::system_clock::TimePoint& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 145 | getNotAfter() const |
| 146 | { |
| 147 | return m_notAfter; |
| 148 | } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 149 | |
| 150 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 151 | setPublicKeyInfo(const PublicKey& key) |
| 152 | { |
| 153 | m_key = key; |
| 154 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 155 | |
| 156 | PublicKey& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 157 | getPublicKeyInfo() |
| 158 | { |
| 159 | return m_key; |
| 160 | } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 162 | const PublicKey& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 163 | getPublicKeyInfo() const |
| 164 | { |
| 165 | return m_key; |
| 166 | } |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 167 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Check if the certificate is valid. |
| 170 | * @return True if the current time is earlier than notBefore. |
| 171 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 172 | bool |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 173 | isTooEarly(); |
| 174 | |
| 175 | /** |
| 176 | * Check if the certificate is valid. |
| 177 | * @return True if the current time is later than notAfter. |
| 178 | */ |
| 179 | bool |
| 180 | isTooLate(); |
| 181 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 182 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 183 | printCertificate(std::ostream& os) const; |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 184 | |
| 185 | protected: |
| 186 | void |
| 187 | decode(); |
| 188 | |
| 189 | protected: |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 190 | SubjectDescriptionList m_subjectDescriptionList; |
| 191 | time::system_clock::TimePoint m_notBefore; |
| 192 | time::system_clock::TimePoint m_notAfter; |
| 193 | PublicKey m_key; |
| 194 | ExtensionList m_extensionList; |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 197 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 198 | Certificate::wireDecode(const Block& wire) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 199 | { |
| 200 | Data::wireDecode(wire); |
| 201 | decode(); |
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 204 | |
| 205 | inline std::ostream& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 206 | operator<<(std::ostream& os, const Certificate& cert) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 207 | { |
| 208 | cert.printCertificate(os); |
| 209 | return os; |
| 210 | } |
| 211 | |
| 212 | } // namespace ndn |
| 213 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 214 | #endif //NDN_SECURITY_CERTIFICATE_HPP |