Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #ifndef SYNC_INTRO_CERTIFICATE_H |
| 12 | #define SYNC_INTRO_CERTIFICATE_H |
| 13 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 14 | #include <ndn-cxx/security/identity-certificate.hpp> |
| 15 | #include <ndn-cxx/security/signature-sha256-with-rsa.hpp> |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 16 | |
| 17 | namespace Sync { |
| 18 | |
| 19 | class IntroCertificate : public ndn::Data |
| 20 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 21 | /** |
| 22 | * Naming convention of IntroCertificate: |
| 23 | * /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version |
| 24 | * Content: introducee's identity certificate; |
| 25 | * KeyLocator: introducer's identity certificate; |
| 26 | */ |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 27 | public: |
| 28 | struct Error : public ndn::Data::Error { Error(const std::string &what) : ndn::Data::Error(what) {} }; |
| 29 | |
| 30 | IntroCertificate() |
| 31 | {} |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 32 | |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 33 | /** |
| 34 | * @brief Construct IntroCertificate from IdentityCertificate |
| 35 | * |
| 36 | * @param syncPrefix |
| 37 | * @param introduceeCert |
| 38 | * @param introducerName |
| 39 | */ |
| 40 | IntroCertificate(const ndn::Name& syncPrefix, |
| 41 | const ndn::IdentityCertificate& introduceeCert, |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 42 | const ndn::Name& introducerCertName); //without version number |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * @brief Construct IntroCertificate using a plain data. |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 46 | * |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 47 | * if data is not actually IntroCertificate, Error will be thrown out. |
| 48 | * |
| 49 | * @param data |
| 50 | * @throws ndn::IntroCertificate::Error. |
| 51 | */ |
| 52 | IntroCertificate(const ndn::Data& data); |
| 53 | |
| 54 | virtual |
| 55 | ~IntroCertificate() {}; |
| 56 | |
| 57 | const ndn::IdentityCertificate& |
| 58 | getIntroduceeCert() const |
| 59 | { |
| 60 | return m_introduceeCert; |
| 61 | } |
| 62 | |
| 63 | const ndn::Name& |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 64 | getIntroducerCertName() const |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 65 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 66 | return m_introducerCertName; |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | const ndn::Name& |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 70 | getIntroduceeCertName() const |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 71 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 72 | return m_introduceeCertName; |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private: |
| 76 | ndn::Name m_syncPrefix; |
| 77 | ndn::IdentityCertificate m_introduceeCert; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 78 | ndn::Name m_introducerCertName; |
| 79 | ndn::Name m_introduceeCertName; |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | inline |
| 83 | IntroCertificate::IntroCertificate(const ndn::Name& syncPrefix, |
| 84 | const ndn::IdentityCertificate& introduceeCert, |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 85 | const ndn::Name& introducerCertName) |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 86 | : m_syncPrefix(syncPrefix) |
| 87 | , m_introduceeCert(introduceeCert) |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 88 | , m_introducerCertName(introducerCertName) |
| 89 | , m_introduceeCertName(introduceeCert.getName().getPrefix(-1)) |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 90 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 91 | // Naming convention /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 92 | ndn::Name dataName = m_syncPrefix; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 93 | dataName.append("CHRONOS-INTRO-CERT") |
| 94 | .append(m_introduceeCertName.wireEncode()) |
| 95 | .append(m_introducerCertName.wireEncode()) |
| 96 | .appendVersion(); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 97 | |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 98 | setName(dataName); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 99 | setContent(m_introduceeCert.wireEncode()); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | inline |
| 103 | IntroCertificate::IntroCertificate(const ndn::Data& data) |
| 104 | : Data(data) |
| 105 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 106 | // Naming convention /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 107 | ndn::Name dataName = data.getName(); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | 7fe5983 | 2014-07-02 12:17:46 -0700 | [diff] [blame^] | 109 | if(dataName.size() < 4 || dataName.get(-4).toUri() != "CHRONOS-INTRO-CERT") |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 110 | throw Error("Not a Sync::IntroCertificate"); |
| 111 | |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 112 | try |
| 113 | { |
| 114 | m_introduceeCert.wireDecode(data.getContent().blockFromValue()); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 115 | m_introducerCertName.wireDecode(dataName.get(-2).blockFromValue()); |
| 116 | m_introduceeCertName.wireDecode(dataName.get(-3).blockFromValue()); |
| 117 | m_syncPrefix = dataName.getPrefix(-4); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 118 | } |
| 119 | catch(ndn::IdentityCertificate::Error& e) |
| 120 | { |
| 121 | throw Error("Cannot decode introducee cert"); |
| 122 | } |
| 123 | catch(ndn::Name::Error& e) |
| 124 | { |
| 125 | throw Error("Cannot decode name"); |
| 126 | } |
| 127 | catch(ndn::Block::Error& e) |
| 128 | { |
| 129 | throw Error("Cannot decode block name"); |
| 130 | } |
| 131 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 132 | if(m_introduceeCertName != m_introduceeCert.getName().getPrefix(-1)) |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 133 | throw Error("Invalid Sync::IntroCertificate (inconsistent introducee name)"); |
| 134 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 135 | ndn::Name keyLocatorName; |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 136 | try |
| 137 | { |
| 138 | ndn::SignatureSha256WithRsa sig(data.getSignature()); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 139 | keyLocatorName = sig.getKeyLocator().getName(); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 140 | } |
| 141 | catch(ndn::KeyLocator::Error& e) |
| 142 | { |
| 143 | throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#1)"); |
| 144 | } |
| 145 | catch(ndn::SignatureSha256WithRsa::Error& e) |
| 146 | { |
| 147 | throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#2)"); |
| 148 | } |
| 149 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 150 | if(m_introducerCertName != keyLocatorName) |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 151 | throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#3)"); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | } // namespace Sync |
| 156 | |
| 157 | #endif //SYNC_INTRO_CERTIFICATE_H |