blob: d6783ce7b2f5805a2a9f66ea7e06123f081d86ea [file] [log] [blame]
Alexander Afanasyev8722d872014-07-02 13:00:29 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
Yingdi Yu0eee6002014-02-11 15:54:17 -08002/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Yingdi Yu0eee6002014-02-11 15:54:17 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Yingdi Yu0eee6002014-02-11 15:54:17 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * 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 Yu0eee6002014-02-11 15:54:17 -080020 */
21
22#ifndef SYNC_INTRO_CERTIFICATE_H
23#define SYNC_INTRO_CERTIFICATE_H
24
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070025#include <ndn-cxx/security/identity-certificate.hpp>
26#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
Yingdi Yu0eee6002014-02-11 15:54:17 -080027
28namespace Sync {
29
30class IntroCertificate : public ndn::Data
31{
Yingdi Yu3da10fe2014-02-27 16:37:34 -080032 /**
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 Yu0eee6002014-02-11 15:54:17 -080038public:
39 struct Error : public ndn::Data::Error { Error(const std::string &what) : ndn::Data::Error(what) {} };
40
41 IntroCertificate()
42 {}
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070043
Yingdi Yu0eee6002014-02-11 15:54:17 -080044 /**
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 Yu3da10fe2014-02-27 16:37:34 -080053 const ndn::Name& introducerCertName); //without version number
Yingdi Yu0eee6002014-02-11 15:54:17 -080054
55 /**
56 * @brief Construct IntroCertificate using a plain data.
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070057 *
Yingdi Yu0eee6002014-02-11 15:54:17 -080058 * 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 Yu3da10fe2014-02-27 16:37:34 -080075 getIntroducerCertName() const
Yingdi Yu0eee6002014-02-11 15:54:17 -080076 {
Yingdi Yu3da10fe2014-02-27 16:37:34 -080077 return m_introducerCertName;
Yingdi Yu0eee6002014-02-11 15:54:17 -080078 }
79
80 const ndn::Name&
Yingdi Yu3da10fe2014-02-27 16:37:34 -080081 getIntroduceeCertName() const
Yingdi Yu0eee6002014-02-11 15:54:17 -080082 {
Yingdi Yu3da10fe2014-02-27 16:37:34 -080083 return m_introduceeCertName;
Yingdi Yu0eee6002014-02-11 15:54:17 -080084 }
85
86private:
87 ndn::Name m_syncPrefix;
88 ndn::IdentityCertificate m_introduceeCert;
Yingdi Yu3da10fe2014-02-27 16:37:34 -080089 ndn::Name m_introducerCertName;
90 ndn::Name m_introduceeCertName;
Yingdi Yu0eee6002014-02-11 15:54:17 -080091};
92
93inline
94IntroCertificate::IntroCertificate(const ndn::Name& syncPrefix,
95 const ndn::IdentityCertificate& introduceeCert,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080096 const ndn::Name& introducerCertName)
Yingdi Yu0eee6002014-02-11 15:54:17 -080097 : m_syncPrefix(syncPrefix)
98 , m_introduceeCert(introduceeCert)
Yingdi Yu3da10fe2014-02-27 16:37:34 -080099 , m_introducerCertName(introducerCertName)
100 , m_introduceeCertName(introduceeCert.getName().getPrefix(-1))
Yingdi Yu0eee6002014-02-11 15:54:17 -0800101{
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800102 // Naming convention /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version
Yingdi Yu0eee6002014-02-11 15:54:17 -0800103 ndn::Name dataName = m_syncPrefix;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800104 dataName.append("CHRONOS-INTRO-CERT")
105 .append(m_introduceeCertName.wireEncode())
106 .append(m_introducerCertName.wireEncode())
107 .appendVersion();
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700108
Yingdi Yu0eee6002014-02-11 15:54:17 -0800109 setName(dataName);
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800110 setContent(m_introduceeCert.wireEncode());
Yingdi Yu0eee6002014-02-11 15:54:17 -0800111}
112
113inline
114IntroCertificate::IntroCertificate(const ndn::Data& data)
115 : Data(data)
116{
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800117 // Naming convention /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version
Yingdi Yu0eee6002014-02-11 15:54:17 -0800118 ndn::Name dataName = data.getName();
Yingdi Yu0eee6002014-02-11 15:54:17 -0800119
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700120 if(dataName.size() < 4 || dataName.get(-4).toUri() != "CHRONOS-INTRO-CERT")
Yingdi Yu0eee6002014-02-11 15:54:17 -0800121 throw Error("Not a Sync::IntroCertificate");
122
Yingdi Yu0eee6002014-02-11 15:54:17 -0800123 try
124 {
125 m_introduceeCert.wireDecode(data.getContent().blockFromValue());
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800126 m_introducerCertName.wireDecode(dataName.get(-2).blockFromValue());
127 m_introduceeCertName.wireDecode(dataName.get(-3).blockFromValue());
128 m_syncPrefix = dataName.getPrefix(-4);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800129 }
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 Yu3da10fe2014-02-27 16:37:34 -0800143 if(m_introduceeCertName != m_introduceeCert.getName().getPrefix(-1))
Yingdi Yu0eee6002014-02-11 15:54:17 -0800144 throw Error("Invalid Sync::IntroCertificate (inconsistent introducee name)");
145
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800146 ndn::Name keyLocatorName;
Yingdi Yu0eee6002014-02-11 15:54:17 -0800147 try
148 {
149 ndn::SignatureSha256WithRsa sig(data.getSignature());
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800150 keyLocatorName = sig.getKeyLocator().getName();
Yingdi Yu0eee6002014-02-11 15:54:17 -0800151 }
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 Yu3da10fe2014-02-27 16:37:34 -0800161 if(m_introducerCertName != keyLocatorName)
Yingdi Yu0eee6002014-02-11 15:54:17 -0800162 throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#3)");
163}
164
165
166} // namespace Sync
167
168#endif //SYNC_INTRO_CERTIFICATE_H