2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
29 #include <lber_types.h>
35 * ber_tag_t represents the identifier octets at the beginning of BER
36 * elements. OpenLDAP treats them as mere big-endian unsigned integers.
38 * Actually the BER identifier octets look like this:
45 * 1 0 = CONTEXT-SPECIFIC
52 * | 5 ... 1 | TAG-NUMBER
54 * For ASN.1 tag numbers >= 0x1F, TAG-NUMBER above is 0x1F and the next
55 * BER octets contain the actual ASN.1 tag number: Big-endian, base
56 * 128, 8.bit = 1 in all but the last octet, minimum number of octets.
59 /* BER classes and mask (in 1st identifier octet) */
60 #define LBER_CLASS_UNIVERSAL ((ber_tag_t) 0x00U)
61 #define LBER_CLASS_APPLICATION ((ber_tag_t) 0x40U)
62 #define LBER_CLASS_CONTEXT ((ber_tag_t) 0x80U)
63 #define LBER_CLASS_PRIVATE ((ber_tag_t) 0xc0U)
64 #define LBER_CLASS_MASK ((ber_tag_t) 0xc0U)
66 /* BER encoding type and mask (in 1st identifier octet) */
67 #define LBER_PRIMITIVE ((ber_tag_t) 0x00U)
68 #define LBER_CONSTRUCTED ((ber_tag_t) 0x20U)
69 #define LBER_ENCODING_MASK ((ber_tag_t) 0x20U)
71 #define LBER_BIG_TAG_MASK ((ber_tag_t) 0x1fU)
72 #define LBER_MORE_TAG_MASK ((ber_tag_t) 0x80U)
75 * LBER_ERROR and LBER_DEFAULT are values that can never appear
76 * as valid BER tags, so it is safe to use them to report errors.
77 * Valid tags have (tag & (ber_tag_t) 0xFF) != 0xFF.
79 #define LBER_ERROR ((ber_tag_t) -1)
80 #define LBER_DEFAULT ((ber_tag_t) -1)
82 /* general BER types we know about */
83 #define LBER_BOOLEAN ((ber_tag_t) 0x01UL)
84 #define LBER_INTEGER ((ber_tag_t) 0x02UL)
85 #define LBER_BITSTRING ((ber_tag_t) 0x03UL)
86 #define LBER_OCTETSTRING ((ber_tag_t) 0x04UL)
87 #define LBER_NULL ((ber_tag_t) 0x05UL)
88 #define LBER_ENUMERATED ((ber_tag_t) 0x0aUL)
89 #define LBER_SEQUENCE ((ber_tag_t) 0x30UL) /* constructed */
90 #define LBER_SET ((ber_tag_t) 0x31UL) /* constructed */
92 /* LBER BerElement options */
93 #define LBER_USE_DER 0x01
95 /* get/set options for BerElement */
96 #define LBER_OPT_BER_OPTIONS 0x01
97 #define LBER_OPT_BER_DEBUG 0x02
98 #define LBER_OPT_BER_REMAINING_BYTES 0x03
99 #define LBER_OPT_BER_TOTAL_BYTES 0x04
100 #define LBER_OPT_BER_BYTES_TO_WRITE 0x05
101 #define LBER_OPT_BER_MEMCTX 0x06
103 #define LBER_OPT_DEBUG_LEVEL LBER_OPT_BER_DEBUG
104 #define LBER_OPT_REMAINING_BYTES LBER_OPT_BER_REMAINING_BYTES
105 #define LBER_OPT_TOTAL_BYTES LBER_OPT_BER_TOTAL_BYTES
106 #define LBER_OPT_BYTES_TO_WRITE LBER_OPT_BER_BYTES_TO_WRITE
108 #define LBER_OPT_LOG_PRINT_FN 0x8001
109 #define LBER_OPT_MEMORY_FNS 0x8002
110 #define LBER_OPT_ERROR_FN 0x8003
111 #define LBER_OPT_LOG_PRINT_FILE 0x8004
113 /* get/set Memory Debug options */
114 #define LBER_OPT_MEMORY_INUSE 0x8005 /* for memory debugging */
115 #define LBER_OPT_LOG_PROC 0x8006 /* for external logging function */
117 typedef int* (*BER_ERRNO_FN) LDAP_P(( void ));
119 typedef void (*BER_LOG_PRINT_FN) LDAP_P(( LDAP_CONST char *buf ));
121 typedef void* (BER_MEMALLOC_FN) LDAP_P(( ber_len_t size, void *ctx ));
122 typedef void* (BER_MEMCALLOC_FN) LDAP_P(( ber_len_t n, ber_len_t size, void *ctx ));
123 typedef void* (BER_MEMREALLOC_FN) LDAP_P(( void *p, ber_len_t size, void *ctx ));
124 typedef void (BER_MEMFREE_FN) LDAP_P(( void *p, void *ctx ));
126 typedef struct lber_memory_fns {
127 BER_MEMALLOC_FN *bmf_malloc;
128 BER_MEMCALLOC_FN *bmf_calloc;
129 BER_MEMREALLOC_FN *bmf_realloc;
130 BER_MEMFREE_FN *bmf_free;
131 } BerMemoryFunctions;
133 /* LBER Sockbuf_IO options */
134 #define LBER_SB_OPT_GET_FD 1
135 #define LBER_SB_OPT_SET_FD 2
136 #define LBER_SB_OPT_HAS_IO 3
137 #define LBER_SB_OPT_SET_NONBLOCK 4
138 #define LBER_SB_OPT_GET_SSL 7
139 #define LBER_SB_OPT_DATA_READY 8
140 #define LBER_SB_OPT_SET_READAHEAD 9
141 #define LBER_SB_OPT_DRAIN 10
142 #define LBER_SB_OPT_NEEDS_READ 11
143 #define LBER_SB_OPT_NEEDS_WRITE 12
144 #define LBER_SB_OPT_GET_MAX_INCOMING 13
145 #define LBER_SB_OPT_SET_MAX_INCOMING 14
147 /* Only meaningful ifdef LDAP_PF_LOCAL_SENDMSG */
148 #define LBER_SB_OPT_UNGET_BUF 15
150 /* Largest option used by the library */
151 #define LBER_SB_OPT_OPT_MAX 15
153 /* LBER IO operations stacking levels */
154 #define LBER_SBIOD_LEVEL_PROVIDER 10
155 #define LBER_SBIOD_LEVEL_TRANSPORT 20
156 #define LBER_SBIOD_LEVEL_APPLICATION 30
158 /* get/set options for Sockbuf */
159 #define LBER_OPT_SOCKBUF_DESC 0x1000
160 #define LBER_OPT_SOCKBUF_OPTIONS 0x1001
161 #define LBER_OPT_SOCKBUF_DEBUG 0x1002
164 LBER_V( char ) ber_pvt_opt_on;
165 #define LBER_OPT_ON ((void *) &ber_pvt_opt_on)
166 #define LBER_OPT_OFF ((void *) 0)
168 #define LBER_OPT_SUCCESS (0)
169 #define LBER_OPT_ERROR (-1)
171 typedef struct berelement BerElement;
172 typedef struct sockbuf Sockbuf;
174 typedef struct sockbuf_io Sockbuf_IO;
176 /* Structure for LBER IO operation descriptor */
177 typedef struct sockbuf_io_desc {
180 Sockbuf_IO *sbiod_io;
182 struct sockbuf_io_desc *sbiod_next;
185 /* Structure for LBER IO operation functions */
187 int (*sbi_setup)( Sockbuf_IO_Desc *sbiod, void *arg );
188 int (*sbi_remove)( Sockbuf_IO_Desc *sbiod );
189 int (*sbi_ctrl)( Sockbuf_IO_Desc *sbiod, int opt, void *arg);
191 ber_slen_t (*sbi_read)( Sockbuf_IO_Desc *sbiod, void *buf,
193 ber_slen_t (*sbi_write)( Sockbuf_IO_Desc *sbiod, void *buf,
196 int (*sbi_close)( Sockbuf_IO_Desc *sbiod );
199 /* Helper macros for LBER IO functions */
200 #define LBER_SBIOD_READ_NEXT( sbiod, buf, len ) \
201 ( (sbiod)->sbiod_next->sbiod_io->sbi_read( (sbiod)->sbiod_next, \
203 #define LBER_SBIOD_WRITE_NEXT( sbiod, buf, len ) \
204 ( (sbiod)->sbiod_next->sbiod_io->sbi_write( (sbiod)->sbiod_next, \
206 #define LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg ) \
207 ( (sbiod)->sbiod_next ? \
208 ( (sbiod)->sbiod_next->sbiod_io->sbi_ctrl( \
209 (sbiod)->sbiod_next, opt, arg ) ) : 0 )
211 /* structure for returning a sequence of octet strings + length */
212 typedef struct berval {
217 typedef BerValue *BerVarray; /* To distinguish from a single bv */
219 /* this should be moved to lber-int.h */
225 ber_error_print LDAP_P((
226 LDAP_CONST char *data ));
230 LDAP_CONST char *data, ber_len_t len ));
234 BerElement *ber, int inout ));
239 typedef int (*BERDecodeCallback) LDAP_P((
249 ber_skip_tag LDAP_P((
254 ber_peek_tag LDAP_P((
259 ber_skip_element LDAP_P((
261 struct berval *bv ));
264 ber_peek_element LDAP_P((
265 LDAP_CONST BerElement *ber,
266 struct berval *bv ));
274 ber_get_enum LDAP_P((
279 ber_get_stringb LDAP_P((
284 #define LBER_BV_ALLOC 0x01 /* allocate/copy result, otherwise in-place */
285 #define LBER_BV_NOTERM 0x02 /* omit NUL-terminator if parsing in-place */
286 #define LBER_BV_STRING 0x04 /* fail if berval contains embedded \0 */
287 /* LBER_BV_STRING currently accepts a terminating \0 in the berval, because
288 * Active Directory sends that in at least the diagonsticMessage field.
292 ber_get_stringbv LDAP_P((
298 ber_get_stringa LDAP_P((
303 ber_get_stringal LDAP_P((
305 struct berval **bv ));
308 ber_get_bitstringa LDAP_P((
314 ber_get_null LDAP_P((
318 ber_get_boolean LDAP_P((
320 ber_int_t *boolval ));
323 ber_first_element LDAP_P((
329 ber_next_element LDAP_P((
332 LDAP_CONST char *last ));
337 LDAP_CONST char *fmt,
341 ber_decode_oid LDAP_P((
343 struct berval *out ));
349 ber_encode_oid LDAP_P((
351 struct berval *out ));
353 typedef int (*BEREncodeCallback) LDAP_P((
358 ber_put_enum LDAP_P((
370 ber_put_ostring LDAP_P((
372 LDAP_CONST char *str,
377 ber_put_berval LDAP_P((
383 ber_put_string LDAP_P((
385 LDAP_CONST char *str,
389 ber_put_bitstring LDAP_P((
391 LDAP_CONST char *str,
396 ber_put_null LDAP_P((
401 ber_put_boolean LDAP_P((
407 ber_start_seq LDAP_P((
412 ber_start_set LDAP_P((
427 LDAP_CONST char *fmt,
436 ber_skip_data LDAP_P((
449 LDAP_CONST char *buf,
451 int zero )); /* nonzero is unsupported from OpenLDAP 2.4.18 */
459 ber_free_buf LDAP_P(( BerElement *ber ));
466 #define LBER_FLUSH_FREE_NEVER (0x0) /* traditional behavior */
467 #define LBER_FLUSH_FREE_ON_SUCCESS (0x1) /* traditional behavior */
468 #define LBER_FLUSH_FREE_ON_ERROR (0x2)
469 #define LBER_FLUSH_FREE_ALWAYS (LBER_FLUSH_FREE_ON_SUCCESS|LBER_FLUSH_FREE_ON_ERROR)
475 int freeit )); /* DEPRECATED */
477 LBER_F( BerElement * )
478 ber_alloc LDAP_P(( void )); /* DEPRECATED */
480 LBER_F( BerElement * )
481 der_alloc LDAP_P(( void )); /* DEPRECATED */
483 LBER_F( BerElement * )
487 LBER_F( BerElement * )
492 ber_get_next LDAP_P((
504 ber_init_w_nullc LDAP_P(( /* DEPRECATED */
513 LBER_F( BerElement * )
515 struct berval *bv ));
520 struct berval **bvPtr ));
523 ber_flatten2 LDAP_P((
529 ber_remaining LDAP_P((
533 * LBER ber accessor functions
537 ber_get_option LDAP_P((
543 ber_set_option LDAP_P((
546 LDAP_CONST void *invalue));
553 ber_sockbuf_alloc LDAP_P((
557 ber_sockbuf_free LDAP_P((
561 ber_sockbuf_add_io LDAP_P((
568 ber_sockbuf_remove_io LDAP_P((
574 ber_sockbuf_ctrl LDAP_P((
579 LBER_V( Sockbuf_IO ) ber_sockbuf_io_tcp;
580 LBER_V( Sockbuf_IO ) ber_sockbuf_io_readahead;
581 LBER_V( Sockbuf_IO ) ber_sockbuf_io_fd;
582 LBER_V( Sockbuf_IO ) ber_sockbuf_io_debug;
583 LBER_V( Sockbuf_IO ) ber_sockbuf_io_udp;
589 ber_memalloc LDAP_P((
593 ber_memrealloc LDAP_P((
598 ber_memcalloc LDAP_P((
607 ber_memvfree LDAP_P((
612 struct berval *bv ));
615 ber_bvecfree LDAP_P((
616 struct berval **bv ));
620 struct berval ***bvec,
621 struct berval *bv ));
623 LBER_F( struct berval * )
625 struct berval *dst, struct berval *src ));
627 LBER_F( struct berval * )
629 struct berval *src ));
631 LBER_F( struct berval * )
633 LDAP_CONST char *, ber_len_t len, int duplicate, struct berval *bv));
635 LBER_F( struct berval * )
637 LDAP_CONST char *, ber_len_t len, int duplicate, struct berval *bv));
639 #define ber_bvstr(a) ((ber_str2bv)((a), 0, 0, NULL))
640 #define ber_bvstrdup(a) ((ber_str2bv)((a), 0, 1, NULL))
644 LDAP_CONST char * ));
648 LDAP_CONST char *s, ber_len_t len ));
652 LDAP_CONST char *s, ber_len_t l ));
654 LBER_F( struct berval * )
655 ber_bvreplace LDAP_P((
656 struct berval *dst, LDAP_CONST struct berval *src ));
659 ber_bvarray_free LDAP_P(( BerVarray p ));
662 ber_bvarray_add LDAP_P(( BerVarray *p, BerValue *bv ));
664 #define ber_bvcmp(v1,v2) \
665 ((v1)->bv_len < (v2)->bv_len \
666 ? -1 : ((v1)->bv_len > (v2)->bv_len \
667 ? 1 : memcmp((v1)->bv_val, (v2)->bv_val, (v1)->bv_len) ))
672 LBER_F( int * ) ber_errno_addr LDAP_P((void));
673 #define ber_errno (*(ber_errno_addr)())
675 #define LBER_ERROR_NONE 0
676 #define LBER_ERROR_PARAM 0x1
677 #define LBER_ERROR_MEMORY 0x2