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