summaryrefslogtreecommitdiff
path: root/usr.sbin/ldapd/ber.h
blob: 179466f522238547deeefcdfe599b445c9337c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*	$OpenBSD: ber.h,v 1.9 2018/11/27 12:09:38 martijn Exp $ */

/*
 * Copyright (c) 2007, 2012 Reyk Floeter <reyk@openbsd.org>
 * Copyright (c) 2006, 2007 Claudio Jeker <claudio@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _BER_H
#define _BER_H

struct ber_octetstring {
	size_t			 ostr_len;
	const void		*ostr_val;
};

struct ber_element {
	struct ber_element	*be_next;
	unsigned int		 be_type;
	unsigned int		 be_encoding;
	size_t			 be_len;
	off_t			 be_offs;
	int			 be_free;
	u_int8_t		 be_class;
	void			(*be_cb)(void *, size_t);
	void			*be_cbarg;
	union {
		struct ber_element	*bv_sub;
		void			*bv_val;
		long long		 bv_numeric;
	} be_union;
#define be_sub		be_union.bv_sub
#define be_val		be_union.bv_val
#define be_numeric	be_union.bv_numeric
};

struct ber {
	off_t	 br_offs;
	u_char	*br_wbuf;
	u_char	*br_wptr;
	u_char	*br_wend;
	u_char	*br_rbuf;
	u_char	*br_rptr;
	u_char	*br_rend;

	unsigned int	(*br_application)(struct ber_element *);
};

/* well-known ber_element types */
#define BER_TYPE_DEFAULT	((unsigned int)-1)
#define BER_TYPE_EOC		0
#define BER_TYPE_BOOLEAN	1
#define BER_TYPE_INTEGER	2
#define BER_TYPE_BITSTRING	3
#define BER_TYPE_OCTETSTRING	4
#define BER_TYPE_NULL		5
#define BER_TYPE_OBJECT		6
#define BER_TYPE_ENUMERATED	10
#define BER_TYPE_SEQUENCE	16
#define BER_TYPE_SET		17

/* ber classes */
#define BER_CLASS_UNIVERSAL	0x0
#define BER_CLASS_UNIV		BER_CLASS_UNIVERSAL
#define BER_CLASS_APPLICATION	0x1
#define BER_CLASS_APP		BER_CLASS_APPLICATION
#define BER_CLASS_CONTEXT	0x2
#define BER_CLASS_PRIVATE	0x3
#define BER_CLASS_MASK		0x3

/* common definitions */
#define BER_MIN_OID_LEN		2		/* OBJECT */
#define BER_MAX_OID_LEN		32		/* OBJECT */
#define BER_MAX_SEQ_ELEMENTS	USHRT_MAX	/* 65535 */

struct ber_oid {
	u_int32_t	bo_id[BER_MAX_OID_LEN + 1];
	size_t		bo_n;
};

__BEGIN_DECLS
struct ber_element	*ber_get_element(unsigned int);
void			 ber_set_header(struct ber_element *, int,
			    unsigned int);
void			 ber_link_elements(struct ber_element *,
			    struct ber_element *);
struct ber_element	*ber_unlink_elements(struct ber_element *);
void			 ber_replace_elements(struct ber_element *,
			    struct ber_element *);
struct ber_element	*ber_add_sequence(struct ber_element *);
struct ber_element	*ber_add_set(struct ber_element *);
struct ber_element	*ber_add_integer(struct ber_element *, long long);
int			 ber_get_integer(struct ber_element *, long long *);
struct ber_element	*ber_add_enumerated(struct ber_element *, long long);
int			 ber_get_enumerated(struct ber_element *, long long *);
struct ber_element	*ber_add_boolean(struct ber_element *, int);
int			 ber_get_boolean(struct ber_element *, int *);
struct ber_element	*ber_add_string(struct ber_element *, const char *);
struct ber_element	*ber_add_nstring(struct ber_element *, const char *,
			    size_t);
struct ber_element	*ber_add_ostring(struct ber_element *,
			    struct ber_octetstring *);
int			 ber_get_string(struct ber_element *, char **);
int			 ber_get_nstring(struct ber_element *, void **,
			    size_t *);
int			 ber_get_ostring(struct ber_element *,
			    struct ber_octetstring *);
struct ber_element	*ber_add_bitstring(struct ber_element *, const void *,
			    size_t);
int			 ber_get_bitstring(struct ber_element *, void **,
			    size_t *);
struct ber_element	*ber_add_null(struct ber_element *);
int			 ber_get_null(struct ber_element *);
struct ber_element	*ber_add_eoc(struct ber_element *);
int			 ber_get_eoc(struct ber_element *);
struct ber_element	*ber_add_oid(struct ber_element *, struct ber_oid *);
struct ber_element	*ber_add_noid(struct ber_element *, struct ber_oid *, int);
struct ber_element	*ber_add_oidstring(struct ber_element *, const char *);
int			 ber_get_oid(struct ber_element *, struct ber_oid *);
size_t			 ber_oid2ber(struct ber_oid *, u_int8_t *, size_t);
int			 ber_string2oid(const char *, struct ber_oid *);
struct ber_element	*ber_printf_elements(struct ber_element *, char *, ...);
int			 ber_scanf_elements(struct ber_element *, char *, ...);
ssize_t			 ber_get_writebuf(struct ber *, void **);
ssize_t			 ber_write_elements(struct ber *, struct ber_element *);
void			 ber_set_readbuf(struct ber *, void *, size_t);
struct ber_element	*ber_read_elements(struct ber *, struct ber_element *);
off_t			 ber_getpos(struct ber_element *);
void			 ber_free_element(struct ber_element *);
void			 ber_free_elements(struct ber_element *);
size_t			 ber_calc_len(struct ber_element *);
void			 ber_set_application(struct ber *,
			    unsigned int (*)(struct ber_element *));
void			 ber_set_writecallback(struct ber_element *,
			    void (*)(void *, size_t), void *);
void			 ber_free(struct ber *);
int			 ber_oid_cmp(struct ber_oid *, struct ber_oid *);

__END_DECLS

#endif /* _BER_H */