blob: 9a01424467eda8f2e9c851a07c4140ee6db211e4 [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>
10#include <ndn-cpp/sha256-with-rsa-signature.hpp>
11#if 0
12#include "../../encoding/der/visitor/certificate-data-visitor.hpp"
13#include "../../encoding/der/visitor/print-visitor.hpp"
14#endif
15#include "../../util/logging.hpp"
16#include "../../c/util/time.h"
17#include <ndn-cpp/security/certificate/certificate.hpp>
18
19INIT_LOGGER("ndn.security.Certificate");
20
21using namespace std;
22using namespace ndn::ptr_lib;
23
24namespace ndn {
25
26Certificate::Certificate()
27 : notBefore_(DBL_MAX)
28 , notAfter_(-DBL_MAX)
29{}
30
31Certificate::Certificate(const Data& data)
32// Use the copy constructor. It clones the signature object.
33: Data(data)
34{
35 // _LOG_DEBUG("Finish local copy: " << getContent().getContent().size());
36
37 decode();
38}
39
40Certificate::~Certificate()
41{
42 //TODO:
43}
44
45bool
46Certificate::isTooEarly()
47{
48 Time now = ndn_getNowMilliseconds();
49 if(now < notBefore_)
50 return true;
51 else
52 return false;
53}
54
55bool
56Certificate::isTooLate()
57{
58 Time now = ndn_getNowMilliseconds();
59 if(now > notAfter_)
60 return true;
61 else
62 return false;
63}
64
65#if 0
66void
67Certificate::encode()
68{
69 Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
70
71 Ptr<der::DerSequence> validity = Ptr<der::DerSequence>::Create();
72 Ptr<der::DerGtime> notBefore = Ptr<der::DerGtime>(new der::DerGtime(notBefore_));
73 Ptr<der::DerGtime> notAfter = Ptr<der::DerGtime>(new der::DerGtime(notAfter_));
74 validity->addChild(notBefore);
75 validity->addChild(notAfter);
76 root->addChild(validity);
77
78 Ptr<der::DerSequence> subjectList = Ptr<der::DerSequence>::Create();
79 SubDescryptList::iterator it = m_subjectList.begin();
80 for(; it != m_subjectList.end(); it++)
81 {
82 Ptr<der::DerNode> child = it->toDER();
83 subjectList->addChild(child);
84 }
85 root->addChild(subjectList);
86
87 root->addChild(key_.toDER());
88
89 if(!m_extnList.empty())
90 {
91 Ptr<der::DerSequence> extnList = Ptr<der::DerSequence>::Create();
92 ExtensionList::iterator it = m_extnList.begin();
93 for(; it != m_extnList.end(); it++)
94 extnList->addChild(it->toDER());
95 root->addChild(extnList);
96 }
97
98 blob_stream blobStream;
99 OutputIterator& start = reinterpret_cast<OutputIterator&>(blobStream);
100
101 root->encode(start);
102
103 Ptr<Blob> blob = blobStream.buf();
104 Content content(blob->buf(), blob->size());
105 setContent(content);
106}
107#endif
108
109void
110Certificate::decode()
111{
112#if 0
113 Blob blob = getContent();
114
115 boost::iostreams::stream
116 <boost::iostreams::array_source> is(blob.buf(), blob.size());
117
118 shared_ptr<der::DerNode> node = der::DerNode::parse(reinterpret_cast<InputIterator&>(is));
119
120 // der::PrintVisitor printVisitor;
121 // node->accept(printVisitor, string(""));
122
123 der::CertificateDataVisitor certDataVisitor;
124 node->accept(certDataVisitor, this);
125#endif
126}
127
128#if 0
129void
130Certificate::printCertificate()
131{
132 cout << "Validity:" << endl;
133 cout << notBefore_ << endl;
134 cout << notAfter_ << endl;
135
136 cout << "Subject Info:" << endl;
137 vector<CertificateSubDescrypt>::iterator it = m_subjectList.begin();
138 for(; it < m_subjectList.end(); it++){
139 cout << it->getOidStr() << "\t" << it->getValue() << endl;
140 }
141
142 boost::iostreams::stream
143 <boost::iostreams::array_source> is(key_.getKeyBlob().buf (), m_key.getKeyBlob().size ());
144
145 Ptr<der::DerNode> keyRoot = der::DerNode::parse(reinterpret_cast<InputIterator&> (is));
146
147 der::PrintVisitor printVisitor;
148 keyRoot->accept(printVisitor, string(""));
149}
150#endif
151
152}