oid_registry.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #ifdef __UBOOT__
  8. #include <linux/compat.h>
  9. #else
  10. #include <linux/module.h>
  11. #include <linux/export.h>
  12. #endif
  13. #include <linux/oid_registry.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/bug.h>
  17. #include "oid_registry_data.c"
  18. MODULE_DESCRIPTION("OID Registry");
  19. MODULE_AUTHOR("Red Hat, Inc.");
  20. MODULE_LICENSE("GPL");
  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. */
  26. enum 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. /* Hash the OID data */
  34. hash = datasize - 1;
  35. for (i = 0; i < datasize; i++)
  36. hash += octets[i] * 33;
  37. hash = (hash >> 24) ^ (hash >> 16) ^ (hash >> 8) ^ hash;
  38. hash &= 0xff;
  39. /* Binary search the OID registry. OIDs are stored in ascending order
  40. * of hash value then ascending order of size and then in ascending
  41. * order of reverse value.
  42. */
  43. i = 0;
  44. k = OID__NR;
  45. while (i < k) {
  46. j = (i + k) / 2;
  47. xhash = oid_search_table[j].hash;
  48. if (xhash > hash) {
  49. k = j;
  50. continue;
  51. }
  52. if (xhash < hash) {
  53. i = j + 1;
  54. continue;
  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. /* Variation is most likely to be at the tail end of the
  67. * OID, so do the comparison in reverse.
  68. */
  69. while (len > 0) {
  70. unsigned char a = oid_data[oid_index[oid] + --len];
  71. unsigned char b = octets[len];
  72. if (a > b) {
  73. k = j;
  74. goto next;
  75. }
  76. if (a < b) {
  77. i = j + 1;
  78. goto next;
  79. }
  80. }
  81. return oid;
  82. next:
  83. ;
  84. }
  85. return OID__NR;
  86. }
  87. EXPORT_SYMBOL_GPL(look_up_OID);
  88. /*
  89. * sprint_OID - Print an Object Identifier into a buffer
  90. * @data: The encoded OID to print
  91. * @datasize: The size of the encoded OID
  92. * @buffer: The buffer to render into
  93. * @bufsize: The size of the buffer
  94. *
  95. * The OID is rendered into the buffer in "a.b.c.d" format and the number of
  96. * bytes is returned. -EBADMSG is returned if the data could not be intepreted
  97. * and -ENOBUFS if the buffer was too small.
  98. */
  99. int sprint_oid(const void *data, size_t datasize, char *buffer, size_t bufsize)
  100. {
  101. const unsigned char *v = data, *end = v + datasize;
  102. unsigned long num;
  103. unsigned char n;
  104. size_t ret;
  105. int count;
  106. if (v >= end)
  107. goto bad;
  108. n = *v++;
  109. ret = count = snprintf(buffer, bufsize, "%u.%u", n / 40, n % 40);
  110. if (count >= bufsize)
  111. return -ENOBUFS;
  112. buffer += count;
  113. bufsize -= count;
  114. while (v < end) {
  115. num = 0;
  116. n = *v++;
  117. if (!(n & 0x80)) {
  118. num = n;
  119. } else {
  120. num = n & 0x7f;
  121. do {
  122. if (v >= end)
  123. goto bad;
  124. n = *v++;
  125. num <<= 7;
  126. num |= n & 0x7f;
  127. } while (n & 0x80);
  128. }
  129. ret += count = snprintf(buffer, bufsize, ".%lu", num);
  130. if (count >= bufsize)
  131. return -ENOBUFS;
  132. buffer += count;
  133. bufsize -= count;
  134. }
  135. return ret;
  136. bad:
  137. snprintf(buffer, bufsize, "(bad)");
  138. return -EBADMSG;
  139. }
  140. EXPORT_SYMBOL_GPL(sprint_oid);
  141. /**
  142. * sprint_OID - Print an Object Identifier into a buffer
  143. * @oid: The OID to print
  144. * @buffer: The buffer to render into
  145. * @bufsize: The size of the buffer
  146. *
  147. * The OID is rendered into the buffer in "a.b.c.d" format and the number of
  148. * bytes is returned.
  149. */
  150. int sprint_OID(enum OID oid, char *buffer, size_t bufsize)
  151. {
  152. int ret;
  153. BUG_ON(oid >= OID__NR);
  154. ret = sprint_oid(oid_data + oid_index[oid],
  155. oid_index[oid + 1] - oid_index[oid],
  156. buffer, bufsize);
  157. BUG_ON(ret == -EBADMSG);
  158. return ret;
  159. }
  160. EXPORT_SYMBOL_GPL(sprint_OID);