All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
certificate.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
9 #include "common.hpp"
10 
11 #include "certificate.hpp"
12 #include "../util/time.hpp"
13 #include "cryptopp.hpp"
14 #include "../encoding/cryptopp/asn_ext.hpp"
15 
16 using namespace std;
17 
18 namespace ndn {
19 
20 Certificate::Certificate()
21  : m_notBefore(time::system_clock::TimePoint::max())
22  , m_notAfter(time::system_clock::TimePoint::min())
23 {
24 }
25 
27  // Use the copy constructor. It clones the signature object.
28  : Data(data)
29 {
30  decode();
31 }
32 
34 {
35  //TODO:
36 }
37 
38 bool
40 {
41  if (time::system_clock::now() < m_notBefore)
42  return true;
43  else
44  return false;
45 }
46 
47 bool
49 {
50  if (time::system_clock::now() > m_notAfter)
51  return true;
52  else
53  return false;
54 }
55 
56 void
58 {
59  // Name
60  // <key_name>/ID-CERT/<id#>
61  // Content
62  // DER encoded idCert:
63  //
64  // idCert ::= SEQUENCE {
65  // validity Validity,
66  // subject Name,
67  // subjectPubKeyInfo SubjectPublicKeyInfo,
68  // extension Extensions OPTIONAL }
69  //
70  // Validity ::= SEQUENCE {
71  // notBefore Time,
72  // notAfter Time }
73  //
74  // Name ::= CHOICE {
75  // RDNSequence }
76  //
77  // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
78  //
79  // RelativeDistinguishedName ::=
80  // SET OF AttributeTypeAndValue
81  //
82  // SubjectPublicKeyInfo ::= SEQUENCE {
83  // algorithm AlgorithmIdentifier
84  // keybits BIT STRING }
85  //
86  // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
87  //
88  // (see http://www.ietf.org/rfc/rfc3280.txt for more detail)
89  //
90  // KeyLocator
91  // issuer’s certificate name
92  // Signature
93 
94  using namespace CryptoPP;
95 
96  OBufferStream os;
97  CryptoPP::FileSink sink(os);
98 
99  // idCert ::= SEQUENCE {
100  // validity Validity,
101  // subject Name,
102  // subjectPubKeyInfo SubjectPublicKeyInfo,
103  // extension Extensions OPTIONAL }
104  DERSequenceEncoder idCert(sink);
105  {
106  // Validity ::= SEQUENCE {
107  // notBefore Time,
108  // notAfter Time }
109  DERSequenceEncoder validity(idCert);
110  {
112  DEREncodeGeneralTime(validity, m_notAfter);
113  }
114  validity.MessageEnd();
115 
116  // Name ::= CHOICE {
117  // RDNSequence }
118  //
119  // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
120  DERSequenceEncoder name(idCert);
121  {
122  for (SubjectDescriptionList::iterator it = m_subjectDescriptionList.begin();
123  it != m_subjectDescriptionList.end(); ++it)
124  {
125  it->encode(name);
126  }
127  }
128  name.MessageEnd();
129 
130  // SubjectPublicKeyInfo
131  m_key.encode(idCert);
132 
133  // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
134  //
135  // Extension ::= SEQUENCE {
136  // extnID OBJECT IDENTIFIER,
137  // critical BOOLEAN DEFAULT FALSE,
138  // extnValue OCTET STRING }
139  if (!m_extensionList.empty())
140  {
141  DERSequenceEncoder extensions(idCert);
142  {
143  for (ExtensionList::iterator it = m_extensionList.begin();
144  it != m_extensionList.end(); ++it)
145  {
146  it->encode(extensions);
147  }
148  }
149  extensions.MessageEnd();
150  }
151  }
152 
153  idCert.MessageEnd();
154 
155  setContent(os.buf());
157 }
158 
159 void
161 {
162  using namespace CryptoPP;
163 
164  OBufferStream os;
165  StringSource source(getContent().value(), getContent().value_size(), true);
166 
167  // idCert ::= SEQUENCE {
168  // validity Validity,
169  // subject Name,
170  // subjectPubKeyInfo SubjectPublicKeyInfo,
171  // extension Extensions OPTIONAL }
172  BERSequenceDecoder idCert(source);
173  {
174  // Validity ::= SEQUENCE {
175  // notBefore Time,
176  // notAfter Time }
177  BERSequenceDecoder validity(idCert);
178  {
179  BERDecodeTime(validity, m_notBefore);
180  BERDecodeTime(validity, m_notAfter);
181  }
182  validity.MessageEnd();
183 
184  // Name ::= CHOICE {
185  // RDNSequence }
186  //
187  // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
188  m_subjectDescriptionList.clear();
189  BERSequenceDecoder name(idCert);
190  {
191  while (!name.EndReached())
192  {
194  }
195  }
196  name.MessageEnd();
197 
198  // SubjectPublicKeyInfo ::= SEQUENCE {
199  // algorithm AlgorithmIdentifier
200  // keybits BIT STRING }
201  m_key.decode(idCert);
202 
203  // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
204  //
205  // Extension ::= SEQUENCE {
206  // extnID OBJECT IDENTIFIER,
207  // critical BOOLEAN DEFAULT FALSE,
208  // extnValue OCTET STRING }
209  m_extensionList.clear();
210  if (!idCert.EndReached())
211  {
212  BERSequenceDecoder extensions(idCert);
213  {
214  while (!extensions.EndReached())
215  {
216  m_extensionList.push_back(CertificateExtension(extensions));
217  }
218  }
219  extensions.MessageEnd();
220  }
221  }
222 
223  idCert.MessageEnd();
224 }
225 
226 void
227 Certificate::printCertificate(std::ostream& os) const
228 {
229  os << "Certificate name:" << endl;
230  os << " " << getName() << endl;
231  os << "Validity:" << endl;
232  {
233  os << " NotBefore: " << time::toIsoString(m_notBefore) << endl;
234  os << " NotAfter: " << time::toIsoString(m_notAfter) << endl;
235  }
236 
237  os << "Subject Description:" << endl;
238  for (SubjectDescriptionList::const_iterator it = m_subjectDescriptionList.begin();
239  it != m_subjectDescriptionList.end(); ++it)
240  {
241  os << " " << it->getOidString() << ": " << it->getValue() << endl;
242  }
243 
244  os << "Public key bits:" << endl;
245  CryptoPP::Base64Encoder encoder(new CryptoPP::FileSink(os), true, 64);
246  m_key.encode(encoder);
247 }
248 
249 } // namespace ndn
Class implementing interface similar to ostringstream, but to construct ndn::Buffer.
Definition: buffer.hpp:176
ExtensionList m_extensionList
size_t DEREncodeGeneralTime(CryptoPP::BufferedTransformation &bt, const time::system_clock::TimePoint &time)
Definition: asn_ext.cpp:20
void setContentType(uint32_t type)
Definition: data.hpp:378
A CertificateSubjectDescription represents the SubjectDescription entry in a Certificate.
void encode(CryptoPP::BufferedTransformation &out) const
Definition: public-key.cpp:36
Certificate()
The default constructor.
Definition: certificate.cpp:20
const Name & getName() const
Definition: data.hpp:346
time::system_clock::TimePoint m_notAfter
void setContent(const uint8_t *content, size_t contentLength)
Set the content to a copy of the data in the vector.
Definition: data.hpp:422
void BERDecodeTime(CryptoPP::BufferedTransformation &bt, time::system_clock::TimePoint &time)
Definition: asn_ext.cpp:36
bool isTooLate()
Check if the certificate is valid.
Definition: certificate.cpp:48
void encode()
encode certificate info into content
Definition: certificate.cpp:57
time::system_clock::TimePoint m_notBefore
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.hpp:135
bool isTooEarly()
Check if the certificate is valid.
Definition: certificate.cpp:39
void decode(CryptoPP::BufferedTransformation &in)
Definition: public-key.cpp:46
SubjectDescriptionList m_subjectDescriptionList
const Block & getContent() const
Get content Block.
Definition: data.hpp:411
virtual ~Certificate()
The virtual destructor.
Definition: certificate.cpp:33
A CertificateExtension represents the Extension entry in a certificate.
void printCertificate(std::ostream &os) const