blob: 6856a2a5fd7e9ab1d3f397e3457d63b6ddf394b8 [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 Thompson4affbf52013-10-18 14:36:46 -0700105 shared_ptr<vector<uint8_t> > blob = blobStream.buf();
Jeff Thompson68192a32013-10-17 17:34:17 -0700106 setContent(blob);
Jeff Thompson855b1402013-10-22 16:11:46 -0700107 getMetaInfo().setType(ndn_ContentType_KEY);
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700108}
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700109
110void
111Certificate::decode()
112{
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700113 Blob blob = getContent();
114
Jeff Thompson2d47db72013-10-17 15:19:52 -0700115 ndnboost::iostreams::stream<ndnboost::iostreams::array_source> is((const char*)blob.buf(), blob.size());
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700116
Jeff Thompson415da1e2013-10-17 16:52:59 -0700117 shared_ptr<der::DerNode> node = der::DerNode::parse(reinterpret_cast<der::InputIterator&>(is));
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700118
119 // der::PrintVisitor printVisitor;
120 // node->accept(printVisitor, string(""));
121
122 der::CertificateDataVisitor certDataVisitor;
123 node->accept(certDataVisitor, this);
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700124}
125
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700126void
127Certificate::printCertificate()
128{
129 cout << "Validity:" << endl;
Jeff Thompson3f0a5072013-10-18 16:34:20 -0700130 cout << der::DerGtime::toIsoString(notBefore_) << endl;
131 cout << der::DerGtime::toIsoString(notAfter_) << endl;
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700132
133 cout << "Subject Info:" << endl;
Jeff Thompson67598332013-10-17 17:57:22 -0700134 vector<CertificateSubjectDescription>::iterator it = subjectDescriptionList_.begin();
135 for(; it < subjectDescriptionList_.end(); it++){
136 cout << it->getOidString() << "\t" << it->getValue() << endl;
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700137 }
138
Jeff Thompson67598332013-10-17 17:57:22 -0700139 ndnboost::iostreams::stream<ndnboost::iostreams::array_source> is((const char*)key_.getKeyDer().buf(), key_.getKeyDer().size());
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700140
Jeff Thompson67598332013-10-17 17:57:22 -0700141 shared_ptr<der::DerNode> keyRoot = der::DerNode::parse(reinterpret_cast<der::InputIterator&> (is));
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700142
143 der::PrintVisitor printVisitor;
144 keyRoot->accept(printVisitor, string(""));
145}
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700146
147}