Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 2 | /* |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | * |
| 21 | * @author Zhiyi Zhang <dreamerbarrychang@gmail.com> |
| 22 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 23 | */ |
| 24 | |
| 25 | #include "certificate.hpp" |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 26 | #include "additional-description.hpp" |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 27 | #include "../../encoding/block-helpers.hpp" |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 28 | #include "../../util/indented-stream.hpp" |
| 29 | #include "../transform.hpp" |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | namespace security { |
| 33 | namespace v2 { |
| 34 | |
| 35 | BOOST_CONCEPT_ASSERT((WireEncodable<Certificate>)); |
| 36 | BOOST_CONCEPT_ASSERT((WireDecodable<Certificate>)); |
| 37 | |
| 38 | // /<NameSpace>/KEY/[KeyId]/[IssuerId]/[Version] |
| 39 | |
| 40 | const ssize_t Certificate::VERSION_OFFSET = -1; |
| 41 | const ssize_t Certificate::ISSUER_ID_OFFSET = -2; |
| 42 | const ssize_t Certificate::KEY_ID_OFFSET = -3; |
| 43 | const ssize_t Certificate::KEY_COMPONENT_OFFSET = -4; |
| 44 | const size_t Certificate::MIN_CERT_NAME_LENGTH = 4; |
| 45 | const size_t Certificate::MIN_KEY_NAME_LENGTH = 2; |
| 46 | const name::Component Certificate::KEY_COMPONENT("KEY"); |
| 47 | |
| 48 | Certificate::Certificate() |
| 49 | { |
| 50 | setContentType(tlv::ContentTypeValue::ContentType_Key); |
| 51 | } |
| 52 | |
| 53 | Certificate::Certificate(Data&& data) |
| 54 | : Data(data) |
| 55 | { |
| 56 | if (!isValidName(getName())) { |
| 57 | BOOST_THROW_EXCEPTION(Data::Error("Name does not follow the naming convention for certificate")); |
| 58 | } |
| 59 | if (getContentType() != tlv::ContentTypeValue::ContentType_Key) { |
| 60 | BOOST_THROW_EXCEPTION(Data::Error("ContentType is not KEY")); |
| 61 | } |
| 62 | if (getFreshnessPeriod() < time::seconds::zero()) { |
| 63 | BOOST_THROW_EXCEPTION(Data::Error("FreshnessPeriod is not set")); |
| 64 | } |
| 65 | if (getContent().value_size() == 0) { |
| 66 | BOOST_THROW_EXCEPTION(Data::Error("Content is empty")); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | Certificate::Certificate(const Data& data) |
| 71 | : Certificate(Data(data)) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | Certificate::Certificate(const Block& block) |
| 76 | : Certificate(Data(block)) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | Name |
| 81 | Certificate::getKeyName() const |
| 82 | { |
| 83 | return getName().getPrefix(KEY_ID_OFFSET + 1); |
| 84 | } |
| 85 | |
| 86 | Name |
| 87 | Certificate::getIdentity() const |
| 88 | { |
| 89 | return getName().getPrefix(KEY_COMPONENT_OFFSET); |
| 90 | } |
| 91 | |
| 92 | name::Component |
| 93 | Certificate::getKeyId() const |
| 94 | { |
| 95 | return getName().at(KEY_ID_OFFSET); |
| 96 | } |
| 97 | |
| 98 | name::Component |
| 99 | Certificate::getIssuerId() const |
| 100 | { |
| 101 | return getName().at(ISSUER_ID_OFFSET); |
| 102 | } |
| 103 | |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 104 | Buffer |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 105 | Certificate::getPublicKey() const |
| 106 | { |
| 107 | if (getContent().value_size() == 0) |
| 108 | BOOST_THROW_EXCEPTION(Data::Error("Content is empty")); |
| 109 | return Buffer(getContent().value(), getContent().value_size()); |
| 110 | } |
| 111 | |
| 112 | ValidityPeriod |
| 113 | Certificate::getValidityPeriod() const |
| 114 | { |
| 115 | return getSignature().getSignatureInfo().getValidityPeriod(); |
| 116 | } |
| 117 | |
| 118 | bool |
| 119 | Certificate::isValid(const time::system_clock::TimePoint& ts) const |
| 120 | { |
| 121 | return getSignature().getSignatureInfo().getValidityPeriod().isValid(ts); |
| 122 | } |
| 123 | |
| 124 | const Block& |
| 125 | Certificate::getExtension(uint32_t type) const |
| 126 | { |
| 127 | return getSignature().getSignatureInfo().getTypeSpecificTlv(type); |
| 128 | } |
| 129 | |
| 130 | bool |
| 131 | Certificate::isValidName(const Name& certName) |
| 132 | { |
| 133 | // /<NameSpace>/KEY/[KeyId]/[IssuerId]/[Version] |
| 134 | return (certName.size() >= Certificate::MIN_CERT_NAME_LENGTH && |
| 135 | certName.get(Certificate::KEY_COMPONENT_OFFSET) == Certificate::KEY_COMPONENT); |
| 136 | } |
| 137 | |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 138 | std::ostream& |
| 139 | operator<<(std::ostream& os, const Certificate& cert) |
| 140 | { |
| 141 | os << "Certificate name:\n"; |
| 142 | os << " " << cert.getName() << "\n"; |
| 143 | os << "Validity:\n"; |
| 144 | { |
| 145 | os << " NotBefore: " << time::toIsoString(cert.getValidityPeriod().getPeriod().first) << "\n"; |
| 146 | os << " NotAfter: " << time::toIsoString(cert.getValidityPeriod().getPeriod().second) << "\n"; |
| 147 | } |
| 148 | |
| 149 | try { |
| 150 | const Block& info = cert.getSignature().getSignatureInfo().getTypeSpecificTlv(tlv::AdditionalDescription); |
| 151 | os << "Additional Description:\n"; |
| 152 | for (const auto& item : v2::AdditionalDescription(info)) { |
| 153 | os << " " << item.first << ": " << item.second << "\n"; |
| 154 | } |
| 155 | } |
| 156 | catch (const SignatureInfo::Error&) { |
| 157 | // ignore |
| 158 | } |
| 159 | |
| 160 | os << "Public key bits:\n"; |
| 161 | { |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 162 | using namespace transform; |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 163 | util::IndentedStream os2(os, " "); |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 164 | bufferSource(cert.getPublicKey().data(), cert.getPublicKey().size()) >> base64Encode() >> streamSink(os2); |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | os << "Signature Information:\n"; |
| 168 | { |
| 169 | os << " Signature Type: " << static_cast<tlv::SignatureTypeValue>(cert.getSignature().getType()) << "\n"; |
| 170 | |
| 171 | if (cert.getSignature().hasKeyLocator()) { |
| 172 | os << " Key Locator: "; |
| 173 | const KeyLocator& keyLocator = cert.getSignature().getKeyLocator(); |
| 174 | if (keyLocator.getType() == KeyLocator::KeyLocator_Name && keyLocator.getName() == cert.getKeyName()) { |
| 175 | os << "Self-Signed "; |
| 176 | } |
| 177 | os << keyLocator << "\n"; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return os; |
| 182 | } |
| 183 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 184 | Name |
| 185 | extractIdentityFromCertName(const Name& certName) |
| 186 | { |
| 187 | if (!Certificate::isValidName(certName)) { |
| 188 | BOOST_THROW_EXCEPTION(std::invalid_argument("Certificate name `" + certName.toUri() + "` " |
| 189 | "does not follow the naming conventions")); |
| 190 | } |
| 191 | |
| 192 | return certName.getPrefix(Certificate::KEY_COMPONENT_OFFSET); // trim everything after and including "KEY" |
| 193 | } |
| 194 | |
| 195 | Name |
| 196 | extractKeyNameFromCertName(const Name& certName) |
| 197 | { |
| 198 | if (!Certificate::isValidName(certName)) { |
| 199 | BOOST_THROW_EXCEPTION(std::invalid_argument("Certificate name `" + certName.toUri() + "` " |
| 200 | "does not follow the naming conventions")); |
| 201 | } |
| 202 | |
| 203 | return certName.getPrefix(Certificate::KEY_ID_OFFSET + 1); // trim everything after key id |
| 204 | } |
| 205 | |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 206 | } // namespace v2 |
| 207 | } // namespace security |
| 208 | } // namespace ndn |