blob: dc96879f9d5ee80ccdd2dcee60d19a48fb7d723a [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
20class Certificate : public Data {
Jeff Thompsonc69163b2013-10-12 13:49:50 -070021public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070022 class Error : public std::runtime_error
23 {
24 public:
25 explicit
26 Error(const std::string& what)
27 : std::runtime_error(what)
28 {
29 }
30 };
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080031
32 typedef std::vector<CertificateSubjectDescription> SubjectDescriptionList;
33 typedef std::vector<CertificateExtension> ExtensionList;
34
Jeff Thompsonc69163b2013-10-12 13:49:50 -070035 /**
36 * The default constructor.
37 */
38 Certificate();
39
40 /**
41 * Create a Certificate from the content in the data packet.
42 * @param data The data packet with the content to decode.
43 */
44 Certificate(const Data& data);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070045
Jeff Thompson965569b2013-10-12 17:52:52 -070046 /**
47 * The virtual destructor.
48 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070049 virtual
Jeff Thompsona5dc3512013-10-17 10:26:19 -070050 ~Certificate();
Jeff Thompson965569b2013-10-12 17:52:52 -070051
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080052 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070053 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070054
Jeff Thompson965569b2013-10-12 17:52:52 -070055 /**
56 * encode certificate info into content
57 */
58 void
59 encode();
60
61 /**
62 * Add a subject description.
63 * @param description The description to be added.
64 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070065 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070066 addSubjectDescription(const CertificateSubjectDescription& description)
67 {
68 m_subjectDescriptionList.push_back(description);
69 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -080070
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071 const SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070072 getSubjectDescriptionList() const
73 {
74 return m_subjectDescriptionList;
75 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076
77 SubjectDescriptionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070078 getSubjectDescriptionList()
79 {
80 return m_subjectDescriptionList;
81 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070082
Jeff Thompson965569b2013-10-12 17:52:52 -070083 /**
84 * Add a certificate extension.
85 * @param extension the extension to be added
86 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070087 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070088 addExtension(const CertificateExtension& extension)
89 {
90 m_extensionList.push_back(extension);
91 }
Jeff Thompson965569b2013-10-12 17:52:52 -070092
Jeff Thompson6ef69b22013-12-18 16:24:45 -080093 const ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070094 getExtensionList() const
95 {
96 return m_extensionList;
97 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070098
Jeff Thompson6ef69b22013-12-18 16:24:45 -080099 ExtensionList&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700100 getExtensionList()
101 {
102 return m_extensionList;
103 }
Jeff Thompson6ef69b22013-12-18 16:24:45 -0800104
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700105 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700106 setNotBefore(const time::system_clock::TimePoint& notBefore)
107 {
108 m_notBefore = notBefore;
109 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700110
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700111 time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700112 getNotBefore()
113 {
114 return m_notBefore;
115 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700116
117 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700118 getNotBefore() const
119 {
120 return m_notBefore;
121 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700122
123 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700124 setNotAfter(const time::system_clock::TimePoint& notAfter)
125 {
126 m_notAfter = notAfter;
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 getNotAfter()
131 {
132 return m_notAfter;
133 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700134
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700135 const time::system_clock::TimePoint&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700136 getNotAfter() const
137 {
138 return m_notAfter;
139 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700140
141 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700142 setPublicKeyInfo(const PublicKey& key)
143 {
144 m_key = key;
145 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700146
147 PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700148 getPublicKeyInfo()
149 {
150 return m_key;
151 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700152
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700153 const PublicKey&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700154 getPublicKeyInfo() const
155 {
156 return m_key;
157 }
Jeff Thompson965569b2013-10-12 17:52:52 -0700158
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700159 // virtual Name
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800160 // getPublicKeyName() const = 0;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700161
Jeff Thompson965569b2013-10-12 17:52:52 -0700162 /**
163 * Check if the certificate is valid.
164 * @return True if the current time is earlier than notBefore.
165 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700166 bool
Jeff Thompson965569b2013-10-12 17:52:52 -0700167 isTooEarly();
168
169 /**
170 * Check if the certificate is valid.
171 * @return True if the current time is later than notAfter.
172 */
173 bool
174 isTooLate();
175
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700176 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700177 printCertificate(std::ostream& os) const;
Jeff Thompson965569b2013-10-12 17:52:52 -0700178
179protected:
180 void
181 decode();
182
183protected:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700184 SubjectDescriptionList m_subjectDescriptionList;
185 time::system_clock::TimePoint m_notBefore;
186 time::system_clock::TimePoint m_notAfter;
187 PublicKey m_key;
188 ExtensionList m_extensionList;
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700189};
190
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800191inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700192Certificate::wireDecode(const Block& wire)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800193{
194 Data::wireDecode(wire);
195 decode();
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700196}
197
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800198
199inline std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700200operator<<(std::ostream& os, const Certificate& cert)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800201{
202 cert.printCertificate(os);
203 return os;
204}
205
206} // namespace ndn
207
Yingdi Yufc40d872014-02-18 12:56:04 -0800208#endif //NDN_SECURITY_CERTIFICATE_HPP