blob: 695faf10a9aee8a5f836822129bff2d65555741a [file] [log] [blame]
Jeff Thompsone7e069b2013-09-27 15:48:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Thompsone7e069b2013-09-27 15:48:48 -070015 */
16
Yingdi Yufc40d872014-02-18 12:56:04 -080017#ifndef NDN_SECURITY_CERTIFICATE_HPP
18#define NDN_SECURITY_CERTIFICATE_HPP
Jeff Thompsone7e069b2013-09-27 15:48:48 -070019
Yingdi Yu4f324632014-01-15 18:10:03 -080020#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080021#include "../data.hpp"
Jeff Thompson965569b2013-10-12 17:52:52 -070022#include "certificate-subject-description.hpp"
23#include "certificate-extension.hpp"
24#include "public-key.hpp"
Jeff Thompsone7e069b2013-09-27 15:48:48 -070025
26namespace ndn {
27
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070028class Certificate : public Data
29{
Jeff Thompsonc69163b2013-10-12 13:49:50 -070030public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070031 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 Afanasyevbf1a67a2014-01-05 23:36:13 -080040
41 typedef std::vector<CertificateSubjectDescription> SubjectDescriptionList;
42 typedef std::vector<CertificateExtension> ExtensionList;
43
Jeff Thompsonc69163b2013-10-12 13:49:50 -070044 /**
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 Afanasyevaa0e7da2014-03-17 14:37:33 -070054
Jeff Thompson965569b2013-10-12 17:52:52 -070055 /**
56 * The virtual destructor.
57 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070058 virtual
Jeff Thompsona5dc3512013-10-17 10:26:19 -070059 ~Certificate();
Jeff Thompson965569b2013-10-12 17:52:52 -070060
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080061 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070062 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063
Jeff Thompson965569b2013-10-12 17:52:52 -070064 /**
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 Afanasyevaa0e7da2014-03-17 14:37:33 -070074 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070075 addSubjectDescription(const CertificateSubjectDescription& description)
76 {
77 m_subjectDescriptionList.push_back(description);
78 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -080079
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070080 const SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070081 getSubjectDescriptionList() const
82 {
83 return m_subjectDescriptionList;
84 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070085
86 SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070087 getSubjectDescriptionList()
88 {
89 return m_subjectDescriptionList;
90 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091
Jeff Thompson965569b2013-10-12 17:52:52 -070092 /**
93 * Add a certificate extension.
94 * @param extension the extension to be added
95 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070096 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070097 addExtension(const CertificateExtension& extension)
98 {
99 m_extensionList.push_back(extension);
100 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700101
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800102 const ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700103 getExtensionList() const
104 {
105 return m_extensionList;
106 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700107
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800108 ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700109 getExtensionList()
110 {
111 return m_extensionList;
112 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800113
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700114 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700115 setNotBefore(const time::system_clock::TimePoint& notBefore)
116 {
117 m_notBefore = notBefore;
118 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700119
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700120 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700121 getNotBefore()
122 {
123 return m_notBefore;
124 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700125
126 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700127 getNotBefore() const
128 {
129 return m_notBefore;
130 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700131
132 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700133 setNotAfter(const time::system_clock::TimePoint& notAfter)
134 {
135 m_notAfter = notAfter;
136 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700137
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700138 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700139 getNotAfter()
140 {
141 return m_notAfter;
142 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700143
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700144 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700145 getNotAfter() const
146 {
147 return m_notAfter;
148 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700149
150 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700151 setPublicKeyInfo(const PublicKey& key)
152 {
153 m_key = key;
154 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700155
156 PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700157 getPublicKeyInfo()
158 {
159 return m_key;
160 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700161
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700162 const PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700163 getPublicKeyInfo() const
164 {
165 return m_key;
166 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700167
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700168 // virtual Name
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800169 // getPublicKeyName() const = 0;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700170
Jeff Thompson965569b2013-10-12 17:52:52 -0700171 /**
172 * Check if the certificate is valid.
173 * @return True if the current time is earlier than notBefore.
174 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700175 bool
Jeff Thompson965569b2013-10-12 17:52:52 -0700176 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 Afanasyevaa0e7da2014-03-17 14:37:33 -0700185 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700186 printCertificate(std::ostream& os) const;
Jeff Thompson965569b2013-10-12 17:52:52 -0700187
188protected:
189 void
190 decode();
191
192protected:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700193 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 Thompsone7e069b2013-09-27 15:48:48 -0700198};
199
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800200inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700201Certificate::wireDecode(const Block& wire)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800202{
203 Data::wireDecode(wire);
204 decode();
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700205}
206
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800207
208inline std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700209operator<<(std::ostream& os, const Certificate& cert)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800210{
211 cert.printCertificate(os);
212 return os;
213}
214
215} // namespace ndn
216
Yingdi Yufc40d872014-02-18 12:56:04 -0800217#endif //NDN_SECURITY_CERTIFICATE_HPP