Coverage Report

Created: 2022-01-14 08:07

/src/botan/src/lib/asn1/asn1_oid.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* ASN.1 OID
3
* (C) 1999-2007 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/asn1_obj.h>
9
#include <botan/der_enc.h>
10
#include <botan/ber_dec.h>
11
#include <botan/internal/bit_ops.h>
12
#include <botan/internal/parsing.h>
13
#include <botan/oids.h>
14
#include <algorithm>
15
16
namespace Botan {
17
18
namespace {
19
20
// returns empty on invalid
21
std::vector<uint32_t> parse_oid_str(const std::string& oid)
22
666k
   {
23
666k
   try
24
666k
      {
25
666k
      std::string elem;
26
666k
      std::vector<uint32_t> oid_elems;
27
28
666k
      for(char c : oid)
29
6.32M
         {
30
6.32M
         if(c == '.')
31
2.19M
            {
32
2.19M
            if(elem.empty())
33
0
               return std::vector<uint32_t>();
34
2.19M
            oid_elems.push_back(to_u32bit(elem));
35
2.19M
            elem.clear();
36
2.19M
            }
37
4.12M
         else
38
4.12M
            {
39
4.12M
            elem += c;
40
4.12M
            }
41
6.32M
         }
42
43
666k
      if(elem.empty())
44
0
         return std::vector<uint32_t>();
45
666k
      oid_elems.push_back(to_u32bit(elem));
46
47
666k
      if(oid_elems.size() < 2)
48
0
         return std::vector<uint32_t>();
49
50
666k
      return oid_elems;
51
666k
      }
52
666k
   catch(Invalid_Argument&) // thrown by to_u32bit
53
666k
      {
54
0
      return std::vector<uint32_t>();
55
0
      }
56
666k
   }
57
58
}
59
60
//static
61
OID OID::from_string(const std::string& str)
62
20.4k
   {
63
20.4k
   if(str.empty())
64
0
      throw Invalid_Argument("OID::from_string argument must be non-empty");
65
66
20.4k
   const OID o = OIDS::str2oid_or_empty(str);
67
20.4k
   if(o.has_value())
68
20.4k
      return o;
69
70
0
   std::vector<uint32_t> raw = parse_oid_str(str);
71
72
0
   if(raw.size() > 0)
73
0
      return OID(std::move(raw));
74
75
0
   throw Lookup_Error("No OID associated with name " + str);
76
0
   }
77
78
/*
79
* ASN.1 OID Constructor
80
*/
81
OID::OID(const std::string& oid_str)
82
666k
   {
83
666k
   if(!oid_str.empty())
84
666k
      {
85
666k
      m_id = parse_oid_str(oid_str);
86
87
666k
      if(m_id.size() < 2 || m_id[0] > 2)
88
0
         throw Decoding_Error("Invalid OID " + oid_str);
89
666k
      if((m_id[0] == 0 || m_id[0] == 1) && m_id[1] > 39)
90
0
         throw Decoding_Error("Invalid OID " + oid_str);
91
666k
      }
92
666k
   }
93
94
/*
95
* Return this OID as a string
96
*/
97
std::string OID::to_string() const
98
108k
   {
99
108k
   std::string out;
100
874k
   for(size_t i = 0; i != m_id.size(); ++i)
101
765k
      {
102
765k
      out += std::to_string(m_id[i]);
103
765k
      if(i != m_id.size() - 1)
104
657k
         out += ".";
105
765k
      }
106
108k
   return out;
107
108k
   }
108
109
std::string OID::to_formatted_string() const
110
29.1k
   {
111
29.1k
   const std::string s = OIDS::oid2str_or_empty(*this);
112
29.1k
   if(!s.empty())
113
22.4k
      return s;
114
6.73k
   return this->to_string();
115
29.1k
   }
116
117
/*
118
* Append another component to the OID
119
*/
120
OID operator+(const OID& oid, uint32_t new_component)
121
0
   {
122
0
   std::vector<uint32_t> val = oid.get_components();
123
0
   val.push_back(new_component);
124
0
   return OID(std::move(val));
125
0
   }
126
127
/*
128
* Compare two OIDs
129
*/
130
bool operator<(const OID& a, const OID& b)
131
848k
   {
132
848k
   const std::vector<uint32_t>& oid1 = a.get_components();
133
848k
   const std::vector<uint32_t>& oid2 = b.get_components();
134
135
848k
   return std::lexicographical_compare(oid1.begin(), oid1.end(),
136
848k
                                       oid2.begin(), oid2.end());
137
848k
   }
138
139
/*
140
* DER encode an OBJECT IDENTIFIER
141
*/
142
void OID::encode_into(DER_Encoder& der) const
143
11.7k
   {
144
11.7k
   if(m_id.size() < 2)
145
0
      throw Invalid_Argument("OID::encode_into: OID is invalid");
146
147
11.7k
   std::vector<uint8_t> encoding;
148
149
11.7k
   if(m_id[0] > 2 || m_id[1] >= 40)
150
492
      throw Encoding_Error("Invalid OID prefix, cannot encode");
151
152
11.2k
   encoding.push_back(static_cast<uint8_t>(40 * m_id[0] + m_id[1]));
153
154
67.3k
   for(size_t i = 2; i != m_id.size(); ++i)
155
56.0k
      {
156
56.0k
      if(m_id[i] == 0)
157
209
         encoding.push_back(0);
158
55.8k
      else
159
55.8k
         {
160
55.8k
         size_t blocks = high_bit(m_id[i]) + 6;
161
55.8k
         blocks = (blocks - (blocks % 7)) / 7;
162
163
55.8k
         BOTAN_ASSERT(blocks > 0, "Math works");
164
165
87.2k
         for(size_t j = 0; j != blocks - 1; ++j)
166
31.3k
            encoding.push_back(0x80 | ((m_id[i] >> 7*(blocks-j-1)) & 0x7F));
167
55.8k
         encoding.push_back(m_id[i] & 0x7F);
168
55.8k
         }
169
56.0k
      }
170
11.2k
   der.add_object(ASN1_Type::ObjectId, ASN1_Class::Universal, encoding);
171
11.2k
   }
172
173
/*
174
* Decode a BER encoded OBJECT IDENTIFIER
175
*/
176
void OID::decode_from(BER_Decoder& decoder)
177
248k
   {
178
248k
   BER_Object obj = decoder.get_next_object();
179
248k
   if(obj.tagging() != (ASN1_Class::Universal | ASN1_Type::ObjectId))
180
1.18k
       throw BER_Bad_Tag("Error decoding OID, unknown tag", obj.tagging());
181
182
247k
   const size_t length = obj.length();
183
247k
   const uint8_t* bits = obj.bits();
184
185
247k
   if(length < 2 && !(length == 1 && bits[0] == 0))
186
788
      {
187
788
      throw BER_Decoding_Error("OID encoding is too short");
188
788
      }
189
190
246k
   m_id.clear();
191
246k
   m_id.push_back(bits[0] / 40);
192
246k
   m_id.push_back(bits[0] % 40);
193
194
246k
   size_t i = 0;
195
1.31M
   while(i != length - 1)
196
1.06M
      {
197
1.06M
      uint32_t component = 0;
198
1.35M
      while(i != length - 1)
199
1.31M
         {
200
1.31M
         ++i;
201
202
1.31M
         if(component >> (32-7))
203
444
            throw Decoding_Error("OID component overflow");
204
205
1.31M
         component = (component << 7) + (bits[i] & 0x7F);
206
207
1.31M
         if(!(bits[i] & 0x80))
208
1.02M
            break;
209
1.31M
         }
210
1.06M
      m_id.push_back(component);
211
1.06M
      }
212
246k
   }
213
214
}