Skip to content

Commit 4f73175

Browse files
dhowellsrustyrussell
authored andcommitted
X.509: Add utility functions to render OIDs as strings
Add a pair of utility functions to render OIDs as strings. The first takes an encoded OID and turns it into a "a.b.c.d" form string: int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize); The second takes an OID enum index and calls the first on the data held therein: int sprint_OID(enum OID oid, char *buffer, size_t bufsize); Signed-off-by: David Howells <[email protected]> Signed-off-by: Rusty Russell <[email protected]>
1 parent a77ad6e commit 4f73175

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

include/linux/oid_registry.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,7 @@ enum OID {
8686
};
8787

8888
extern enum OID look_up_OID(const void *data, size_t datasize);
89+
extern int sprint_oid(const void *, size_t, char *, size_t);
90+
extern int sprint_OID(enum OID, char *, size_t);
8991

9092
#endif /* _LINUX_OID_REGISTRY_H */

lib/oid_registry.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
#include <linux/export.h>
1313
#include <linux/oid_registry.h>
14+
#include <linux/kernel.h>
15+
#include <linux/errno.h>
16+
#include <linux/bug.h>
1417
#include "oid_registry_data.c"
1518

1619
/**
@@ -87,3 +90,81 @@ enum OID look_up_OID(const void *data, size_t datasize)
8790
return OID__NR;
8891
}
8992
EXPORT_SYMBOL_GPL(look_up_OID);
93+
94+
/*
95+
* sprint_OID - Print an Object Identifier into a buffer
96+
* @data: The encoded OID to print
97+
* @datasize: The size of the encoded OID
98+
* @buffer: The buffer to render into
99+
* @bufsize: The size of the buffer
100+
*
101+
* The OID is rendered into the buffer in "a.b.c.d" format and the number of
102+
* bytes is returned. -EBADMSG is returned if the data could not be intepreted
103+
* and -ENOBUFS if the buffer was too small.
104+
*/
105+
int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize)
106+
{
107+
const unsigned char *v = data, *end = v + datasize;
108+
unsigned long num;
109+
unsigned char n;
110+
size_t ret;
111+
int count;
112+
113+
if (v >= end)
114+
return -EBADMSG;
115+
116+
n = *v++;
117+
ret = count = snprintf(buffer, bufsize, "%u.%u", n / 40, n % 40);
118+
buffer += count;
119+
bufsize -= count;
120+
if (bufsize == 0)
121+
return -ENOBUFS;
122+
123+
while (v < end) {
124+
num = 0;
125+
n = *v++;
126+
if (!(n & 0x80)) {
127+
num = n;
128+
} else {
129+
num = n & 0x7f;
130+
do {
131+
if (v >= end)
132+
return -EBADMSG;
133+
n = *v++;
134+
num <<= 7;
135+
num |= n & 0x7f;
136+
} while (n & 0x80);
137+
}
138+
ret += count = snprintf(buffer, bufsize, ".%lu", num);
139+
buffer += count;
140+
bufsize -= count;
141+
if (bufsize == 0)
142+
return -ENOBUFS;
143+
}
144+
145+
return ret;
146+
}
147+
EXPORT_SYMBOL_GPL(sprint_oid);
148+
149+
/**
150+
* sprint_OID - Print an Object Identifier into a buffer
151+
* @oid: The OID to print
152+
* @buffer: The buffer to render into
153+
* @bufsize: The size of the buffer
154+
*
155+
* The OID is rendered into the buffer in "a.b.c.d" format and the number of
156+
* bytes is returned.
157+
*/
158+
int sprint_OID(enum OID oid, char *buffer, size_t bufsize)
159+
{
160+
int ret;
161+
162+
BUG_ON(oid >= OID__NR);
163+
164+
ret = sprint_oid(oid_data + oid_index[oid],
165+
oid_index[oid + 1] - oid_index[oid],
166+
buffer, bufsize);
167+
BUG_ON(ret == -EBADMSG);
168+
return ret;
169+
}
170+
EXPORT_SYMBOL_GPL(sprint_OID);

0 commit comments

Comments
 (0)