blob: 3275ac5965169399309ad11d880bd0b2aba8a8d0 [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
Alexander Afanasyev8e96e582013-11-19 12:07:04 -08009#include <ndn-cpp/common.hpp>
10
Jeff Thompsona5dc3512013-10-17 10:26:19 -070011#include <float.h>
Alexander Afanasyev8e96e582013-11-19 12:07:04 -080012
13#if NDN_CPP_USE_SYSTEM_BOOST
14#include <boost/iostreams/stream.hpp>
15#include <boost/iostreams/device/array.hpp>
16namespace ndnboost = boost;
17#else
Jeff Thompson415da1e2013-10-17 16:52:59 -070018// 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 -070019#include <ndnboost/iostreams/stream.hpp>
20#include <ndnboost/iostreams/device/array.hpp>
Alexander Afanasyev8e96e582013-11-19 12:07:04 -080021#endif
22
Jeff Thompsona5dc3512013-10-17 10:26:19 -070023#include <ndn-cpp/sha256-with-rsa-signature.hpp>
Jeff Thompson415da1e2013-10-17 16:52:59 -070024#include "../../encoding/der/der.hpp"
Jeff Thompsona5dc3512013-10-17 10:26:19 -070025#include "../../encoding/der/visitor/certificate-data-visitor.hpp"
26#include "../../encoding/der/visitor/print-visitor.hpp"
Jeff Thompsona5dc3512013-10-17 10:26:19 -070027#include "../../util/logging.hpp"
Jeff Thompson68192a32013-10-17 17:34:17 -070028#include "../../util/blob-stream.hpp"
Jeff Thompsona5dc3512013-10-17 10:26:19 -070029#include "../../c/util/time.h"
30#include <ndn-cpp/security/certificate/certificate.hpp>
31
32INIT_LOGGER("ndn.security.Certificate");
33
34using namespace std;
Jeff Thompsona5dc3512013-10-17 10:26:19 -070035
36namespace ndn {
37
38Certificate::Certificate()
39 : notBefore_(DBL_MAX)
40 , notAfter_(-DBL_MAX)
41{}
42
43Certificate::Certificate(const Data& data)
44// Use the copy constructor. It clones the signature object.
45: Data(data)
46{
47 // _LOG_DEBUG("Finish local copy: " << getContent().getContent().size());
48
49 decode();
50}
51
52Certificate::~Certificate()
53{
54 //TODO:
55}
56
57bool
58Certificate::isTooEarly()
59{
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070060 MillisecondsSince1970 now = ndn_getNowMilliseconds();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070061 if(now < notBefore_)
62 return true;
63 else
64 return false;
65}
66
67bool
68Certificate::isTooLate()
69{
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070070 MillisecondsSince1970 now = ndn_getNowMilliseconds();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070071 if(now > notAfter_)
72 return true;
73 else
74 return false;
75}
76
Jeff Thompsona5dc3512013-10-17 10:26:19 -070077void
78Certificate::encode()
79{
Jeff Thompsonce115762013-12-18 14:59:56 -080080 ptr_lib::shared_ptr<der::DerSequence> root(new der::DerSequence());
Jeff Thompsona5dc3512013-10-17 10:26:19 -070081
Jeff Thompsonce115762013-12-18 14:59:56 -080082 ptr_lib::shared_ptr<der::DerSequence> validity(new der::DerSequence());
83 ptr_lib::shared_ptr<der::DerGtime> notBefore(new der::DerGtime(notBefore_));
84 ptr_lib::shared_ptr<der::DerGtime> notAfter(new der::DerGtime(notAfter_));
Jeff Thompsona5dc3512013-10-17 10:26:19 -070085 validity->addChild(notBefore);
86 validity->addChild(notAfter);
87 root->addChild(validity);
88
Jeff Thompsonce115762013-12-18 14:59:56 -080089 ptr_lib::shared_ptr<der::DerSequence> subjectList(new der::DerSequence());
Jeff Thompson415da1e2013-10-17 16:52:59 -070090 SubjectDescriptionList::iterator it = subjectDescriptionList_.begin();
91 for(; it != subjectDescriptionList_.end(); it++)
Jeff Thompsona5dc3512013-10-17 10:26:19 -070092 {
Jeff Thompsonce115762013-12-18 14:59:56 -080093 ptr_lib::shared_ptr<der::DerNode> child = it->toDer();
Jeff Thompsona5dc3512013-10-17 10:26:19 -070094 subjectList->addChild(child);
95 }
96 root->addChild(subjectList);
97
Jeff Thompson415da1e2013-10-17 16:52:59 -070098 root->addChild(key_.toDer());
Jeff Thompsona5dc3512013-10-17 10:26:19 -070099
Jeff Thompson415da1e2013-10-17 16:52:59 -0700100 if(!extensionList_.empty())
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700101 {
Jeff Thompsonce115762013-12-18 14:59:56 -0800102 ptr_lib::shared_ptr<der::DerSequence> extnList(new der::DerSequence());
Jeff Thompson415da1e2013-10-17 16:52:59 -0700103 ExtensionList::iterator it = extensionList_.begin();
104 for(; it != extensionList_.end(); it++)
105 extnList->addChild(it->toDer());
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700106 root->addChild(extnList);
107 }
108
109 blob_stream blobStream;
Jeff Thompson68192a32013-10-17 17:34:17 -0700110 der::OutputIterator& start = reinterpret_cast<der::OutputIterator&>(blobStream);
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700111
112 root->encode(start);
113
Jeff Thompsonce115762013-12-18 14:59:56 -0800114 ptr_lib::shared_ptr<vector<uint8_t> > blob = blobStream.buf();
Jeff Thompson68192a32013-10-17 17:34:17 -0700115 setContent(blob);
Jeff Thompson855b1402013-10-22 16:11:46 -0700116 getMetaInfo().setType(ndn_ContentType_KEY);
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700117}
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700118
119void
120Certificate::decode()
121{
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700122 Blob blob = getContent();
123
Jeff Thompson2d47db72013-10-17 15:19:52 -0700124 ndnboost::iostreams::stream<ndnboost::iostreams::array_source> is((const char*)blob.buf(), blob.size());
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700125
Jeff Thompsonce115762013-12-18 14:59:56 -0800126 ptr_lib::shared_ptr<der::DerNode> node = der::DerNode::parse(reinterpret_cast<der::InputIterator&>(is));
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700127
128 // der::PrintVisitor printVisitor;
129 // node->accept(printVisitor, string(""));
130
131 der::CertificateDataVisitor certDataVisitor;
132 node->accept(certDataVisitor, this);
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700133}
134
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700135void
136Certificate::printCertificate()
137{
138 cout << "Validity:" << endl;
Jeff Thompson3f0a5072013-10-18 16:34:20 -0700139 cout << der::DerGtime::toIsoString(notBefore_) << endl;
140 cout << der::DerGtime::toIsoString(notAfter_) << endl;
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700141
142 cout << "Subject Info:" << endl;
Jeff Thompson67598332013-10-17 17:57:22 -0700143 vector<CertificateSubjectDescription>::iterator it = subjectDescriptionList_.begin();
144 for(; it < subjectDescriptionList_.end(); it++){
145 cout << it->getOidString() << "\t" << it->getValue() << endl;
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700146 }
147
Jeff Thompson67598332013-10-17 17:57:22 -0700148 ndnboost::iostreams::stream<ndnboost::iostreams::array_source> is((const char*)key_.getKeyDer().buf(), key_.getKeyDer().size());
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700149
Jeff Thompsonce115762013-12-18 14:59:56 -0800150 ptr_lib::shared_ptr<der::DerNode> keyRoot = der::DerNode::parse(reinterpret_cast<der::InputIterator&> (is));
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700151
152 der::PrintVisitor printVisitor;
153 keyRoot->accept(printVisitor, string(""));
154}
Jeff Thompsona5dc3512013-10-17 10:26:19 -0700155
156}