1 /* decode.c - ber input decoding routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
35 #include <ac/stdlib.h>
36 #include <ac/stdarg.h>
37 #include <ac/string.h>
38 #include <ac/socket.h>
43 /* out->bv_len should be the buffer size on input */
45 ber_decode_oid( BerValue *in, BerValue *out )
47 const unsigned char *der;
54 assert( out != NULL );
56 /* need 4 chars/inbyte + \0 for input={7f 7f 7f...} */
57 if ( !out->bv_val || (out->bv_len+3)/4 <= in->bv_len )
61 der = (unsigned char *) in->bv_val;
63 for ( i=0; i < in->bv_len; i++ ) {
65 if ( !( der[i] & 0x80 )) {
67 /* Initial "x.y": val=x*40+y, x<=2, y<40 if x<2 */
69 val1 = (val < 80 ? val/40 : 2);
71 ptr += sprintf( ptr, "%u", val1 );
73 ptr += sprintf( ptr, ".%lu", val );
75 } else if ( val - 1UL < LBER_OID_COMPONENT_MAX >> 7 ) {
78 /* val would overflow, or is 0 from invalid initial 0x80 octet */
82 if ( ptr == NULL || val != 0 )
85 out->bv_len = ptr - out->bv_val;
89 /* Return tag, with *bv = rest of element (starting at length octets) */
91 ber_tag_and_rest( const BerElement *ber, struct berval *bv )
97 assert( ber != NULL );
98 assert( LBER_VALID( ber ) );
100 ptr = (unsigned char *) ber->ber_ptr;
101 rest = (unsigned char *) ber->ber_end - ptr;
107 if ( (char *) ptr == ber->ber_buf ) {
112 if ( (tag & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) {
121 tag |= *ptr++ & 0xffU;
124 if ( ! (tag & LBER_MORE_TAG_MASK) ) {
127 } while ( tag <= (ber_tag_t)-1 / 256 );
130 /* Error or unsupported tag size */
135 bv->bv_val = (char *) ptr;
139 /* Return the tag - LBER_DEFAULT returned means trouble */
141 ber_get_tag( BerElement *ber )
144 ber_tag_t tag = ber_tag_and_rest( ber, &bv );
146 ber->ber_ptr = bv.bv_val;
150 /* Return next element's tag and point *bv at its contents in-place */
152 ber_peek_element( const BerElement *ber, struct berval *bv )
159 assert( bv != NULL );
162 * Any ber element looks like this: tag length contents.
163 * Assuming everything's ok, we return the tag, and point
164 * bv at the contents.
167 * 1) definite lengths
168 * 2) primitive encodings used whenever possible
174 * First, we read the tag.
176 tag = ber_tag_and_rest( ber, bv );
179 ptr = (unsigned char *) bv->bv_val;
180 if ( tag == LBER_DEFAULT || rest == 0 ) {
185 * Next, read the length. The first octet determines the length
186 * of the length. If bit 8 is 0, the length is the short form,
187 * otherwise if the octet != 0x80 it's the long form, otherwise
188 * the ber element has the unsupported indefinite-length format.
189 * Lengths that do not fit in a ber_len_t are not accepted.
197 if ( len - 1U > sizeof(ber_len_t) - 1U || rest < len ) {
198 /* Indefinite-length/too long length/not enough data */
204 for( len = *ptr++ & 0xffU; --i; len |= *ptr++ & 0xffU ) {
209 /* BER element should have enough data left */
216 bv->bv_val = (char *) ptr;
220 /* Move past next element, point *bv at it in-place, and return its tag.
221 * The caller may \0-terminate *bv, as next octet is saved in ber->ber_tag.
222 * See ber_get_stringbv(ber, bv, LBER_BV_NOTERM) for an exported wrapper.
225 ber_skip_element( BerElement *ber, struct berval *bv )
227 ber_tag_t tag = ber_peek_element( ber, bv );
229 if ( tag != LBER_DEFAULT ) {
230 ber->ber_ptr = bv->bv_val + bv->bv_len;
231 ber->ber_tag = *(unsigned char *) ber->ber_ptr;
243 ber_tag_t tag = ber_peek_element( ber, &bv );
250 ber_skip_tag( BerElement *ber, ber_len_t *lenp )
253 ber_tag_t tag = ber_peek_element( ber, &bv );
255 ber->ber_ptr = bv.bv_val;
256 ber->ber_tag = *(unsigned char *) ber->ber_ptr;
271 assert( num != NULL );
273 tag = ber_skip_element( ber, &bv );
275 if ( tag == LBER_DEFAULT || len > sizeof(ber_int_t) ) {
279 /* parse two's complement integer */
281 unsigned char *buf = (unsigned char *) bv.bv_val;
283 ber_int_t netnum = buf[0] & 0xff;
286 netnum = (netnum ^ 0x80) - 0x80;
288 /* shift in the bytes */
289 for( i = 1; i < len; i++ ) {
290 netnum = (netnum << 8 ) | buf[i];
307 return ber_get_int( ber, num );
319 if ( (tag = ber_skip_element( ber, &bv )) == LBER_DEFAULT ) {
323 /* must fit within allocated space with termination */
324 if ( bv.bv_len >= *len ) {
328 memcpy( buf, bv.bv_val, bv.bv_len );
329 buf[bv.bv_len] = '\0';
335 /* Definitions for get_string vector
337 * ChArray, BvArray, and BvVec are self-explanatory.
338 * BvOff is a struct berval embedded in an array of larger structures
339 * of siz bytes at off bytes from the beginning of the struct.
341 enum bgbvc { ChArray, BvArray, BvVec, BvOff };
343 /* Use this single cookie for state, to keep actual
344 * stack use to the absolute minimum.
346 typedef struct bgbvr {
347 const enum bgbvc choice;
348 const int alloc; /* choice == BvOff ? 0 : LBER_ALLOC */
349 ber_len_t siz; /* input array element size, output count */
350 ber_len_t off; /* BvOff offset to the struct berval */
355 ber_get_stringbvl( BerElement *ber, bgbvr *b )
359 ber_len_t tot_size = 0, siz = b->siz;
361 struct berval bv, *bvp = NULL;
363 char **ca; /* ChArray */
364 BerVarray ba; /* BvArray */
365 struct berval **bv; /* BvVec */
366 char *bo; /* BvOff */
369 /* For rewinding, just like ber_peek_tag() */
373 if ( ber_first_element( ber, &bv.bv_len, &last ) != LBER_DEFAULT ) {
374 for ( ; ber->ber_ptr < last; i++, tot_size += siz ) {
375 if ( ber_skip_element( ber, &bv ) == LBER_DEFAULT )
385 /* Allocate and NULL-terminate the result vector */
386 b->result = ber_memalloc_x( tot_size + siz, ber->ber_memctx );
387 if ( b->result == NULL ) {
397 res.ba[i].bv_val = NULL;
404 res.bo = (char *) b->result + b->off;
405 ((struct berval *) (res.bo + tot_size))->bv_val = NULL;
411 ber_skip_tag( ber, &bv.bv_len );
415 tag = ber_next_element( ber, &bv.bv_len, last );
416 if ( ber_get_stringbv( ber, &bv, b->alloc ) == LBER_DEFAULT ) {
420 /* store my result */
423 res.ca[n] = bv.bv_val;
429 bvp = ber_memalloc_x( sizeof( struct berval ),
432 ber_memfree_x( bv.bv_val, ber->ber_memctx );
439 *(struct berval *)(res.bo + tot_size) = bv;
447 if (b->choice != BvOff) { /* BvOff does not have b->alloc set */
451 ber_memfree_x(res.ca[n], ber->ber_memctx);
454 ber_memfree_x(res.ba[n].bv_val, ber->ber_memctx);
457 ber_memfree_x(res.bv[n]->bv_val, ber->ber_memctx);
458 ber_memfree_x(res.bv[n], ber->ber_memctx);
465 ber_memfree_x(b->result, ber->ber_memctx);
471 ber_get_stringbv( BerElement *ber, struct berval *bv, int option )
476 tag = ber_skip_element( ber, bv );
477 if ( tag == LBER_DEFAULT ) {
483 if ( option & LBER_BV_ALLOC ) {
484 bv->bv_val = (char *) ber_memalloc_x( bv->bv_len + 1,
486 if ( bv->bv_val == NULL ) {
490 if ( bv->bv_len != 0 ) {
491 memcpy( bv->bv_val, data, bv->bv_len );
495 if ( !( option & LBER_BV_NOTERM ))
496 data[bv->bv_len] = '\0';
502 ber_get_stringbv_null( BerElement *ber, struct berval *bv, int option )
507 tag = ber_skip_element( ber, bv );
508 if ( tag == LBER_DEFAULT || bv->bv_len == 0 ) {
514 if ( option & LBER_BV_ALLOC ) {
515 bv->bv_val = (char *) ber_memalloc_x( bv->bv_len + 1,
517 if ( bv->bv_val == NULL ) {
521 memcpy( bv->bv_val, data, bv->bv_len );
524 if ( !( option & LBER_BV_NOTERM ))
525 data[bv->bv_len] = '\0';
531 ber_get_stringa( BerElement *ber, char **buf )
536 assert( buf != NULL );
538 tag = ber_get_stringbv( ber, &bv, LBER_BV_ALLOC );
545 ber_get_stringa_null( BerElement *ber, char **buf )
550 assert( buf != NULL );
552 tag = ber_get_stringbv_null( ber, &bv, LBER_BV_ALLOC );
559 ber_get_stringal( BerElement *ber, struct berval **bv )
563 assert( ber != NULL );
564 assert( bv != NULL );
566 *bv = (struct berval *) ber_memalloc_x( sizeof(struct berval),
572 tag = ber_get_stringbv( ber, *bv, LBER_BV_ALLOC );
573 if ( tag == LBER_DEFAULT ) {
574 ber_memfree_x( *bv, ber->ber_memctx );
588 unsigned char unusedbits;
590 assert( buf != NULL );
591 assert( blen != NULL );
593 if ( (tag = ber_skip_element( ber, &data )) == LBER_DEFAULT ) {
597 if ( --data.bv_len > (ber_len_t)-1 / 8 ) {
600 unusedbits = *(unsigned char *) data.bv_val++;
601 if ( unusedbits > 7 ) {
605 *buf = (char *) ber_memalloc_x( data.bv_len, ber->ber_memctx );
606 if ( *buf == NULL ) {
609 memcpy( *buf, data.bv_val, data.bv_len );
611 *blen = data.bv_len * 8 - unusedbits;
620 ber_get_null( BerElement *ber )
623 ber_tag_t tag = ber_skip_tag( ber, &len );
625 return( len == 0 ? tag : LBER_DEFAULT );
633 return ber_get_int( ber, boolval );
642 assert( last != NULL );
644 /* skip the sequence header, use the len to mark where to stop */
645 if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
650 *last = ber->ber_ptr + *len;
656 return ber_peek_tag( ber, len );
663 LDAP_CONST char *last )
665 assert( ber != NULL );
666 assert( last != NULL );
667 assert( LBER_VALID( ber ) );
669 if ( ber->ber_ptr >= last ) {
673 return ber_peek_tag( ber, len );
678 ber_scanf ( BerElement *ber,
679 LDAP_CONST char *fmt,
683 LDAP_CONST char *fmt_reset;
684 char *s, **ss, ***sss;
685 struct berval data, *bval, **bvp, ***bvpp;
694 assert( ber != NULL );
695 assert( fmt != NULL );
696 assert( LBER_VALID( ber ) );
700 if ( ber->ber_debug & (LDAP_DEBUG_TRACE|LDAP_DEBUG_BER)) {
701 ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
702 "ber_scanf fmt (%s) ber:\n", fmt );
703 ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
706 for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
707 /* When this is modified, remember to update
708 * the error-cleanup code below accordingly. */
710 case '!': { /* Hook */
711 BERDecodeCallback *f;
714 f = va_arg( ap, BERDecodeCallback * );
715 p = va_arg( ap, void * );
717 rc = (*f)( ber, p, 0 );
720 case 'a': /* octet string - allocate storage as needed */
721 ss = va_arg( ap, char ** );
722 rc = ber_get_stringa( ber, ss );
725 case 'A': /* octet string - allocate storage as needed,
726 * but return NULL if len == 0 */
727 ss = va_arg( ap, char ** );
728 rc = ber_get_stringa_null( ber, ss );
731 case 'b': /* boolean */
732 i = va_arg( ap, ber_int_t * );
733 rc = ber_get_boolean( ber, i );
736 case 'B': /* bit string - allocate storage as needed */
737 ss = va_arg( ap, char ** );
738 l = va_arg( ap, ber_len_t * ); /* for length, in bits */
739 rc = ber_get_bitstringa( ber, ss, l );
742 case 'e': /* enumerated */
743 case 'i': /* integer */
744 i = va_arg( ap, ber_int_t * );
745 rc = ber_get_int( ber, i );
748 case 'l': /* length of next item */
749 l = va_arg( ap, ber_len_t * );
750 rc = ber_peek_tag( ber, l );
753 case 'm': /* octet string in berval, in-place */
754 bval = va_arg( ap, struct berval * );
755 rc = ber_get_stringbv( ber, bval, 0 );
758 case 'M': /* bvoffarray - must include address of
759 * a record len, and record offset.
760 * number of records will be returned thru
761 * len ptr on finish. parsed in-place.
764 bgbvr cookie = { BvOff, 0 };
765 bvp = va_arg( ap, struct berval ** );
766 l = va_arg( ap, ber_len_t * );
768 cookie.off = va_arg( ap, ber_len_t );
769 rc = ber_get_stringbvl( ber, &cookie );
770 *bvp = cookie.result;
776 rc = ber_get_null( ber );
779 case 'o': /* octet string in a supplied berval */
780 bval = va_arg( ap, struct berval * );
781 rc = ber_get_stringbv( ber, bval, LBER_BV_ALLOC );
784 case 'O': /* octet string - allocate & include length */
785 bvp = va_arg( ap, struct berval ** );
786 rc = ber_get_stringal( ber, bvp );
789 case 's': /* octet string - in a buffer */
790 s = va_arg( ap, char * );
791 l = va_arg( ap, ber_len_t * );
792 rc = ber_get_stringb( ber, s, l );
795 case 't': /* tag of next item */
796 t = va_arg( ap, ber_tag_t * );
797 *t = rc = ber_peek_tag( ber, &len );
800 case 'T': /* skip tag of next item */
801 t = va_arg( ap, ber_tag_t * );
802 *t = rc = ber_skip_tag( ber, &len );
805 case 'v': /* sequence of strings */
808 ChArray, LBER_BV_ALLOC, sizeof( char * )
810 rc = ber_get_stringbvl( ber, &cookie );
811 *(va_arg( ap, char *** )) = cookie.result;
815 case 'V': /* sequence of strings + lengths */
818 BvVec, LBER_BV_ALLOC, sizeof( struct berval * )
820 rc = ber_get_stringbvl( ber, &cookie );
821 *(va_arg( ap, struct berval *** )) = cookie.result;
825 case 'W': /* bvarray */
828 BvArray, LBER_BV_ALLOC, sizeof( struct berval )
830 rc = ber_get_stringbvl( ber, &cookie );
831 *(va_arg( ap, struct berval ** )) = cookie.result;
835 case 'x': /* skip the next element - whatever it is */
836 rc = ber_skip_element( ber, &data );
839 case '{': /* begin sequence */
840 case '[': /* begin set */
842 case 'v': case 'V': case 'W': case 'M':
845 rc = ber_skip_tag( ber, &len );
850 case '}': /* end sequence */
851 case ']': /* end set */
855 if( ber->ber_debug ) {
856 ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
857 "ber_scanf: unknown fmt %c\n", *fmt );
866 if ( rc == LBER_DEFAULT ) {
868 * Error. Reclaim malloced memory that was given to the caller.
869 * Set allocated pointers to NULL, "data length" outvalues to 0.
873 for ( ; fmt_reset < fmt; fmt_reset++ ) {
874 switch ( *fmt_reset ) {
875 case '!': { /* Hook */
876 BERDecodeCallback *f;
879 f = va_arg( ap, BERDecodeCallback * );
880 p = va_arg( ap, void * );
882 (void) (*f)( ber, p, 1 );
885 case 'a': /* octet string - allocate storage as needed */
887 ss = va_arg( ap, char ** );
888 ber_memfree_x( *ss, ber->ber_memctx );
892 case 'b': /* boolean */
893 case 'e': /* enumerated */
894 case 'i': /* integer */
895 (void) va_arg( ap, ber_int_t * );
898 case 'l': /* length of next item */
899 *(va_arg( ap, ber_len_t * )) = 0;
902 case 'm': /* berval in-place */
903 bval = va_arg( ap, struct berval * );
907 case 'M': /* BVoff array in-place */
908 bvp = va_arg( ap, struct berval ** );
909 ber_memfree_x( *bvp, ber->ber_memctx );
911 *(va_arg( ap, ber_len_t * )) = 0;
912 (void) va_arg( ap, ber_len_t );
915 case 'o': /* octet string in a supplied berval */
916 bval = va_arg( ap, struct berval * );
917 ber_memfree_x( bval->bv_val, ber->ber_memctx );
921 case 'O': /* octet string - allocate & include length */
922 bvp = va_arg( ap, struct berval ** );
923 ber_bvfree_x( *bvp, ber->ber_memctx );
927 case 's': /* octet string - in a buffer */
928 (void) va_arg( ap, char * );
929 *(va_arg( ap, ber_len_t * )) = 0;
932 case 't': /* tag of next item */
933 case 'T': /* skip tag of next item */
934 (void) va_arg( ap, ber_tag_t * );
937 case 'B': /* bit string - allocate storage as needed */
938 ss = va_arg( ap, char ** );
939 ber_memfree_x( *ss, ber->ber_memctx );
941 *(va_arg( ap, ber_len_t * )) = 0; /* for length, in bits */
944 case 'v': /* sequence of strings */
945 sss = va_arg( ap, char *** );
946 ber_memvfree_x( (void **) *sss, ber->ber_memctx );
950 case 'V': /* sequence of strings + lengths */
951 bvpp = va_arg( ap, struct berval *** );
952 ber_bvecfree_x( *bvpp, ber->ber_memctx );
956 case 'W': /* BerVarray */
957 bvp = va_arg( ap, struct berval ** );
958 ber_bvarray_free_x( *bvp, ber->ber_memctx );
963 case 'x': /* skip the next element - whatever it is */
964 case '{': /* begin sequence */
965 case '[': /* begin set */
966 case '}': /* end sequence */
967 case ']': /* end set */
971 /* format should be good */