blob: d27f7a491ba7434592b30b2bec5060ddfa17f500 [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
15#include "certificate-subject-description.hpp"
16#include "certificate-extension.hpp"
17#include "public-key.hpp"
Jeff Thompsone7e069b2013-09-27 15:48:48 -070018
19namespace ndn {
20
21class Certificate : public Data {
Jeff Thompsonc69163b2013-10-12 13:49:50 -070022public:
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080023 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
24
25 typedef std::vector<CertificateSubjectDescription> SubjectDescriptionList;
26 typedef std::vector<CertificateExtension> ExtensionList;
27
Jeff Thompsonc69163b2013-10-12 13:49:50 -070028 /**
29 * The default constructor.
30 */
31 Certificate();
32
33 /**
34 * Create a Certificate from the content in the data packet.
35 * @param data The data packet with the content to decode.
36 */
37 Certificate(const Data& data);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070038
Jeff Thompson965569b2013-10-12 17:52:52 -070039 /**
40 * The virtual destructor.
41 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070042 virtual
Jeff Thompsona5dc3512013-10-17 10:26:19 -070043 ~Certificate();
Jeff Thompson965569b2013-10-12 17:52:52 -070044
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080045 inline void
46 wireDecode(const Block &wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070047
Jeff Thompson965569b2013-10-12 17:52:52 -070048 /**
49 * encode certificate info into content
50 */
51 void
52 encode();
53
54 /**
55 * Add a subject description.
56 * @param description The description to be added.
57 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070058 void
Jeff Thompson965569b2013-10-12 17:52:52 -070059 addSubjectDescription(const CertificateSubjectDescription& description) { subjectDescriptionList_.push_back(description); }
Jeff Thompson6ef69b22013-12-18 16:24:45 -080060
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061 const SubjectDescriptionList&
Jeff Thompson6ef69b22013-12-18 16:24:45 -080062 getSubjectDescriptionList() const { return subjectDescriptionList_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063
64 SubjectDescriptionList&
Jeff Thompson6ef69b22013-12-18 16:24:45 -080065 getSubjectDescriptionList() { return subjectDescriptionList_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066
Jeff Thompson965569b2013-10-12 17:52:52 -070067 /**
68 * Add a certificate extension.
69 * @param extension the extension to be added
70 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071 void
Jeff Thompson965569b2013-10-12 17:52:52 -070072 addExtension(const CertificateExtension& extension) { extensionList_.push_back(extension); }
73
Jeff Thompson6ef69b22013-12-18 16:24:45 -080074 const ExtensionList&
75 getExtensionList() const { return extensionList_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076
Jeff Thompson6ef69b22013-12-18 16:24:45 -080077 ExtensionList&
78 getExtensionList() { return extensionList_; }
79
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070080 void
81 setNotBefore(const time::system_clock::TimePoint& notBefore) { notBefore_ = notBefore; }
Jeff Thompson965569b2013-10-12 17:52:52 -070082
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070083 time::system_clock::TimePoint&
Jeff Thompson965569b2013-10-12 17:52:52 -070084 getNotBefore() { return notBefore_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070085
86 const time::system_clock::TimePoint&
Jeff Thompson965569b2013-10-12 17:52:52 -070087 getNotBefore() const { return notBefore_; }
88
89 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070090 setNotAfter(const time::system_clock::TimePoint& notAfter) { notAfter_ = notAfter; }
Jeff Thompson965569b2013-10-12 17:52:52 -070091
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070092 time::system_clock::TimePoint&
Jeff Thompson965569b2013-10-12 17:52:52 -070093 getNotAfter() { return notAfter_; }
94
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070095 const time::system_clock::TimePoint&
Jeff Thompson965569b2013-10-12 17:52:52 -070096 getNotAfter() const { return notAfter_; }
97
98 void
99 setPublicKeyInfo(const PublicKey& key) { key_ = key; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700100
101 PublicKey&
Jeff Thompson965569b2013-10-12 17:52:52 -0700102 getPublicKeyInfo() { return key_; }
103
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700104 const PublicKey&
Jeff Thompson965569b2013-10-12 17:52:52 -0700105 getPublicKeyInfo() const { return key_; }
106
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700107 // virtual Name
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800108 // getPublicKeyName() const = 0;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700109
Jeff Thompson965569b2013-10-12 17:52:52 -0700110 /**
111 * Check if the certificate is valid.
112 * @return True if the current time is earlier than notBefore.
113 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700114 bool
Jeff Thompson965569b2013-10-12 17:52:52 -0700115 isTooEarly();
116
117 /**
118 * Check if the certificate is valid.
119 * @return True if the current time is later than notAfter.
120 */
121 bool
122 isTooLate();
123
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700124 void
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800125 printCertificate(std::ostream &os) const;
Jeff Thompson965569b2013-10-12 17:52:52 -0700126
127protected:
128 void
129 decode();
130
131protected:
132 SubjectDescriptionList subjectDescriptionList_;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700133 time::system_clock::TimePoint notBefore_;
134 time::system_clock::TimePoint notAfter_;
Jeff Thompson965569b2013-10-12 17:52:52 -0700135 PublicKey key_;
136 ExtensionList extensionList_;
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700137};
138
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800139inline void
140Certificate::wireDecode(const Block &wire)
141{
142 Data::wireDecode(wire);
143 decode();
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700144}
145
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800146
147inline std::ostream&
148operator <<(std::ostream &os, const Certificate &cert)
149{
150 cert.printCertificate(os);
151 return os;
152}
153
154} // namespace ndn
155
Yingdi Yufc40d872014-02-18 12:56:04 -0800156#endif //NDN_SECURITY_CERTIFICATE_HPP