validate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. if (((unsigned char)*p < (unsigned char)'\x90') || ((unsigned char)*p > (unsigned char)'\xBF')) {
  224. rc = 1;
  225. goto exit;
  226. }
  227. } else if (*p == '\xF4') {
  228. /* The next byte must be %x80-BF. */
  229. if (((unsigned char)*p < (unsigned char)'\x80') || ((unsigned char)*p > (unsigned char)'\xBF')) {
  230. rc = 1;
  231. goto exit;
  232. }
  233. } else {
  234. /* The next byte must each be 'UTF0'. */
  235. p++;
  236. if (!IS_UTF0(*p)) {
  237. rc = 1;
  238. goto exit;
  239. }
  240. }
  241. /* The last 2 bytes must be 'UTF0'. */
  242. p++;
  243. if (!IS_UTF0(*p) || !IS_UTF0(*(p + 1))) {
  244. rc = 1;
  245. goto exit;
  246. }
  247. /* Advance the pointer so last is set correctly
  248. * when we return. */
  249. p++;
  250. } else {
  251. /* We found an illegal first byte. */
  252. rc = 1;
  253. goto exit;
  254. }
  255. exit:
  256. if (last) {
  257. *last = (const char *)p;
  258. }
  259. return (rc);
  260. }
  261. /* Validates that a non '\0' terminated string is UTF8. This
  262. * function will not read past the byte pointed to by the end
  263. * parameter. The last pointer will be filled in to point to
  264. * the address of the last byte of the last validated character
  265. * if the string is valid, or the last byte processed in the
  266. * invalid case.
  267. *
  268. * Returns 0 if it is valid and non-zero otherwise. */
  269. int
  270. utf8string_validate(
  271. const char *begin,
  272. const char *end,
  273. const char **last)
  274. {
  275. int rc = 0; /* Assume string is valid */
  276. const char *p = NULL;
  277. if ((begin == NULL) || (end == NULL)) {
  278. rc = 1;
  279. goto exit;
  280. }
  281. for (p = begin; p <= end; p++) {
  282. if ((rc = utf8char_validate(p, end, &p)) != 0) {
  283. goto exit;
  284. }
  285. }
  286. /* Adjust the pointer so last is set correctly for caller. */
  287. p--;
  288. exit:
  289. if (last) {
  290. *last = p;
  291. }
  292. return (rc);
  293. }
  294. /*
  295. * Validates a distinguishedName as degined in RFC 4514. Returns
  296. * 0 if the value from begin to end is a valid distinguishedName.
  297. * Returns 1 otherwise.
  298. */
  299. int
  300. distinguishedname_validate(
  301. const char *begin,
  302. const char *end)
  303. {
  304. int rc = 0; /* Assume value is valid */
  305. const char *p = begin;
  306. const char *last = NULL;
  307. /* Per RFC 4514:
  308. *
  309. * distinguishedName = [ relativeDistinguishedName
  310. * *( COMMA relativeDistinguishedName ) ]
  311. * relativeDistinguishedName = attributeTypeAndValue
  312. * *( PLUS attributeTypeAndValue )
  313. * attributeTypeAndValue = attribyteType EQUALS attributeValue
  314. * attributeType = descr / numericoid
  315. * attributeValue = string / hexstring
  316. */
  317. /* Validate one RDN at a time in a loop. */
  318. while (p <= end) {
  319. if ((rc = rdn_validate(p, end, &last)) != 0) {
  320. goto exit;
  321. }
  322. p = last + 1;
  323. /* p should be pointing at a comma, or one past
  324. * the end of the entire dn value. If we have
  325. * not reached the end, ensure that the next
  326. * character is a comma and that there is at
  327. * least another character after the comma. */
  328. if ((p <= end) && ((p == end) || (*p != ','))) {
  329. rc = 1;
  330. goto exit;
  331. }
  332. /* Advance the pointer past the comma so it
  333. * points at the beginning of the next RDN
  334. * (if there is one). */
  335. p++;
  336. }
  337. exit:
  338. return rc;
  339. }
  340. /*
  341. * Helper function for validating a DN. This function will validate
  342. * a single RDN. If the RDN is valid, 0 will be returned, otherwise
  343. * non-zero will be returned. A pointer to the last character processed
  344. * will be set in the "last parameter. This will be the end of the RDN
  345. * in the valid case, and the illegal character in the invalid case.
  346. */
  347. int
  348. rdn_validate(const char *begin, const char *end, const char **last)
  349. {
  350. int rc = 0; /* Assume RDN is valid */
  351. int numericform = 0;
  352. char *separator = NULL;
  353. const char *p = begin;
  354. /* Find the '=', then use the helpers for descr and numericoid */
  355. if ((separator = PL_strnchr(p, '=', end - begin + 1)) == NULL) {
  356. rc = 1;
  357. goto exit;
  358. }
  359. /* Process an attribute type. The 'descr'
  360. * form must start with a 'leadkeychar'. */
  361. if (IS_LEADKEYCHAR(*p)) {
  362. if ((rc = keystring_validate(p, separator - 1))) {
  363. goto exit;
  364. }
  365. /* See if the 'numericoid' form is being used */
  366. } else if (isdigit(*p)) {
  367. numericform = 1;
  368. if ((rc = numericoid_validate(p, separator - 1))) {
  369. goto exit;
  370. }
  371. } else {
  372. rc = 1;
  373. goto exit;
  374. }
  375. /* Advance the pointer past the '=' and make sure
  376. * we're not past the end of the string. */
  377. p = separator + 1;
  378. if (p > end) {
  379. rc = 1;
  380. goto exit;
  381. }
  382. /* The value must be a 'hexstring' if the 'numericoid'
  383. * form of 'attributeType' is used. Per RFC 4514:
  384. *
  385. * hexstring = SHARP 1*hexpair
  386. * hexpair = HEX HEX
  387. */
  388. if (numericform) {
  389. if ((p == end) || !IS_SHARP(*p)) {
  390. rc = 1;
  391. goto exit;
  392. }
  393. p++;
  394. /* The value must be a 'string' when the 'descr' form
  395. * of 'attributeType' is used. Per RFC 4514:
  396. *
  397. * string = [ ( leadchar / pair ) [ *( stringchar / pair )
  398. * ( trailchar / pair ) ] ]
  399. *
  400. * leadchar = LUTF1 / UTFMB
  401. * trailchar = TUTF1 / UTFMB
  402. * stringchar = SUTF1 / UTFMB
  403. *
  404. * pair = ESC (ESC / special / hexpair )
  405. * special = escaped / SPACE / SHARP / EQUALS
  406. * escaped = DQUOTE / PLUS / COMMA / SEMI / LANGLE / RANGLE
  407. * hexpair = HEX HEX
  408. */
  409. } else {
  410. /* Check the leadchar to see if anything illegal
  411. * is there. We need to allow a 'pair' to get
  412. * through, so we'll assume that a '\' is the
  413. * start of a 'pair' for now. */
  414. if (IS_UTF1(*p) && !IS_ESC(*p) && !IS_LUTF1(*p)) {
  415. rc = 1;
  416. goto exit;
  417. }
  418. }
  419. /* Loop through string until we find the ',' separator, a '+'
  420. * char indicating a multi-value RDN, or we reach the end. */
  421. while ((p <= end) && (*p != ',') && (*p != '+')) {
  422. if (numericform) {
  423. /* Process a single 'hexpair' */
  424. if ((p == end) || !isxdigit(*p) || !isxdigit(*p + 1)) {
  425. rc = 1;
  426. goto exit;
  427. }
  428. p = p + 2;
  429. } else {
  430. /* Check for a valid 'stringchar'. We handle
  431. * multi-byte characters separately. */
  432. if (IS_UTF1(*p)) {
  433. /* If we're at the end, check if we have
  434. * a valid 'trailchar'. */
  435. if ((p == end) && !IS_TUTF1(*p)) {
  436. rc = 1;
  437. goto exit;
  438. /* Check for a 'pair'. */
  439. } else if (IS_ESC(*p)) {
  440. /* We're guaranteed to still have at
  441. * least one more character, so lets
  442. * take a look at it. */
  443. p++;
  444. if (!IS_ESC(*p) && !IS_SPECIAL(*p)) {
  445. /* The only thing valid now
  446. * is a 'hexpair'. */
  447. if ((p == end) || !isxdigit(*p) || !isxdigit(*p + 1)) {
  448. rc = 1;
  449. goto exit;
  450. }
  451. p++;
  452. }
  453. /* Only allow 'SUTF1' chars now. */
  454. } else if (!IS_SUTF1(*p)) {
  455. rc = 1;
  456. goto exit;
  457. }
  458. p++;
  459. } else {
  460. /* Validate a single 'UTFMB' (multi-byte) character. */
  461. if (utf8char_validate(p, end, &p) != 0) {
  462. rc = 1;
  463. goto exit;
  464. }
  465. /* Advance the pointer past the multi-byte char. */
  466. p++;
  467. }
  468. }
  469. }
  470. /* We'll end up either at the comma, a '+', or one past end.
  471. * If we are processing a multi-valued RDN, we recurse to
  472. * process the next 'attributeTypeAndValue'. */
  473. if ((p <= end) && (*p == '+')) {
  474. /* Make sure that there is something after the '+'. */
  475. if (p == end) {
  476. rc = 1;
  477. goto exit;
  478. }
  479. p++;
  480. /* Recurse to process the next value. We need to reset p to
  481. * ensure that last is set correctly for the original caller. */
  482. rc = rdn_validate(p, end, last);
  483. p = *last + 1;
  484. }
  485. exit:
  486. *last = p - 1;
  487. return rc;
  488. }
  489. int
  490. bitstring_validate_internal(const char *begin, const char *end)
  491. {
  492. int rc = 0; /* assume the value is valid */
  493. const char *p = NULL;
  494. /* Per RFC4517:
  495. *
  496. * BitString = SQUOTE *binary-digit SQUOTE "B"
  497. * binary-digit = "0" / "1"
  498. */
  499. /* Check that the value starts with a SQUOTE and
  500. * ends with SQUOTE "B". */
  501. if (!IS_SQUOTE(*begin) || (*end != 'B') ||
  502. !IS_SQUOTE(*(end - 1))) {
  503. rc = 1;
  504. goto exit;
  505. }
  506. /* Ensure that only '0' and '1' are between the SQUOTE chars. */
  507. for (p = begin + 1; p <= end - 2; p++) {
  508. if ((*p != '0') && (*p != '1')) {
  509. rc = 1;
  510. goto exit;
  511. }
  512. }
  513. exit:
  514. return rc;
  515. }