Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 22 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
| 23 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 26 | #include "common.hpp" |
Alexander Afanasyev | 8e96e58 | 2013-11-19 12:07:04 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 28 | #include "certificate.hpp" |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 29 | #include "../util/time.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 30 | #include "cryptopp.hpp" |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 31 | #include "../encoding/cryptopp/asn_ext.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 32 | #include "../encoding/buffer-stream.hpp" |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 33 | |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 34 | namespace ndn { |
| 35 | |
| 36 | Certificate::Certificate() |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 37 | : m_notBefore(time::system_clock::TimePoint::max()) |
| 38 | , m_notAfter(time::system_clock::TimePoint::min()) |
| 39 | { |
| 40 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 41 | |
| 42 | Certificate::Certificate(const Data& data) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 43 | // Use the copy constructor. It clones the signature object. |
| 44 | : Data(data) |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 45 | { |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 46 | decode(); |
| 47 | } |
| 48 | |
| 49 | Certificate::~Certificate() |
| 50 | { |
| 51 | //TODO: |
| 52 | } |
| 53 | |
| 54 | bool |
| 55 | Certificate::isTooEarly() |
| 56 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 57 | if (time::system_clock::now() < m_notBefore) |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 58 | return true; |
| 59 | else |
| 60 | return false; |
| 61 | } |
| 62 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 63 | bool |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 64 | Certificate::isTooLate() |
| 65 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 66 | if (time::system_clock::now() > m_notAfter) |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 67 | return true; |
| 68 | else |
| 69 | return false; |
| 70 | } |
| 71 | |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 72 | void |
| 73 | Certificate::encode() |
| 74 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 75 | // Name |
| 76 | // <key_name>/ID-CERT/<id#> |
| 77 | // Content |
| 78 | // DER encoded idCert: |
| 79 | // |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame^] | 80 | // idCert ::= SEQUENCE { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 81 | // validity Validity, |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame^] | 82 | // subject Name, |
| 83 | // subjectPubKeyInfo SubjectPublicKeyInfo, |
| 84 | // extension Extensions OPTIONAL } |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 85 | // |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame^] | 86 | // Validity ::= SEQUENCE { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 87 | // notBefore Time, |
| 88 | // notAfter Time } |
| 89 | // |
| 90 | // Name ::= CHOICE { |
| 91 | // RDNSequence } |
| 92 | // |
| 93 | // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
| 94 | // |
| 95 | // RelativeDistinguishedName ::= |
| 96 | // SET OF AttributeTypeAndValue |
| 97 | // |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame^] | 98 | // SubjectPublicKeyInfo ::= SEQUENCE { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 99 | // algorithm AlgorithmIdentifier |
| 100 | // keybits BIT STRING } |
| 101 | // |
| 102 | // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 103 | // |
| 104 | // (see http://www.ietf.org/rfc/rfc3280.txt for more detail) |
| 105 | // |
| 106 | // KeyLocator |
| 107 | // issuer’s certificate name |
| 108 | // Signature |
| 109 | |
| 110 | using namespace CryptoPP; |
| 111 | |
| 112 | OBufferStream os; |
| 113 | CryptoPP::FileSink sink(os); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 114 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 115 | // idCert ::= SEQUENCE { |
| 116 | // validity Validity, |
| 117 | // subject Name, |
| 118 | // subjectPubKeyInfo SubjectPublicKeyInfo, |
| 119 | // extension Extensions OPTIONAL } |
| 120 | DERSequenceEncoder idCert(sink); |
| 121 | { |
| 122 | // Validity ::= SEQUENCE { |
| 123 | // notBefore Time, |
| 124 | // notAfter Time } |
| 125 | DERSequenceEncoder validity(idCert); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 126 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 127 | DEREncodeGeneralTime(validity, m_notBefore); |
| 128 | DEREncodeGeneralTime(validity, m_notAfter); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 129 | } |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 130 | validity.MessageEnd(); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 131 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 132 | // Name ::= CHOICE { |
| 133 | // RDNSequence } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 134 | // |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 135 | // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
| 136 | DERSequenceEncoder name(idCert); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 137 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 138 | for (SubjectDescriptionList::iterator it = m_subjectDescriptionList.begin(); |
| 139 | it != m_subjectDescriptionList.end(); ++it) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 140 | { |
| 141 | it->encode(name); |
| 142 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 143 | } |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 144 | name.MessageEnd(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 145 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 146 | // SubjectPublicKeyInfo |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 147 | m_key.encode(idCert); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 148 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 149 | // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 150 | // |
| 151 | // Extension ::= SEQUENCE { |
| 152 | // extnID OBJECT IDENTIFIER, |
| 153 | // critical BOOLEAN DEFAULT FALSE, |
| 154 | // extnValue OCTET STRING } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 155 | if (!m_extensionList.empty()) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 156 | { |
| 157 | DERSequenceEncoder extensions(idCert); |
| 158 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 159 | for (ExtensionList::iterator it = m_extensionList.begin(); |
| 160 | it != m_extensionList.end(); ++it) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 161 | { |
| 162 | it->encode(extensions); |
| 163 | } |
| 164 | } |
| 165 | extensions.MessageEnd(); |
| 166 | } |
| 167 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 168 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 169 | idCert.MessageEnd(); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 170 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 171 | setContent(os.buf()); |
| 172 | setContentType(MetaInfo::TYPE_KEY); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 173 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 174 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 175 | void |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 176 | Certificate::decode() |
| 177 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 178 | using namespace CryptoPP; |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 179 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 180 | OBufferStream os; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 181 | StringSource source(getContent().value(), getContent().value_size(), true); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 183 | // idCert ::= SEQUENCE { |
| 184 | // validity Validity, |
| 185 | // subject Name, |
| 186 | // subjectPubKeyInfo SubjectPublicKeyInfo, |
| 187 | // extension Extensions OPTIONAL } |
| 188 | BERSequenceDecoder idCert(source); |
| 189 | { |
| 190 | // Validity ::= SEQUENCE { |
| 191 | // notBefore Time, |
| 192 | // notAfter Time } |
| 193 | BERSequenceDecoder validity(idCert); |
| 194 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 195 | BERDecodeTime(validity, m_notBefore); |
| 196 | BERDecodeTime(validity, m_notAfter); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 197 | } |
| 198 | validity.MessageEnd(); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 199 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 200 | // Name ::= CHOICE { |
| 201 | // RDNSequence } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 202 | // |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 203 | // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 204 | m_subjectDescriptionList.clear(); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 205 | BERSequenceDecoder name(idCert); |
| 206 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 207 | while (!name.EndReached()) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 208 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 209 | m_subjectDescriptionList.push_back(CertificateSubjectDescription(name)); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | name.MessageEnd(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 213 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 214 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 215 | // algorithm AlgorithmIdentifier |
| 216 | // keybits BIT STRING } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 217 | m_key.decode(idCert); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 218 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 219 | // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 220 | // |
| 221 | // Extension ::= SEQUENCE { |
| 222 | // extnID OBJECT IDENTIFIER, |
| 223 | // critical BOOLEAN DEFAULT FALSE, |
| 224 | // extnValue OCTET STRING } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 225 | m_extensionList.clear(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 226 | if (!idCert.EndReached()) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 227 | { |
| 228 | BERSequenceDecoder extensions(idCert); |
| 229 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 230 | while (!extensions.EndReached()) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 231 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 232 | m_extensionList.push_back(CertificateExtension(extensions)); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | extensions.MessageEnd(); |
| 236 | } |
| 237 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 238 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 239 | idCert.MessageEnd(); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 242 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 243 | Certificate::printCertificate(std::ostream& os) const |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 244 | { |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame^] | 245 | os << "Certificate name:" << std::endl; |
| 246 | os << " " << getName() << std::endl; |
| 247 | os << "Validity:" << std::endl; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 248 | { |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame^] | 249 | os << " NotBefore: " << time::toIsoString(m_notBefore) << std::endl; |
| 250 | os << " NotAfter: " << time::toIsoString(m_notAfter) << std::endl; |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame^] | 253 | os << "Subject Description:" << std::endl; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 254 | for (SubjectDescriptionList::const_iterator it = m_subjectDescriptionList.begin(); |
| 255 | it != m_subjectDescriptionList.end(); ++it) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 256 | { |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame^] | 257 | os << " " << it->getOidString() << ": " << it->getValue() << std::endl; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 258 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 259 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame^] | 260 | os << "Public key bits:" << std::endl; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 261 | CryptoPP::Base64Encoder encoder(new CryptoPP::FileSink(os), true, 64); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 262 | m_key.encode(encoder); |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 263 | } |
Jeff Thompson | a5dc351 | 2013-10-17 10:26:19 -0700 | [diff] [blame] | 264 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 265 | } // namespace ndn |