blob: 82b6364a1de26f58e03a1ce65a58d47400451656 [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;
Jeff Thompsona5dc3512013-10-17 10:26:19 -070025
26namespace ndn {
27
28Certificate::Certificate()
29 : notBefore_(DBL_MAX)
30 , notAfter_(-DBL_MAX)
31{}
32
33Certificate::Certificate(const Data& data)
34// Use the copy constructor. It clones the signature object.
35: Data(data)
36{
37 // _LOG_DEBUG("Finish local copy: " << getContent().getContent().size());
38
39 decode();
40}
41
42Certificate::~Certificate()
43{
44 //TODO:
45}
46
47bool
48Certificate::isTooEarly()
49{
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070050 MillisecondsSince1970 now = ndn_getNowMilliseconds();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070051 if(now < notBefore_)
52 return true;
53 else
54 return false;
55}
56
57bool
58Certificate::isTooLate()
59{
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070060 MillisecondsSince1970 now = ndn_getNowMilliseconds();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070061 if(now > notAfter_)
62 return true;
63 else
64 return false;
65}
66
Jeff Thompsona5dc3512013-10-17 10:26:19 -070067void
68Certificate::encode()
69{
Jeff Thompsonce115762013-12-18 14:59:56 -080070 ptr_lib::shared_ptr<der::DerSequence> root(new der::DerSequence());
Jeff Thompsona5dc3512013-10-17 10:26:19 -070071
Jeff Thompsonce115762013-12-18 14:59:56 -080072 ptr_lib::shared_ptr<der::DerSequence> validity(new der::DerSequence());
73 ptr_lib::shared_ptr<der::DerGtime> notBefore(new der::DerGtime(notBefore_));
74 ptr_lib::shared_ptr<der::DerGtime> notAfter(new der::DerGtime(notAfter_));
Jeff Thompsona5dc3512013-10-17 10:26:19 -070075 validity->addChild(notBefore);
76 validity->addChild(notAfter);
77 root->addChild(validity);
78
Jeff Thompsonce115762013-12-18 14:59:56 -080079 ptr_lib::shared_ptr<der::DerSequence> subjectList(new der::DerSequence());
Jeff Thompson415da1e2013-10-17 16:52:59 -070080 SubjectDescriptionList::iterator it = subjectDescriptionList_.begin();
81 for(; it != subjectDescriptionList_.end(); it++)
Jeff Thompsona5dc3512013-10-17 10:26:19 -070082 {
Jeff Thompsonce115762013-12-18 14:59:56 -080083 ptr_lib::shared_ptr<der::DerNode> child = it->toDer();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070084 subjectList->addChild(child);
85 }
86 root->addChild(subjectList);
87
Jeff Thompson415da1e2013-10-17 16:52:59 -070088 root->addChild(key_.toDer());
Jeff Thompsona5dc3512013-10-17 10:26:19 -070089
Jeff Thompson415da1e2013-10-17 16:52:59 -070090 if(!extensionList_.empty())
Jeff Thompsona5dc3512013-10-17 10:26:19 -070091 {
Jeff Thompsonce115762013-12-18 14:59:56 -080092 ptr_lib::shared_ptr<der::DerSequence> extnList(new der::DerSequence());
Jeff Thompson415da1e2013-10-17 16:52:59 -070093 ExtensionList::iterator it = extensionList_.begin();
94 for(; it != extensionList_.end(); it++)
95 extnList->addChild(it->toDer());
Jeff Thompsona5dc3512013-10-17 10:26:19 -070096 root->addChild(extnList);
97 }
98
99 blob_stream blobStream;
Jeff Thompson68192a32013-10-17 17:34:17 -0700100 der::OutputIterator& start = reinterpret_cast<der::OutputIterator&>(blobStream);
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700101
102 root->encode(start);
103
Jeff Thompsonce115762013-12-18 14:59:56 -0800104 ptr_lib::shared_ptr<vector<uint8_t> > blob = blobStream.buf();
Jeff Thompson68192a32013-10-17 17:34:17 -0700105 setContent(blob);
Jeff Thompson855b1402013-10-22 16:11:46 -0700106 getMetaInfo().setType(ndn_ContentType_KEY);
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 Thompsonce115762013-12-18 14:59:56 -0800116 ptr_lib::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;
Jeff Thompson3f0a5072013-10-18 16:34:20 -0700129 cout << der::DerGtime::toIsoString(notBefore_) << endl;
130 cout << der::DerGtime::toIsoString(notAfter_) << endl;
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700131
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 Thompsonce115762013-12-18 14:59:56 -0800140 ptr_lib::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}