blob: c0fe8e0398e5f1d83bef1e1f258f8a4d79729acf [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
Yingdi Yufc40d872014-02-18 12:56:04 -080026#ifndef NDN_SECURITY_CERTIFICATE_HPP
27#define NDN_SECURITY_CERTIFICATE_HPP
Jeff Thompsone7e069b2013-09-27 15:48:48 -070028
Yingdi Yu4f324632014-01-15 18:10:03 -080029#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080030#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 {
36
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070037class Certificate : public Data
38{
Jeff Thompsonc69163b2013-10-12 13:49:50 -070039public:
Yingdi Yu80979ba2014-11-25 14:38:36 -080040 class Error : public Data::Error
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041 {
42 public:
43 explicit
44 Error(const std::string& what)
Yingdi Yu80979ba2014-11-25 14:38:36 -080045 : Data::Error(what)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070046 {
47 }
48 };
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080049
50 typedef std::vector<CertificateSubjectDescription> SubjectDescriptionList;
51 typedef std::vector<CertificateExtension> ExtensionList;
52
Jeff Thompsonc69163b2013-10-12 13:49:50 -070053 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -080054 * @brief The default constructor.
Jeff Thompsonc69163b2013-10-12 13:49:50 -070055 */
56 Certificate();
57
58 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -080059 * @brief Create a Certificate from the content in the data packet.
Jeff Thompsonc69163b2013-10-12 13:49:50 -070060 * @param data The data packet with the content to decode.
61 */
Alexander Afanasyeva4297a62014-06-19 13:29:34 -070062 explicit
Jeff Thompsonc69163b2013-10-12 13:49:50 -070063 Certificate(const Data& data);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064
Jeff Thompson965569b2013-10-12 17:52:52 -070065 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -080066 * @brief Create a Certificate from the a block
67 * @param block The raw block of the certificate
Jeff Thompson965569b2013-10-12 17:52:52 -070068 */
Yingdi Yu80979ba2014-11-25 14:38:36 -080069 explicit
70 Certificate(const Block& block);
71
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072 virtual
Jeff Thompsona5dc3512013-10-17 10:26:19 -070073 ~Certificate();
Jeff Thompson965569b2013-10-12 17:52:52 -070074
Yingdi Yu80979ba2014-11-25 14:38:36 -080075 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070077
Jeff Thompson965569b2013-10-12 17:52:52 -070078 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -080079 * @brief encode certificate info into content
Jeff Thompson965569b2013-10-12 17:52:52 -070080 */
81 void
82 encode();
83
84 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -080085 * @brief Add a subject description.
Jeff Thompson965569b2013-10-12 17:52:52 -070086 * @param description The description to be added.
87 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070089 addSubjectDescription(const CertificateSubjectDescription& description)
90 {
91 m_subjectDescriptionList.push_back(description);
92 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -080093
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070094 const SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070095 getSubjectDescriptionList() const
96 {
97 return m_subjectDescriptionList;
98 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070099
100 SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700101 getSubjectDescriptionList()
102 {
103 return m_subjectDescriptionList;
104 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700105
Jeff Thompson965569b2013-10-12 17:52:52 -0700106 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -0800107 * @brief Add a certificate extension.
Jeff Thompson965569b2013-10-12 17:52:52 -0700108 * @param extension the extension to be added
109 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700110 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700111 addExtension(const CertificateExtension& extension)
112 {
113 m_extensionList.push_back(extension);
114 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700115
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800116 const ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700117 getExtensionList() const
118 {
119 return m_extensionList;
120 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700121
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800122 ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700123 getExtensionList()
124 {
125 return m_extensionList;
126 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800127
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700128 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700129 setNotBefore(const time::system_clock::TimePoint& notBefore)
130 {
131 m_notBefore = notBefore;
132 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700133
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700134 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700135 getNotBefore()
136 {
137 return m_notBefore;
138 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700139
140 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700141 getNotBefore() const
142 {
143 return m_notBefore;
144 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700145
146 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700147 setNotAfter(const time::system_clock::TimePoint& notAfter)
148 {
149 m_notAfter = notAfter;
150 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700151
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700152 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700153 getNotAfter()
154 {
155 return m_notAfter;
156 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700157
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700158 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700159 getNotAfter() const
160 {
161 return m_notAfter;
162 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700163
164 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700165 setPublicKeyInfo(const PublicKey& key)
166 {
167 m_key = key;
168 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700169
170 PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700171 getPublicKeyInfo()
172 {
173 return m_key;
174 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700175
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700176 const PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700177 getPublicKeyInfo() const
178 {
179 return m_key;
180 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700181
Jeff Thompson965569b2013-10-12 17:52:52 -0700182 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -0800183 * @brief Check if the certificate is valid.
Jeff Thompson965569b2013-10-12 17:52:52 -0700184 * @return True if the current time is earlier than notBefore.
185 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700186 bool
Jeff Thompson965569b2013-10-12 17:52:52 -0700187 isTooEarly();
188
189 /**
Yingdi Yu80979ba2014-11-25 14:38:36 -0800190 * @brief Check if the certificate is valid.
Jeff Thompson965569b2013-10-12 17:52:52 -0700191 * @return True if the current time is later than notAfter.
192 */
193 bool
194 isTooLate();
195
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700196 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700197 printCertificate(std::ostream& os) const;
Jeff Thompson965569b2013-10-12 17:52:52 -0700198
199protected:
200 void
201 decode();
202
203protected:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700204 SubjectDescriptionList m_subjectDescriptionList;
205 time::system_clock::TimePoint m_notBefore;
206 time::system_clock::TimePoint m_notAfter;
207 PublicKey m_key;
208 ExtensionList m_extensionList;
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700209};
210
Yingdi Yu80979ba2014-11-25 14:38:36 -0800211std::ostream&
212operator<<(std::ostream& os, const Certificate& cert);
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800213} // namespace ndn
214
Yingdi Yu80979ba2014-11-25 14:38:36 -0800215#endif // NDN_SECURITY_CERTIFICATE_HPP