Coverage Report

Created: 2022-11-24 06:56

/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
720k
   {
23
720k
   try
24
720k
      {
25
720k
      std::string elem;
26
720k
      std::vector<uint32_t> oid_elems;
27
28
720k
      for(char c : oid)
29
6.83M
         {
30
6.83M
         if(c == '.')
31
2.37M
            {
32
2.37M
            if(elem.empty())
33
0
               return std::vector<uint32_t>();
34
2.37M
            oid_elems.push_back(to_u32bit(elem));
35
2.37M
            elem.clear();
36
2.37M
            }
37
4.46M
         else
38
4.46M
            {
39
4.46M
            elem += c;
40
4.46M
            }
41
6.83M
         }
42
43
720k
      if(elem.empty())
44
0
         return std::vector<uint32_t>();
45
720k
      oid_elems.push_back(to_u32bit(elem));
46
47
720k
      if(oid_elems.size() < 2)
48
0
         return std::vector<uint32_t>();
49
50
720k
      return oid_elems;
51
720k
      }
52
720k
   catch(Invalid_Argument&) // thrown by to_u32bit
53
720k
      {
54
0
      return std::vector<uint32_t>();
55
0
      }
56
720k
   }
57
58
}
59
60
//static
61
OID OID::from_string(const std::string& str)
62
22.9k
   {
63
22.9k
   if(str.empty())
64
0
      throw Invalid_Argument("OID::from_string argument must be non-empty");
65
66
22.9k
   OID o = OIDS::str2oid_or_empty(str);
67
22.9k
   if(o.has_value())
68
22.9k
      return o;
69
70
0
   std::vector<uint32_t> raw = parse_oid_str(str);
71
72
0
   if(!raw.empty())
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
720k
   {
83
720k
   if(!oid_str.empty())
84
720k
      {
85
720k
      m_id = parse_oid_str(oid_str);
86
87
720k
      if(m_id.size() < 2 || m_id[0] > 2)
88
0
         throw Decoding_Error("Invalid OID " + oid_str);
89
720k
      if((m_id[0] == 0 || m_id[0] == 1) && m_id[1] > 39)
90
0
         throw Decoding_Error("Invalid OID " + oid_str);
91
720k
      }
92
720k
   }
93
94
/*
95
* Return this OID as a string
96
*/
97
std::string OID::to_string() const
98
119k
   {
99
119k
   std::string out;
100
957k
   for(size_t i = 0; i != m_id.size(); ++i)
101
837k
      {
102
837k
      out += std::to_string(m_id[i]);
103
837k
      if(i != m_id.size() - 1)
104
718k
         out += ".";
105
837k
      }
106
119k
   return out;
107
119k
   }
108
109
std::string OID::to_formatted_string() const
110
32.7k
   {
111
32.7k
   std::string s = OIDS::oid2str_or_empty(*this);
112
32.7k
   if(!s.empty())
113
25.9k
      return s;
114
6.74k
   return this->to_string();
115
32.7k
   }
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
865k
   {
132
865k
   const std::vector<uint32_t>& oid1 = a.get_components();
133
865k
   const std::vector<uint32_t>& oid2 = b.get_components();
134
135
865k
   return std::lexicographical_compare(oid1.begin(), oid1.end(),
136
865k
                                       oid2.begin(), oid2.end());
137
865k
   }
138
139
/*
140
* DER encode an OBJECT IDENTIFIER
141
*/
142
void OID::encode_into(DER_Encoder& der) const
143
14.0k
   {
144
14.0k
   if(m_id.size() < 2)
145
0
      throw Invalid_Argument("OID::encode_into: OID is invalid");
146
147
14.0k
   std::vector<uint8_t> encoding;
148
149
14.0k
   if(m_id[0] > 2 || m_id[1] >= 40)
150
438
      throw Encoding_Error("Invalid OID prefix, cannot encode");
151
152
13.5k
   encoding.push_back(static_cast<uint8_t>(40 * m_id[0] + m_id[1]));
153
154
79.4k
   for(size_t i = 2; i != m_id.size(); ++i)
155
65.8k
      {
156
65.8k
      if(m_id[i] == 0)
157
298
         encoding.push_back(0);
158
65.6k
      else
159
65.6k
         {
160
65.6k
         size_t blocks = high_bit(m_id[i]) + 6;
161
65.6k
         blocks = (blocks - (blocks % 7)) / 7;
162
163
65.6k
         BOTAN_ASSERT(blocks > 0, "Math works");
164
165
103k
         for(size_t j = 0; j != blocks - 1; ++j)
166
37.6k
            encoding.push_back(0x80 | ((m_id[i] >> 7*(blocks-j-1)) & 0x7F));
167
65.6k
         encoding.push_back(m_id[i] & 0x7F);
168
65.6k
         }
169
65.8k
      }
170
13.5k
   der.add_object(ASN1_Type::ObjectId, ASN1_Class::Universal, encoding);
171
13.5k
   }
172
173
/*
174
* Decode a BER encoded OBJECT IDENTIFIER
175
*/
176
void OID::decode_from(BER_Decoder& decoder)
177
286k
   {
178
286k
   BER_Object obj = decoder.get_next_object();
179
286k
   if(obj.tagging() != (ASN1_Class::Universal | ASN1_Type::ObjectId))
180
1.52k
       throw BER_Bad_Tag("Error decoding OID, unknown tag", obj.tagging());
181
182
285k
   const size_t length = obj.length();
183
285k
   const uint8_t* bits = obj.bits();
184
185
285k
   if(length < 2 && !(length == 1 && bits[0] == 0))
186
850
      {
187
850
      throw BER_Decoding_Error("OID encoding is too short");
188
850
      }
189
190
284k
   m_id.clear();
191
284k
   m_id.push_back(bits[0] / 40);
192
284k
   m_id.push_back(bits[0] % 40);
193
194
284k
   size_t i = 0;
195
1.48M
   while(i != length - 1)
196
1.19M
      {
197
1.19M
      uint32_t component = 0;
198
1.52M
      while(i != length - 1)
199
1.48M
         {
200
1.48M
         ++i;
201
202
1.48M
         if(component >> (32-7))
203
486
            throw Decoding_Error("OID component overflow");
204
205
1.48M
         component = (component << 7) + (bits[i] & 0x7F);
206
207
1.48M
         if(!(bits[i] & 0x80))
208
1.15M
            break;
209
1.48M
         }
210
1.19M
      m_id.push_back(component);
211
1.19M
      }
212
284k
   }
213
214
}