blob: cff98cd8a9a92519a6532d3527657bfea2ac1c6f [file] [log] [blame]
Jeff Thompsonc0573432013-09-19 17:41:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08008#include "common.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -07009
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080010#include "oid.hpp"
11
Junxiao Shi482ccc52014-03-31 13:05:24 -070012#include "../security/cryptopp.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070013
14using namespace std;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080015using namespace CryptoPP;
Jeff Thompsonc0573432013-09-19 17:41:36 -070016
17namespace ndn {
18
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070019OID::OID(const char* oid)
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080020{
21 construct(oid);
22}
23
Jeff Thompsonc0573432013-09-19 17:41:36 -070024OID::OID(const string& oid)
25{
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080026 construct(oid);
27}
28
29void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070030OID::construct(const std::string& oid)
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080031{
Jeff Thompsonc0573432013-09-19 17:41:36 -070032 string str = oid + ".";
33
34 size_t pos = 0;
35 size_t ppos = 0;
36
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070037 while (string::npos != pos) {
Jeff Thompsonc0573432013-09-19 17:41:36 -070038 ppos = pos;
39
40 pos = str.find_first_of('.', pos);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041 if (pos == string::npos)
Jeff Thompsone589c3f2013-10-12 17:30:50 -070042 break;
Jeff Thompsonc0573432013-09-19 17:41:36 -070043
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044 m_oid.push_back(atoi(str.substr(ppos, pos - ppos).c_str()));
Jeff Thompsonc0573432013-09-19 17:41:36 -070045
46 pos++;
47 }
48}
49
Jeff Thompson6c729a22013-12-20 10:37:59 -080050string OID::toString() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070051{
52 ostringstream convert;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070053
54 for (vector<int>::const_iterator it = m_oid.begin(); it != m_oid.end(); ++it) {
55 if (it != m_oid.begin())
Jeff Thompsonc0573432013-09-19 17:41:36 -070056 convert << ".";
57 convert << *it;
58 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070059
Jeff Thompsonc0573432013-09-19 17:41:36 -070060 return convert.str();
61}
62
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080063bool
64OID::equal(const OID& oid) const
Jeff Thompsonc0573432013-09-19 17:41:36 -070065{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070066 vector<int>::const_iterator i = m_oid.begin();
67 vector<int>::const_iterator j = oid.m_oid.begin();
68
69 for (; i != m_oid.end () && j != oid.m_oid.end (); i++, j++) {
70 if (*i != *j)
Jeff Thompsonc0573432013-09-19 17:41:36 -070071 return false;
72 }
73
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070074 if (i == m_oid.end () && j == oid.m_oid.end ())
Jeff Thompsonc0573432013-09-19 17:41:36 -070075 return true;
76 else
77 return false;
78}
79
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080080inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070081EncodeValue(BufferedTransformation& bt, word32 v)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080082{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083 for (unsigned int i = RoundUpToMultipleOf(STDMAX(7U,BitPrecision(v)), 7U) - 7; i != 0; i -= 7)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080084 bt.Put((byte)(0x80 | ((v >> i) & 0x7f)));
85 bt.Put((byte)(v & 0x7f));
86}
87
88inline size_t
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089DecodeValue(BufferedTransformation& bt, word32& v)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080090{
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080091 v = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070092 size_t i = 0;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080093 while (true)
94 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095 byte b;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080096 if (!bt.Get(b))
97 BERDecodeError();
98 i++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070099 if (v >> (8*sizeof(v) - 7)) // v about to overflow
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800100 BERDecodeError();
101 v <<= 7;
102 v += b & 0x7f;
103 if (!(b & 0x80))
104 return i;
105 }
106}
107
108void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700109OID::encode(CryptoPP::BufferedTransformation& out) const
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800110{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700111 BOOST_ASSERT(m_oid.size() >= 2);
112
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800113 ByteQueue temp;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700114 temp.Put(byte(m_oid[0] * 40 + m_oid[1]));
115 for (size_t i = 2; i < m_oid.size(); i++)
116 EncodeValue(temp, m_oid[i]);
117
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800118 out.Put(OBJECT_IDENTIFIER);
119 DERLengthEncode(out, temp.CurrentSize());
120 temp.TransferTo(out);
121}
122
123void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124OID::decode(CryptoPP::BufferedTransformation& in)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800125{
126 byte b;
127 if (!in.Get(b) || b != OBJECT_IDENTIFIER)
128 BERDecodeError();
129
130 size_t length;
131 if (!BERLengthDecode(in, length) || length < 1)
132 BERDecodeError();
133
134 if (!in.Get(b))
135 BERDecodeError();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800137 length--;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700138 m_oid.resize(2);
139 m_oid[0] = b / 40;
140 m_oid[1] = b % 40;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800141
142 while (length > 0)
143 {
144 word32 v;
145 size_t valueLen = DecodeValue(in, v);
146 if (valueLen > length)
147 BERDecodeError();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700148 m_oid.push_back(v);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800149 length -= valueLen;
150 }
151}
152
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700153} // namespace ndn