blob: f2f70bf8f77a439baf4433476a7a7774325f24e3 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompsone7e069b2013-09-27 15:48:48 -07002/**
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
22 * @author Jeff Thompson <jefft0@remap.ucla.edu>
23 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Jeff Thompsone7e069b2013-09-27 15:48:48 -070024 */
25
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070026#ifndef NDN_SECURITY_V1_CERTIFICATE_HPP
27#define NDN_SECURITY_V1_CERTIFICATE_HPP
Jeff Thompsone7e069b2013-09-27 15:48:48 -070028
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070029#include "../../common.hpp"
30#include "../../data.hpp"
Jeff Thompson965569b2013-10-12 17:52:52 -070031#include "certificate-subject-description.hpp"
32#include "certificate-extension.hpp"
33#include "public-key.hpp"
Jeff Thompsone7e069b2013-09-27 15:48:48 -070034
35namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070036namespace security {
37namespace v1 {
Jeff Thompsone7e069b2013-09-27 15:48:48 -070038
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070039class Certificate : public Data
40{
Jeff Thompsonc69163b2013-10-12 13:49:50 -070041public:
Yingdi Yu80979ba2014-11-25 14:38:36 -080042 class Error : public Data::Error
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043 {
44 public:
45 explicit
46 Error(const std::string& what)
Yingdi Yu80979ba2014-11-25 14:38:36 -080047 : Data::Error(what)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070048 {
49 }
50 };
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080051
52 typedef std::vector<CertificateSubjectDescription> SubjectDescriptionList;
53 typedef std::vector<CertificateExtension> ExtensionList;
54
Jeff Thompsonc69163b2013-10-12 13:49:50 -070055 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -080056 * @brief The default constructor.
Jeff Thompsonc69163b2013-10-12 13:49:50 -070057 */
58 Certificate();
59
60 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -080061 * @brief Create a Certificate from the content in the data packet.
Jeff Thompsonc69163b2013-10-12 13:49:50 -070062 * @param data The data packet with the content to decode.
63 */
Alexander Afanasyeva4297a62014-06-19 13:29:34 -070064 explicit
Jeff Thompsonc69163b2013-10-12 13:49:50 -070065 Certificate(const Data& data);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066
Jeff Thompson965569b2013-10-12 17:52:52 -070067 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -080068 * @brief Create a Certificate from the a block
69 * @param block The raw block of the certificate
Jeff Thompson965569b2013-10-12 17:52:52 -070070 */
Yingdi Yu80979ba2014-11-25 14:38:36 -080071 explicit
72 Certificate(const Block& block);
73
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 virtual
Jeff Thompsona5dc3512013-10-17 10:26:19 -070075 ~Certificate();
Jeff Thompson965569b2013-10-12 17:52:52 -070076
Yingdi Yu80979ba2014-11-25 14:38:36 -080077 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070079
Jeff Thompson965569b2013-10-12 17:52:52 -070080 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -080081 * @brief encode certificate info into content
Jeff Thompson965569b2013-10-12 17:52:52 -070082 */
83 void
84 encode();
85
86 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -080087 * @brief Add a subject description.
Jeff Thompson965569b2013-10-12 17:52:52 -070088 * @param description The description to be added.
89 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070090 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070091 addSubjectDescription(const CertificateSubjectDescription& description)
92 {
93 m_subjectDescriptionList.push_back(description);
94 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -080095
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070096 const SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070097 getSubjectDescriptionList() const
98 {
99 return m_subjectDescriptionList;
100 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700101
102 SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700103 getSubjectDescriptionList()
104 {
105 return m_subjectDescriptionList;
106 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700107
Jeff Thompson965569b2013-10-12 17:52:52 -0700108 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -0800109 * @brief Add a certificate extension.
Jeff Thompson965569b2013-10-12 17:52:52 -0700110 * @param extension the extension to be added
111 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700113 addExtension(const CertificateExtension& extension)
114 {
115 m_extensionList.push_back(extension);
116 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700117
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800118 const ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700119 getExtensionList() const
120 {
121 return m_extensionList;
122 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700123
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800124 ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700125 getExtensionList()
126 {
127 return m_extensionList;
128 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800129
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700130 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700131 setNotBefore(const time::system_clock::TimePoint& notBefore)
132 {
133 m_notBefore = notBefore;
134 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700135
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700136 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700137 getNotBefore()
138 {
139 return m_notBefore;
140 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700141
142 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700143 getNotBefore() const
144 {
145 return m_notBefore;
146 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700147
148 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700149 setNotAfter(const time::system_clock::TimePoint& notAfter)
150 {
151 m_notAfter = notAfter;
152 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700153
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700154 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700155 getNotAfter()
156 {
157 return m_notAfter;
158 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700159
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700160 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700161 getNotAfter() const
162 {
163 return m_notAfter;
164 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700165
166 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700167 setPublicKeyInfo(const PublicKey& key)
168 {
169 m_key = key;
170 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700171
172 PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700173 getPublicKeyInfo()
174 {
175 return m_key;
176 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700177
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700178 const PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700179 getPublicKeyInfo() const
180 {
181 return m_key;
182 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700183
Jeff Thompson965569b2013-10-12 17:52:52 -0700184 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -0800185 * @brief Check if the certificate is valid.
Jeff Thompson965569b2013-10-12 17:52:52 -0700186 * @return True if the current time is earlier than notBefore.
187 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700188 bool
Jeff Thompson965569b2013-10-12 17:52:52 -0700189 isTooEarly();
190
191 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -0800192 * @brief Check if the certificate is valid.
Jeff Thompson965569b2013-10-12 17:52:52 -0700193 * @return True if the current time is later than notAfter.
194 */
195 bool
196 isTooLate();
197
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700198 void
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800199 printCertificate(std::ostream& os, const std::string& indent = "") const;
Jeff Thompson965569b2013-10-12 17:52:52 -0700200
201protected:
202 void
203 decode();
204
205protected:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700206 SubjectDescriptionList m_subjectDescriptionList;
207 time::system_clock::TimePoint m_notBefore;
208 time::system_clock::TimePoint m_notAfter;
209 PublicKey m_key;
210 ExtensionList m_extensionList;
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700211};
212
Yingdi Yu80979ba2014-11-25 14:38:36 -0800213std::ostream&
214operator<<(std::ostream& os, const Certificate& cert);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700215
216} // namespace v1
217} // namespace security
218
219#ifdef NDN_CXX_KEEP_SECURITY_V1_ALIASES
220/// @deprecated When needed, use explicit namespace
221using security::v1::Certificate;
222#endif // NDN_CXX_KEEP_SECURITY_V1_ALIASES
223
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800224} // namespace ndn
225
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700226#endif // NDN_SECURITY_V1_CERTIFICATE_HPP