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 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 168 | // virtual Name |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 169 | // getPublicKeyName() const = 0; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 170 | |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 171 | /** |
172 | * Check if the certificate is valid. | ||||
173 | * @return True if the current time is earlier than notBefore. | ||||
174 | */ | ||||
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 175 | bool |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 176 | isTooEarly(); |
177 | |||||
178 | /** | ||||
179 | * Check if the certificate is valid. | ||||
180 | * @return True if the current time is later than notAfter. | ||||
181 | */ | ||||
182 | bool | ||||
183 | isTooLate(); | ||||
184 | |||||
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 185 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 186 | printCertificate(std::ostream& os) const; |
Jeff Thompson | 965569b | 2013-10-12 17:52:52 -0700 | [diff] [blame] | 187 | |
188 | protected: | ||||
189 | void | ||||
190 | decode(); | ||||
191 | |||||
192 | protected: | ||||
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 193 | SubjectDescriptionList m_subjectDescriptionList; |
194 | time::system_clock::TimePoint m_notBefore; | ||||
195 | time::system_clock::TimePoint m_notAfter; | ||||
196 | PublicKey m_key; | ||||
197 | ExtensionList m_extensionList; | ||||
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 198 | }; |
199 | |||||
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 200 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 201 | Certificate::wireDecode(const Block& wire) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 202 | { |
203 | Data::wireDecode(wire); | ||||
204 | decode(); | ||||
Jeff Thompson | e7e069b | 2013-09-27 15:48:48 -0700 | [diff] [blame] | 205 | } |
206 | |||||
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 207 | |
208 | inline std::ostream& | ||||
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 209 | operator<<(std::ostream& os, const Certificate& cert) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 210 | { |
211 | cert.printCertificate(os); | ||||
212 | return os; | ||||
213 | } | ||||
214 | |||||
215 | } // namespace ndn | ||||
216 | |||||
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 217 | #endif //NDN_SECURITY_CERTIFICATE_HPP |