identity-certificate.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "identity-certificate.hpp"
23 #include "../../util/concepts.hpp"
24 
25 namespace ndn {
26 namespace security {
27 namespace v1 {
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  ssize_t 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 v1
147 } // namespace security
148 } // namespace ndn
static Name certificateNameToPublicKeyName(const Name &certificateName)
Get the public key name from the full certificate name.
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:75
Data & setName(const Name &name)
Set name.
Definition: data.cpp:169
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
BOOST_CONCEPT_ASSERT((WireEncodable< Certificate >))
Name & append(const Component &component)
Append a component.
Definition: name.hpp:256
IdentityCertificate()
The default constructor.
Data & setFreshnessPeriod(const time::milliseconds &freshnessPeriod)
Definition: data.cpp:249
std::string toUri() const
Get URI representation of the name.
Definition: name.cpp:122
static bool isIdentityCertificate(const Certificate &certificate)
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 number of components.
Definition: name.hpp:154
Represents an absolute name.
Definition: name.hpp:42
const Name & getName() const
Get name.
Definition: data.hpp:121
PartialName getSubName(ssize_t iStartComponent, size_t nComponents=npos) const
Extract some components as a sub-name (PartialName)
Definition: name.cpp:199
Represents a Data packet.
Definition: data.hpp:35
void wireDecode(const Block &wire)
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:164