NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
oid.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "oid.hpp"
23 
24 #include "../security/v1/cryptopp.hpp"
25 
26 #include <sstream>
27 
28 namespace ndn {
29 
30 static const int OID_MAGIC_NUMBER = 40;
31 
32 Oid::Oid(const char* oid)
33  : Oid(std::string(oid))
34 {
35 }
36 
37 Oid::Oid(const std::string& oid)
38 {
39  std::string str = oid + ".";
40 
41  size_t pos = 0;
42  size_t ppos = 0;
43 
44  while (std::string::npos != pos) {
45  ppos = pos;
46 
47  pos = str.find_first_of('.', pos);
48  if (pos == std::string::npos)
49  break;
50 
51  m_oid.push_back(atoi(str.substr(ppos, pos - ppos).c_str()));
52 
53  pos++;
54  }
55 }
56 
57 std::string
59 {
60  std::ostringstream convert;
61 
62  for (std::vector<int>::const_iterator it = m_oid.begin(); it != m_oid.end(); ++it) {
63  if (it != m_oid.begin())
64  convert << ".";
65  convert << *it;
66  }
67 
68  return convert.str();
69 }
70 
71 bool
72 Oid::equal(const Oid& oid) const
73 {
74  std::vector<int>::const_iterator i = m_oid.begin();
75  std::vector<int>::const_iterator j = oid.m_oid.begin();
76 
77  for (; i != m_oid.end() && j != oid.m_oid.end(); i++, j++) {
78  if (*i != *j)
79  return false;
80  }
81 
82  return (i == m_oid.end() && j == oid.m_oid.end()); // keep parenthesis for readability.
83 }
84 
85 inline void
86 encodeValue(CryptoPP::BufferedTransformation& bt, CryptoPP::word32 v)
87 {
88  using namespace CryptoPP;
89 
90  for (unsigned int i = RoundUpToMultipleOf(STDMAX(7U, BitPrecision(v)), 7U) - 7; i != 0; i -= 7)
91  bt.Put(static_cast<byte>(0x80 | ((v >> i) & 0x7f)));
92  bt.Put(static_cast<byte>(v & 0x7f));
93 }
94 
95 inline size_t
96 decodeValue(CryptoPP::BufferedTransformation& bt, CryptoPP::word32& v)
97 {
98  using namespace CryptoPP;
99 
100  v = 0;
101  size_t i = 0;
102  while (true)
103  {
104  byte b;
105  if (!bt.Get(b))
106  BERDecodeError();
107  i++;
108  if (v >> (8 * sizeof(v) - 7)) // v about to overflow
109  BERDecodeError();
110  v <<= 7;
111  v += b & 0x7f;
112  if ((b & 0x80) == 0)
113  return i;
114  }
115 }
116 
117 void
118 Oid::encode(CryptoPP::BufferedTransformation& out) const
119 {
120  using namespace CryptoPP;
121 
122  BOOST_ASSERT(m_oid.size() >= 2);
123 
124  ByteQueue temp;
125  temp.Put(byte(m_oid[0] * OID_MAGIC_NUMBER + m_oid[1]));
126  for (size_t i = 2; i < m_oid.size(); i++)
127  encodeValue(temp, m_oid[i]);
128 
129  out.Put(OBJECT_IDENTIFIER);
130  DERLengthEncode(out, temp.CurrentSize());
131  temp.TransferTo(out);
132 }
133 
134 void
135 Oid::decode(CryptoPP::BufferedTransformation& in)
136 {
137  using namespace CryptoPP;
138 
139  byte b;
140  if (!in.Get(b) || b != OBJECT_IDENTIFIER)
141  BERDecodeError();
142 
143  size_t length;
144  if (!BERLengthDecode(in, length) || length < 1)
145  BERDecodeError();
146 
147  if (!in.Get(b))
148  BERDecodeError();
149 
150  length--;
151  m_oid.resize(2);
152  m_oid[0] = b / OID_MAGIC_NUMBER;
153  m_oid[1] = b % OID_MAGIC_NUMBER;
154 
155  while (length > 0)
156  {
157  word32 v;
158  size_t valueLen = decodeValue(in, v);
159  if (valueLen > length)
160  BERDecodeError();
161  m_oid.push_back(v);
162  length -= valueLen;
163  }
164 }
165 
166 namespace oid {
167 const Oid RSA("1.2.840.113549.1.1.1");
168 const Oid ECDSA("1.2.840.10045.2.1");
169 
170 const Oid ATTRIBUTE_NAME("2.5.4.41");
171 } // namespace oid
172 
173 } // namespace ndn
void encode(CryptoPP::BufferedTransformation &out) const
Definition: oid.cpp:118
void decode(CryptoPP::BufferedTransformation &in)
Definition: oid.cpp:135
void encodeValue(CryptoPP::BufferedTransformation &bt, CryptoPP::word32 v)
Definition: oid.cpp:86
const Oid ECDSA("1.2.840.10045.2.1")
Definition: oid.hpp:102
Copyright (c) 2011-2015 Regents of the University of California.
Copyright (c) 2013-2016 Regents of the University of California.
Definition: oid.hpp:29
STL namespace.
const Oid ATTRIBUTE_NAME("2.5.4.41")
Definition: oid.hpp:105
Oid()=default
std::string toString() const
Definition: oid.cpp:58
Definition: oid.hpp:35
size_t decodeValue(CryptoPP::BufferedTransformation &bt, CryptoPP::word32 &v)
Definition: oid.cpp:96
static const int OID_MAGIC_NUMBER
Definition: oid.cpp:30