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 | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 2 | /** |
Junxiao Shi | 8ca4325 | 2015-06-11 21:29:43 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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. |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 22 | #include "common.hpp" |
| 23 | |
| 24 | #include "identity-certificate.hpp" |
Yingdi Yu | 80979ba | 2014-11-25 14:38:36 -0800 | [diff] [blame] | 25 | #include "../util/concepts.hpp" |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 26 | |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 27 | namespace ndn { |
| 28 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 29 | using std::string; |
| 30 | |
Yingdi Yu | 80979ba | 2014-11-25 14:38:36 -0800 | [diff] [blame] | 31 | BOOST_CONCEPT_ASSERT((WireEncodable<IdentityCertificate>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireDecodable<IdentityCertificate>)); |
| 33 | static_assert(std::is_base_of<Certificate::Error, IdentityCertificate::Error>::value, |
| 34 | "IdentityCertificate::Error must inherit from Certificate::Error"); |
| 35 | |
| 36 | IdentityCertificate::IdentityCertificate() |
| 37 | { |
Junxiao Shi | 8ca4325 | 2015-06-11 21:29:43 -0700 | [diff] [blame] | 38 | this->setFreshnessPeriod(time::hours(1)); |
Yingdi Yu | 80979ba | 2014-11-25 14:38:36 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | IdentityCertificate::IdentityCertificate(const Data& data) |
| 42 | : Certificate(data) |
| 43 | { |
| 44 | setPublicKeyName(); |
| 45 | } |
| 46 | |
| 47 | IdentityCertificate::IdentityCertificate(const Block& block) |
| 48 | : Certificate(block) |
| 49 | { |
| 50 | setPublicKeyName(); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | IdentityCertificate::wireDecode(const Block& wire) |
| 55 | { |
| 56 | Certificate::wireDecode(wire); |
| 57 | setPublicKeyName(); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | IdentityCertificate::setName(const Name& name) |
| 62 | { |
| 63 | Certificate::setName(name); |
| 64 | setPublicKeyName(); |
| 65 | } |
| 66 | |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 67 | bool |
| 68 | IdentityCertificate::isCorrectName(const Name& name) |
| 69 | { |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 70 | string idString("ID-CERT"); |
Davide Pesavento | 8f5cbdc | 2015-09-13 00:59:28 +0200 | [diff] [blame] | 71 | ssize_t i = name.size() - 1; |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 72 | for (; i >= 0; i--) { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 73 | if (name.get(i).toUri() == idString) |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 74 | break; |
| 75 | } |
| 76 | |
| 77 | if (i < 0) |
| 78 | return false; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 79 | |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 80 | string keyString("KEY"); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 81 | size_t keyIndex = 0; |
| 82 | for (; keyIndex < name.size(); keyIndex++) { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 83 | if (name.get(keyIndex).toUri() == keyString) |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 87 | if (keyIndex >= name.size()) |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 88 | return false; |
| 89 | |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 90 | return true; |
| 91 | } |
| 92 | |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 93 | void |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 94 | IdentityCertificate::setPublicKeyName() |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 95 | { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 96 | if (!isCorrectName(getName())) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 97 | BOOST_THROW_EXCEPTION(Error("Wrong Identity Certificate Name")); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 98 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 99 | m_publicKeyName = certificateNameToPublicKeyName(getName()); |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 100 | } |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 101 | |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 102 | bool |
| 103 | IdentityCertificate::isIdentityCertificate(const Certificate& certificate) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 104 | { |
| 105 | return dynamic_cast<const IdentityCertificate*>(&certificate); |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | Name |
| 109 | IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName) |
| 110 | { |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 111 | string idString("ID-CERT"); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 112 | bool foundIdString = false; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 113 | size_t idCertComponentIndex = certificateName.size() - 1; |
| 114 | for (; idCertComponentIndex + 1 > 0; --idCertComponentIndex) { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 115 | if (certificateName.get(idCertComponentIndex).toUri() == idString) |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 116 | { |
| 117 | foundIdString = true; |
| 118 | break; |
| 119 | } |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 120 | } |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 122 | if (!foundIdString) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 123 | BOOST_THROW_EXCEPTION(Error("Incorrect identity certificate name " + certificateName.toUri())); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 125 | Name tmpName = certificateName.getSubName(0, idCertComponentIndex); |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 126 | string keyString("KEY"); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 127 | bool foundKeyString = false; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 128 | size_t keyComponentIndex = 0; |
| 129 | for (; keyComponentIndex < tmpName.size(); keyComponentIndex++) { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 130 | if (tmpName.get(keyComponentIndex).toUri() == keyString) |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 131 | { |
| 132 | foundKeyString = true; |
| 133 | break; |
| 134 | } |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 135 | } |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 136 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 137 | if (!foundKeyString) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 138 | BOOST_THROW_EXCEPTION(Error("Incorrect identity certificate name " + certificateName.toUri())); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 139 | |
| 140 | return tmpName |
| 141 | .getSubName(0, keyComponentIndex) |
| 142 | .append(tmpName.getSubName(keyComponentIndex + 1, |
| 143 | tmpName.size() - keyComponentIndex - 1)); |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 146 | } // namespace ndn |