Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/web/index.html> |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef SYNC_INTRO_CERTIFICATE_H |
| 23 | #define SYNC_INTRO_CERTIFICATE_H |
| 24 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 25 | #include <ndn-cxx/security/identity-certificate.hpp> |
| 26 | #include <ndn-cxx/security/signature-sha256-with-rsa.hpp> |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 27 | |
| 28 | namespace Sync { |
| 29 | |
| 30 | class IntroCertificate : public ndn::Data |
| 31 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 32 | /** |
| 33 | * Naming convention of IntroCertificate: |
| 34 | * /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version |
| 35 | * Content: introducee's identity certificate; |
| 36 | * KeyLocator: introducer's identity certificate; |
| 37 | */ |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 38 | public: |
| 39 | struct Error : public ndn::Data::Error { Error(const std::string &what) : ndn::Data::Error(what) {} }; |
| 40 | |
| 41 | IntroCertificate() |
| 42 | {} |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 43 | |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 44 | /** |
| 45 | * @brief Construct IntroCertificate from IdentityCertificate |
| 46 | * |
| 47 | * @param syncPrefix |
| 48 | * @param introduceeCert |
| 49 | * @param introducerName |
| 50 | */ |
| 51 | IntroCertificate(const ndn::Name& syncPrefix, |
| 52 | const ndn::IdentityCertificate& introduceeCert, |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 53 | const ndn::Name& introducerCertName); //without version number |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * @brief Construct IntroCertificate using a plain data. |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 57 | * |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 58 | * if data is not actually IntroCertificate, Error will be thrown out. |
| 59 | * |
| 60 | * @param data |
| 61 | * @throws ndn::IntroCertificate::Error. |
| 62 | */ |
| 63 | IntroCertificate(const ndn::Data& data); |
| 64 | |
| 65 | virtual |
| 66 | ~IntroCertificate() {}; |
| 67 | |
| 68 | const ndn::IdentityCertificate& |
| 69 | getIntroduceeCert() const |
| 70 | { |
| 71 | return m_introduceeCert; |
| 72 | } |
| 73 | |
| 74 | const ndn::Name& |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 75 | getIntroducerCertName() const |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 76 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 77 | return m_introducerCertName; |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | const ndn::Name& |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 81 | getIntroduceeCertName() const |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 82 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 83 | return m_introduceeCertName; |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private: |
| 87 | ndn::Name m_syncPrefix; |
| 88 | ndn::IdentityCertificate m_introduceeCert; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 89 | ndn::Name m_introducerCertName; |
| 90 | ndn::Name m_introduceeCertName; |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | inline |
| 94 | IntroCertificate::IntroCertificate(const ndn::Name& syncPrefix, |
| 95 | const ndn::IdentityCertificate& introduceeCert, |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 96 | const ndn::Name& introducerCertName) |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 97 | : m_syncPrefix(syncPrefix) |
| 98 | , m_introduceeCert(introduceeCert) |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 99 | , m_introducerCertName(introducerCertName) |
| 100 | , m_introduceeCertName(introduceeCert.getName().getPrefix(-1)) |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 101 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 102 | // Naming convention /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 103 | ndn::Name dataName = m_syncPrefix; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 104 | dataName.append("CHRONOS-INTRO-CERT") |
| 105 | .append(m_introduceeCertName.wireEncode()) |
| 106 | .append(m_introducerCertName.wireEncode()) |
| 107 | .appendVersion(); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 108 | |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 109 | setName(dataName); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 110 | setContent(m_introduceeCert.wireEncode()); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | inline |
| 114 | IntroCertificate::IntroCertificate(const ndn::Data& data) |
| 115 | : Data(data) |
| 116 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 117 | // Naming convention /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 118 | ndn::Name dataName = data.getName(); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | 7fe5983 | 2014-07-02 12:17:46 -0700 | [diff] [blame] | 120 | if(dataName.size() < 4 || dataName.get(-4).toUri() != "CHRONOS-INTRO-CERT") |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 121 | throw Error("Not a Sync::IntroCertificate"); |
| 122 | |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 123 | try |
| 124 | { |
| 125 | m_introduceeCert.wireDecode(data.getContent().blockFromValue()); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 126 | m_introducerCertName.wireDecode(dataName.get(-2).blockFromValue()); |
| 127 | m_introduceeCertName.wireDecode(dataName.get(-3).blockFromValue()); |
| 128 | m_syncPrefix = dataName.getPrefix(-4); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 129 | } |
| 130 | catch(ndn::IdentityCertificate::Error& e) |
| 131 | { |
| 132 | throw Error("Cannot decode introducee cert"); |
| 133 | } |
| 134 | catch(ndn::Name::Error& e) |
| 135 | { |
| 136 | throw Error("Cannot decode name"); |
| 137 | } |
| 138 | catch(ndn::Block::Error& e) |
| 139 | { |
| 140 | throw Error("Cannot decode block name"); |
| 141 | } |
| 142 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 143 | if(m_introduceeCertName != m_introduceeCert.getName().getPrefix(-1)) |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 144 | throw Error("Invalid Sync::IntroCertificate (inconsistent introducee name)"); |
| 145 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 146 | ndn::Name keyLocatorName; |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 147 | try |
| 148 | { |
| 149 | ndn::SignatureSha256WithRsa sig(data.getSignature()); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 150 | keyLocatorName = sig.getKeyLocator().getName(); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 151 | } |
| 152 | catch(ndn::KeyLocator::Error& e) |
| 153 | { |
| 154 | throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#1)"); |
| 155 | } |
| 156 | catch(ndn::SignatureSha256WithRsa::Error& e) |
| 157 | { |
| 158 | throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#2)"); |
| 159 | } |
| 160 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 161 | if(m_introducerCertName != keyLocatorName) |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 162 | throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#3)"); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | } // namespace Sync |
| 167 | |
| 168 | #endif //SYNC_INTRO_CERTIFICATE_H |