blob: f9085b24a30e3f435341e37853038934042a1750 [file] [log] [blame]
Jeff Thompsonc0573432013-09-19 17:41:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
Jeff Thompsonc0573432013-09-19 17:41:36 -070012 */
13
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080014#include "common.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070015
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080016#include "oid.hpp"
17
Junxiao Shi482ccc52014-03-31 13:05:24 -070018#include "../security/cryptopp.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070019
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070020#include <sstream>
21
Jeff Thompsonc0573432013-09-19 17:41:36 -070022using namespace std;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080023using namespace CryptoPP;
Jeff Thompsonc0573432013-09-19 17:41:36 -070024
25namespace ndn {
26
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070027OID::OID(const char* oid)
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080028{
29 construct(oid);
30}
31
Jeff Thompsonc0573432013-09-19 17:41:36 -070032OID::OID(const string& oid)
33{
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080034 construct(oid);
35}
36
37void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038OID::construct(const std::string& oid)
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080039{
Jeff Thompsonc0573432013-09-19 17:41:36 -070040 string str = oid + ".";
41
42 size_t pos = 0;
43 size_t ppos = 0;
44
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045 while (string::npos != pos) {
Jeff Thompsonc0573432013-09-19 17:41:36 -070046 ppos = pos;
47
48 pos = str.find_first_of('.', pos);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049 if (pos == string::npos)
Jeff Thompsone589c3f2013-10-12 17:30:50 -070050 break;
Jeff Thompsonc0573432013-09-19 17:41:36 -070051
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052 m_oid.push_back(atoi(str.substr(ppos, pos - ppos).c_str()));
Jeff Thompsonc0573432013-09-19 17:41:36 -070053
54 pos++;
55 }
56}
57
Jeff Thompson6c729a22013-12-20 10:37:59 -080058string OID::toString() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070059{
60 ostringstream convert;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061
62 for (vector<int>::const_iterator it = m_oid.begin(); it != m_oid.end(); ++it) {
63 if (it != m_oid.begin())
Jeff Thompsonc0573432013-09-19 17:41:36 -070064 convert << ".";
65 convert << *it;
66 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067
Jeff Thompsonc0573432013-09-19 17:41:36 -070068 return convert.str();
69}
70
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080071bool
72OID::equal(const OID& oid) const
Jeff Thompsonc0573432013-09-19 17:41:36 -070073{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070074 vector<int>::const_iterator i = m_oid.begin();
75 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)
Jeff Thompsonc0573432013-09-19 17:41:36 -070079 return false;
80 }
81
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070082 if (i == m_oid.end () && j == oid.m_oid.end ())
Jeff Thompsonc0573432013-09-19 17:41:36 -070083 return true;
84 else
85 return false;
86}
87
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080088inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089EncodeValue(BufferedTransformation& bt, word32 v)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080090{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091 for (unsigned int i = RoundUpToMultipleOf(STDMAX(7U,BitPrecision(v)), 7U) - 7; i != 0; i -= 7)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080092 bt.Put((byte)(0x80 | ((v >> i) & 0x7f)));
93 bt.Put((byte)(v & 0x7f));
94}
95
96inline size_t
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097DecodeValue(BufferedTransformation& bt, word32& v)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080098{
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080099 v = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700100 size_t i = 0;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800101 while (true)
102 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700103 byte b;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800104 if (!bt.Get(b))
105 BERDecodeError();
106 i++;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700107 if (v >> (8*sizeof(v) - 7)) // v about to overflow
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800108 BERDecodeError();
109 v <<= 7;
110 v += b & 0x7f;
111 if (!(b & 0x80))
112 return i;
113 }
114}
115
116void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700117OID::encode(CryptoPP::BufferedTransformation& out) const
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800118{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700119 BOOST_ASSERT(m_oid.size() >= 2);
120
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800121 ByteQueue temp;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122 temp.Put(byte(m_oid[0] * 40 + m_oid[1]));
123 for (size_t i = 2; i < m_oid.size(); i++)
124 EncodeValue(temp, m_oid[i]);
125
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800126 out.Put(OBJECT_IDENTIFIER);
127 DERLengthEncode(out, temp.CurrentSize());
128 temp.TransferTo(out);
129}
130
131void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700132OID::decode(CryptoPP::BufferedTransformation& in)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800133{
134 byte b;
135 if (!in.Get(b) || b != OBJECT_IDENTIFIER)
136 BERDecodeError();
137
138 size_t length;
139 if (!BERLengthDecode(in, length) || length < 1)
140 BERDecodeError();
141
142 if (!in.Get(b))
143 BERDecodeError();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700144
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800145 length--;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700146 m_oid.resize(2);
147 m_oid[0] = b / 40;
148 m_oid[1] = b % 40;
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800149
150 while (length > 0)
151 {
152 word32 v;
153 size_t valueLen = DecodeValue(in, v);
154 if (valueLen > length)
155 BERDecodeError();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700156 m_oid.push_back(v);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800157 length -= valueLen;
158 }
159}
160
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700161} // namespace ndn