blob: eff2bbc1a1a1500c7fb43118e363e41bae59e5e4 [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:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070040 class Error : public std::runtime_error
41 {
42 public:
43 explicit
44 Error(const std::string& what)
45 : std::runtime_error(what)
46 {
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 /**
54 * The default constructor.
55 */
56 Certificate();
57
58 /**
59 * Create a Certificate from the content in the data packet.
60 * @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 /**
66 * The virtual destructor.
67 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070068 virtual
Jeff Thompsona5dc3512013-10-17 10:26:19 -070069 ~Certificate();
Jeff Thompson965569b2013-10-12 17:52:52 -070070
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080071 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070072 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070073
Jeff Thompson965569b2013-10-12 17:52:52 -070074 /**
75 * encode certificate info into content
76 */
77 void
78 encode();
79
80 /**
81 * Add a subject description.
82 * @param description The description to be added.
83 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070085 addSubjectDescription(const CertificateSubjectDescription& description)
86 {
87 m_subjectDescriptionList.push_back(description);
88 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -080089
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070090 const SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070091 getSubjectDescriptionList() const
92 {
93 return m_subjectDescriptionList;
94 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070095
96 SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070097 getSubjectDescriptionList()
98 {
99 return m_subjectDescriptionList;
100 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700101
Jeff Thompson965569b2013-10-12 17:52:52 -0700102 /**
103 * Add a certificate extension.
104 * @param extension the extension to be added
105 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700106 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700107 addExtension(const CertificateExtension& extension)
108 {
109 m_extensionList.push_back(extension);
110 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700111
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800112 const ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700113 getExtensionList() const
114 {
115 return m_extensionList;
116 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700117
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800118 ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700119 getExtensionList()
120 {
121 return m_extensionList;
122 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800123
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700124 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700125 setNotBefore(const time::system_clock::TimePoint& notBefore)
126 {
127 m_notBefore = notBefore;
128 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700129
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700130 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700131 getNotBefore()
132 {
133 return m_notBefore;
134 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700135
136 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700137 getNotBefore() const
138 {
139 return m_notBefore;
140 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700141
142 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700143 setNotAfter(const time::system_clock::TimePoint& notAfter)
144 {
145 m_notAfter = notAfter;
146 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700147
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700148 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700149 getNotAfter()
150 {
151 return m_notAfter;
152 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700153
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700154 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700155 getNotAfter() const
156 {
157 return m_notAfter;
158 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700159
160 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700161 setPublicKeyInfo(const PublicKey& key)
162 {
163 m_key = key;
164 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700165
166 PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700167 getPublicKeyInfo()
168 {
169 return m_key;
170 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700171
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700172 const PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700173 getPublicKeyInfo() const
174 {
175 return m_key;
176 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700177
Jeff Thompson965569b2013-10-12 17:52:52 -0700178 /**
179 * Check if the certificate is valid.
180 * @return True if the current time is earlier than notBefore.
181 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700182 bool
Jeff Thompson965569b2013-10-12 17:52:52 -0700183 isTooEarly();
184
185 /**
186 * Check if the certificate is valid.
187 * @return True if the current time is later than notAfter.
188 */
189 bool
190 isTooLate();
191
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700192 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700193 printCertificate(std::ostream& os) const;
Jeff Thompson965569b2013-10-12 17:52:52 -0700194
195protected:
196 void
197 decode();
198
199protected:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700200 SubjectDescriptionList m_subjectDescriptionList;
201 time::system_clock::TimePoint m_notBefore;
202 time::system_clock::TimePoint m_notAfter;
203 PublicKey m_key;
204 ExtensionList m_extensionList;
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700205};
206
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800207inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700208Certificate::wireDecode(const Block& wire)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800209{
210 Data::wireDecode(wire);
211 decode();
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700212}
213
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800214
215inline std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700216operator<<(std::ostream& os, const Certificate& cert)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800217{
218 cert.printCertificate(os);
219 return os;
220}
221
222} // namespace ndn
223
Yingdi Yufc40d872014-02-18 12:56:04 -0800224#endif //NDN_SECURITY_CERTIFICATE_HPP