blob: 737c921b1bca6a6236391636540a24de018b3b55 [file] [log] [blame]
Yingdi Yu0eee6002014-02-11 15:54:17 -08001/* -*- 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 Yu7c64e5c2014-04-30 14:06:37 -070014#include <ndn-cxx/security/identity-certificate.hpp>
15#include <ndn-cxx/security/signature-sha256-with-rsa.hpp>
Yingdi Yu0eee6002014-02-11 15:54:17 -080016
17namespace Sync {
18
19class IntroCertificate : public ndn::Data
20{
Yingdi Yu3da10fe2014-02-27 16:37:34 -080021 /**
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 Yu0eee6002014-02-11 15:54:17 -080027public:
28 struct Error : public ndn::Data::Error { Error(const std::string &what) : ndn::Data::Error(what) {} };
29
30 IntroCertificate()
31 {}
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070032
Yingdi Yu0eee6002014-02-11 15:54:17 -080033 /**
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 Yu3da10fe2014-02-27 16:37:34 -080042 const ndn::Name& introducerCertName); //without version number
Yingdi Yu0eee6002014-02-11 15:54:17 -080043
44 /**
45 * @brief Construct IntroCertificate using a plain data.
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070046 *
Yingdi Yu0eee6002014-02-11 15:54:17 -080047 * 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 Yu3da10fe2014-02-27 16:37:34 -080064 getIntroducerCertName() const
Yingdi Yu0eee6002014-02-11 15:54:17 -080065 {
Yingdi Yu3da10fe2014-02-27 16:37:34 -080066 return m_introducerCertName;
Yingdi Yu0eee6002014-02-11 15:54:17 -080067 }
68
69 const ndn::Name&
Yingdi Yu3da10fe2014-02-27 16:37:34 -080070 getIntroduceeCertName() const
Yingdi Yu0eee6002014-02-11 15:54:17 -080071 {
Yingdi Yu3da10fe2014-02-27 16:37:34 -080072 return m_introduceeCertName;
Yingdi Yu0eee6002014-02-11 15:54:17 -080073 }
74
75private:
76 ndn::Name m_syncPrefix;
77 ndn::IdentityCertificate m_introduceeCert;
Yingdi Yu3da10fe2014-02-27 16:37:34 -080078 ndn::Name m_introducerCertName;
79 ndn::Name m_introduceeCertName;
Yingdi Yu0eee6002014-02-11 15:54:17 -080080};
81
82inline
83IntroCertificate::IntroCertificate(const ndn::Name& syncPrefix,
84 const ndn::IdentityCertificate& introduceeCert,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080085 const ndn::Name& introducerCertName)
Yingdi Yu0eee6002014-02-11 15:54:17 -080086 : m_syncPrefix(syncPrefix)
87 , m_introduceeCert(introduceeCert)
Yingdi Yu3da10fe2014-02-27 16:37:34 -080088 , m_introducerCertName(introducerCertName)
89 , m_introduceeCertName(introduceeCert.getName().getPrefix(-1))
Yingdi Yu0eee6002014-02-11 15:54:17 -080090{
Yingdi Yu3da10fe2014-02-27 16:37:34 -080091 // Naming convention /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version
Yingdi Yu0eee6002014-02-11 15:54:17 -080092 ndn::Name dataName = m_syncPrefix;
Yingdi Yu3da10fe2014-02-27 16:37:34 -080093 dataName.append("CHRONOS-INTRO-CERT")
94 .append(m_introduceeCertName.wireEncode())
95 .append(m_introducerCertName.wireEncode())
96 .appendVersion();
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070097
Yingdi Yu0eee6002014-02-11 15:54:17 -080098 setName(dataName);
Yingdi Yu3da10fe2014-02-27 16:37:34 -080099 setContent(m_introduceeCert.wireEncode());
Yingdi Yu0eee6002014-02-11 15:54:17 -0800100}
101
102inline
103IntroCertificate::IntroCertificate(const ndn::Data& data)
104 : Data(data)
105{
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800106 // Naming convention /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version
Yingdi Yu0eee6002014-02-11 15:54:17 -0800107 ndn::Name dataName = data.getName();
Yingdi Yu0eee6002014-02-11 15:54:17 -0800108
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700109 if(dataName.size() < 4 || dataName.get(-4).toUri() != "CHRONOS-INTRO-CERT")
Yingdi Yu0eee6002014-02-11 15:54:17 -0800110 throw Error("Not a Sync::IntroCertificate");
111
Yingdi Yu0eee6002014-02-11 15:54:17 -0800112 try
113 {
114 m_introduceeCert.wireDecode(data.getContent().blockFromValue());
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800115 m_introducerCertName.wireDecode(dataName.get(-2).blockFromValue());
116 m_introduceeCertName.wireDecode(dataName.get(-3).blockFromValue());
117 m_syncPrefix = dataName.getPrefix(-4);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800118 }
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 Yu3da10fe2014-02-27 16:37:34 -0800132 if(m_introduceeCertName != m_introduceeCert.getName().getPrefix(-1))
Yingdi Yu0eee6002014-02-11 15:54:17 -0800133 throw Error("Invalid Sync::IntroCertificate (inconsistent introducee name)");
134
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800135 ndn::Name keyLocatorName;
Yingdi Yu0eee6002014-02-11 15:54:17 -0800136 try
137 {
138 ndn::SignatureSha256WithRsa sig(data.getSignature());
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800139 keyLocatorName = sig.getKeyLocator().getName();
Yingdi Yu0eee6002014-02-11 15:54:17 -0800140 }
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 Yu3da10fe2014-02-27 16:37:34 -0800150 if(m_introducerCertName != keyLocatorName)
Yingdi Yu0eee6002014-02-11 15:54:17 -0800151 throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#3)");
152}
153
154
155} // namespace Sync
156
157#endif //SYNC_INTRO_CERTIFICATE_H