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