blob: 48dae45a14a33e8b5fc1ca6e3feead6c5cd7a778 [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
14#include <ndn-cpp-dev/security/identity-certificate.hpp>
15#include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp>
16
17namespace Sync {
18
19class IntroCertificate : public ndn::Data
20{
21public:
22 struct Error : public ndn::Data::Error { Error(const std::string &what) : ndn::Data::Error(what) {} };
23
24 IntroCertificate()
25 {}
26
27 /**
28 * @brief Construct IntroCertificate from IdentityCertificate
29 *
30 * @param syncPrefix
31 * @param introduceeCert
32 * @param introducerName
33 */
34 IntroCertificate(const ndn::Name& syncPrefix,
35 const ndn::IdentityCertificate& introduceeCert,
36 const ndn::Name& introducerName); //without version number
37
38 /**
39 * @brief Construct IntroCertificate using a plain data.
40 *
41 * if data is not actually IntroCertificate, Error will be thrown out.
42 *
43 * @param data
44 * @throws ndn::IntroCertificate::Error.
45 */
46 IntroCertificate(const ndn::Data& data);
47
48 virtual
49 ~IntroCertificate() {};
50
51 const ndn::IdentityCertificate&
52 getIntroduceeCert() const
53 {
54 return m_introduceeCert;
55 }
56
57 const ndn::Name&
58 getIntroducerName() const
59 {
60 return m_introducerName;
61 }
62
63 const ndn::Name&
64 getIntroduceeName() const
65 {
66 return m_introduceeName;
67 }
68
69private:
70 ndn::Name m_syncPrefix;
71 ndn::IdentityCertificate m_introduceeCert;
72 ndn::Name m_introducerName;
73 ndn::Name m_introduceeName;
74};
75
76inline
77IntroCertificate::IntroCertificate(const ndn::Name& syncPrefix,
78 const ndn::IdentityCertificate& introduceeCert,
79 const ndn::Name& introducerName)
80 : m_syncPrefix(syncPrefix)
81 , m_introduceeCert(introduceeCert)
82 , m_introducerName(introducerName)
83 , m_introduceeName(introduceeCert.getName().getPrefix(-1))
84{
85 // Naming convention /<sync_prefix>/intro-cert/introducee_certname/introducer_certname/version/
86 ndn::Name dataName = m_syncPrefix;
87 dataName.append("intro-cert").append(introduceeCert.getName().getPrefix(-1).wireEncode()).append(introducerName.wireEncode()).appendVersion();
88
89 setName(dataName);
90 setContent(introduceeCert.wireEncode());
91}
92
93inline
94IntroCertificate::IntroCertificate(const ndn::Data& data)
95 : Data(data)
96{
97 ndn::Name dataName = data.getName();
98 ndn::Name introduceeCertName;
99 ndn::Name introducerName;
100
101 if(dataName.size() < 4 || dataName.get(-4).toEscapedString() != "intro-cert")
102 throw Error("Not a Sync::IntroCertificate");
103
104 m_syncPrefix = dataName.getPrefix(-4);
105
106 try
107 {
108 m_introduceeCert.wireDecode(data.getContent().blockFromValue());
109 m_introducerName.wireDecode(dataName.get(-2).blockFromValue());
110 introduceeCertName.wireDecode(dataName.get(-3).blockFromValue());
111 }
112 catch(ndn::IdentityCertificate::Error& e)
113 {
114 throw Error("Cannot decode introducee cert");
115 }
116 catch(ndn::Name::Error& e)
117 {
118 throw Error("Cannot decode name");
119 }
120 catch(ndn::Block::Error& e)
121 {
122 throw Error("Cannot decode block name");
123 }
124
125 if(introduceeCertName != m_introduceeCert.getName().getPrefix(-1))
126 throw Error("Invalid Sync::IntroCertificate (inconsistent introducee name)");
127
128 m_introduceeName = introduceeCertName;
129
130 try
131 {
132 ndn::SignatureSha256WithRsa sig(data.getSignature());
133 introducerName = sig.getKeyLocator().getName();
134 }
135 catch(ndn::KeyLocator::Error& e)
136 {
137 throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#1)");
138 }
139 catch(ndn::SignatureSha256WithRsa::Error& e)
140 {
141 throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#2)");
142 }
143
144 if(m_introducerName != introducerName)
145 throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#3)");
146
147 if(m_introducerName != introducerName)
148 throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#3)");
149}
150
151
152} // namespace Sync
153
154#endif //SYNC_INTRO_CERTIFICATE_H