NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
identity-certificate.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "common.hpp"
23 
24 #include "identity-certificate.hpp"
25 #include "../util/concepts.hpp"
26 
27 namespace ndn {
28 
29 using std::string;
30 
31 BOOST_CONCEPT_ASSERT((WireEncodable<IdentityCertificate>));
32 BOOST_CONCEPT_ASSERT((WireDecodable<IdentityCertificate>));
33 static_assert(std::is_base_of<Certificate::Error, IdentityCertificate::Error>::value,
34  "IdentityCertificate::Error must inherit from Certificate::Error");
35 
37 {
38  this->setFreshnessPeriod(time::hours(1));
39 }
40 
42  : Certificate(data)
43 {
44  setPublicKeyName();
45 }
46 
48  : Certificate(block)
49 {
50  setPublicKeyName();
51 }
52 
53 void
55 {
57  setPublicKeyName();
58 }
59 
60 void
62 {
64  setPublicKeyName();
65 }
66 
67 bool
68 IdentityCertificate::isCorrectName(const Name& name)
69 {
70  string idString("ID-CERT");
71  int i = name.size() - 1;
72  for (; i >= 0; i--) {
73  if (name.get(i).toUri() == idString)
74  break;
75  }
76 
77  if (i < 0)
78  return false;
79 
80  string keyString("KEY");
81  size_t keyIndex = 0;
82  for (; keyIndex < name.size(); keyIndex++) {
83  if (name.get(keyIndex).toUri() == keyString)
84  break;
85  }
86 
87  if (keyIndex >= name.size())
88  return false;
89 
90  return true;
91 }
92 
93 void
94 IdentityCertificate::setPublicKeyName()
95 {
96  if (!isCorrectName(getName()))
97  BOOST_THROW_EXCEPTION(Error("Wrong Identity Certificate Name"));
98 
100 }
101 
102 bool
104 {
105  return dynamic_cast<const IdentityCertificate*>(&certificate);
106 }
107 
108 Name
110 {
111  string idString("ID-CERT");
112  bool foundIdString = false;
113  size_t idCertComponentIndex = certificateName.size() - 1;
114  for (; idCertComponentIndex + 1 > 0; --idCertComponentIndex) {
115  if (certificateName.get(idCertComponentIndex).toUri() == idString)
116  {
117  foundIdString = true;
118  break;
119  }
120  }
121 
122  if (!foundIdString)
123  BOOST_THROW_EXCEPTION(Error("Incorrect identity certificate name " + certificateName.toUri()));
124 
125  Name tmpName = certificateName.getSubName(0, idCertComponentIndex);
126  string keyString("KEY");
127  bool foundKeyString = false;
128  size_t keyComponentIndex = 0;
129  for (; keyComponentIndex < tmpName.size(); keyComponentIndex++) {
130  if (tmpName.get(keyComponentIndex).toUri() == keyString)
131  {
132  foundKeyString = true;
133  break;
134  }
135  }
136 
137  if (!foundKeyString)
138  BOOST_THROW_EXCEPTION(Error("Incorrect identity certificate name " + certificateName.toUri()));
139 
140  return tmpName
141  .getSubName(0, keyComponentIndex)
142  .append(tmpName.getSubName(keyComponentIndex + 1,
143  tmpName.size() - keyComponentIndex - 1));
144 }
145 
146 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
Data & setName(const Name &name)
Set name to a copy of the given Name.
Definition: data.cpp:170
void wireDecode(const Block &wire)
Definition: certificate.cpp:69
static Name certificateNameToPublicKeyName(const Name &certificateName)
Get the public key name from the full certificate name.
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
void wireDecode(const Block &wire)
Data & setFreshnessPeriod(const time::milliseconds &freshnessPeriod)
Definition: data.cpp:212
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:343
std::string toUri() const
Encode this name as a URI.
Definition: name.cpp:183
IdentityCertificate()
The default constructor.
void setName(const Name &name)
void toUri(std::ostream &os) const
Write *this to the output stream, escaping characters according to the NDN URI Scheme.
size_t size() const
Get the number of components.
Definition: name.hpp:408
static bool isIdentityCertificate(const Certificate &certificate)
Name abstraction to represent an absolute name.
Definition: name.hpp:46
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
Definition: name.hpp:148
PartialName getSubName(ssize_t iStartComponent, size_t nComponents=npos) const
Extract a sub-name (PartialName) of nComponents components starting from iStartComponent.
Definition: name.cpp:274
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
represents a Data packet
Definition: data.hpp:39
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:419