validate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2009 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. #include <config.h>
  10. #endif
  11. /* validate.c - syntax validation helper functions */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include "syntax.h"
  16. /* Helper function for processing a 'keystring'.
  17. *
  18. * Returns 0 is the value between begin and end is a valid 'keystring'.
  19. * Returns non-zero if the value is not a valide 'keystring'.
  20. */
  21. int
  22. keystring_validate(
  23. const char *begin,
  24. const char *end)
  25. {
  26. int rc = 0; /* assume the value is valid */
  27. const char *p = begin;
  28. if ((begin == NULL) || (end == NULL)) {
  29. rc = 1;
  30. goto exit;
  31. }
  32. /* Per RFC4512:
  33. *
  34. * keystring = leadkeychar *keychar
  35. */
  36. if (IS_LEADKEYCHAR(*p)) {
  37. for (p++; p <= end; p++) {
  38. if (!IS_KEYCHAR(*p)) {
  39. rc = 1;
  40. goto exit;
  41. }
  42. }
  43. } else {
  44. rc = 1;
  45. goto exit;
  46. }
  47. exit:
  48. return (rc);
  49. }
  50. /* Helper function for processing a 'numericoid'.
  51. *
  52. * Returns 0 is the value between begin and end is a valid 'numericoid'.
  53. * Returns non-zero if the value is not a valide 'numericoid'.
  54. */
  55. int
  56. numericoid_validate(
  57. const char *begin,
  58. const char *end)
  59. {
  60. int rc = 0; /* assume the value is valid */
  61. int found_separator = 0;
  62. const char *p = NULL;
  63. if ((begin == NULL) || (end == NULL)) {
  64. rc = 1;
  65. goto exit;
  66. }
  67. /* Per RFC 4512:
  68. *
  69. * numericoid = number 1*( DOT number )
  70. */
  71. /* one pass of this loop should process one element of the oid (number DOT) */
  72. for (p = begin; p <= end; p++) {
  73. if (IS_LDIGIT(*p)) {
  74. /* loop until we get to a separator char */
  75. while (*p != '.') {
  76. p++;
  77. if (p > end) {
  78. /* ensure we got at least 2 elements */
  79. if (!found_separator) {
  80. rc = 1;
  81. goto exit;
  82. } else {
  83. /* looks like a valid numericoid */
  84. goto exit;
  85. }
  86. } else if (*p == '.') {
  87. /* we can not end with a '.' */
  88. if (p == end) {
  89. rc = 1;
  90. goto exit;
  91. } else {
  92. found_separator = 1;
  93. }
  94. } else if (!isdigit(*p)) {
  95. rc = 1;
  96. goto exit;
  97. }
  98. }
  99. } else if (*p == '0') {
  100. p++;
  101. if (p > end) {
  102. /* ensure we got at least 2 elements */
  103. if (!found_separator) {
  104. rc = 1;
  105. goto exit;
  106. } else {
  107. /* looks like a valid numericoid */
  108. goto exit;
  109. }
  110. } else if (*p != '.') {
  111. /* a leading 0 is not allowed unless the entire element is simply 0 */
  112. rc = 1;
  113. goto exit;
  114. }
  115. /* At this point, *p is '.'. We can not end with a '.' */
  116. if (p == end) {
  117. rc = 1;
  118. goto exit;
  119. } else {
  120. found_separator = 1;
  121. }
  122. } else {
  123. rc = 1;
  124. goto exit;
  125. }
  126. }
  127. exit:
  128. return (rc);
  129. }
  130. /* Helper to validate a single UTF-8 character.
  131. * It is assumed that the first byte of the character
  132. * is pointed to by begin. This function will not read
  133. * past the byte pointed to by the end parameter. The
  134. * last pointer will be filled in the the address of
  135. * the last byte of the validated character if the
  136. * character is valid, or the last byte processed
  137. * in the invalid case.
  138. *
  139. * Returns 0 if it is valid and non-zero otherwise. */
  140. int
  141. utf8char_validate(
  142. const char *begin,
  143. const char *end,
  144. const char **last)
  145. {
  146. int rc = 0; /* Assume char is valid */
  147. const char *p = begin;
  148. if ((begin == NULL) || (end == NULL)) {
  149. rc = 1;
  150. goto exit;
  151. }
  152. /* Per RFC 4512:
  153. *
  154. * UTF8 = UTF1 / UTFMB
  155. * UTFMB = UTF2 / UTF3 / UTF4
  156. * UTF0 = %x80-BF
  157. * UTF1 = %x00-7F
  158. * UTF2 = %xC2-DF UTF0
  159. * UTF3 = %xE0 %xA0-BF UTF0 / %xE1-EC 2(UTF0) /
  160. * %xED %x80-9F UTF0 / %xEE-EF 2(UTF0)
  161. * UTF4 = %xF0 %x90-BF 2(UTF0) / %xF1-F3 3(UTF0) /
  162. * %xF4 %x80-8F 2(UTF0)
  163. */
  164. /* If we have a single byte (ASCII) character, we
  165. * don't really have any work to do. */
  166. if (IS_UTF1(*p)) {
  167. goto exit;
  168. } else if (IS_UTF2(*p)) {
  169. /* Ensure that there is another byte
  170. * and that is is 'UTF0'. */
  171. if ((p == end) || !IS_UTF0(*(p + 1))) {
  172. rc = 1;
  173. goto exit;
  174. }
  175. /* Advance p so last is set correctly */
  176. p++;
  177. } else if (IS_UTF3(*p)) {
  178. /* Ensure that there are at least 2 more bytes. */
  179. if (end - p < 2) {
  180. rc = 1;
  181. goto exit;
  182. }
  183. /* The first byte determines what is legal for
  184. * the second byte. */
  185. if (*p == '\xE0') {
  186. /* The next byte must be %xA0-BF. */
  187. p++;
  188. if (((unsigned char)*p < (unsigned char)'\xA0') || ((unsigned char)*p > (unsigned char)'\xBF')) {
  189. rc = 1;
  190. goto exit;
  191. }
  192. } else if (*p == '\xED') {
  193. /* The next byte must be %x80-9F. */
  194. p++;
  195. if (((unsigned char)*p < (unsigned char)'\x80') || ((unsigned char)*p > (unsigned char)'\x9F')) {
  196. rc = 1;
  197. goto exit;
  198. }
  199. } else {
  200. /* The next byte must each be 'UTF0'. */
  201. p++;
  202. if (!IS_UTF0(*p)) {
  203. rc = 1;
  204. goto exit;
  205. }
  206. }
  207. /* The last byte must be 'UTF0'. */
  208. p++;
  209. if (!IS_UTF0(*p)) {
  210. rc = 1;
  211. goto exit;
  212. }
  213. } else if (IS_UTF4(*p)) {
  214. /* Ensure that there are at least 3 more bytes. */
  215. if (end - p < 3) {
  216. rc = 1;
  217. goto exit;
  218. }
  219. /* The first byte determines what is legal for
  220. * the second byte. */
  221. if (*p == '\xF0') {
  222. /* The next byte must be %x90-BF. */
  223. p++;
  224. if (((unsigned char)*p < (unsigned char)'\x90') || ((unsigned char)*p > (unsigned char)'\xBF')) {
  225. rc = 1;
  226. goto exit;
  227. }
  228. } else if (*p == '\xF4') {
  229. /* The next byte must be %x80-BF. */
  230. p++;
  231. if (((unsigned char)*p < (unsigned char)'\x80') || ((unsigned char)*p > (unsigned char)'\xBF')) {
  232. rc = 1;
  233. goto exit;
  234. }
  235. } else {
  236. /* The next byte must each be 'UTF0'. */
  237. p++;
  238. if (!IS_UTF0(*p)) {
  239. rc = 1;
  240. goto exit;
  241. }
  242. }
  243. /* The last 2 bytes must be 'UTF0'. */
  244. p++;
  245. if (!IS_UTF0(*p) || !IS_UTF0(*(p + 1))) {
  246. rc = 1;
  247. goto exit;
  248. }
  249. /* Advance the pointer so last is set correctly
  250. * when we return. */
  251. p++;
  252. } else {
  253. /* We found an illegal first byte. */
  254. rc = 1;
  255. goto exit;
  256. }
  257. exit:
  258. if (last) {
  259. *last = (const char *)p;
  260. }
  261. return (rc);
  262. }
  263. /* Validates that a non '\0' terminated string is UTF8. This
  264. * function will not read past the byte pointed to by the end
  265. * parameter. The last pointer will be filled in to point to
  266. * the address of the last byte of the last validated character
  267. * if the string is valid, or the last byte processed in the
  268. * invalid case.
  269. *
  270. * Returns 0 if it is valid and non-zero otherwise. */
  271. int
  272. utf8string_validate(
  273. const char *begin,
  274. const char *end,
  275. const char **last)
  276. {
  277. int rc = 0; /* Assume string is valid */
  278. const char *p = NULL;
  279. if ((begin == NULL) || (end == NULL)) {
  280. rc = 1;
  281. goto exit;
  282. }
  283. for (p = begin; p <= end; p++) {
  284. if ((rc = utf8char_validate(p, end, &p)) != 0) {
  285. goto exit;
  286. }
  287. }
  288. /* Adjust the pointer so last is set correctly for caller. */
  289. p--;
  290. exit:
  291. if (last) {
  292. *last = p;
  293. }
  294. return (rc);
  295. }
  296. /*
  297. * Validates a distinguishedName as degined in RFC 4514. Returns
  298. * 0 if the value from begin to end is a valid distinguishedName.
  299. * Returns 1 otherwise.
  300. */
  301. int
  302. distinguishedname_validate(
  303. const char *begin,
  304. const char *end)
  305. {
  306. int rc = 0; /* Assume value is valid */
  307. const char *p = begin;
  308. const char *last = NULL;
  309. /* Per RFC 4514:
  310. *
  311. * distinguishedName = [ relativeDistinguishedName
  312. * *( COMMA relativeDistinguishedName ) ]
  313. * relativeDistinguishedName = attributeTypeAndValue
  314. * *( PLUS attributeTypeAndValue )
  315. * attributeTypeAndValue = attribyteType EQUALS attributeValue
  316. * attributeType = descr / numericoid
  317. * attributeValue = string / hexstring
  318. */
  319. /* Validate one RDN at a time in a loop. */
  320. while (p <= end) {
  321. if ((rc = rdn_validate(p, end, &last)) != 0) {
  322. goto exit;
  323. }
  324. p = last + 1;
  325. /* p should be pointing at a comma, or one past
  326. * the end of the entire dn value. If we have
  327. * not reached the end, ensure that the next
  328. * character is a comma and that there is at
  329. * least another character after the comma. */
  330. if ((p <= end) && ((p == end) || (*p != ','))) {
  331. rc = 1;
  332. goto exit;
  333. }
  334. /* Advance the pointer past the comma so it
  335. * points at the beginning of the next RDN
  336. * (if there is one). */
  337. p++;
  338. }
  339. exit:
  340. return rc;
  341. }
  342. /*
  343. * Helper function for validating a DN. This function will validate
  344. * a single RDN. If the RDN is valid, 0 will be returned, otherwise
  345. * non-zero will be returned. A pointer to the last character processed
  346. * will be set in the "last parameter. This will be the end of the RDN
  347. * in the valid case, and the illegal character in the invalid case.
  348. */
  349. int
  350. rdn_validate(const char *begin, const char *end, const char **last)
  351. {
  352. int rc = 0; /* Assume RDN is valid */
  353. int numericform = 0;
  354. char *separator = NULL;
  355. const char *p = begin;
  356. /* Find the '=', then use the helpers for descr and numericoid */
  357. if ((separator = PL_strnchr(p, '=', end - begin + 1)) == NULL) {
  358. rc = 1;
  359. goto exit;
  360. }
  361. /* Process an attribute type. The 'descr'
  362. * form must start with a 'leadkeychar'. */
  363. if (IS_LEADKEYCHAR(*p)) {
  364. if ((rc = keystring_validate(p, separator - 1))) {
  365. goto exit;
  366. }
  367. /* See if the 'numericoid' form is being used */
  368. } else if (isdigit(*p)) {
  369. numericform = 1;
  370. if ((rc = numericoid_validate(p, separator - 1))) {
  371. goto exit;
  372. }
  373. } else {
  374. rc = 1;
  375. goto exit;
  376. }
  377. /* Advance the pointer past the '=' and make sure
  378. * we're not past the end of the string. */
  379. p = separator + 1;
  380. if (p > end) {
  381. rc = 1;
  382. goto exit;
  383. }
  384. /* The value must be a 'hexstring' if the 'numericoid'
  385. * form of 'attributeType' is used. Per RFC 4514:
  386. *
  387. * hexstring = SHARP 1*hexpair
  388. * hexpair = HEX HEX
  389. */
  390. if (numericform) {
  391. if ((p == end) || !IS_SHARP(*p)) {
  392. rc = 1;
  393. goto exit;
  394. }
  395. p++;
  396. /* The value must be a 'string' when the 'descr' form
  397. * of 'attributeType' is used. Per RFC 4514:
  398. *
  399. * string = [ ( leadchar / pair ) [ *( stringchar / pair )
  400. * ( trailchar / pair ) ] ]
  401. *
  402. * leadchar = LUTF1 / UTFMB
  403. * trailchar = TUTF1 / UTFMB
  404. * stringchar = SUTF1 / UTFMB
  405. *
  406. * pair = ESC (ESC / special / hexpair )
  407. * special = escaped / SPACE / SHARP / EQUALS
  408. * escaped = DQUOTE / PLUS / COMMA / SEMI / LANGLE / RANGLE
  409. * hexpair = HEX HEX
  410. */
  411. } else {
  412. /* Check the leadchar to see if anything illegal
  413. * is there. We need to allow a 'pair' to get
  414. * through, so we'll assume that a '\' is the
  415. * start of a 'pair' for now. */
  416. if (IS_UTF1(*p) && !IS_ESC(*p) && !IS_LUTF1(*p)) {
  417. rc = 1;
  418. goto exit;
  419. }
  420. }
  421. /* Loop through string until we find the ',' separator, a '+'
  422. * char indicating a multi-value RDN, or we reach the end. */
  423. while ((p <= end) && (*p != ',') && (*p != '+')) {
  424. if (numericform) {
  425. /* Process a single 'hexpair' */
  426. if ((p == end) || !isxdigit(*p) || !isxdigit(*p + 1)) {
  427. rc = 1;
  428. goto exit;
  429. }
  430. p = p + 2;
  431. } else {
  432. /* Check for a valid 'stringchar'. We handle
  433. * multi-byte characters separately. */
  434. if (IS_UTF1(*p)) {
  435. /* If we're at the end, check if we have
  436. * a valid 'trailchar'. */
  437. if ((p == end) && !IS_TUTF1(*p)) {
  438. rc = 1;
  439. goto exit;
  440. /* Check for a 'pair'. */
  441. } else if (IS_ESC(*p)) {
  442. /* We're guaranteed to still have at
  443. * least one more character, so lets
  444. * take a look at it. */
  445. p++;
  446. if (!IS_ESC(*p) && !IS_SPECIAL(*p)) {
  447. /* The only thing valid now
  448. * is a 'hexpair'. */
  449. if ((p == end) || !isxdigit(*p) || !isxdigit(*p + 1)) {
  450. rc = 1;
  451. goto exit;
  452. }
  453. p++;
  454. }
  455. /* Only allow 'SUTF1' chars now. */
  456. } else if (!IS_SUTF1(*p)) {
  457. rc = 1;
  458. goto exit;
  459. }
  460. p++;
  461. } else {
  462. /* Validate a single 'UTFMB' (multi-byte) character. */
  463. if (utf8char_validate(p, end, &p) != 0) {
  464. rc = 1;
  465. goto exit;
  466. }
  467. /* Advance the pointer past the multi-byte char. */
  468. p++;
  469. }
  470. }
  471. }
  472. /* We'll end up either at the comma, a '+', or one past end.
  473. * If we are processing a multi-valued RDN, we recurse to
  474. * process the next 'attributeTypeAndValue'. */
  475. if ((p <= end) && (*p == '+')) {
  476. /* Make sure that there is something after the '+'. */
  477. if (p == end) {
  478. rc = 1;
  479. goto exit;
  480. }
  481. p++;
  482. /* Recurse to process the next value. We need to reset p to
  483. * ensure that last is set correctly for the original caller. */
  484. rc = rdn_validate(p, end, last);
  485. p = *last + 1;
  486. }
  487. exit:
  488. *last = p - 1;
  489. return rc;
  490. }
  491. int
  492. bitstring_validate_internal(const char *begin, const char *end)
  493. {
  494. int rc = 0; /* assume the value is valid */
  495. const char *p = NULL;
  496. /* Per RFC4517:
  497. *
  498. * BitString = SQUOTE *binary-digit SQUOTE "B"
  499. * binary-digit = "0" / "1"
  500. */
  501. /* Check that the value starts with a SQUOTE and
  502. * ends with SQUOTE "B". */
  503. if (!IS_SQUOTE(*begin) || (*end != 'B') ||
  504. !IS_SQUOTE(*(end - 1))) {
  505. rc = 1;
  506. goto exit;
  507. }
  508. /* Ensure that only '0' and '1' are between the SQUOTE chars. */
  509. for (p = begin + 1; p <= end - 2; p++) {
  510. if ((*p != '0') && (*p != '1')) {
  511. rc = 1;
  512. goto exit;
  513. }
  514. }
  515. exit:
  516. return rc;
  517. }