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 | #include "certificate-subject-description.hpp" |
15 | #include "certificate-extension.hpp" | ||||
16 | #include "public-key.hpp" | ||||
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 17 | |
18 | namespace ndn { | ||||
19 | |||||
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 20 | class Certificate : public Data |
21 | { | ||||
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 |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 67 | addSubjectDescription(const CertificateSubjectDescription& description) |
68 | { | ||||
69 | m_subjectDescriptionList.push_back(description); | ||||
70 | } | ||||
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 72 | const SubjectDescriptionList& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 73 | getSubjectDescriptionList() const |
74 | { | ||||
75 | return m_subjectDescriptionList; | ||||
76 | } | ||||
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 77 | |
78 | SubjectDescriptionList& | ||||
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 79 | getSubjectDescriptionList() |
80 | { | ||||
81 | return m_subjectDescriptionList; | ||||
82 | } | ||||
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 83 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 84 | /** |
85 | * Add a certificate extension. | ||||
86 | * @param extension the extension to be added | ||||
87 | */ | ||||
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 88 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 89 | addExtension(const CertificateExtension& extension) |
90 | { | ||||
91 | m_extensionList.push_back(extension); | ||||
92 | } | ||||
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 93 | |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 94 | const ExtensionList& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 95 | getExtensionList() const |
96 | { | ||||
97 | return m_extensionList; | ||||
98 | } | ||||
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 99 | |
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 100 | ExtensionList& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 101 | getExtensionList() |
102 | { | ||||
103 | return m_extensionList; | ||||
104 | } | ||||
Jeff Thompson | 6ef69b2 | 2013-12-18 16:24:45 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 106 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 107 | setNotBefore(const time::system_clock::TimePoint& notBefore) |
108 | { | ||||
109 | m_notBefore = notBefore; | ||||
110 | } | ||||
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 111 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 112 | time::system_clock::TimePoint& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 113 | getNotBefore() |
114 | { | ||||
115 | return m_notBefore; | ||||
116 | } | ||||
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 117 | |
118 | const time::system_clock::TimePoint& | ||||
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 119 | getNotBefore() const |
120 | { | ||||
121 | return m_notBefore; | ||||
122 | } | ||||
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 123 | |
124 | void | ||||
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 125 | setNotAfter(const time::system_clock::TimePoint& notAfter) |
126 | { | ||||
127 | m_notAfter = notAfter; | ||||
128 | } | ||||
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 130 | time::system_clock::TimePoint& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 131 | getNotAfter() |
132 | { | ||||
133 | return m_notAfter; | ||||
134 | } | ||||
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 136 | const time::system_clock::TimePoint& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 137 | getNotAfter() const |
138 | { | ||||
139 | return m_notAfter; | ||||
140 | } | ||||
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 141 | |
142 | void | ||||
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 143 | setPublicKeyInfo(const PublicKey& key) |
144 | { | ||||
145 | m_key = key; | ||||
146 | } | ||||
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 147 | |
148 | PublicKey& | ||||
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 149 | getPublicKeyInfo() |
150 | { | ||||
151 | return m_key; | ||||
152 | } | ||||
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 153 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 154 | const PublicKey& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 155 | getPublicKeyInfo() const |
156 | { | ||||
157 | return m_key; | ||||
158 | } | ||||
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 159 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 160 | // virtual Name |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 161 | // getPublicKeyName() const = 0; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 162 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 163 | /** |
164 | * Check if the certificate is valid. | ||||
165 | * @return True if the current time is earlier than notBefore. | ||||
166 | */ | ||||
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 167 | bool |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 168 | isTooEarly(); |
169 | |||||
170 | /** | ||||
171 | * Check if the certificate is valid. | ||||
172 | * @return True if the current time is later than notAfter. | ||||
173 | */ | ||||
174 | bool | ||||
175 | isTooLate(); | ||||
176 | |||||
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 177 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 178 | printCertificate(std::ostream& os) const; |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 179 | |
180 | protected: | ||||
181 | void | ||||
182 | decode(); | ||||
183 | |||||
184 | protected: | ||||
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 185 | SubjectDescriptionList m_subjectDescriptionList; |
186 | time::system_clock::TimePoint m_notBefore; | ||||
187 | time::system_clock::TimePoint m_notAfter; | ||||
188 | PublicKey m_key; | ||||
189 | ExtensionList m_extensionList; | ||||
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 190 | }; |
191 | |||||
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 192 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 193 | Certificate::wireDecode(const Block& wire) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 194 | { |
195 | Data::wireDecode(wire); | ||||
196 | decode(); | ||||
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 197 | } |
198 | |||||
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 199 | |
200 | inline std::ostream& | ||||
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 201 | operator<<(std::ostream& os, const Certificate& cert) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 202 | { |
203 | cert.printCertificate(os); | ||||
204 | return os; | ||||
205 | } | ||||
206 | |||||
207 | } // namespace ndn | ||||
208 | |||||
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 209 | #endif //NDN_SECURITY_CERTIFICATE_HPP |