Loading...
1/* ASN.1 Object identifier (OID) registry
2 *
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/export.h>
14#include <linux/oid_registry.h>
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/bug.h>
18#include "oid_registry_data.c"
19
20MODULE_DESCRIPTION("OID Registry");
21MODULE_AUTHOR("Red Hat, Inc.");
22MODULE_LICENSE("GPL");
23
24/**
25 * look_up_OID - Find an OID registration for the specified data
26 * @data: Binary representation of the OID
27 * @datasize: Size of the binary representation
28 */
29enum OID look_up_OID(const void *data, size_t datasize)
30{
31 const unsigned char *octets = data;
32 enum OID oid;
33 unsigned char xhash;
34 unsigned i, j, k, hash;
35 size_t len;
36
37 /* Hash the OID data */
38 hash = datasize - 1;
39
40 for (i = 0; i < datasize; i++)
41 hash += octets[i] * 33;
42 hash = (hash >> 24) ^ (hash >> 16) ^ (hash >> 8) ^ hash;
43 hash &= 0xff;
44
45 /* Binary search the OID registry. OIDs are stored in ascending order
46 * of hash value then ascending order of size and then in ascending
47 * order of reverse value.
48 */
49 i = 0;
50 k = OID__NR;
51 while (i < k) {
52 j = (i + k) / 2;
53
54 xhash = oid_search_table[j].hash;
55 if (xhash > hash) {
56 k = j;
57 continue;
58 }
59 if (xhash < hash) {
60 i = j + 1;
61 continue;
62 }
63
64 oid = oid_search_table[j].oid;
65 len = oid_index[oid + 1] - oid_index[oid];
66 if (len > datasize) {
67 k = j;
68 continue;
69 }
70 if (len < datasize) {
71 i = j + 1;
72 continue;
73 }
74
75 /* Variation is most likely to be at the tail end of the
76 * OID, so do the comparison in reverse.
77 */
78 while (len > 0) {
79 unsigned char a = oid_data[oid_index[oid] + --len];
80 unsigned char b = octets[len];
81 if (a > b) {
82 k = j;
83 goto next;
84 }
85 if (a < b) {
86 i = j + 1;
87 goto next;
88 }
89 }
90 return oid;
91 next:
92 ;
93 }
94
95 return OID__NR;
96}
97EXPORT_SYMBOL_GPL(look_up_OID);
98
99/*
100 * sprint_OID - Print an Object Identifier into a buffer
101 * @data: The encoded OID to print
102 * @datasize: The size of the encoded OID
103 * @buffer: The buffer to render into
104 * @bufsize: The size of the buffer
105 *
106 * The OID is rendered into the buffer in "a.b.c.d" format and the number of
107 * bytes is returned. -EBADMSG is returned if the data could not be intepreted
108 * and -ENOBUFS if the buffer was too small.
109 */
110int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize)
111{
112 const unsigned char *v = data, *end = v + datasize;
113 unsigned long num;
114 unsigned char n;
115 size_t ret;
116 int count;
117
118 if (v >= end)
119 return -EBADMSG;
120
121 n = *v++;
122 ret = count = snprintf(buffer, bufsize, "%u.%u", n / 40, n % 40);
123 buffer += count;
124 bufsize -= count;
125 if (bufsize == 0)
126 return -ENOBUFS;
127
128 while (v < end) {
129 num = 0;
130 n = *v++;
131 if (!(n & 0x80)) {
132 num = n;
133 } else {
134 num = n & 0x7f;
135 do {
136 if (v >= end)
137 return -EBADMSG;
138 n = *v++;
139 num <<= 7;
140 num |= n & 0x7f;
141 } while (n & 0x80);
142 }
143 ret += count = snprintf(buffer, bufsize, ".%lu", num);
144 buffer += count;
145 bufsize -= count;
146 if (bufsize == 0)
147 return -ENOBUFS;
148 }
149
150 return ret;
151}
152EXPORT_SYMBOL_GPL(sprint_oid);
153
154/**
155 * sprint_OID - Print an Object Identifier into a buffer
156 * @oid: The OID to print
157 * @buffer: The buffer to render into
158 * @bufsize: The size of the buffer
159 *
160 * The OID is rendered into the buffer in "a.b.c.d" format and the number of
161 * bytes is returned.
162 */
163int sprint_OID(enum OID oid, char *buffer, size_t bufsize)
164{
165 int ret;
166
167 BUG_ON(oid >= OID__NR);
168
169 ret = sprint_oid(oid_data + oid_index[oid],
170 oid_index[oid + 1] - oid_index[oid],
171 buffer, bufsize);
172 BUG_ON(ret == -EBADMSG);
173 return ret;
174}
175EXPORT_SYMBOL_GPL(sprint_OID);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* ASN.1 Object identifier (OID) registry
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
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"
16
17MODULE_DESCRIPTION("OID Registry");
18MODULE_AUTHOR("Red Hat, Inc.");
19MODULE_LICENSE("GPL");
20
21/**
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
25 */
26enum OID look_up_OID(const void *data, size_t datasize)
27{
28 const unsigned char *octets = data;
29 enum OID oid;
30 unsigned char xhash;
31 unsigned i, j, k, hash;
32 size_t len;
33
34 /* Hash the OID data */
35 hash = datasize - 1;
36
37 for (i = 0; i < datasize; i++)
38 hash += octets[i] * 33;
39 hash = (hash >> 24) ^ (hash >> 16) ^ (hash >> 8) ^ hash;
40 hash &= 0xff;
41
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.
45 */
46 i = 0;
47 k = OID__NR;
48 while (i < k) {
49 j = (i + k) / 2;
50
51 xhash = oid_search_table[j].hash;
52 if (xhash > hash) {
53 k = j;
54 continue;
55 }
56 if (xhash < hash) {
57 i = j + 1;
58 continue;
59 }
60
61 oid = oid_search_table[j].oid;
62 len = oid_index[oid + 1] - oid_index[oid];
63 if (len > datasize) {
64 k = j;
65 continue;
66 }
67 if (len < datasize) {
68 i = j + 1;
69 continue;
70 }
71
72 /* Variation is most likely to be at the tail end of the
73 * OID, so do the comparison in reverse.
74 */
75 while (len > 0) {
76 unsigned char a = oid_data[oid_index[oid] + --len];
77 unsigned char b = octets[len];
78 if (a > b) {
79 k = j;
80 goto next;
81 }
82 if (a < b) {
83 i = j + 1;
84 goto next;
85 }
86 }
87 return oid;
88 next:
89 ;
90 }
91
92 return OID__NR;
93}
94EXPORT_SYMBOL_GPL(look_up_OID);
95
96/**
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
101 *
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.
105 */
106int parse_OID(const void *data, size_t datasize, enum OID *oid)
107{
108 const unsigned char *v = data;
109
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)
112 return -EBADMSG;
113
114 *oid = look_up_OID(data + 2, datasize - 2);
115 return 0;
116}
117EXPORT_SYMBOL_GPL(parse_OID);
118
119/*
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
125 *
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.
129 */
130int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize)
131{
132 const unsigned char *v = data, *end = v + datasize;
133 unsigned long num;
134 unsigned char n;
135 size_t ret;
136 int count;
137
138 if (v >= end)
139 goto bad;
140
141 n = *v++;
142 ret = count = snprintf(buffer, bufsize, "%u.%u", n / 40, n % 40);
143 if (count >= bufsize)
144 return -ENOBUFS;
145 buffer += count;
146 bufsize -= count;
147
148 while (v < end) {
149 n = *v++;
150 if (!(n & 0x80)) {
151 num = n;
152 } else {
153 num = n & 0x7f;
154 do {
155 if (v >= end)
156 goto bad;
157 n = *v++;
158 num <<= 7;
159 num |= n & 0x7f;
160 } while (n & 0x80);
161 }
162 ret += count = snprintf(buffer, bufsize, ".%lu", num);
163 if (count >= bufsize)
164 return -ENOBUFS;
165 buffer += count;
166 bufsize -= count;
167 }
168
169 return ret;
170
171bad:
172 snprintf(buffer, bufsize, "(bad)");
173 return -EBADMSG;
174}
175EXPORT_SYMBOL_GPL(sprint_oid);
176
177/**
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
182 *
183 * The OID is rendered into the buffer in "a.b.c.d" format and the number of
184 * bytes is returned.
185 */
186int sprint_OID(enum OID oid, char *buffer, size_t bufsize)
187{
188 int ret;
189
190 BUG_ON(oid >= OID__NR);
191
192 ret = sprint_oid(oid_data + oid_index[oid],
193 oid_index[oid + 1] - oid_index[oid],
194 buffer, bufsize);
195 BUG_ON(ret == -EBADMSG);
196 return ret;
197}
198EXPORT_SYMBOL_GPL(sprint_OID);