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  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCrypto Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.6
29  **/
30 
31 #ifndef _ASN1_H
32 #define _ASN1_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "mpi/mpi.h"
37 
38 //Tag number mask
39 #define ASN1_TAG_NUMBER_MASK 0x1F
40 
41 //ASN.1 encoding
42 #define ASN1_ENCODING_MASK 0x20
43 #define ASN1_ENCODING_PRIMITIVE 0x00
44 #define ASN1_ENCODING_CONSTRUCTED 0x20
45 
46 //ASN.1 class
47 #define ASN1_CLASS_MASK 0xC0
48 #define ASN1_CLASS_UNIVERSAL 0x00
49 #define ASN1_CLASS_APPLICATION 0x40
50 #define ASN1_CLASS_CONTEXT_SPECIFIC 0x80
51 #define ASN1_CLASS_PRIVATE 0xC0
52 
53 //C++ guard
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 
59 /**
60  * @brief ASN.1 data types
61  **/
62 
63 typedef enum
64 {
90 } Asn1Type;
91 
92 
93 /**
94  * @brief ASN.1 tag
95  **/
96 
97 typedef struct
98 {
102  size_t length;
103  const uint8_t *value;
104  size_t totalLength;
105 } Asn1Tag;
106 
107 
108 //ASN.1 related functions
109 error_t asn1ReadTag(const uint8_t *data, size_t length, Asn1Tag *tag);
110 error_t asn1ReadSequence(const uint8_t *data, size_t length, Asn1Tag *tag);
111 error_t asn1ReadOctetString(const uint8_t *data, size_t length, Asn1Tag *tag);
112 error_t asn1ReadOid(const uint8_t *data, size_t length, Asn1Tag *tag);
113 
114 error_t asn1ReadBoolean(const uint8_t *data, size_t length, Asn1Tag *tag,
115  bool_t *value);
116 
117 error_t asn1ReadInt32(const uint8_t *data, size_t length, Asn1Tag *tag,
118  int32_t *value);
119 
120 error_t asn1ReadMpi(const uint8_t *data, size_t length, Asn1Tag *tag,
121  Mpi *value);
122 
123 error_t asn1WriteTag(Asn1Tag *tag, bool_t reverse, uint8_t *data,
124  size_t *written);
125 
126 error_t asn1WriteInt32(int32_t value, bool_t reverse, uint8_t *data,
127  size_t *written);
128 
129 error_t asn1WriteMpi(const Mpi *value, bool_t reverse, uint8_t *data,
130  size_t *written);
131 
132 error_t asn1CheckTag(const Asn1Tag *tag, bool_t constructed, uint_t objClass,
133  uint_t objType);
134 
135 error_t asn1CheckOid(const Asn1Tag *tag, const uint8_t *oid, size_t length);
136 
137 error_t asn1DumpObject(const uint8_t *data, size_t length, uint_t level);
138 
139 //C++ guard
140 #ifdef __cplusplus
141 }
142 #endif
143 
144 #endif
uint8_t length
Definition: dtls_misc.h:149
@ ASN1_TYPE_UTC_TIME
Definition: asn1.h:83
@ ASN1_TYPE_GENERALIZED_TIME
Definition: asn1.h:84
int bool_t
Definition: compiler_port.h:49
Arbitrary precision integer.
Definition: mpi.h:69
@ ASN1_TYPE_VIDEOTEX_STRING
Definition: asn1.h:81
error_t asn1CheckOid(const Asn1Tag *tag, const uint8_t *oid, size_t length)
Check ASN.1 tag against a specified OID.
Definition: asn1.c:663
@ ASN1_TYPE_UTF8_STRING
Definition: asn1.h:75
@ ASN1_TYPE_IA5_STRING
Definition: asn1.h:82
error_t asn1DumpObject(const uint8_t *data, size_t length, uint_t level)
Display an ASN.1 data object.
Definition: asn1.c:690
uint8_t level
Definition: tls.h:1702
error_t asn1ReadInt32(const uint8_t *data, size_t length, Asn1Tag *tag, int32_t *value)
Read a 32-bit integer from the input stream.
Definition: asn1.c:285
size_t totalLength
Definition: asn1.h:104
size_t length
Definition: asn1.h:102
error_t asn1WriteTag(Asn1Tag *tag, bool_t reverse, uint8_t *data, size_t *written)
Write an ASN.1 tag.
Definition: asn1.c:377
error_t
Error codes.
Definition: error.h:42
@ ASN1_TYPE_GRAPHIC_STRING
Definition: asn1.h:85
@ ASN1_TYPE_ENUMERATED
Definition: asn1.h:74
@ ASN1_TYPE_REAL
Definition: asn1.h:73
ASN.1 tag.
Definition: asn1.h:97
error_t asn1WriteMpi(const Mpi *value, bool_t reverse, uint8_t *data, size_t *written)
Write a multiple-precision integer from the output stream.
Definition: asn1.c:572
@ ASN1_TYPE_UNIVERSAL_STRING
Definition: asn1.h:88
error_t asn1ReadTag(const uint8_t *data, size_t length, Asn1Tag *tag)
Read an ASN.1 tag from the input stream.
Definition: asn1.c:52
@ ASN1_TYPE_VISIBLE_STRING
Definition: asn1.h:86
MPI (Multiple Precision Integer Arithmetic)
General definitions for cryptographic algorithms.
uint8_t oid[1]
Definition: mib_common.h:186
error_t asn1WriteInt32(int32_t value, bool_t reverse, uint8_t *data, size_t *written)
Write a 32-bit integer to the output stream.
Definition: asn1.c:516
error_t asn1ReadOid(const uint8_t *data, size_t length, Asn1Tag *tag)
Read an object identifier from the input stream.
Definition: asn1.c:218
uint_t objClass
Definition: asn1.h:100
@ ASN1_TYPE_PRINTABLE_STRING
Definition: asn1.h:79
error_t asn1ReadOctetString(const uint8_t *data, size_t length, Asn1Tag *tag)
Read an octet string from the input stream.
Definition: asn1.c:190
@ ASN1_TYPE_TELETEX_STRING
Definition: asn1.h:80
@ ASN1_TYPE_OCTET_STRING
Definition: asn1.h:68
@ ASN1_TYPE_INTEGER
Definition: asn1.h:66
Asn1Type
ASN.1 data types.
Definition: asn1.h:63
error_t asn1ReadSequence(const uint8_t *data, size_t length, Asn1Tag *tag)
Read an ASN.1 sequence from the input stream.
Definition: asn1.c:163
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:637
error_t asn1ReadBoolean(const uint8_t *data, size_t length, Asn1Tag *tag, bool_t *value)
Read a boolean from the input stream.
Definition: asn1.c:247
@ ASN1_TYPE_OBJECT_DESCRIPTOR
Definition: asn1.h:71
bool_t constructed
Definition: asn1.h:99
@ ASN1_TYPE_OBJECT_IDENTIFIER
Definition: asn1.h:70
@ ASN1_TYPE_SEQUENCE
Definition: asn1.h:76
@ ASN1_TYPE_BMP_STRING
Definition: asn1.h:89
@ ASN1_TYPE_BIT_STRING
Definition: asn1.h:67
uint8_t value[]
Definition: dtls_misc.h:150
@ ASN1_TYPE_EXTERNAL
Definition: asn1.h:72
unsigned int uint_t
Definition: compiler_port.h:45
uint8_t data[]
Definition: dtls_misc.h:176
@ ASN1_TYPE_NUMERIC_STRING
Definition: asn1.h:78
@ ASN1_TYPE_BOOLEAN
Definition: asn1.h:65
@ ASN1_TYPE_SET
Definition: asn1.h:77
const uint8_t * value
Definition: asn1.h:103
error_t asn1ReadMpi(const uint8_t *data, size_t length, Asn1Tag *tag, Mpi *value)
Read a multiple-precision integer from the input stream.
Definition: asn1.c:334
uint_t objType
Definition: asn1.h:101
@ ASN1_TYPE_GENERAL_STRING
Definition: asn1.h:87
@ ASN1_TYPE_NULL
Definition: asn1.h:69