oid_registry.c 4.4 KB

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