oid_registry.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include <linux/export.h>
  12. #include <linux/oid_registry.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/bug.h>
  16. #include "oid_registry_data.c"
  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. return -EBADMSG;
  104. n = *v++;
  105. ret = count = snprintf(buffer, bufsize, "%u.%u", n / 40, n % 40);
  106. buffer += count;
  107. bufsize -= count;
  108. if (bufsize == 0)
  109. return -ENOBUFS;
  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. return -EBADMSG;
  120. n = *v++;
  121. num <<= 7;
  122. num |= n & 0x7f;
  123. } while (n & 0x80);
  124. }
  125. ret += count = snprintf(buffer, bufsize, ".%lu", num);
  126. buffer += count;
  127. bufsize -= count;
  128. if (bufsize == 0)
  129. return -ENOBUFS;
  130. }
  131. return ret;
  132. }
  133. EXPORT_SYMBOL_GPL(sprint_oid);
  134. /**
  135. * sprint_OID - Print an Object Identifier into a buffer
  136. * @oid: The OID to print
  137. * @buffer: The buffer to render into
  138. * @bufsize: The size of the buffer
  139. *
  140. * The OID is rendered into the buffer in "a.b.c.d" format and the number of
  141. * bytes is returned.
  142. */
  143. int sprint_OID(enum OID oid, char *buffer, size_t bufsize)
  144. {
  145. int ret;
  146. BUG_ON(oid >= OID__NR);
  147. ret = sprint_oid(oid_data + oid_index[oid],
  148. oid_index[oid + 1] - oid_index[oid],
  149. buffer, bufsize);
  150. BUG_ON(ret == -EBADMSG);
  151. return ret;
  152. }
  153. EXPORT_SYMBOL_GPL(sprint_OID);