validate.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2009 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /* validate.c - syntax validation helper functions */
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <sys/types.h>
  44. #include "syntax.h"
  45. /* Helper function for processing a 'keystring'.
  46. *
  47. * Returns 0 is the value between begin and end is a valid 'keystring'.
  48. * Returns non-zero if the value is not a valide 'keystring'.
  49. */
  50. int keystring_validate(
  51. const char *begin,
  52. const char *end
  53. )
  54. {
  55. int rc = 0; /* assume the value is valid */
  56. const char *p = begin;
  57. if ((begin == NULL) || (end == NULL)) {
  58. rc = 1;
  59. goto exit;
  60. }
  61. /* Per RFC4512:
  62. *
  63. * keystring = leadkeychar *keychar
  64. */
  65. if (IS_LEADKEYCHAR(*p)) {
  66. for (p++; p <= end; p++) {
  67. if (!IS_KEYCHAR(*p)) {
  68. rc = 1;
  69. goto exit;
  70. }
  71. }
  72. } else {
  73. rc = 1;
  74. goto exit;
  75. }
  76. exit:
  77. return( rc );
  78. }
  79. /* Helper function for processing a 'numericoid'.
  80. *
  81. * Returns 0 is the value between begin and end is a valid 'numericoid'.
  82. * Returns non-zero if the value is not a valide 'numericoid'.
  83. */
  84. int numericoid_validate(
  85. const char *begin,
  86. const char *end
  87. )
  88. {
  89. int rc = 0; /* assume the value is valid */
  90. int found_separator = 0;
  91. const char *p = NULL;
  92. if ((begin == NULL) || (end == NULL)) {
  93. rc = 1;
  94. goto exit;
  95. }
  96. /* Per RFC 4512:
  97. *
  98. * numericoid = number 1*( DOT number )
  99. */
  100. /* one pass of this loop should process one element of the oid (number DOT) */
  101. for (p = begin; p <= end; p++) {
  102. if (IS_LDIGIT(*p)) {
  103. /* loop until we get to a separator char */
  104. while(*p != '.') {
  105. p++;
  106. if (p > end) {
  107. /* ensure we got at least 2 elements */
  108. if (!found_separator) {
  109. rc = 1;
  110. goto exit;
  111. } else {
  112. /* looks like a valid numericoid */
  113. goto exit;
  114. }
  115. } else if (*p == '.') {
  116. /* we can not end with a '.' */
  117. if (p == end) {
  118. rc = 1;
  119. goto exit;
  120. } else {
  121. found_separator = 1;
  122. }
  123. } else if (!isdigit(*p)) {
  124. rc = 1;
  125. goto exit;
  126. }
  127. }
  128. } else if (*p == '0') {
  129. p++;
  130. if (p > end) {
  131. /* ensure we got at least 2 elements */
  132. if (!found_separator) {
  133. rc = 1;
  134. goto exit;
  135. } else {
  136. /* looks like a valid numericoid */
  137. goto exit;
  138. }
  139. } else if (*p != '.') {
  140. /* a leading 0 is not allowed unless the entire element is simply 0 */
  141. rc = 1;
  142. goto exit;
  143. }
  144. /* At this point, *p is '.'. We can not end with a '.' */
  145. if (p == end) {
  146. rc = 1;
  147. goto exit;
  148. } else {
  149. found_separator = 1;
  150. }
  151. } else {
  152. rc = 1;
  153. goto exit;
  154. }
  155. }
  156. exit:
  157. return(rc);
  158. }
  159. /* Helper to validate a single UTF-8 character.
  160. * It is assumed that the first byte of the character
  161. * is pointed to by begin. This function will not read
  162. * past the byte pointed to by the end parameter. The
  163. * last pointer will be filled in the the address of
  164. * the last byte of the validated character if the
  165. * character is valid, or the last byte processed
  166. * in the invalid case.
  167. *
  168. * Returns 0 if it is valid and non-zero otherwise. */
  169. int utf8char_validate(
  170. const char *begin,
  171. const char *end,
  172. const char **last
  173. )
  174. {
  175. int rc = 0; /* Assume char is valid */
  176. const char *p = begin;
  177. if ((begin == NULL) || (end == NULL)) {
  178. rc = 1;
  179. goto exit;
  180. }
  181. /* Per RFC 4512:
  182. *
  183. * UTF8 = UTF1 / UTFMB
  184. * UTFMB = UTF2 / UTF3 / UTF4
  185. * UTF0 = %x80-BF
  186. * UTF1 = %x00-7F
  187. * UTF2 = %xC2-DF UTF0
  188. * UTF3 = %xE0 %xA0-BF UTF0 / %xE1-EC 2(UTF0) /
  189. * %xED %x80-9F UTF0 / %xEE-EF 2(UTF0)
  190. * UTF4 = %xF0 %x90-BF 2(UTF0) / %xF1-F3 3(UTF0) /
  191. * %xF4 %x80-8F 2(UTF0)
  192. */
  193. /* If we have a single byte (ASCII) character, we
  194. * don't really have any work to do. */
  195. if (IS_UTF1(*p)) {
  196. goto exit;
  197. } else if (IS_UTF2(*p)) {
  198. /* Ensure that there is another byte
  199. * and that is is 'UTF0'. */
  200. if ((p == end) || !IS_UTF0(*(p + 1))) {
  201. rc = 1;
  202. goto exit;
  203. }
  204. /* Advance p so last is set correctly */
  205. p++;
  206. } else if (IS_UTF3(*p)) {
  207. /* Ensure that there are at least 2 more bytes. */
  208. if (end - p < 2) {
  209. rc = 1;
  210. goto exit;
  211. }
  212. /* The first byte determines what is legal for
  213. * the second byte. */
  214. if (*p == '\xE0') {
  215. /* The next byte must be %xA0-BF. */
  216. p++;
  217. if (((unsigned char)*p < (unsigned char)'\xA0') || ((unsigned char)*p > (unsigned char)'\xBF')) {
  218. rc = 1;
  219. goto exit;
  220. }
  221. } else if (*p == '\xED') {
  222. /* The next byte must be %x80-9F. */
  223. p++;
  224. if (((unsigned char)*p < (unsigned char)'\x80') || ((unsigned char)*p > (unsigned char)'\x9F')) {
  225. rc = 1;
  226. goto exit;
  227. }
  228. } else {
  229. /* The next byte must each be 'UTF0'. */
  230. p++;
  231. if (!IS_UTF0(*p)) {
  232. rc = 1;
  233. goto exit;
  234. }
  235. }
  236. /* The last byte must be 'UTF0'. */
  237. p++;
  238. if (!IS_UTF0(*p)) {
  239. rc = 1;
  240. goto exit;
  241. }
  242. } else if (IS_UTF4(*p)) {
  243. /* Ensure that there are at least 3 more bytes. */
  244. if (end - p < 3) {
  245. rc = 1;
  246. goto exit;
  247. }
  248. /* The first byte determines what is legal for
  249. * the second byte. */
  250. if (*p == '\xF0') {
  251. /* The next byte must be %x90-BF. */
  252. if (((unsigned char)*p < (unsigned char)'\x90') || ((unsigned char)*p > (unsigned char)'\xBF')) {
  253. rc = 1;
  254. goto exit;
  255. }
  256. } else if (*p == '\xF4') {
  257. /* The next byte must be %x80-BF. */
  258. if (((unsigned char)*p < (unsigned char)'\x80') || ((unsigned char)*p > (unsigned char)'\xBF')) {
  259. rc = 1;
  260. goto exit;
  261. }
  262. } else {
  263. /* The next byte must each be 'UTF0'. */
  264. p++;
  265. if (!IS_UTF0(*p)) {
  266. rc = 1;
  267. goto exit;
  268. }
  269. }
  270. /* The last 2 bytes must be 'UTF0'. */
  271. p++;
  272. if (!IS_UTF0(*p) || !IS_UTF0(*(p + 1))) {
  273. rc = 1;
  274. goto exit;
  275. }
  276. /* Advance the pointer so last is set correctly
  277. * when we return. */
  278. p++;
  279. } else {
  280. /* We found an illegal first byte. */
  281. rc = 1;
  282. goto exit;
  283. }
  284. exit:
  285. if (last) {
  286. *last = (const char *)p;
  287. }
  288. return(rc);
  289. }
  290. /* Validates that a non '\0' terminated string is UTF8. This
  291. * function will not read past the byte pointed to by the end
  292. * parameter. The last pointer will be filled in to point to
  293. * the address of the last byte of the last validated character
  294. * if the string is valid, or the last byte processed in the
  295. * invalid case.
  296. *
  297. * Returns 0 if it is valid and non-zero otherwise. */
  298. int utf8string_validate(
  299. const char *begin,
  300. const char *end,
  301. const char **last
  302. )
  303. {
  304. int rc = 0; /* Assume string is valid */
  305. const char *p = NULL;
  306. if ((begin == NULL) || (end == NULL)) {
  307. rc = 1;
  308. goto exit;
  309. }
  310. for (p = begin; p <= end; p++) {
  311. if ((rc = utf8char_validate(p, end, &p)) != 0) {
  312. goto exit;
  313. }
  314. }
  315. /* Adjust the pointer so last is set correctly for caller. */
  316. p--;
  317. exit:
  318. if (last) {
  319. *last = p;
  320. }
  321. return(rc);
  322. }