blob: b41b739a40b0bf12baf255c3e12100e09fbae433 [file] [log] [blame]
Jeff Thompsona5dc3512013-10-17 10:26:19 -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#include <float.h>
Jeff Thompson415da1e2013-10-17 16:52:59 -070010// We can use ndnboost::iostreams because this is internal and will not conflict with the application if it uses boost::iostreams.
Jeff Thompson2d47db72013-10-17 15:19:52 -070011#include <ndnboost/iostreams/stream.hpp>
12#include <ndnboost/iostreams/device/array.hpp>
Jeff Thompsona5dc3512013-10-17 10:26:19 -070013#include <ndn-cpp/sha256-with-rsa-signature.hpp>
Jeff Thompson415da1e2013-10-17 16:52:59 -070014#include "../../encoding/der/der.hpp"
Jeff Thompsona5dc3512013-10-17 10:26:19 -070015#include "../../encoding/der/visitor/certificate-data-visitor.hpp"
Jeff Thompson415da1e2013-10-17 16:52:59 -070016#if 0
Jeff Thompsona5dc3512013-10-17 10:26:19 -070017#include "../../encoding/der/visitor/print-visitor.hpp"
18#endif
19#include "../../util/logging.hpp"
20#include "../../c/util/time.h"
21#include <ndn-cpp/security/certificate/certificate.hpp>
22
23INIT_LOGGER("ndn.security.Certificate");
24
25using namespace std;
26using namespace ndn::ptr_lib;
27
28namespace ndn {
29
30Certificate::Certificate()
31 : notBefore_(DBL_MAX)
32 , notAfter_(-DBL_MAX)
33{}
34
35Certificate::Certificate(const Data& data)
36// Use the copy constructor. It clones the signature object.
37: Data(data)
38{
39 // _LOG_DEBUG("Finish local copy: " << getContent().getContent().size());
40
41 decode();
42}
43
44Certificate::~Certificate()
45{
46 //TODO:
47}
48
49bool
50Certificate::isTooEarly()
51{
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070052 MillisecondsSince1970 now = ndn_getNowMilliseconds();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070053 if(now < notBefore_)
54 return true;
55 else
56 return false;
57}
58
59bool
60Certificate::isTooLate()
61{
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070062 MillisecondsSince1970 now = ndn_getNowMilliseconds();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070063 if(now > notAfter_)
64 return true;
65 else
66 return false;
67}
68
69#if 0
70void
71Certificate::encode()
72{
Jeff Thompson415da1e2013-10-17 16:52:59 -070073 shared_ptr<der::DerSequence> root(new der::DerSequence());
Jeff Thompsona5dc3512013-10-17 10:26:19 -070074
Jeff Thompson415da1e2013-10-17 16:52:59 -070075 shared_ptr<der::DerSequence> validity(new der::DerSequence());
76 shared_ptr<der::DerGtime> notBefore(new der::DerGtime(notBefore_));
77 shared_ptr<der::DerGtime> notAfter(new der::DerGtime(notAfter_));
Jeff Thompsona5dc3512013-10-17 10:26:19 -070078 validity->addChild(notBefore);
79 validity->addChild(notAfter);
80 root->addChild(validity);
81
Jeff Thompson415da1e2013-10-17 16:52:59 -070082 shared_ptr<der::DerSequence> subjectList(new der::DerSequence());
83 SubjectDescriptionList::iterator it = subjectDescriptionList_.begin();
84 for(; it != subjectDescriptionList_.end(); it++)
Jeff Thompsona5dc3512013-10-17 10:26:19 -070085 {
Jeff Thompson415da1e2013-10-17 16:52:59 -070086 shared_ptr<der::DerNode> child = it->toDer();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070087 subjectList->addChild(child);
88 }
89 root->addChild(subjectList);
90
Jeff Thompson415da1e2013-10-17 16:52:59 -070091 root->addChild(key_.toDer());
Jeff Thompsona5dc3512013-10-17 10:26:19 -070092
Jeff Thompson415da1e2013-10-17 16:52:59 -070093 if(!extensionList_.empty())
Jeff Thompsona5dc3512013-10-17 10:26:19 -070094 {
Jeff Thompson415da1e2013-10-17 16:52:59 -070095 shared_ptr<der::DerSequence> extnList(new der::DerSequence());
96 ExtensionList::iterator it = extensionList_.begin();
97 for(; it != extensionList_.end(); it++)
98 extnList->addChild(it->toDer());
Jeff Thompsona5dc3512013-10-17 10:26:19 -070099 root->addChild(extnList);
100 }
101
102 blob_stream blobStream;
103 OutputIterator& start = reinterpret_cast<OutputIterator&>(blobStream);
104
105 root->encode(start);
106
Jeff Thompson415da1e2013-10-17 16:52:59 -0700107 shared_ptr<Blob> blob = blobStream.buf();
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700108 Content content(blob->buf(), blob->size());
109 setContent(content);
110}
111#endif
112
113void
114Certificate::decode()
115{
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700116 Blob blob = getContent();
117
Jeff Thompson2d47db72013-10-17 15:19:52 -0700118 ndnboost::iostreams::stream<ndnboost::iostreams::array_source> is((const char*)blob.buf(), blob.size());
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700119
Jeff Thompson415da1e2013-10-17 16:52:59 -0700120 shared_ptr<der::DerNode> node = der::DerNode::parse(reinterpret_cast<der::InputIterator&>(is));
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700121
122 // der::PrintVisitor printVisitor;
123 // node->accept(printVisitor, string(""));
124
125 der::CertificateDataVisitor certDataVisitor;
126 node->accept(certDataVisitor, this);
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700127}
128
129#if 0
130void
131Certificate::printCertificate()
132{
133 cout << "Validity:" << endl;
134 cout << notBefore_ << endl;
135 cout << notAfter_ << endl;
136
137 cout << "Subject Info:" << endl;
138 vector<CertificateSubDescrypt>::iterator it = m_subjectList.begin();
139 for(; it < m_subjectList.end(); it++){
140 cout << it->getOidStr() << "\t" << it->getValue() << endl;
141 }
142
143 boost::iostreams::stream
144 <boost::iostreams::array_source> is(key_.getKeyBlob().buf (), m_key.getKeyBlob().size ());
145
Jeff Thompson415da1e2013-10-17 16:52:59 -0700146 shared_ptr<der::DerNode> keyRoot = der::DerNode::parse(reinterpret_cast<InputIterator&> (is));
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700147
148 der::PrintVisitor printVisitor;
149 keyRoot->accept(printVisitor, string(""));
150}
151#endif
152
153}