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