Botan 3.6.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
155/*
156* Compare two OIDs
157*/
158bool operator<(const OID& a, const OID& b) {
159 const std::vector<uint32_t>& oid1 = a.get_components();
160 const std::vector<uint32_t>& oid2 = b.get_components();
161
162 return std::lexicographical_compare(oid1.begin(), oid1.end(), oid2.begin(), oid2.end());
163}
164
165/*
166* DER encode an OBJECT IDENTIFIER
167*/
169 if(m_id.size() < 2) {
170 throw Invalid_Argument("OID::encode_into: OID is invalid");
171 }
172
173 auto append = [](std::vector<uint8_t>& encoding, uint32_t z) {
174 if(z <= 0x7F) {
175 encoding.push_back(static_cast<uint8_t>(z));
176 } else {
177 size_t z7 = (high_bit(z) + 7 - 1) / 7;
178
179 for(size_t j = 0; j != z7; ++j) {
180 uint8_t zp = static_cast<uint8_t>(z >> (7 * (z7 - j - 1)) & 0x7F);
181
182 if(j != z7 - 1) {
183 zp |= 0x80;
184 }
185
186 encoding.push_back(zp);
187 }
188 }
189 };
190
191 std::vector<uint8_t> encoding;
192
193 // We know 40 * root can't overflow because root is between 0 and 2
194 auto first = BOTAN_ASSERT_IS_SOME(checked_add(40 * m_id[0], m_id[1]));
195
196 append(encoding, first);
197
198 for(size_t i = 2; i != m_id.size(); ++i) {
199 append(encoding, m_id[i]);
200 }
202}
203
204/*
205* Decode a BER encoded OBJECT IDENTIFIER
206*/
208 BER_Object obj = decoder.get_next_object();
210 throw BER_Bad_Tag("Error decoding OID, unknown tag", obj.tagging());
211 }
212
213 if(obj.length() == 0) {
214 throw BER_Decoding_Error("OID encoding is too short");
215 }
216
217 auto consume = [](BufferSlicer& data) -> uint32_t {
218 BOTAN_ASSERT_NOMSG(!data.empty());
219 uint32_t b = data.take_byte();
220
221 if(b > 0x7F) {
222 b &= 0x7F;
223
224 // Even BER requires that the OID have minimal length, ie that
225 // the first byte of a multibyte encoding cannot be zero
226 // See X.690 section 8.19.2
227 if(b == 0) {
228 throw Decoding_Error("Leading zero byte in multibyte OID encoding");
229 }
230
231 while(true) {
232 if(data.empty()) {
233 throw Decoding_Error("Truncated OID value");
234 }
235
236 const uint8_t next = data.take_byte();
237 const bool more = (next & 0x80);
238 const uint8_t value = next & 0x7F;
239
240 if((b >> (32 - 7)) != 0) {
241 throw Decoding_Error("OID component overflow");
242 }
243
244 b = (b << 7) | value;
245
246 if(!more) {
247 break;
248 }
249 }
250 }
251
252 return b;
253 };
254
255 BufferSlicer data(obj.data());
256 std::vector<uint32_t> parts;
257 while(!data.empty()) {
258 const uint32_t comp = consume(data);
259
260 if(parts.empty()) {
261 // divide into root and second arc
262
263 const uint32_t root_arc = [](uint32_t b0) -> uint32_t {
264 if(b0 < 40) {
265 return 0;
266 } else if(b0 < 80) {
267 return 1;
268 } else {
269 return 2;
270 }
271 }(comp);
272
273 parts.push_back(root_arc);
274 BOTAN_ASSERT_NOMSG(comp >= 40 * root_arc);
275 parts.push_back(comp - 40 * root_arc);
276 } else {
277 parts.push_back(comp);
278 }
279 }
280
281 m_id = parts;
282}
283
284} // 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:56
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:69
void encode_into(DER_Encoder &) const override
Definition asn1_oid.cpp:168
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:309
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
OID()=default
bool has_value() const
Definition asn1_obj.h:272
void decode_from(BER_Decoder &) override
Definition asn1_oid.cpp:207
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:158
constexpr size_t high_bit(T n)
Definition bit_ops.h:58
const SIMD_8x32 & b
#define BOTAN_ASSERT_IS_SOME(v)
Definition stl_util.h:398