blob: 3fcea670a5a32e097b379bab0d4d3d3533b6bdbb [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 */
62 Certificate(const Data& data);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063
Jeff Thompson965569b2013-10-12 17:52:52 -070064 /**
65 * The virtual destructor.
66 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067 virtual
Jeff Thompsona5dc3512013-10-17 10:26:19 -070068 ~Certificate();
Jeff Thompson965569b2013-10-12 17:52:52 -070069
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080070 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070071 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072
Jeff Thompson965569b2013-10-12 17:52:52 -070073 /**
74 * encode certificate info into content
75 */
76 void
77 encode();
78
79 /**
80 * Add a subject description.
81 * @param description The description to be added.
82 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070083 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070084 addSubjectDescription(const CertificateSubjectDescription& description)
85 {
86 m_subjectDescriptionList.push_back(description);
87 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -080088
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070089 const SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070090 getSubjectDescriptionList() const
91 {
92 return m_subjectDescriptionList;
93 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070094
95 SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070096 getSubjectDescriptionList()
97 {
98 return m_subjectDescriptionList;
99 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700100
Jeff Thompson965569b2013-10-12 17:52:52 -0700101 /**
102 * Add a certificate extension.
103 * @param extension the extension to be added
104 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700105 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700106 addExtension(const CertificateExtension& extension)
107 {
108 m_extensionList.push_back(extension);
109 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700110
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800111 const ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700112 getExtensionList() const
113 {
114 return m_extensionList;
115 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700116
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800117 ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700118 getExtensionList()
119 {
120 return m_extensionList;
121 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800122
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700123 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700124 setNotBefore(const time::system_clock::TimePoint& notBefore)
125 {
126 m_notBefore = notBefore;
127 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700128
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700129 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700130 getNotBefore()
131 {
132 return m_notBefore;
133 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700134
135 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700136 getNotBefore() const
137 {
138 return m_notBefore;
139 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700140
141 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700142 setNotAfter(const time::system_clock::TimePoint& notAfter)
143 {
144 m_notAfter = notAfter;
145 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700146
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700147 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700148 getNotAfter()
149 {
150 return m_notAfter;
151 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700152
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700153 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700154 getNotAfter() const
155 {
156 return m_notAfter;
157 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700158
159 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700160 setPublicKeyInfo(const PublicKey& key)
161 {
162 m_key = key;
163 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700164
165 PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700166 getPublicKeyInfo()
167 {
168 return m_key;
169 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700170
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700171 const PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700172 getPublicKeyInfo() const
173 {
174 return m_key;
175 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700176
Jeff Thompson965569b2013-10-12 17:52:52 -0700177 /**
178 * Check if the certificate is valid.
179 * @return True if the current time is earlier than notBefore.
180 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700181 bool
Jeff Thompson965569b2013-10-12 17:52:52 -0700182 isTooEarly();
183
184 /**
185 * Check if the certificate is valid.
186 * @return True if the current time is later than notAfter.
187 */
188 bool
189 isTooLate();
190
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700191 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700192 printCertificate(std::ostream& os) const;
Jeff Thompson965569b2013-10-12 17:52:52 -0700193
194protected:
195 void
196 decode();
197
198protected:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700199 SubjectDescriptionList m_subjectDescriptionList;
200 time::system_clock::TimePoint m_notBefore;
201 time::system_clock::TimePoint m_notAfter;
202 PublicKey m_key;
203 ExtensionList m_extensionList;
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700204};
205
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800206inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700207Certificate::wireDecode(const Block& wire)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800208{
209 Data::wireDecode(wire);
210 decode();
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700211}
212
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800213
214inline std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700215operator<<(std::ostream& os, const Certificate& cert)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800216{
217 cert.printCertificate(os);
218 return os;
219}
220
221} // namespace ndn
222
Yingdi Yufc40d872014-02-18 12:56:04 -0800223#endif //NDN_SECURITY_CERTIFICATE_HPP