]>
git.ipfire.org Git - thirdparty/linux.git/blob - lib/oid_registry.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* ASN.1 Object identifier (OID) registry
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/module.h>
9 #include <linux/export.h>
10 #include <linux/oid_registry.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/bug.h>
14 #include <linux/asn1.h>
15 #include "oid_registry_data.c"
17 MODULE_DESCRIPTION("OID Registry");
18 MODULE_AUTHOR("Red Hat, Inc.");
19 MODULE_LICENSE("GPL");
22 * look_up_OID - Find an OID registration for the specified data
23 * @data: Binary representation of the OID
24 * @datasize: Size of the binary representation
26 enum OID
look_up_OID(const void *data
, size_t datasize
)
28 const unsigned char *octets
= data
;
31 unsigned i
, j
, k
, hash
;
34 /* Hash the OID data */
37 for (i
= 0; i
< datasize
; i
++)
38 hash
+= octets
[i
] * 33;
39 hash
= (hash
>> 24) ^ (hash
>> 16) ^ (hash
>> 8) ^ hash
;
42 /* Binary search the OID registry. OIDs are stored in ascending order
43 * of hash value then ascending order of size and then in ascending
44 * order of reverse value.
51 xhash
= oid_search_table
[j
].hash
;
61 oid
= oid_search_table
[j
].oid
;
62 len
= oid_index
[oid
+ 1] - oid_index
[oid
];
72 /* Variation is most likely to be at the tail end of the
73 * OID, so do the comparison in reverse.
76 unsigned char a
= oid_data
[oid_index
[oid
] + --len
];
77 unsigned char b
= octets
[len
];
94 EXPORT_SYMBOL_GPL(look_up_OID
);
97 * parse_OID - Parse an OID from a bytestream
98 * @data: Binary representation of the header + OID
99 * @datasize: Size of the binary representation
100 * @oid: Pointer to oid to return result
102 * Parse an OID from a bytestream that holds the OID in the format
103 * ASN1_OID | length | oid. The length indicator must equal to datasize - 2.
104 * -EBADMSG is returned if the bytestream is too short.
106 int parse_OID(const void *data
, size_t datasize
, enum OID
*oid
)
108 const unsigned char *v
= data
;
110 /* we need 2 bytes of header and at least 1 byte for oid */
111 if (datasize
< 3 || v
[0] != ASN1_OID
|| v
[1] != datasize
- 2)
114 *oid
= look_up_OID(data
+ 2, datasize
- 2);
117 EXPORT_SYMBOL_GPL(parse_OID
);
120 * sprint_OID - Print an Object Identifier into a buffer
121 * @data: The encoded OID to print
122 * @datasize: The size of the encoded OID
123 * @buffer: The buffer to render into
124 * @bufsize: The size of the buffer
126 * The OID is rendered into the buffer in "a.b.c.d" format and the number of
127 * bytes is returned. -EBADMSG is returned if the data could not be interpreted
128 * and -ENOBUFS if the buffer was too small.
130 int sprint_oid(const void *data
, size_t datasize
, char *buffer
, size_t bufsize
)
132 const unsigned char *v
= data
, *end
= v
+ datasize
;
142 ret
= count
= snprintf(buffer
, bufsize
, "%u.%u", n
/ 40, n
% 40);
143 if (count
>= bufsize
)
162 ret
+= count
= snprintf(buffer
, bufsize
, ".%lu", num
);
163 if (count
>= bufsize
)
172 snprintf(buffer
, bufsize
, "(bad)");
175 EXPORT_SYMBOL_GPL(sprint_oid
);
178 * sprint_OID - Print an Object Identifier into a buffer
179 * @oid: The OID to print
180 * @buffer: The buffer to render into
181 * @bufsize: The size of the buffer
183 * The OID is rendered into the buffer in "a.b.c.d" format and the number of
186 int sprint_OID(enum OID oid
, char *buffer
, size_t bufsize
)
190 BUG_ON(oid
>= OID__NR
);
192 ret
= sprint_oid(oid_data
+ oid_index
[oid
],
193 oid_index
[oid
+ 1] - oid_index
[oid
],
195 BUG_ON(ret
== -EBADMSG
);
198 EXPORT_SYMBOL_GPL(sprint_OID
);