asn1.h
Go to the documentation of this file.
1 /**
2  * @file asn1.h
3  * @brief ASN.1 (Abstract Syntax Notation One)
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneCrypto Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _ASN1_H
30 #define _ASN1_H
31 
32 //Dependencies
33 #include "core/crypto.h"
34 
35 //Tag number mask
36 #define ASN1_TAG_NUMBER_MASK 0x1F
37 
38 //ASN.1 encoding
39 #define ASN1_ENCODING_MASK 0x20
40 #define ASN1_ENCODING_PRIMITIVE 0x00
41 #define ASN1_ENCODING_CONSTRUCTED 0x20
42 
43 //ASN.1 class
44 #define ASN1_CLASS_MASK 0xC0
45 #define ASN1_CLASS_UNIVERSAL 0x00
46 #define ASN1_CLASS_APPLICATION 0x40
47 #define ASN1_CLASS_CONTEXT_SPECIFIC 0x80
48 #define ASN1_CLASS_PRIVATE 0xC0
49 
50 //C++ guard
51 #ifdef __cplusplus
52  extern "C" {
53 #endif
54 
55 
56 /**
57  * @brief ASN.1 data types
58  **/
59 
60 typedef enum
61 {
87 } Asn1Type;
88 
89 
90 /**
91  * @brief ASN.1 tag
92  **/
93 
94 typedef struct
95 {
99  size_t length;
100  const uint8_t *value;
101  size_t totalLength;
102 } Asn1Tag;
103 
104 
105 //ASN.1 related functions
106 error_t asn1ReadTag(const uint8_t *data, size_t length, Asn1Tag *tag);
107 error_t asn1ReadSequence(const uint8_t *data, size_t length, Asn1Tag *tag);
108 error_t asn1ReadInt32(const uint8_t *data, size_t length, Asn1Tag *tag, int32_t *value);
109 
110 error_t asn1WriteTag(Asn1Tag *tag, bool_t reverse, uint8_t *data, size_t *written);
111 error_t asn1WriteInt32(int32_t value, bool_t reverse, uint8_t *data, size_t *written);
112 
113 error_t asn1CheckTag(const Asn1Tag *tag, bool_t constructed, uint_t objClass, uint_t objType);
114 error_t asn1CheckOid(const Asn1Tag *tag, const uint8_t *oid, size_t length);
115 
116 error_t asn1DumpObject(const uint8_t *data, size_t length, uint_t level);
117 
118 //C++ guard
119 #ifdef __cplusplus
120  }
121 #endif
122 
123 #endif
uint_t objType
Definition: asn1.h:98
error_t asn1DumpObject(const uint8_t *data, size_t length, uint_t level)
Display an ASN.1 data object.
Definition: asn1.c:478
error_t asn1CheckTag(const Asn1Tag *tag, bool_t constructed, uint_t objClass, uint_t objType)
Enforce the type of a specified tag.
Definition: asn1.c:426
size_t totalLength
Definition: asn1.h:101
General definitions for cryptographic algorithms.
error_t asn1ReadSequence(const uint8_t *data, size_t length, Asn1Tag *tag)
Read an ASN.1 sequence from the input stream.
Definition: asn1.c:158
error_t asn1WriteInt32(int32_t value, bool_t reverse, uint8_t *data, size_t *written)
Write an integer to the output stream.
Definition: asn1.c:372
error_t asn1WriteTag(Asn1Tag *tag, bool_t reverse, uint8_t *data, size_t *written)
Write an ASN.1 tag.
Definition: asn1.c:234
uint8_t level
Definition: tls.h:1696
Asn1Type
ASN.1 data types.
Definition: asn1.h:60
ASN.1 tag.
Definition: asn1.h:94
size_t length
Definition: asn1.h:99
bool_t constructed
Definition: asn1.h:96
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
error_t asn1ReadTag(const uint8_t *data, size_t length, Asn1Tag *tag)
Read an ASN.1 tag from the input stream.
Definition: asn1.c:50
uint8_t data[]
Definition: dtls_misc.h:167
uint8_t value[]
Definition: dtls_misc.h:141
uint_t objClass
Definition: asn1.h:97
error_t asn1CheckOid(const Asn1Tag *tag, const uint8_t *oid, size_t length)
Check ASN.1 tag against a specified OID.
Definition: asn1.c:451
uint8_t length
Definition: dtls_misc.h:140
uint8_t oid[1]
Definition: mib_common.h:184
error_t asn1ReadInt32(const uint8_t *data, size_t length, Asn1Tag *tag, int32_t *value)
Read an integer from the input stream.
Definition: asn1.c:186
int bool_t
Definition: compiler_port.h:47
const uint8_t * value
Definition: asn1.h:100