oid_registry.c 3.7 KB

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