Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 8 | #include "common.hpp" |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 9 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 10 | #include "oid.hpp" |
| 11 | |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 12 | #include "../security/cryptopp.hpp" |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 13 | |
| 14 | using namespace std; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 15 | using namespace CryptoPP; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 16 | |
| 17 | namespace ndn { |
| 18 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 19 | OID::OID(const char* oid) |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 20 | { |
| 21 | construct(oid); |
| 22 | } |
| 23 | |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 24 | OID::OID(const string& oid) |
| 25 | { |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 26 | construct(oid); |
| 27 | } |
| 28 | |
| 29 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 30 | OID::construct(const std::string& oid) |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 31 | { |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 32 | string str = oid + "."; |
| 33 | |
| 34 | size_t pos = 0; |
| 35 | size_t ppos = 0; |
| 36 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 37 | while (string::npos != pos) { |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 38 | ppos = pos; |
| 39 | |
| 40 | pos = str.find_first_of('.', pos); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 41 | if (pos == string::npos) |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 42 | break; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 44 | m_oid.push_back(atoi(str.substr(ppos, pos - ppos).c_str())); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 45 | |
| 46 | pos++; |
| 47 | } |
| 48 | } |
| 49 | |
Jeff Thompson | 6c729a2 | 2013-12-20 10:37:59 -0800 | [diff] [blame] | 50 | string OID::toString() const |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 51 | { |
| 52 | ostringstream convert; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 53 | |
| 54 | for (vector<int>::const_iterator it = m_oid.begin(); it != m_oid.end(); ++it) { |
| 55 | if (it != m_oid.begin()) |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 56 | convert << "."; |
| 57 | convert << *it; |
| 58 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 60 | return convert.str(); |
| 61 | } |
| 62 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 63 | bool |
| 64 | OID::equal(const OID& oid) const |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 65 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 66 | 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 Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 74 | if (i == m_oid.end () && j == oid.m_oid.end ()) |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 75 | return true; |
| 76 | else |
| 77 | return false; |
| 78 | } |
| 79 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 80 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 81 | EncodeValue(BufferedTransformation& bt, word32 v) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 82 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 83 | for (unsigned int i = RoundUpToMultipleOf(STDMAX(7U,BitPrecision(v)), 7U) - 7; i != 0; i -= 7) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 84 | bt.Put((byte)(0x80 | ((v >> i) & 0x7f))); |
| 85 | bt.Put((byte)(v & 0x7f)); |
| 86 | } |
| 87 | |
| 88 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 89 | DecodeValue(BufferedTransformation& bt, word32& v) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 90 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 91 | v = 0; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 92 | size_t i = 0; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 93 | while (true) |
| 94 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 95 | byte b; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 96 | if (!bt.Get(b)) |
| 97 | BERDecodeError(); |
| 98 | i++; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 99 | if (v >> (8*sizeof(v) - 7)) // v about to overflow |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 100 | BERDecodeError(); |
| 101 | v <<= 7; |
| 102 | v += b & 0x7f; |
| 103 | if (!(b & 0x80)) |
| 104 | return i; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 109 | OID::encode(CryptoPP::BufferedTransformation& out) const |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 110 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 111 | BOOST_ASSERT(m_oid.size() >= 2); |
| 112 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 113 | ByteQueue temp; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 114 | 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 Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 118 | out.Put(OBJECT_IDENTIFIER); |
| 119 | DERLengthEncode(out, temp.CurrentSize()); |
| 120 | temp.TransferTo(out); |
| 121 | } |
| 122 | |
| 123 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 124 | OID::decode(CryptoPP::BufferedTransformation& in) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 125 | { |
| 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 Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 136 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 137 | length--; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 138 | m_oid.resize(2); |
| 139 | m_oid[0] = b / 40; |
| 140 | m_oid[1] = b % 40; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 141 | |
| 142 | while (length > 0) |
| 143 | { |
| 144 | word32 v; |
| 145 | size_t valueLen = DecodeValue(in, v); |
| 146 | if (valueLen > length) |
| 147 | BERDecodeError(); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 148 | m_oid.push_back(v); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 149 | length -= valueLen; |
| 150 | } |
| 151 | } |
| 152 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 153 | } // namespace ndn |