aboutsummaryrefslogtreecommitdiffstats
path: root/lib/oid_registry.c
diff options
context:
space:
mode:
authorDavid Howells <dhowells@redhat.com>2012-09-21 18:30:46 -0400
committerRusty Russell <rusty@rustcorp.com.au>2012-10-07 23:20:18 -0400
commita77ad6ea0b0bb1f9d1f52ed494bd72a5fdde208e (patch)
tree96f3bcb637dc80c7a92611237ce4a9b3f6466387 /lib/oid_registry.c
parent0b1568a4536ff287a87908d7fc35c05bd7736a53 (diff)
X.509: Implement simple static OID registry
Implement a simple static OID registry that allows the mapping of an encoded OID to an enum value for ease of use. The OID registry index enum appears in the: linux/oid_registry.h header file. A script generates the registry from lines in the header file that look like: <sp*>OID_foo,<sp*>/*<sp*>1.2.3.4<sp*>*/ The actual OID is taken to be represented by the numbers with interpolated dots in the comment. All other lines in the header are ignored. The registry is queries by calling: OID look_up_oid(const void *data, size_t datasize); This returns a number from the registry enum representing the OID if found or OID__NR if not. Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'lib/oid_registry.c')
-rw-r--r--lib/oid_registry.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/oid_registry.c b/lib/oid_registry.c
new file mode 100644
index 000000000000..33cfd171f627
--- /dev/null
+++ b/lib/oid_registry.c
@@ -0,0 +1,89 @@
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/export.h>
13#include <linux/oid_registry.h>
14#include "oid_registry_data.c"
15
16/**
17 * look_up_OID - Find an OID registration for the specified data
18 * @data: Binary representation of the OID
19 * @datasize: Size of the binary representation
20 */
21enum OID look_up_OID(const void *data, size_t datasize)
22{
23 const unsigned char *octets = data;
24 enum OID oid;
25 unsigned char xhash;
26 unsigned i, j, k, hash;
27 size_t len;
28
29 /* Hash the OID data */
30 hash = datasize - 1;
31
32 for (i = 0; i < datasize; i++)
33 hash += octets[i] * 33;
34 hash = (hash >> 24) ^ (hash >> 16) ^ (hash >> 8) ^ hash;
35 hash &= 0xff;
36
37 /* Binary search the OID registry. OIDs are stored in ascending order
38 * of hash value then ascending order of size and then in ascending
39 * order of reverse value.
40 */
41 i = 0;
42 k = OID__NR;
43 while (i < k) {
44 j = (i + k) / 2;
45
46 xhash = oid_search_table[j].hash;
47 if (xhash > hash) {
48 k = j;
49 continue;
50 }
51 if (xhash < hash) {
52 i = j + 1;
53 continue;
54 }
55
56 oid = oid_search_table[j].oid;
57 len = oid_index[oid + 1] - oid_index[oid];
58 if (len > datasize) {
59 k = j;
60 continue;
61 }
62 if (len < datasize) {
63 i = j + 1;
64 continue;
65 }
66
67 /* Variation is most likely to be at the tail end of the
68 * OID, so do the comparison in reverse.
69 */
70 while (len > 0) {
71 unsigned char a = oid_data[oid_index[oid] + --len];
72 unsigned char b = octets[len];
73 if (a > b) {
74 k = j;
75 goto next;
76 }
77 if (a < b) {
78 i = j + 1;
79 goto next;
80 }
81 }
82 return oid;
83 next:
84 ;
85 }
86
87 return OID__NR;
88}
89EXPORT_SYMBOL_GPL(look_up_OID);