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 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 12 | #include <cryptopp/asn.h> |
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 | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 19 | OID::OID(const char *oid) |
| 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 |
| 30 | OID::construct(const std::string &oid) |
| 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 | |
| 37 | while(string::npos != pos){ |
| 38 | ppos = pos; |
| 39 | |
| 40 | pos = str.find_first_of('.', pos); |
| 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 | |
| 44 | oid_.push_back(atoi(str.substr(ppos, pos - ppos).c_str())); |
| 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; |
| 53 | |
Jeff Thompson | 6c729a2 | 2013-12-20 10:37:59 -0800 | [diff] [blame] | 54 | vector<int>::const_iterator it = oid_.begin(); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 55 | for(; it < oid_.end(); it++){ |
| 56 | if(it != oid_.begin()) |
| 57 | convert << "."; |
| 58 | convert << *it; |
| 59 | } |
| 60 | |
| 61 | return convert.str(); |
| 62 | } |
| 63 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 64 | bool |
| 65 | OID::equal(const OID& oid) const |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 66 | { |
| 67 | vector<int>::const_iterator i = oid_.begin(); |
| 68 | vector<int>::const_iterator j = oid.oid_.begin(); |
| 69 | |
| 70 | for (; i != oid_.end () && j != oid.oid_.end (); i++, j++) { |
| 71 | if(*i != *j) |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | if (i == oid_.end () && j == oid.oid_.end ()) |
| 76 | return true; |
| 77 | else |
| 78 | return false; |
| 79 | } |
| 80 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 81 | inline void |
| 82 | EncodeValue(BufferedTransformation &bt, word32 v) |
| 83 | { |
| 84 | for (unsigned int i=RoundUpToMultipleOf(STDMAX(7U,BitPrecision(v)), 7U)-7; i != 0; i-=7) |
| 85 | bt.Put((byte)(0x80 | ((v >> i) & 0x7f))); |
| 86 | bt.Put((byte)(v & 0x7f)); |
| 87 | } |
| 88 | |
| 89 | inline size_t |
| 90 | DecodeValue(BufferedTransformation &bt, word32 &v) |
| 91 | { |
| 92 | byte b; |
| 93 | size_t i=0; |
| 94 | v = 0; |
| 95 | while (true) |
| 96 | { |
| 97 | if (!bt.Get(b)) |
| 98 | BERDecodeError(); |
| 99 | i++; |
| 100 | if (v >> (8*sizeof(v)-7)) // v about to overflow |
| 101 | BERDecodeError(); |
| 102 | v <<= 7; |
| 103 | v += b & 0x7f; |
| 104 | if (!(b & 0x80)) |
| 105 | return i; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | OID::encode(CryptoPP::BufferedTransformation &out) const |
| 111 | { |
| 112 | assert(oid_.size() >= 2); |
| 113 | ByteQueue temp; |
| 114 | temp.Put(byte(oid_[0] * 40 + oid_[1])); |
| 115 | for (size_t i=2; i<oid_.size(); i++) |
| 116 | EncodeValue(temp, oid_[i]); |
| 117 | out.Put(OBJECT_IDENTIFIER); |
| 118 | DERLengthEncode(out, temp.CurrentSize()); |
| 119 | temp.TransferTo(out); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | OID::decode(CryptoPP::BufferedTransformation &in) |
| 124 | { |
| 125 | byte b; |
| 126 | if (!in.Get(b) || b != OBJECT_IDENTIFIER) |
| 127 | BERDecodeError(); |
| 128 | |
| 129 | size_t length; |
| 130 | if (!BERLengthDecode(in, length) || length < 1) |
| 131 | BERDecodeError(); |
| 132 | |
| 133 | if (!in.Get(b)) |
| 134 | BERDecodeError(); |
| 135 | |
| 136 | length--; |
| 137 | oid_.resize(2); |
| 138 | oid_[0] = b / 40; |
| 139 | oid_[1] = b % 40; |
| 140 | |
| 141 | while (length > 0) |
| 142 | { |
| 143 | word32 v; |
| 144 | size_t valueLen = DecodeValue(in, v); |
| 145 | if (valueLen > length) |
| 146 | BERDecodeError(); |
| 147 | oid_.push_back(v); |
| 148 | length -= valueLen; |
| 149 | } |
| 150 | } |
| 151 | |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 152 | } |