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.
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.
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.
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
34
* Copyright (C) 2009 Red Hat, Inc.
35
* All rights reserved.
36
* END COPYRIGHT BLOCK **/
42
/* validate.c - syntax validation helper functions */
46
#include <sys/types.h>
49
/* Helper function for processing a 'keystring'.
51
* Returns 0 is the value between begin and end is a valid 'keystring'.
52
* Returns non-zero if the value is not a valide 'keystring'.
54
int keystring_validate(
59
int rc = 0; /* assume the value is valid */
60
const char *p = begin;
62
if ((begin == NULL) || (end == NULL)) {
69
* keystring = leadkeychar *keychar
71
if (IS_LEADKEYCHAR(*p)) {
72
for (p++; p <= end; p++) {
73
if (!IS_KEYCHAR(*p)) {
87
/* Helper function for processing a 'numericoid'.
89
* Returns 0 is the value between begin and end is a valid 'numericoid'.
90
* Returns non-zero if the value is not a valide 'numericoid'.
92
int numericoid_validate(
97
int rc = 0; /* assume the value is valid */
98
int found_separator = 0;
101
if ((begin == NULL) || (end == NULL)) {
108
* numericoid = number 1*( DOT number )
111
/* one pass of this loop should process one element of the oid (number DOT) */
112
for (p = begin; p <= end; p++) {
114
/* loop until we get to a separator char */
118
/* ensure we got at least 2 elements */
119
if (!found_separator) {
123
/* looks like a valid numericoid */
126
} else if (*p == '.') {
127
/* we can not end with a '.' */
134
} else if (!isdigit(*p)) {
139
} else if (*p == '0') {
142
/* ensure we got at least 2 elements */
143
if (!found_separator) {
147
/* looks like a valid numericoid */
150
} else if (*p != '.') {
151
/* a leading 0 is not allowed unless the entire element is simply 0 */
156
/* At this point, *p is '.'. We can not end with a '.' */
173
/* Helper to validate a single UTF-8 character.
174
* It is assumed that the first byte of the character
175
* is pointed to by begin. This function will not read
176
* past the byte pointed to by the end parameter. The
177
* last pointer will be filled in the the address of
178
* the last byte of the validated character if the
179
* character is valid, or the last byte processed
180
* in the invalid case.
182
* Returns 0 if it is valid and non-zero otherwise. */
183
int utf8char_validate(
189
int rc = 0; /* Assume char is valid */
190
const char *p = begin;
192
if ((begin == NULL) || (end == NULL)) {
199
* UTF8 = UTF1 / UTFMB
200
* UTFMB = UTF2 / UTF3 / UTF4
203
* UTF2 = %xC2-DF UTF0
204
* UTF3 = %xE0 %xA0-BF UTF0 / %xE1-EC 2(UTF0) /
205
* %xED %x80-9F UTF0 / %xEE-EF 2(UTF0)
206
* UTF4 = %xF0 %x90-BF 2(UTF0) / %xF1-F3 3(UTF0) /
207
* %xF4 %x80-8F 2(UTF0)
210
/* If we have a single byte (ASCII) character, we
211
* don't really have any work to do. */
214
} else if (IS_UTF2(*p)) {
215
/* Ensure that there is another byte
216
* and that is is 'UTF0'. */
217
if ((p == end) || !IS_UTF0(*(p + 1))) {
222
/* Advance p so last is set correctly */
224
} else if (IS_UTF3(*p)) {
225
/* Ensure that there are at least 2 more bytes. */
231
/* The first byte determines what is legal for
232
* the second byte. */
234
/* The next byte must be %xA0-BF. */
236
if (((unsigned char)*p < (unsigned char)'\xA0') || ((unsigned char)*p > (unsigned char)'\xBF')) {
240
} else if (*p == '\xED') {
241
/* The next byte must be %x80-9F. */
243
if (((unsigned char)*p < (unsigned char)'\x80') || ((unsigned char)*p > (unsigned char)'\x9F')) {
248
/* The next byte must each be 'UTF0'. */
256
/* The last byte must be 'UTF0'. */
262
} else if (IS_UTF4(*p)) {
263
/* Ensure that there are at least 3 more bytes. */
269
/* The first byte determines what is legal for
270
* the second byte. */
272
/* The next byte must be %x90-BF. */
273
if (((unsigned char)*p < (unsigned char)'\x90') || ((unsigned char)*p > (unsigned char)'\xBF')) {
277
} else if (*p == '\xF4') {
278
/* The next byte must be %x80-BF. */
279
if (((unsigned char)*p < (unsigned char)'\x80') || ((unsigned char)*p > (unsigned char)'\xBF')) {
284
/* The next byte must each be 'UTF0'. */
292
/* The last 2 bytes must be 'UTF0'. */
294
if (!IS_UTF0(*p) || !IS_UTF0(*(p + 1))) {
299
/* Advance the pointer so last is set correctly
303
/* We found an illegal first byte. */
310
*last = (const char *)p;
315
/* Validates that a non '\0' terminated string is UTF8. This
316
* function will not read past the byte pointed to by the end
317
* parameter. The last pointer will be filled in to point to
318
* the address of the last byte of the last validated character
319
* if the string is valid, or the last byte processed in the
322
* Returns 0 if it is valid and non-zero otherwise. */
323
int utf8string_validate(
329
int rc = 0; /* Assume string is valid */
330
const char *p = NULL;
332
if ((begin == NULL) || (end == NULL)) {
337
for (p = begin; p <= end; p++) {
338
if ((rc = utf8char_validate(p, end, &p)) != 0) {
343
/* Adjust the pointer so last is set correctly for caller. */
354
* Validates a distinguishedName as degined in RFC 4514. Returns
355
* 0 if the value from begin to end is a valid distinguishedName.
356
* Returns 1 otherwise.
358
int distinguishedname_validate(
363
int rc = 0; /* Assume value is valid */
364
const char *p = begin;
365
const char *last = NULL;
369
* distinguishedName = [ relativeDistinguishedName
370
* *( COMMA relativeDistinguishedName ) ]
371
* relativeDistinguishedName = attributeTypeAndValue
372
* *( PLUS attributeTypeAndValue )
373
* attributeTypeAndValue = attribyteType EQUALS attributeValue
374
* attributeType = descr / numericoid
375
* attributeValue = string / hexstring
378
/* Validate one RDN at a time in a loop. */
380
if ((rc = rdn_validate(p, end, &last)) != 0) {
385
/* p should be pointing at a comma, or one past
386
* the end of the entire dn value. If we have
387
* not reached the end, ensure that the next
388
* character is a comma and that there is at
389
* least another character after the comma. */
390
if ((p <= end) && ((p == end) || (*p != ','))) {
395
/* Advance the pointer past the comma so it
396
* points at the beginning of the next RDN
397
* (if there is one). */
406
* Helper function for validating a DN. This function will validate
407
* a single RDN. If the RDN is valid, 0 will be returned, otherwise
408
* non-zero will be returned. A pointer to the last character processed
409
* will be set in the "last parameter. This will be the end of the RDN
410
* in the valid case, and the illegal character in the invalid case.
412
int rdn_validate( const char *begin, const char *end, const char **last )
414
int rc = 0; /* Assume RDN is valid */
416
char *separator = NULL;
417
const char *p = begin;
419
/* Find the '=', then use the helpers for descr and numericoid */
420
if ((separator = PL_strnchr(p, '=', end - begin + 1)) == NULL) {
425
/* Process an attribute type. The 'descr'
426
* form must start with a 'leadkeychar'. */
427
if (IS_LEADKEYCHAR(*p)) {
428
if ((rc = keystring_validate(p, separator - 1))) {
431
/* See if the 'numericoid' form is being used */
432
} else if (isdigit(*p)) {
434
if ((rc = numericoid_validate(p, separator - 1))) {
442
/* Advance the pointer past the '=' and make sure
443
* we're not past the end of the string. */
450
/* The value must be a 'hexstring' if the 'numericoid'
451
* form of 'attributeType' is used. Per RFC 4514:
453
* hexstring = SHARP 1*hexpair
457
if ((p == end) || !IS_SHARP(*p)) {
462
/* The value must be a 'string' when the 'descr' form
463
* of 'attributeType' is used. Per RFC 4514:
465
* string = [ ( leadchar / pair ) [ *( stringchar / pair )
466
* ( trailchar / pair ) ] ]
468
* leadchar = LUTF1 / UTFMB
469
* trailchar = TUTF1 / UTFMB
470
* stringchar = SUTF1 / UTFMB
472
* pair = ESC (ESC / special / hexpair )
473
* special = escaped / SPACE / SHARP / EQUALS
474
* escaped = DQUOTE / PLUS / COMMA / SEMI / LANGLE / RANGLE
478
/* Check the leadchar to see if anything illegal
479
* is there. We need to allow a 'pair' to get
480
* through, so we'll assume that a '\' is the
481
* start of a 'pair' for now. */
482
if (IS_UTF1(*p) && !IS_ESC(*p) && !IS_LUTF1(*p)) {
488
/* Loop through string until we find the ',' separator, a '+'
489
* char indicating a multi-value RDN, or we reach the end. */
490
while ((p <= end) && (*p != ',') && (*p != '+')) {
492
/* Process a single 'hexpair' */
493
if ((p == end) || !isxdigit(*p) || !isxdigit(*p + 1)) {
499
/* Check for a valid 'stringchar'. We handle
500
* multi-byte characters separately. */
502
/* If we're at the end, check if we have
503
* a valid 'trailchar'. */
504
if ((p == end) && !IS_TUTF1(*p)) {
507
/* Check for a 'pair'. */
508
} else if (IS_ESC(*p)) {
509
/* We're guaranteed to still have at
510
* least one more character, so lets
511
* take a look at it. */
513
if (!IS_ESC(*p) && !IS_SPECIAL(*p)) {
514
/* The only thing valid now
516
if ((p == end) || !isxdigit(*p) ||!isxdigit(*p + 1)) {
522
/* Only allow 'SUTF1' chars now. */
523
} else if (!IS_SUTF1(*p)) {
530
/* Validate a single 'UTFMB' (multi-byte) character. */
531
if (utf8char_validate(p, end, &p ) != 0) {
536
/* Advance the pointer past the multi-byte char. */
542
/* We'll end up either at the comma, a '+', or one past end.
543
* If we are processing a multi-valued RDN, we recurse to
544
* process the next 'attributeTypeAndValue'. */
545
if ((p <= end) && (*p == '+')) {
546
/* Make sure that there is something after the '+'. */
553
/* Recurse to process the next value. We need to reset p to
554
* ensure that last is set correctly for the original caller. */
555
rc = rdn_validate( p, end, last );
565
bitstring_validate_internal(const char *begin, const char *end)
567
int rc = 0; /* assume the value is valid */
568
const char *p = NULL;
572
* BitString = SQUOTE *binary-digit SQUOTE "B"
573
* binary-digit = "0" / "1"
576
/* Check that the value starts with a SQUOTE and
577
* ends with SQUOTE "B". */
578
if (!IS_SQUOTE(*begin) || (*end != 'B') ||
579
!IS_SQUOTE(*(end - 1))) {
584
/* Ensure that only '0' and '1' are between the SQUOTE chars. */
585
for (p = begin + 1; p <= end - 2; p++) {
586
if ((*p != '0') && (*p != '1')) {