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