1 /*
2  * ProFTPD - mod_snmp ASN.1 support
3  * Copyright (c) 2008-2016 TJ Saunders
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  *
19  * As a special exemption, TJ Saunders and other respective copyright holders
20  * give permission to link this program with OpenSSL, and distribute the
21  * resulting executable, without including the source code for OpenSSL in the
22  * source distribution.
23  */
24 
25 #ifndef MOD_SNMP_ASN1_H
26 #define MOD_SNMP_ASN1_H
27 
28 #include "mod_snmp.h"
29 
30 typedef uint32_t oid_t;
31 
32 /* ASN.1 OIDs */
33 
34 /* Per RFC 1905, Section 4.1, an SNMP OID can have a maximum of 128
35  * sub-identifiers.
36  */
37 #define SNMP_ASN1_OID_MAX_LEN		128
38 #define SNMP_ASN1_OID_MAX_ID		0xffff
39 
40 /* ASN.1 Tag Types (RFC1155, Section 3.2.1) */
41 #define SNMP_ASN1_TYPE_BOOLEAN		0x01
42 #define SNMP_ASN1_TYPE_INTEGER		0x02
43 #define SNMP_ASN1_TYPE_BITSTRING	0x03
44 #define SNMP_ASN1_TYPE_OCTETSTRING	0x04
45 #define SNMP_ASN1_TYPE_NULL		0x05
46 #define SNMP_ASN1_TYPE_OID		0x06
47 #define SNMP_ASN1_TYPE_SEQUENCE		0x10
48 #define SNMP_ASN1_TYPE_SET		0x11
49 
50 /* ASN.1 Tag Class values */
51 #define SNMP_ASN1_CLASS_UNIVERSAL	0x00
52 #define SNMP_ASN1_CLASS_APPLICATION	0x40
53 #define SNMP_ASN1_CLASS_CONTEXT		0x80
54 #define SNMP_ASN1_CLASS_PRIVATE		0xc0
55 
56 /* ASN.1 Tag Primitive/Construct values */
57 #define SNMP_ASN1_PRIMITIVE		0x00
58 #define SNMP_ASN1_CONSTRUCT		0x20
59 
60 /* ASN.1 Tag Length values */
61 #define SNMP_ASN1_LEN_LONG		0x80
62 #define SNMP_ASN1_LEN_EXTENSION		0xff
63 #define SNMP_ASN1_LEN_INDEFINITE	0x80
64 
65 const char *snmp_asn1_get_oidstr(pool *p, oid_t *asn1_oid,
66   unsigned int asn1_oidlen);
67 const char *snmp_asn1_get_tagstr(pool *p, unsigned char asn1_type);
68 
69 /* API flags */
70 #define SNMP_ASN1_FL_KNOWN_LEN		0x01
71 #define SNMP_ASN1_FL_NO_TRACE_TYPESTR	0x02
72 #define SNMP_ASN1_FL_UNSIGNED		0x04
73 
74 int snmp_asn1_read_header(pool *p, unsigned char **buf, size_t *buflen,
75   unsigned char *asn1_type, unsigned int *asn1_len, int flags);
76 int snmp_asn1_read_int(pool *p, unsigned char **buf, size_t *buflen,
77   unsigned char *asn1_type, long *asn1_int, int flags);
78 int snmp_asn1_read_uint(pool *p, unsigned char **buf, size_t *buflen,
79   unsigned char *asn1_type, unsigned long *asn1_uint);
80 int snmp_asn1_read_null(pool *p, unsigned char **buf, size_t *buflen,
81   unsigned char *asn1_type);
82 int snmp_asn1_read_oid(pool *p, unsigned char **buf, size_t *buflen,
83   unsigned char *asn1_type, oid_t *asn1_oid, unsigned int *asn1_oidlen);
84 
85 /* XXX Need a matching snmp_asn1_read_bitstring() function? */
86 int snmp_asn1_read_string(pool *p, unsigned char **buf, size_t *buflen,
87   unsigned char *asn1_type, char **asn_1str, unsigned int *asn1_strlen);
88 
89 /* XXX Need an snmp_asn1_read_sequence() function? */
90 
91 int snmp_asn1_write_header(pool *p, unsigned char **buf, size_t *buflen,
92   unsigned char asn1_type, unsigned int asn1_len, int flags);
93 int snmp_asn1_write_int(pool *p, unsigned char **buf, size_t *buflen,
94   unsigned char asn1_type, long asn1_int, int flags);
95 int snmp_asn1_write_uint(pool *p, unsigned char **buf, size_t *buflen,
96   unsigned char asn1_type, unsigned long asn1_uint);
97 int snmp_asn1_write_null(pool *p, unsigned char **buf, size_t *buflen,
98   unsigned char asn1_type);
99 int snmp_asn1_write_oid(pool *p, unsigned char **buf, size_t *buflen,
100   unsigned char asn1_type, oid_t *asn1_oid, unsigned int asn1_oidlen);
101 
102 /* XXX Need a matching snmp_asn1_write_bitstring() function? */
103 int snmp_asn1_write_string(pool *p, unsigned char **buf, size_t *buflen,
104   unsigned char asn1_type, const char *asn1_str, unsigned int asn1_strlen);
105 
106 /* The asn1_ex argument should be an enum, for the different exception
107  * identifiers:
108  *
109  *  noSuchObject(0)
110  *  noSuchInstance(1)
111  *  endOfMibView(2)
112  *
113  *
114  * XXX Need a corresponding snmp_asn1_read_exception() function.
115  */
116 int snmp_asn1_write_exception(pool *p, unsigned char **buf, size_t *buflen,
117   unsigned char asn1_type, unsigned char asn1_ex);
118 
119 /* XXX Need an snmp_asn1_write_sequence() function? */
120 
121 #endif /* MOD_SNMP_ASN1_H */
122