blob: 8f6288c5c2f9d66c1d9304af5671a16b696c7498 [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"
16#include "../../encoding/der/visitor/print-visitor.hpp"
Jeff Thompsona5dc3512013-10-17 10:26:19 -070017#include "../../util/logging.hpp"
Jeff Thompson68192a32013-10-17 17:34:17 -070018#include "../../util/blob-stream.hpp"
Jeff Thompsona5dc3512013-10-17 10:26:19 -070019#include "../../c/util/time.h"
20#include <ndn-cpp/security/certificate/certificate.hpp>
21
22INIT_LOGGER("ndn.security.Certificate");
23
24using namespace std;
25using namespace ndn::ptr_lib;
26
27namespace ndn {
28
29Certificate::Certificate()
30 : notBefore_(DBL_MAX)
31 , notAfter_(-DBL_MAX)
32{}
33
34Certificate::Certificate(const Data& data)
35// Use the copy constructor. It clones the signature object.
36: Data(data)
37{
38 // _LOG_DEBUG("Finish local copy: " << getContent().getContent().size());
39
40 decode();
41}
42
43Certificate::~Certificate()
44{
45 //TODO:
46}
47
48bool
49Certificate::isTooEarly()
50{
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070051 MillisecondsSince1970 now = ndn_getNowMilliseconds();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070052 if(now < notBefore_)
53 return true;
54 else
55 return false;
56}
57
58bool
59Certificate::isTooLate()
60{
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070061 MillisecondsSince1970 now = ndn_getNowMilliseconds();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070062 if(now > notAfter_)
63 return true;
64 else
65 return false;
66}
67
Jeff Thompsona5dc3512013-10-17 10:26:19 -070068void
69Certificate::encode()
70{
Jeff Thompson415da1e2013-10-17 16:52:59 -070071 shared_ptr<der::DerSequence> root(new der::DerSequence());
Jeff Thompsona5dc3512013-10-17 10:26:19 -070072
Jeff Thompson415da1e2013-10-17 16:52:59 -070073 shared_ptr<der::DerSequence> validity(new der::DerSequence());
74 shared_ptr<der::DerGtime> notBefore(new der::DerGtime(notBefore_));
75 shared_ptr<der::DerGtime> notAfter(new der::DerGtime(notAfter_));
Jeff Thompsona5dc3512013-10-17 10:26:19 -070076 validity->addChild(notBefore);
77 validity->addChild(notAfter);
78 root->addChild(validity);
79
Jeff Thompson415da1e2013-10-17 16:52:59 -070080 shared_ptr<der::DerSequence> subjectList(new der::DerSequence());
81 SubjectDescriptionList::iterator it = subjectDescriptionList_.begin();
82 for(; it != subjectDescriptionList_.end(); it++)
Jeff Thompsona5dc3512013-10-17 10:26:19 -070083 {
Jeff Thompson415da1e2013-10-17 16:52:59 -070084 shared_ptr<der::DerNode> child = it->toDer();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070085 subjectList->addChild(child);
86 }
87 root->addChild(subjectList);
88
Jeff Thompson415da1e2013-10-17 16:52:59 -070089 root->addChild(key_.toDer());
Jeff Thompsona5dc3512013-10-17 10:26:19 -070090
Jeff Thompson415da1e2013-10-17 16:52:59 -070091 if(!extensionList_.empty())
Jeff Thompsona5dc3512013-10-17 10:26:19 -070092 {
Jeff Thompson415da1e2013-10-17 16:52:59 -070093 shared_ptr<der::DerSequence> extnList(new der::DerSequence());
94 ExtensionList::iterator it = extensionList_.begin();
95 for(; it != extensionList_.end(); it++)
96 extnList->addChild(it->toDer());
Jeff Thompsona5dc3512013-10-17 10:26:19 -070097 root->addChild(extnList);
98 }
99
100 blob_stream blobStream;
Jeff Thompson68192a32013-10-17 17:34:17 -0700101 der::OutputIterator& start = reinterpret_cast<der::OutputIterator&>(blobStream);
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700102
103 root->encode(start);
104
Jeff Thompson68192a32013-10-17 17:34:17 -0700105 shared_ptr<std::vector<uint8_t> > blob = blobStream.buf();
106 setContent(blob);
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700107}
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700108
109void
110Certificate::decode()
111{
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700112 Blob blob = getContent();
113
Jeff Thompson2d47db72013-10-17 15:19:52 -0700114 ndnboost::iostreams::stream<ndnboost::iostreams::array_source> is((const char*)blob.buf(), blob.size());
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700115
Jeff Thompson415da1e2013-10-17 16:52:59 -0700116 shared_ptr<der::DerNode> node = der::DerNode::parse(reinterpret_cast<der::InputIterator&>(is));
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700117
118 // der::PrintVisitor printVisitor;
119 // node->accept(printVisitor, string(""));
120
121 der::CertificateDataVisitor certDataVisitor;
122 node->accept(certDataVisitor, this);
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700123}
124
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700125void
126Certificate::printCertificate()
127{
128 cout << "Validity:" << endl;
129 cout << notBefore_ << endl;
130 cout << notAfter_ << endl;
131
132 cout << "Subject Info:" << endl;
Jeff Thompson67598332013-10-17 17:57:22 -0700133 vector<CertificateSubjectDescription>::iterator it = subjectDescriptionList_.begin();
134 for(; it < subjectDescriptionList_.end(); it++){
135 cout << it->getOidString() << "\t" << it->getValue() << endl;
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700136 }
137
Jeff Thompson67598332013-10-17 17:57:22 -0700138 ndnboost::iostreams::stream<ndnboost::iostreams::array_source> is((const char*)key_.getKeyDer().buf(), key_.getKeyDer().size());
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700139
Jeff Thompson67598332013-10-17 17:57:22 -0700140 shared_ptr<der::DerNode> keyRoot = der::DerNode::parse(reinterpret_cast<der::InputIterator&> (is));
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700141
142 der::PrintVisitor printVisitor;
143 keyRoot->accept(printVisitor, string(""));
144}
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700145
146}