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; |
| 25 | using namespace ndn::ptr_lib; |
| 26 | |
| 27 | namespace ndn { |
| 28 | |
| 29 | Certificate::Certificate() |
| 30 | : notBefore_(DBL_MAX) |
| 31 | , notAfter_(-DBL_MAX) |
| 32 | {} |
| 33 | |
| 34 | Certificate::Certificate(const Data& data) |
| 35 | // Use the copy constructor. It clones the signature object. |
| 36 | : Data(data) |
| 37 | { |
| 38 | // _LOG_DEBUG("Finish local copy: " << getContent().getContent().size()); |
| 39 | |
| 40 | decode(); |
| 41 | } |
| 42 | |
| 43 | Certificate::~Certificate() |
| 44 | { |
| 45 | //TODO: |
| 46 | } |
| 47 | |
| 48 | bool |
| 49 | Certificate::isTooEarly() |
| 50 | { |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 51 | MillisecondsSince1970 now = ndn_getNowMilliseconds(); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 52 | if(now < notBefore_) |
| 53 | return true; |
| 54 | else |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | bool |
| 59 | Certificate::isTooLate() |
| 60 | { |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 61 | MillisecondsSince1970 now = ndn_getNowMilliseconds(); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 62 | if(now > notAfter_) |
| 63 | return true; |
| 64 | else |
| 65 | return false; |
| 66 | } |
| 67 | |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 68 | void |
| 69 | Certificate::encode() |
| 70 | { |
Jeff Thompson | 415da1e | 2013-10-17 16:52:59 -0700 | [diff] [blame] | 71 | shared_ptr<der::DerSequence> root(new der::DerSequence()); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 72 | |
Jeff Thompson | 415da1e | 2013-10-17 16:52:59 -0700 | [diff] [blame] | 73 | shared_ptr<der::DerSequence> validity(new der::DerSequence()); |
| 74 | shared_ptr<der::DerGtime> notBefore(new der::DerGtime(notBefore_)); |
| 75 | shared_ptr<der::DerGtime> notAfter(new der::DerGtime(notAfter_)); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 76 | validity->addChild(notBefore); |
| 77 | validity->addChild(notAfter); |
| 78 | root->addChild(validity); |
| 79 | |
Jeff Thompson | 415da1e | 2013-10-17 16:52:59 -0700 | [diff] [blame] | 80 | shared_ptr<der::DerSequence> subjectList(new der::DerSequence()); |
| 81 | SubjectDescriptionList::iterator it = subjectDescriptionList_.begin(); |
| 82 | for(; it != subjectDescriptionList_.end(); it++) |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 83 | { |
Jeff Thompson | 415da1e | 2013-10-17 16:52:59 -0700 | [diff] [blame] | 84 | shared_ptr<der::DerNode> child = it->toDer(); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 85 | subjectList->addChild(child); |
| 86 | } |
| 87 | root->addChild(subjectList); |
| 88 | |
Jeff Thompson | 415da1e | 2013-10-17 16:52:59 -0700 | [diff] [blame] | 89 | root->addChild(key_.toDer()); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 90 | |
Jeff Thompson | 415da1e | 2013-10-17 16:52:59 -0700 | [diff] [blame] | 91 | if(!extensionList_.empty()) |
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 | shared_ptr<der::DerSequence> extnList(new der::DerSequence()); |
| 94 | ExtensionList::iterator it = extensionList_.begin(); |
| 95 | for(; it != extensionList_.end(); it++) |
| 96 | extnList->addChild(it->toDer()); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 97 | root->addChild(extnList); |
| 98 | } |
| 99 | |
| 100 | blob_stream blobStream; |
Jeff Thompson | 68192a3 | 2013-10-17 17:34:17 -0700 | [diff] [blame] | 101 | der::OutputIterator& start = reinterpret_cast<der::OutputIterator&>(blobStream); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 102 | |
| 103 | root->encode(start); |
| 104 | |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 105 | shared_ptr<vector<uint8_t> > blob = blobStream.buf(); |
Jeff Thompson | 68192a3 | 2013-10-17 17:34:17 -0700 | [diff] [blame] | 106 | setContent(blob); |
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 | 415da1e | 2013-10-17 16:52:59 -0700 | [diff] [blame] | 116 | 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 | 6759833 | 2013-10-17 17:57:22 -0700 | [diff] [blame] | 140 | 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 | } |