blob: 157b878134a7a5ef03e3fe03d69e03619850f44b [file] [log] [blame]
Jeff Thompsone7e069b2013-09-27 15:48:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
Yingdi Yufc40d872014-02-18 12:56:04 -08009#ifndef NDN_SECURITY_CERTIFICATE_HPP
10#define NDN_SECURITY_CERTIFICATE_HPP
Jeff Thompsone7e069b2013-09-27 15:48:48 -070011
Yingdi Yu4f324632014-01-15 18:10:03 -080012#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080013#include "../data.hpp"
Jeff Thompson965569b2013-10-12 17:52:52 -070014#include "certificate-subject-description.hpp"
15#include "certificate-extension.hpp"
16#include "public-key.hpp"
Jeff Thompsone7e069b2013-09-27 15:48:48 -070017
18namespace ndn {
19
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070020class Certificate : public Data
21{
Jeff Thompsonc69163b2013-10-12 13:49:50 -070022public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070023 class Error : public std::runtime_error
24 {
25 public:
26 explicit
27 Error(const std::string& what)
28 : std::runtime_error(what)
29 {
30 }
31 };
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080032
33 typedef std::vector<CertificateSubjectDescription> SubjectDescriptionList;
34 typedef std::vector<CertificateExtension> ExtensionList;
35
Jeff Thompsonc69163b2013-10-12 13:49:50 -070036 /**
37 * The default constructor.
38 */
39 Certificate();
40
41 /**
42 * Create a Certificate from the content in the data packet.
43 * @param data The data packet with the content to decode.
44 */
45 Certificate(const Data& data);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070046
Jeff Thompson965569b2013-10-12 17:52:52 -070047 /**
48 * The virtual destructor.
49 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070050 virtual
Jeff Thompsona5dc3512013-10-17 10:26:19 -070051 ~Certificate();
Jeff Thompson965569b2013-10-12 17:52:52 -070052
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080053 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070055
Jeff Thompson965569b2013-10-12 17:52:52 -070056 /**
57 * encode certificate info into content
58 */
59 void
60 encode();
61
62 /**
63 * Add a subject description.
64 * @param description The description to be added.
65 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070067 addSubjectDescription(const CertificateSubjectDescription& description)
68 {
69 m_subjectDescriptionList.push_back(description);
70 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -080071
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072 const SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070073 getSubjectDescriptionList() const
74 {
75 return m_subjectDescriptionList;
76 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070077
78 SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070079 getSubjectDescriptionList()
80 {
81 return m_subjectDescriptionList;
82 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070083
Jeff Thompson965569b2013-10-12 17:52:52 -070084 /**
85 * Add a certificate extension.
86 * @param extension the extension to be added
87 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070089 addExtension(const CertificateExtension& extension)
90 {
91 m_extensionList.push_back(extension);
92 }
Jeff Thompson965569b2013-10-12 17:52:52 -070093
Jeff Thompson6ef69b22013-12-18 16:24:45 -080094 const ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070095 getExtensionList() const
96 {
97 return m_extensionList;
98 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070099
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800100 ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700101 getExtensionList()
102 {
103 return m_extensionList;
104 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800105
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700106 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700107 setNotBefore(const time::system_clock::TimePoint& notBefore)
108 {
109 m_notBefore = notBefore;
110 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700111
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700113 getNotBefore()
114 {
115 return m_notBefore;
116 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700117
118 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700119 getNotBefore() const
120 {
121 return m_notBefore;
122 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700123
124 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700125 setNotAfter(const time::system_clock::TimePoint& notAfter)
126 {
127 m_notAfter = notAfter;
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 getNotAfter()
132 {
133 return m_notAfter;
134 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700135
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700136 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700137 getNotAfter() const
138 {
139 return m_notAfter;
140 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700141
142 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700143 setPublicKeyInfo(const PublicKey& key)
144 {
145 m_key = key;
146 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700147
148 PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700149 getPublicKeyInfo()
150 {
151 return m_key;
152 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700153
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700154 const PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700155 getPublicKeyInfo() const
156 {
157 return m_key;
158 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700159
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700160 // virtual Name
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800161 // getPublicKeyName() const = 0;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700162
Jeff Thompson965569b2013-10-12 17:52:52 -0700163 /**
164 * Check if the certificate is valid.
165 * @return True if the current time is earlier than notBefore.
166 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700167 bool
Jeff Thompson965569b2013-10-12 17:52:52 -0700168 isTooEarly();
169
170 /**
171 * Check if the certificate is valid.
172 * @return True if the current time is later than notAfter.
173 */
174 bool
175 isTooLate();
176
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700177 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700178 printCertificate(std::ostream& os) const;
Jeff Thompson965569b2013-10-12 17:52:52 -0700179
180protected:
181 void
182 decode();
183
184protected:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700185 SubjectDescriptionList m_subjectDescriptionList;
186 time::system_clock::TimePoint m_notBefore;
187 time::system_clock::TimePoint m_notAfter;
188 PublicKey m_key;
189 ExtensionList m_extensionList;
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700190};
191
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800192inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700193Certificate::wireDecode(const Block& wire)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800194{
195 Data::wireDecode(wire);
196 decode();
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700197}
198
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800199
200inline std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700201operator<<(std::ostream& os, const Certificate& cert)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800202{
203 cert.printCertificate(os);
204 return os;
205}
206
207} // namespace ndn
208
Yingdi Yufc40d872014-02-18 12:56:04 -0800209#endif //NDN_SECURITY_CERTIFICATE_HPP