blob: 260638a3a84c8cf7a9838bcb62a9da6d2608e71a [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
9#ifndef NDN_CERTIFICATE_HPP
Jeff Thompson965569b2013-10-12 17:52:52 -070010#define NDN_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);
Jeff Thompson965569b2013-10-12 17:52:52 -070038
39 /**
40 * The virtual destructor.
41 */
42 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);
47
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 */
58 void
59 addSubjectDescription(const CertificateSubjectDescription& description) { subjectDescriptionList_.push_back(description); }
Jeff Thompson6ef69b22013-12-18 16:24:45 -080060
61 const SubjectDescriptionList&
62 getSubjectDescriptionList() const { return subjectDescriptionList_; }
63
64 SubjectDescriptionList&
65 getSubjectDescriptionList() { return subjectDescriptionList_; }
Jeff Thompson965569b2013-10-12 17:52:52 -070066
67 /**
68 * Add a certificate extension.
69 * @param extension the extension to be added
70 */
71 void
72 addExtension(const CertificateExtension& extension) { extensionList_.push_back(extension); }
73
Jeff Thompson6ef69b22013-12-18 16:24:45 -080074 const ExtensionList&
75 getExtensionList() const { return extensionList_; }
76
77 ExtensionList&
78 getExtensionList() { return extensionList_; }
79
Jeff Thompson965569b2013-10-12 17:52:52 -070080 void
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070081 setNotBefore(const MillisecondsSince1970& notBefore) { notBefore_ = notBefore; }
Jeff Thompson965569b2013-10-12 17:52:52 -070082
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070083 MillisecondsSince1970&
Jeff Thompson965569b2013-10-12 17:52:52 -070084 getNotBefore() { return notBefore_; }
85
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070086 const MillisecondsSince1970&
Jeff Thompson965569b2013-10-12 17:52:52 -070087 getNotBefore() const { return notBefore_; }
88
89 void
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070090 setNotAfter(const MillisecondsSince1970& notAfter) { notAfter_ = notAfter; }
Jeff Thompson965569b2013-10-12 17:52:52 -070091
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070092 MillisecondsSince1970&
Jeff Thompson965569b2013-10-12 17:52:52 -070093 getNotAfter() { return notAfter_; }
94
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070095 const MillisecondsSince1970&
Jeff Thompson965569b2013-10-12 17:52:52 -070096 getNotAfter() const { return notAfter_; }
97
98 void
99 setPublicKeyInfo(const PublicKey& key) { key_ = key; }
100
101 PublicKey&
102 getPublicKeyInfo() { return key_; }
103
104 const PublicKey&
105 getPublicKeyInfo() const { return key_; }
106
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800107 // virtual Name
108 // getPublicKeyName() const = 0;
Jeff Thompson965569b2013-10-12 17:52:52 -0700109
110 /**
111 * Check if the certificate is valid.
112 * @return True if the current time is earlier than notBefore.
113 */
114 bool
115 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
124 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_;
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700133 MillisecondsSince1970 notBefore_;
134 MillisecondsSince1970 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
Jeff Thompsone7e069b2013-09-27 15:48:48 -0700156#endif