All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
identity-certificate.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
9 #include "common.hpp"
10 
11 #include "identity-certificate.hpp"
12 
13 using namespace std;
14 
15 namespace ndn {
16 
17 bool
18 IdentityCertificate::isCorrectName(const Name& name)
19 {
20  string idString("ID-CERT");
21  int i = name.size() - 1;
22  for (; i >= 0; i--) {
23  if (name.get(i).toEscapedString() == idString)
24  break;
25  }
26 
27  if (i < 0)
28  return false;
29 
30  string keyString("KEY");
31  size_t keyIndex = 0;
32  for (; keyIndex < name.size(); keyIndex++) {
33  if (name.get(keyIndex).toEscapedString() == keyString)
34  break;
35  }
36 
37  if (keyIndex >= name.size())
38  return false;
39 
40  return true;
41 }
42 
43 void
44 IdentityCertificate::setPublicKeyName()
45 {
46  if (!isCorrectName(getName()))
47  throw Error("Wrong Identity Certificate Name!");
48 
49  m_publicKeyName = certificateNameToPublicKeyName(getName());
50 }
51 
52 bool
53 IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
54 {
55  return dynamic_cast<const IdentityCertificate*>(&certificate);
56 }
57 
58 Name
59 IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
60 {
61  string idString("ID-CERT");
62  bool foundIdString = false;
63  size_t idCertComponentIndex = certificateName.size() - 1;
64  for (; idCertComponentIndex + 1 > 0; --idCertComponentIndex) {
65  if (certificateName.get(idCertComponentIndex).toEscapedString() == idString)
66  {
67  foundIdString = true;
68  break;
69  }
70  }
71 
72  if (!foundIdString)
73  throw Error("Incorrect identity certificate name " + certificateName.toUri());
74 
75  Name tmpName = certificateName.getSubName(0, idCertComponentIndex);
76  string keyString("KEY");
77  bool foundKeyString = false;
78  size_t keyComponentIndex = 0;
79  for (; keyComponentIndex < tmpName.size(); keyComponentIndex++) {
80  if (tmpName.get(keyComponentIndex).toEscapedString() == keyString)
81  {
82  foundKeyString = true;
83  break;
84  }
85  }
86 
87  if (!foundKeyString)
88  throw Error("Incorrect identity certificate name " + certificateName.toUri());
89 
90  return tmpName
91  .getSubName(0, keyComponentIndex)
92  .append(tmpName.getSubName(keyComponentIndex + 1,
93  tmpName.size() - keyComponentIndex - 1));
94 }
95 
96 } // namespace ndn
Name getSubName(size_t iStartComponent, size_t nComponents) const
Get a new name, constructed as a subset of components.
Definition: name.hpp:632
void toEscapedString(std::ostream &result) const
Write the value to result, escaping characters according to the NDN URI Scheme.
std::string toUri() const
Encode this name as a URI.
Definition: name.hpp:536
size_t size() const
Get the number of components.
Definition: name.hpp:329
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:340