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