validate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. }
  323. /*
  324. * Validates a distinguishedName as degined in RFC 4514. Returns
  325. * 0 if the value from begin to end is a valid distinguishedName.
  326. * Returns 1 otherwise.
  327. */
  328. int distinguishedname_validate(
  329. const char *begin,
  330. const char *end
  331. )
  332. {
  333. int rc = 0; /* Assume value is valid */
  334. const char *p = begin;
  335. const char *last = NULL;
  336. /* Per RFC 4514:
  337. *
  338. * distinguishedName = [ relativeDistinguishedName
  339. * *( COMMA relativeDistinguishedName ) ]
  340. * relativeDistinguishedName = attributeTypeAndValue
  341. * *( PLUS attributeTypeAndValue )
  342. * attributeTypeAndValue = attribyteType EQUALS attributeValue
  343. * attributeType = descr / numericoid
  344. * attributeValue = string / hexstring
  345. */
  346. /* Validate one RDN at a time in a loop. */
  347. while (p <= end) {
  348. if ((rc = rdn_validate(p, end, &last)) != 0) {
  349. goto exit;
  350. }
  351. p = last + 1;
  352. /* p should be pointing at a comma, or one past
  353. * the end of the entire dn value. If we have
  354. * not reached the end, ensure that the next
  355. * character is a comma and that there is at
  356. * least another character after the comma. */
  357. if ((p <= end) && ((p == end) || (*p != ','))) {
  358. rc = 1;
  359. goto exit;
  360. }
  361. /* Advance the pointer past the comma so it
  362. * points at the beginning of the next RDN
  363. * (if there is one). */
  364. p++;
  365. }
  366. exit:
  367. return rc;
  368. }
  369. /*
  370. * Helper function for validating a DN. This function will validate
  371. * a single RDN. If the RDN is valid, 0 will be returned, otherwise
  372. * non-zero will be returned. A pointer to the last character processed
  373. * will be set in the "last parameter. This will be the end of the RDN
  374. * in the valid case, and the illegal character in the invalid case.
  375. */
  376. int rdn_validate( const char *begin, const char *end, const char **last )
  377. {
  378. int rc = 0; /* Assume RDN is valid */
  379. int numericform = 0;
  380. char *separator = NULL;
  381. const char *p = begin;
  382. /* Find the '=', then use the helpers for descr and numericoid */
  383. if ((separator = PL_strnchr(p, '=', end - begin + 1)) == NULL) {
  384. rc = 1;
  385. goto exit;
  386. }
  387. /* Process an attribute type. The 'descr'
  388. * form must start with a 'leadkeychar'. */
  389. if (IS_LEADKEYCHAR(*p)) {
  390. if ((rc = keystring_validate(p, separator - 1))) {
  391. goto exit;
  392. }
  393. /* See if the 'numericoid' form is being used */
  394. } else if (isdigit(*p)) {
  395. numericform = 1;
  396. if ((rc = numericoid_validate(p, separator - 1))) {
  397. goto exit;
  398. }
  399. } else {
  400. rc = 1;
  401. goto exit;
  402. }
  403. /* Advance the pointer past the '=' and make sure
  404. * we're not past the end of the string. */
  405. p = separator + 1;
  406. if (p > end) {
  407. rc = 1;
  408. goto exit;
  409. }
  410. /* The value must be a 'hexstring' if the 'numericoid'
  411. * form of 'attributeType' is used. Per RFC 4514:
  412. *
  413. * hexstring = SHARP 1*hexpair
  414. * hexpair = HEX HEX
  415. */
  416. if (numericform) {
  417. if ((p == end) || !IS_SHARP(*p)) {
  418. rc = 1;
  419. goto exit;
  420. }
  421. p++;
  422. /* The value must be a 'string' when the 'descr' form
  423. * of 'attributeType' is used. Per RFC 4514:
  424. *
  425. * string = [ ( leadchar / pair ) [ *( stringchar / pair )
  426. * ( trailchar / pair ) ] ]
  427. *
  428. * leadchar = LUTF1 / UTFMB
  429. * trailchar = TUTF1 / UTFMB
  430. * stringchar = SUTF1 / UTFMB
  431. *
  432. * pair = ESC (ESC / special / hexpair )
  433. * special = escaped / SPACE / SHARP / EQUALS
  434. * escaped = DQUOTE / PLUS / COMMA / SEMI / LANGLE / RANGLE
  435. * hexpair = HEX HEX
  436. */
  437. } else {
  438. /* Check the leadchar to see if anything illegal
  439. * is there. We need to allow a 'pair' to get
  440. * through, so we'll assume that a '\' is the
  441. * start of a 'pair' for now. */
  442. if (IS_UTF1(*p) && !IS_ESC(*p) && !IS_LUTF1(*p)) {
  443. rc = 1;
  444. goto exit;
  445. }
  446. }
  447. /* Loop through string until we find the ',' separator, a '+'
  448. * char indicating a multi-value RDN, or we reach the end. */
  449. while ((p <= end) && (*p != ',') && (*p != '+')) {
  450. if (numericform) {
  451. /* Process a single 'hexpair' */
  452. if ((p == end) || !isxdigit(*p) || !isxdigit(*p + 1)) {
  453. rc = 1;
  454. goto exit;
  455. }
  456. p = p + 2;
  457. } else {
  458. /* Check for a valid 'stringchar'. We handle
  459. * multi-byte characters separately. */
  460. if (IS_UTF1(*p)) {
  461. /* If we're at the end, check if we have
  462. * a valid 'trailchar'. */
  463. if ((p == end) && !IS_TUTF1(*p)) {
  464. rc = 1;
  465. goto exit;
  466. /* Check for a 'pair'. */
  467. } else if (IS_ESC(*p)) {
  468. /* We're guaranteed to still have at
  469. * least one more character, so lets
  470. * take a look at it. */
  471. p++;
  472. if (!IS_ESC(*p) && !IS_SPECIAL(*p)) {
  473. /* The only thing valid now
  474. * is a 'hexpair'. */
  475. if ((p == end) || !isxdigit(*p) ||!isxdigit(*p + 1)) {
  476. rc = 1;
  477. goto exit;
  478. }
  479. p++;
  480. }
  481. /* Only allow 'SUTF1' chars now. */
  482. } else if (!IS_SUTF1(*p)) {
  483. rc = 1;
  484. goto exit;
  485. }
  486. p++;
  487. } else {
  488. /* Validate a single 'UTFMB' (multi-byte) character. */
  489. if (utf8char_validate(p, end, &p ) != 0) {
  490. rc = 1;
  491. goto exit;
  492. }
  493. /* Advance the pointer past the multi-byte char. */
  494. p++;
  495. }
  496. }
  497. }
  498. /* We'll end up either at the comma, a '+', or one past end.
  499. * If we are processing a multi-valued RDN, we recurse to
  500. * process the next 'attributeTypeAndValue'. */
  501. if ((p <= end) && (*p == '+')) {
  502. /* Make sure that there is something after the '+'. */
  503. if (p == end) {
  504. rc = 1;
  505. goto exit;
  506. }
  507. p++;
  508. /* Recurse to process the next value. We need to reset p to
  509. * ensure that last is set correctly for the original caller. */
  510. rc = rdn_validate( p, end, last );
  511. p = *last + 1;
  512. }
  513. exit:
  514. *last = p - 1;
  515. return rc;
  516. }
  517. int
  518. bitstring_validate_internal(const char *begin, const char *end)
  519. {
  520. int rc = 0; /* assume the value is valid */
  521. const char *p = NULL;
  522. /* Per RFC4517:
  523. *
  524. * BitString = SQUOTE *binary-digit SQUOTE "B"
  525. * binary-digit = "0" / "1"
  526. */
  527. /* Check that the value starts with a SQUOTE and
  528. * ends with SQUOTE "B". */
  529. if (!IS_SQUOTE(*begin) || (*end != 'B') ||
  530. !IS_SQUOTE(*(end - 1))) {
  531. rc = 1;
  532. goto exit;
  533. }
  534. /* Ensure that only '0' and '1' are between the SQUOTE chars. */
  535. for (p = begin + 1; p <= end - 2; p++) {
  536. if ((*p != '0') && (*p != '1')) {
  537. rc = 1;
  538. goto exit;
  539. }
  540. }
  541. exit:
  542. return rc;
  543. }