suricata
util-decode-der.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011-2012 ANSSI
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /**
29  * \file
30  *
31  * \author Pierre Chifflier <pierre.chifflier@ssi.gouv.fr>
32  *
33  */
34 
35 #ifndef __UTIL_DECODE_DER_H__
36 #define __UTIL_DECODE_DER_H__
37 
38 #define ASN1_CLASS_UNIVERSAL 0
39 #define ASN1_CLASS_APPLICATION 1
40 #define ASN1_CLASS_CONTEXTSPEC 2
41 #define ASN1_CLASS_PRIVATE 3
42 
43 #define ASN1_UNKNOWN 0
44 #define ASN1_BOOLEAN 0x01
45 #define ASN1_INTEGER 0x02
46 #define ASN1_BITSTRING 0x03
47 #define ASN1_OCTETSTRING 0x04
48 #define ASN1_NULL 0x05
49 #define ASN1_OID 0x06
50 #define ASN1_UTF8STRING 0x0c
51 #define ASN1_SEQUENCE 0x10
52 #define ASN1_SET 0x11
53 #define ASN1_PRINTSTRING 0x13
54 #define ASN1_T61STRING 0x14
55 #define ASN1_IA5STRING 0x16
56 #define ASN1_UTCTIME 0x17
57 #define ASN1_GENERALIZEDTIME 0x18
58 
59 typedef struct Asn1ElementType_ {
60  uint8_t cls:2;
61  uint8_t pc:1;
62  uint8_t tag:5;
63 } __attribute__((packed)) Asn1ElementType;
64 
65 /* Generic ASN.1 element
66  * Presence and meaning of fields depends on the header and type values.
67  */
68 typedef struct Asn1Generic_ {
69  Asn1ElementType header;
70  uint8_t type;
71  uint32_t length; /* length of node, including header */
72 
73  struct Asn1Generic_ *data; /* only if type is structured */
74 
75  char *str;
76  uint32_t strlen;
77  uint64_t value;
78  struct Asn1Generic_ *next; /* only if type is sequence */
79 } Asn1Generic;
80 
81 /* Generic error (failed allocation, etc.) */
82 #define ERR_DER_GENERIC 0x01
83 /* Unknown ASN.1 element type */
84 #define ERR_DER_UNKNOWN_ELEMENT 0x02
85 /* One element requires to read more bytes than available */
86 #define ERR_DER_ELEMENT_SIZE_TOO_BIG 0x03
87 /* One element size is invalid (e.g more than 4 bytes long) */
88 #define ERR_DER_INVALID_SIZE 0x04
89 /* Unsupported string type */
90 #define ERR_DER_UNSUPPORTED_STRING 0x05
91 /* Missing field or element */
92 #define ERR_DER_MISSING_ELEMENT 0x06
93 /* Generic error */
94 #define ERR_DER_RECURSION_LIMIT 0x07
95 /* Unexpected or unknown tag */
96 #define ERR_DER_INVALID_TAG 0x08
97 /* Invalid element: empty object, etc. */
98 #define ERR_DER_INVALID_OBJECT 0x09
99 
100 Asn1Generic * DecodeDer(const unsigned char *buffer, uint32_t size, uint32_t *errcode) __attribute__((nonnull));
101 void DerFree(Asn1Generic *a);
102 
103 #ifdef AFLFUZZ_DER
104 int DerParseDataFromFile(char *filename);
105 #endif
106 
107 #endif /* __UTIL_DECODE_DER_H__ */
uint64_t value
Asn1Generic * DecodeDer(const unsigned char *buffer, uint32_t size, uint32_t *errcode) __attribute__((nonnull))
struct Asn1Generic_ * next
Asn1ElementType header
uint32_t length
struct Asn1Generic_ * data
void DerFree(Asn1Generic *a)
struct Asn1Generic_ Asn1Generic
struct Asn1ElementType_ __attribute__((packed)) Asn1ElementType
DNP3 application object header.
uint32_t strlen