blob: 3470134255d95552e328f696b597f8d7a42afdfd [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 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
Jeff Thompson965569b2013-10-12 17:52:52 -070067 addSubjectDescription(const CertificateSubjectDescription& description) { subjectDescriptionList_.push_back(description); }
Jeff Thompson6ef69b22013-12-18 16:24:45 -080068
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069 const SubjectDescriptionList&
Jeff Thompson6ef69b22013-12-18 16:24:45 -080070 getSubjectDescriptionList() const { return subjectDescriptionList_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071
72 SubjectDescriptionList&
Jeff Thompson6ef69b22013-12-18 16:24:45 -080073 getSubjectDescriptionList() { return subjectDescriptionList_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074
Jeff Thompson965569b2013-10-12 17:52:52 -070075 /**
76 * Add a certificate extension.
77 * @param extension the extension to be added
78 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070079 void
Jeff Thompson965569b2013-10-12 17:52:52 -070080 addExtension(const CertificateExtension& extension) { extensionList_.push_back(extension); }
81
Jeff Thompson6ef69b22013-12-18 16:24:45 -080082 const ExtensionList&
83 getExtensionList() const { return extensionList_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084
Jeff Thompson6ef69b22013-12-18 16:24:45 -080085 ExtensionList&
86 getExtensionList() { return extensionList_; }
87
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088 void
89 setNotBefore(const time::system_clock::TimePoint& notBefore) { notBefore_ = notBefore; }
Jeff Thompson965569b2013-10-12 17:52:52 -070090
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091 time::system_clock::TimePoint&
Jeff Thompson965569b2013-10-12 17:52:52 -070092 getNotBefore() { return notBefore_; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070093
94 const time::system_clock::TimePoint&
Jeff Thompson965569b2013-10-12 17:52:52 -070095 getNotBefore() const { return notBefore_; }
96
97 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070098 setNotAfter(const time::system_clock::TimePoint& notAfter) { notAfter_ = notAfter; }
Jeff Thompson965569b2013-10-12 17:52:52 -070099
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700100 time::system_clock::TimePoint&
Jeff Thompson965569b2013-10-12 17:52:52 -0700101 getNotAfter() { return notAfter_; }
102
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700103 const time::system_clock::TimePoint&
Jeff Thompson965569b2013-10-12 17:52:52 -0700104 getNotAfter() const { return notAfter_; }
105
106 void
107 setPublicKeyInfo(const PublicKey& key) { key_ = key; }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700108
109 PublicKey&
Jeff Thompson965569b2013-10-12 17:52:52 -0700110 getPublicKeyInfo() { return key_; }
111
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112 const PublicKey&
Jeff Thompson965569b2013-10-12 17:52:52 -0700113 getPublicKeyInfo() const { return key_; }
114
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700115 // virtual Name
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800116 // getPublicKeyName() const = 0;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700117
Jeff Thompson965569b2013-10-12 17:52:52 -0700118 /**
119 * Check if the certificate is valid.
120 * @return True if the current time is earlier than notBefore.
121 */
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700122 bool
Jeff Thompson965569b2013-10-12 17:52:52 -0700123 isTooEarly();
124
125 /**
126 * Check if the certificate is valid.
127 * @return True if the current time is later than notAfter.
128 */
129 bool
130 isTooLate();
131
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700132 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700133 printCertificate(std::ostream& os) const;
Jeff Thompson965569b2013-10-12 17:52:52 -0700134
135protected:
136 void
137 decode();
138
139protected:
140 SubjectDescriptionList subjectDescriptionList_;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700141 time::system_clock::TimePoint notBefore_;
142 time::system_clock::TimePoint notAfter_;
Jeff Thompson965569b2013-10-12 17:52:52 -0700143 PublicKey key_;
144 ExtensionList extensionList_;
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700145};
146
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800147inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700148Certificate::wireDecode(const Block& wire)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800149{
150 Data::wireDecode(wire);
151 decode();
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700152}
153
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800154
155inline std::ostream&
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700156operator <<(std::ostream& os, const Certificate& cert)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800157{
158 cert.printCertificate(os);
159 return os;
160}
161
162} // namespace ndn
163
Yingdi Yufc40d872014-02-18 12:56:04 -0800164#endif //NDN_SECURITY_CERTIFICATE_HPP