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