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