1 /**
2  * @file
3  * Abstract Syntax Notation One (ISO 8824, 8825) codec.
4  */
5 
6 /*
7  * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
8  * Copyright (c) 2016 Elias Oenal.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * Author: Christiaan Simons <christiaan.simons@axon.tv>
34  *         Martin Hentschel <info@cl-soft.de>
35  *         Elias Oenal <lwip@eliasoenal.com>
36  */
37 
38 #ifndef LWIP_HDR_APPS_SNMP_ASN1_H
39 #define LWIP_HDR_APPS_SNMP_ASN1_H
40 
41 #include "lwip/apps/snmp_opts.h"
42 
43 #if LWIP_SNMP
44 
45 #include "lwip/err.h"
46 #include "lwip/apps/snmp_core.h"
47 #include "snmp_pbuf_stream.h"
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 #define SNMP_ASN1_TLV_INDEFINITE_LENGTH 0x80
54 
55 #define SNMP_ASN1_CLASS_MASK        0xC0
56 #define SNMP_ASN1_CONTENTTYPE_MASK  0x20
57 #define SNMP_ASN1_DATATYPE_MASK     0x1F
58 #define SNMP_ASN1_DATATYPE_EXTENDED 0x1F /* DataType indicating that datatype is encoded in following bytes */
59 
60 /* context specific (SNMP) tags (from SNMP spec. RFC1157 and RFC1905) */
61 #define SNMP_ASN1_CONTEXT_PDU_GET_REQ      0
62 #define SNMP_ASN1_CONTEXT_PDU_GET_NEXT_REQ 1
63 #define SNMP_ASN1_CONTEXT_PDU_GET_RESP     2
64 #define SNMP_ASN1_CONTEXT_PDU_SET_REQ      3
65 #define SNMP_ASN1_CONTEXT_PDU_TRAP         4
66 #define SNMP_ASN1_CONTEXT_PDU_GET_BULK_REQ 5
67 #define SNMP_ASN1_CONTEXT_PDU_INFORM_REQ   6
68 #define SNMP_ASN1_CONTEXT_PDU_V2_TRAP      7
69 #define SNMP_ASN1_CONTEXT_PDU_REPORT       8
70 
71 #define SNMP_ASN1_CONTEXT_VARBIND_NO_SUCH_OBJECT      0
72 #define SNMP_ASN1_CONTEXT_VARBIND_END_OF_MIB_VIEW     2
73 
74 struct snmp_asn1_tlv
75 {
76   u8_t  type;       /* only U8 because extended types are not specified by SNMP */
77   u8_t  type_len;   /* encoded length of 'type' field (normally 1) */
78   u8_t  length_len; /* indicates how many bytes are required to encode the 'value_len' field */
79   u16_t value_len;  /* encoded length of the value */
80 };
81 #define SNMP_ASN1_TLV_HDR_LENGTH(tlv) ((tlv).type_len + (tlv).length_len)
82 #define SNMP_ASN1_TLV_LENGTH(tlv) ((tlv).type_len + (tlv).length_len + (tlv).value_len)
83 #define SNMP_ASN1_SET_TLV_PARAMS(tlv, type_, length_len_, value_len_) do { (tlv).type = (type_); (tlv).type_len = 0; (tlv).length_len = (length_len_); (tlv).value_len = (value_len_); } while (0);
84 
85 err_t snmp_asn1_dec_tlv(struct snmp_pbuf_stream* pbuf_stream, struct snmp_asn1_tlv* tlv);
86 err_t snmp_asn1_dec_u32t(struct snmp_pbuf_stream *pbuf_stream, u16_t len, u32_t *value);
87 err_t snmp_asn1_dec_u64t(struct snmp_pbuf_stream *pbuf_stream, u16_t len, u32_t *value);
88 err_t snmp_asn1_dec_s32t(struct snmp_pbuf_stream *pbuf_stream, u16_t len, s32_t *value);
89 err_t snmp_asn1_dec_oid(struct snmp_pbuf_stream *pbuf_stream, u16_t len, u32_t* oid, u8_t* oid_len, u8_t oid_max_len);
90 err_t snmp_asn1_dec_raw(struct snmp_pbuf_stream *pbuf_stream, u16_t len, u8_t *buf, u16_t* buf_len, u16_t buf_max_len);
91 
92 err_t snmp_ans1_enc_tlv(struct snmp_pbuf_stream* pbuf_stream, struct snmp_asn1_tlv* tlv);
93 
94 void snmp_asn1_enc_length_cnt(u16_t length, u8_t *octets_needed);
95 void snmp_asn1_enc_u32t_cnt(u32_t value, u16_t *octets_needed);
96 void snmp_asn1_enc_u64t_cnt(const u32_t *value, u16_t *octets_needed);
97 void snmp_asn1_enc_s32t_cnt(s32_t value, u16_t *octets_needed);
98 void snmp_asn1_enc_oid_cnt(const u32_t *oid, u16_t oid_len, u16_t *octets_needed);
99 err_t snmp_asn1_enc_oid(struct snmp_pbuf_stream* pbuf_stream, const u32_t *oid, u16_t oid_len);
100 err_t snmp_asn1_enc_s32t(struct snmp_pbuf_stream* pbuf_stream, u16_t octets_needed, s32_t value);
101 err_t snmp_asn1_enc_u32t(struct snmp_pbuf_stream* pbuf_stream, u16_t octets_needed, u32_t value);
102 err_t snmp_asn1_enc_u64t(struct snmp_pbuf_stream* pbuf_stream, u16_t octets_needed, const u32_t* value);
103 err_t snmp_asn1_enc_raw(struct snmp_pbuf_stream* pbuf_stream, const u8_t *raw, u16_t raw_len);
104 
105 #ifdef __cplusplus
106 }
107 #endif
108 
109 #endif /* LWIP_SNMP */
110 
111 #endif /* LWIP_HDR_APPS_SNMP_ASN1_H */
112