Botan 3.7.1
Crypto and TLS for C&
asn1_oid.cpp
Go to the documentation of this file.
1/*
2* ASN.1 OID
3* (C) 1999-2007,2024 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/asn1_obj.h>
9
10#include <botan/ber_dec.h>
11#include <botan/der_enc.h>
12#include <botan/internal/bit_ops.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/int_utils.h>
15#include <botan/internal/oid_map.h>
16#include <botan/internal/parsing.h>
17#include <botan/internal/stl_util.h>
18#include <algorithm>
19#include <span>
20#include <sstream>
21
22namespace Botan {
23
24namespace {
25
26void oid_valid_check(std::span<const uint32_t> oid) {
27 BOTAN_ARG_CHECK(oid.size() >= 2, "OID too short to be valid");
28 BOTAN_ARG_CHECK(oid[0] <= 2, "OID root out of range");
29 BOTAN_ARG_CHECK(oid[1] <= 39 || oid[0] == 2, "OID second arc too large");
30 // This last is a limitation of using 32 bit integers when decoding
31 // not a limitation of ASN.1 object identifiers in general
32 BOTAN_ARG_CHECK(oid[1] <= 0xFFFFFFAF, "OID second arc too large");
33}
34
35// returns empty on invalid
36std::vector<uint32_t> parse_oid_str(std::string_view oid) {
37 try {
38 std::string elem;
39 std::vector<uint32_t> oid_elems;
40
41 for(char c : oid) {
42 if(c == '.') {
43 if(elem.empty()) {
44 return std::vector<uint32_t>();
45 }
46 oid_elems.push_back(to_u32bit(elem));
47 elem.clear();
48 } else {
49 elem += c;
50 }
51 }
52
53 if(!elem.empty()) {
54 oid_elems.push_back(to_u32bit(elem));
55 }
56
57 return oid_elems;
58 } catch(Invalid_Argument&) {
59 // thrown by to_u32bit
60 return std::vector<uint32_t>();
61 }
62}
63
64} // namespace
65
66//static
67void OID::register_oid(const OID& oid, std::string_view name) {
69}
70
71//static
72std::optional<OID> OID::from_name(std::string_view name) {
73 if(name.empty()) {
74 throw Invalid_Argument("OID::from_name argument must be non-empty");
75 }
76
78 if(o.has_value()) {
79 return std::optional(o);
80 }
81
82 return std::nullopt;
83}
84
85//static
86OID OID::from_string(std::string_view str) {
87 if(str.empty()) {
88 throw Invalid_Argument("OID::from_string argument must be non-empty");
89 }
90
92 if(o.has_value()) {
93 return o;
94 }
95
96 // Try to parse as a dotted decimal
97 try {
98 return OID(str);
99 } catch(...) {}
100
101 throw Lookup_Error(fmt("No OID associated with name '{}'", str));
102}
103
104OID::OID(std::initializer_list<uint32_t> init) : m_id(init) {
105 oid_valid_check(m_id);
106}
107
108OID::OID(std::vector<uint32_t>&& init) : m_id(std::move(init)) {
109 oid_valid_check(m_id);
110}
111
112/*
113* ASN.1 OID Constructor
114*/
115OID::OID(std::string_view oid_str) {
116 if(!oid_str.empty()) {
117 m_id = parse_oid_str(oid_str);
118 oid_valid_check(m_id);
119 }
120}
121
122/*
123* Return this OID as a string
124*/
125std::string OID::to_string() const {
126 std::ostringstream out;
127
128 for(size_t i = 0; i != m_id.size(); ++i) {
129 // avoid locale issues with integer formatting
130 out << std::to_string(m_id[i]);
131 if(i != m_id.size() - 1) {
132 out << ".";
133 }
134 }
135
136 return out.str();
137}
138
139std::string OID::to_formatted_string() const {
140 std::string s = this->human_name_or_empty();
141 if(!s.empty()) {
142 return s;
143 }
144 return this->to_string();
145}
146
147std::string OID::human_name_or_empty() const {
148 return OID_Map::global_registry().oid2str(*this);
149}
150
152 return !human_name_or_empty().empty();
153}
154
155size_t OID::hash_code() const {
156 constexpr uint64_t mod = 0xffffffffffffffc5;
157 uint64_t hash = 0;
158 for(auto id : m_id) {
159 hash = (hash * 257 + id) % mod;
160 }
161 return static_cast<size_t>(hash);
162}
163
164/*
165* Compare two OIDs
166*/
167bool operator<(const OID& a, const OID& b) {
168 const std::vector<uint32_t>& oid1 = a.get_components();
169 const std::vector<uint32_t>& oid2 = b.get_components();
170
171 return std::lexicographical_compare(oid1.begin(), oid1.end(), oid2.begin(), oid2.end());
172}
173
174/*
175* DER encode an OBJECT IDENTIFIER
176*/
178 if(m_id.size() < 2) {
179 throw Invalid_Argument("OID::encode_into: OID is invalid");
180 }
181
182 auto append = [](std::vector<uint8_t>& encoding, uint32_t z) {
183 if(z <= 0x7F) {
184 encoding.push_back(static_cast<uint8_t>(z));
185 } else {
186 size_t z7 = (high_bit(z) + 7 - 1) / 7;
187
188 for(size_t j = 0; j != z7; ++j) {
189 uint8_t zp = static_cast<uint8_t>(z >> (7 * (z7 - j - 1)) & 0x7F);
190
191 if(j != z7 - 1) {
192 zp |= 0x80;
193 }
194
195 encoding.push_back(zp);
196 }
197 }
198 };
199
200 std::vector<uint8_t> encoding;
201
202 // We know 40 * root can't overflow because root is between 0 and 2
203 auto first = BOTAN_ASSERT_IS_SOME(checked_add(40 * m_id[0], m_id[1]));
204
205 append(encoding, first);
206
207 for(size_t i = 2; i != m_id.size(); ++i) {
208 append(encoding, m_id[i]);
209 }
211}
212
213/*
214* Decode a BER encoded OBJECT IDENTIFIER
215*/
217 BER_Object obj = decoder.get_next_object();
219 throw BER_Bad_Tag("Error decoding OID, unknown tag", obj.tagging());
220 }
221
222 if(obj.length() == 0) {
223 throw BER_Decoding_Error("OID encoding is too short");
224 }
225
226 auto consume = [](BufferSlicer& data) -> uint32_t {
227 BOTAN_ASSERT_NOMSG(!data.empty());
228 uint32_t b = data.take_byte();
229
230 if(b > 0x7F) {
231 b &= 0x7F;
232
233 // Even BER requires that the OID have minimal length, ie that
234 // the first byte of a multibyte encoding cannot be zero
235 // See X.690 section 8.19.2
236 if(b == 0) {
237 throw Decoding_Error("Leading zero byte in multibyte OID encoding");
238 }
239
240 while(true) {
241 if(data.empty()) {
242 throw Decoding_Error("Truncated OID value");
243 }
244
245 const uint8_t next = data.take_byte();
246 const bool more = (next & 0x80);
247 const uint8_t value = next & 0x7F;
248
249 if((b >> (32 - 7)) != 0) {
250 throw Decoding_Error("OID component overflow");
251 }
252
253 b = (b << 7) | value;
254
255 if(!more) {
256 break;
257 }
258 }
259 }
260
261 return b;
262 };
263
264 BufferSlicer data(obj.data());
265 std::vector<uint32_t> parts;
266 while(!data.empty()) {
267 const uint32_t comp = consume(data);
268
269 if(parts.empty()) {
270 // divide into root and second arc
271
272 const uint32_t root_arc = [](uint32_t b0) -> uint32_t {
273 if(b0 < 40) {
274 return 0;
275 } else if(b0 < 80) {
276 return 1;
277 } else {
278 return 2;
279 }
280 }(comp);
281
282 parts.push_back(root_arc);
283 BOTAN_ASSERT_NOMSG(comp >= 40 * root_arc);
284 parts.push_back(comp - 40 * root_arc);
285 } else {
286 parts.push_back(comp);
287 }
288 }
289
290 m_id = parts;
291}
292
293} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
BER_Object get_next_object()
Definition ber_dec.cpp:245
size_t length() const
Definition asn1_obj.h:153
uint32_t tagging() const
Definition asn1_obj.h:141
std::span< const uint8_t > data() const
Definition asn1_obj.h:155
bool empty() const
Definition stl_util.h:129
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
Definition der_enc.cpp:222
std::string oid2str(const OID &oid)
Definition oid_map.cpp:53
void add_oid(const OID &oid, std::string_view str)
Definition oid_map.cpp:21
static OID_Map & global_registry()
Definition oid_map.cpp:16
OID str2oid(std::string_view str)
Definition oid_map.cpp:64
void encode_into(DER_Encoder &) const override
Definition asn1_oid.cpp:177
std::string to_formatted_string() const
Definition asn1_oid.cpp:139
bool registered_oid() const
Definition asn1_oid.cpp:151
const std::vector< uint32_t > & get_components() const
Definition asn1_obj.h:317
static std::optional< OID > from_name(std::string_view name)
Definition asn1_oid.cpp:72
static void register_oid(const OID &oid, std::string_view name)
Definition asn1_oid.cpp:67
std::string human_name_or_empty() const
Definition asn1_oid.cpp:147
size_t hash_code() const
Definition asn1_oid.cpp:155
OID()=default
bool has_value() const
Definition asn1_obj.h:272
void decode_from(BER_Decoder &) override
Definition asn1_oid.cpp:216
std::string to_string() const
Definition asn1_oid.cpp:125
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:86
int(* init)(CTX *)
std::string name
uint32_t to_u32bit(std::string_view str_view)
Definition parsing.cpp:32
constexpr std::optional< T > checked_add(T a, T b)
Definition int_utils.h:19
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
bool operator<(const OID &a, const OID &b)
Definition asn1_oid.cpp:167
constexpr size_t high_bit(T n)
Definition bit_ops.h:61
const SIMD_8x32 & b
#define BOTAN_ASSERT_IS_SOME(v)
Definition stl_util.h:398