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 | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 9 | #include "common.hpp" |
Alexander Afanasyev | 8e96e58 | 2013-11-19 12:07:04 -0800 | [diff] [blame] | 10 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 11 | #include "certificate.hpp" |
Alexander Afanasyev | 8e96e58 | 2013-11-19 12:07:04 -0800 | [diff] [blame] | 12 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 13 | #include "../util/logging.hpp" |
| 14 | #include "../util/time.hpp" |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 15 | |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 16 | #include "cryptopp.hpp" |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 17 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 18 | #include "../encoding/cryptopp/asn_ext.hpp" |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 20 | INIT_LOGGER("ndn.Certificate"); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 21 | |
| 22 | using namespace std; |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 23 | |
| 24 | namespace ndn { |
| 25 | |
| 26 | Certificate::Certificate() |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 27 | : notBefore_(time::system_clock::TimePoint::max()) |
| 28 | , notAfter_(time::system_clock::TimePoint::min()) |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 29 | {} |
| 30 | |
| 31 | Certificate::Certificate(const Data& data) |
| 32 | // Use the copy constructor. It clones the signature object. |
| 33 | : Data(data) |
| 34 | { |
| 35 | // _LOG_DEBUG("Finish local copy: " << getContent().getContent().size()); |
| 36 | |
| 37 | decode(); |
| 38 | } |
| 39 | |
| 40 | Certificate::~Certificate() |
| 41 | { |
| 42 | //TODO: |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | Certificate::isTooEarly() |
| 47 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 48 | if(time::system_clock::now() < notBefore_) |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 49 | return true; |
| 50 | else |
| 51 | return false; |
| 52 | } |
| 53 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 54 | bool |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 55 | Certificate::isTooLate() |
| 56 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 57 | if(time::system_clock::now() > notAfter_) |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 58 | return true; |
| 59 | else |
| 60 | return false; |
| 61 | } |
| 62 | |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 63 | void |
| 64 | Certificate::encode() |
| 65 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 66 | // Name |
| 67 | // <key_name>/ID-CERT/<id#> |
| 68 | // Content |
| 69 | // DER encoded idCert: |
| 70 | // |
| 71 | // idCert ::= SEQUENCE { |
| 72 | // validity Validity, |
| 73 | // subject Name, |
| 74 | // subjectPubKeyInfo SubjectPublicKeyInfo, |
| 75 | // extension Extensions OPTIONAL } |
| 76 | // |
| 77 | // Validity ::= SEQUENCE { |
| 78 | // notBefore Time, |
| 79 | // notAfter Time } |
| 80 | // |
| 81 | // Name ::= CHOICE { |
| 82 | // RDNSequence } |
| 83 | // |
| 84 | // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
| 85 | // |
| 86 | // RelativeDistinguishedName ::= |
| 87 | // SET OF AttributeTypeAndValue |
| 88 | // |
| 89 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 90 | // algorithm AlgorithmIdentifier |
| 91 | // keybits BIT STRING } |
| 92 | // |
| 93 | // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 94 | // |
| 95 | // (see http://www.ietf.org/rfc/rfc3280.txt for more detail) |
| 96 | // |
| 97 | // KeyLocator |
| 98 | // issuer’s certificate name |
| 99 | // Signature |
| 100 | |
| 101 | using namespace CryptoPP; |
| 102 | |
| 103 | OBufferStream os; |
| 104 | CryptoPP::FileSink sink(os); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 105 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 106 | // idCert ::= SEQUENCE { |
| 107 | // validity Validity, |
| 108 | // subject Name, |
| 109 | // subjectPubKeyInfo SubjectPublicKeyInfo, |
| 110 | // extension Extensions OPTIONAL } |
| 111 | DERSequenceEncoder idCert(sink); |
| 112 | { |
| 113 | // Validity ::= SEQUENCE { |
| 114 | // notBefore Time, |
| 115 | // notAfter Time } |
| 116 | DERSequenceEncoder validity(idCert); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 117 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 118 | DEREncodeGeneralTime(validity, notBefore_); |
| 119 | DEREncodeGeneralTime(validity, notAfter_); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 120 | } |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 121 | validity.MessageEnd(); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 122 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 123 | // Name ::= CHOICE { |
| 124 | // RDNSequence } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 125 | // |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 126 | // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
| 127 | DERSequenceEncoder name(idCert); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 128 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 129 | for(SubjectDescriptionList::iterator it = subjectDescriptionList_.begin(); |
| 130 | it != subjectDescriptionList_.end(); ++it) |
| 131 | { |
| 132 | it->encode(name); |
| 133 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 134 | } |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 135 | name.MessageEnd(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 136 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 137 | // SubjectPublicKeyInfo |
| 138 | key_.encode(idCert); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 139 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 140 | // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 141 | // |
| 142 | // Extension ::= SEQUENCE { |
| 143 | // extnID OBJECT IDENTIFIER, |
| 144 | // critical BOOLEAN DEFAULT FALSE, |
| 145 | // extnValue OCTET STRING } |
| 146 | if(!extensionList_.empty()) |
| 147 | { |
| 148 | DERSequenceEncoder extensions(idCert); |
| 149 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 150 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 151 | for(ExtensionList::iterator it = extensionList_.begin(); |
| 152 | it != extensionList_.end(); ++it) |
| 153 | { |
| 154 | it->encode(extensions); |
| 155 | } |
| 156 | } |
| 157 | extensions.MessageEnd(); |
| 158 | } |
| 159 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 160 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 161 | idCert.MessageEnd(); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 162 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 163 | setContent(os.buf()); |
| 164 | setContentType(MetaInfo::TYPE_KEY); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 165 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 166 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 167 | void |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 168 | Certificate::decode() |
| 169 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 170 | using namespace CryptoPP; |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 171 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 172 | OBufferStream os; |
| 173 | CryptoPP::StringSource source(getContent().value(), getContent().value_size(), true); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 174 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 175 | // idCert ::= SEQUENCE { |
| 176 | // validity Validity, |
| 177 | // subject Name, |
| 178 | // subjectPubKeyInfo SubjectPublicKeyInfo, |
| 179 | // extension Extensions OPTIONAL } |
| 180 | BERSequenceDecoder idCert(source); |
| 181 | { |
| 182 | // Validity ::= SEQUENCE { |
| 183 | // notBefore Time, |
| 184 | // notAfter Time } |
| 185 | BERSequenceDecoder validity(idCert); |
| 186 | { |
| 187 | BERDecodeTime(validity, notBefore_); |
| 188 | BERDecodeTime(validity, notAfter_); |
| 189 | } |
| 190 | validity.MessageEnd(); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 191 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 192 | // Name ::= CHOICE { |
| 193 | // RDNSequence } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 194 | // |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 195 | // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
| 196 | subjectDescriptionList_.clear(); |
| 197 | BERSequenceDecoder name(idCert); |
| 198 | { |
| 199 | while(!name.EndReached()) |
| 200 | { |
| 201 | subjectDescriptionList_.push_back(CertificateSubjectDescription(name)); |
| 202 | } |
| 203 | } |
| 204 | name.MessageEnd(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 205 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 206 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 207 | // algorithm AlgorithmIdentifier |
| 208 | // keybits BIT STRING } |
| 209 | key_.decode(idCert); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 210 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 211 | // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 212 | // |
| 213 | // Extension ::= SEQUENCE { |
| 214 | // extnID OBJECT IDENTIFIER, |
| 215 | // critical BOOLEAN DEFAULT FALSE, |
| 216 | // extnValue OCTET STRING } |
| 217 | extensionList_.clear(); |
| 218 | if(!idCert.EndReached()) |
| 219 | { |
| 220 | BERSequenceDecoder extensions(idCert); |
| 221 | { |
| 222 | while(!extensions.EndReached()) |
| 223 | { |
| 224 | extensionList_.push_back(CertificateExtension(extensions)); |
| 225 | } |
| 226 | } |
| 227 | extensions.MessageEnd(); |
| 228 | } |
| 229 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 230 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 231 | idCert.MessageEnd(); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 234 | void |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 235 | Certificate::printCertificate(std::ostream &os) const |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 236 | { |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 237 | os << "Certificate name:" << endl; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 238 | os << " " << getName() << endl; |
| 239 | os << "Validity:" << endl; |
| 240 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 241 | os << " NotBefore: " << time::toIsoString(notBefore_) << endl; |
| 242 | os << " NotAfter: " << time::toIsoString(notAfter_) << endl; |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 245 | os << "Subject Description:" << endl; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 246 | for(SubjectDescriptionList::const_iterator it = subjectDescriptionList_.begin(); |
| 247 | it != subjectDescriptionList_.end(); ++it) |
| 248 | { |
| 249 | os << " " << it->getOidString() << ": " << it->getValue() << endl; |
| 250 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 251 | |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 252 | os << "Public key bits:" << endl; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 253 | CryptoPP::Base64Encoder encoder(new CryptoPP::FileSink(os), true, 64); |
| 254 | key_.encode(encoder); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 255 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 256 | // 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] | 257 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 258 | // ptr_lib::shared_ptr<der::DerNode> keyRoot = der::DerNode::parse(reinterpret_cast<der::InputIterator&> (is)); |
| 259 | |
| 260 | // der::PrintVisitor printVisitor; |
| 261 | // keyRoot->accept(printVisitor, string("")); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 262 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 263 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 264 | } // namespace ndn |