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-2021 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 2.1.2
29 **/
30
31#ifndef _ASN1_H
32#define _ASN1_H
33
34//Dependencies
35#include "core/crypto.h"
36
37//Multiple precision integer arithmetic supported?
38#if (MPI_SUPPORT == ENABLED)
39 #include "mpi/mpi.h"
40#endif
41
42//Tag number mask
43#define ASN1_TAG_NUMBER_MASK 0x1F
44
45//ASN.1 encoding
46#define ASN1_ENCODING_MASK 0x20
47#define ASN1_ENCODING_PRIMITIVE 0x00
48#define ASN1_ENCODING_CONSTRUCTED 0x20
49
50//ASN.1 class
51#define ASN1_CLASS_MASK 0xC0
52#define ASN1_CLASS_UNIVERSAL 0x00
53#define ASN1_CLASS_APPLICATION 0x40
54#define ASN1_CLASS_CONTEXT_SPECIFIC 0x80
55#define ASN1_CLASS_PRIVATE 0xC0
56
57//C++ guard
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62
63/**
64 * @brief ASN.1 data types
65 **/
66
67typedef enum
68{
94} Asn1Type;
95
96
97/**
98 * @brief ASN.1 tag
99 **/
100
101typedef struct
102{
106 size_t length;
107 const uint8_t *value;
109} Asn1Tag;
110
111
112//ASN.1 related functions
113error_t asn1ReadTag(const uint8_t *data, size_t length, Asn1Tag *tag);
114error_t asn1ReadSequence(const uint8_t *data, size_t length, Asn1Tag *tag);
115error_t asn1ReadOctetString(const uint8_t *data, size_t length, Asn1Tag *tag);
116error_t asn1ReadOid(const uint8_t *data, size_t length, Asn1Tag *tag);
117
118error_t asn1ReadBoolean(const uint8_t *data, size_t length, Asn1Tag *tag,
119 bool_t *value);
120
121error_t asn1ReadInt32(const uint8_t *data, size_t length, Asn1Tag *tag,
122 int32_t *value);
123
124error_t asn1WriteTag(Asn1Tag *tag, bool_t reverse, uint8_t *data,
125 size_t *written);
126
127error_t asn1WriteInt32(int32_t value, bool_t reverse, uint8_t *data,
128 size_t *written);
129
130#if (MPI_SUPPORT == ENABLED)
131
132error_t asn1ReadMpi(const uint8_t *data, size_t length, Asn1Tag *tag,
133 Mpi *value);
134
135error_t asn1WriteMpi(const Mpi *value, bool_t reverse, uint8_t *data,
136 size_t *written);
137
138#endif
139
140error_t asn1CheckTag(const Asn1Tag *tag, bool_t constructed, uint_t objClass,
141 uint_t objType);
142
143error_t asn1CheckOid(const Asn1Tag *tag, const uint8_t *oid, size_t length);
144
145error_t asn1DumpObject(const uint8_t *data, size_t length, uint_t level);
146
147//C++ guard
148#ifdef __cplusplus
149}
150#endif
151
152#endif
error_t asn1ReadOctetString(const uint8_t *data, size_t length, Asn1Tag *tag)
Read an octet string from the input stream.
Definition: asn1.c:190
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:495
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
Asn1Type
ASN.1 data types.
Definition: asn1.h:68
@ ASN1_TYPE_GENERAL_STRING
Definition: asn1.h:91
@ ASN1_TYPE_OBJECT_DESCRIPTOR
Definition: asn1.h:75
@ ASN1_TYPE_VISIBLE_STRING
Definition: asn1.h:90
@ ASN1_TYPE_BMP_STRING
Definition: asn1.h:93
@ ASN1_TYPE_ENUMERATED
Definition: asn1.h:78
@ ASN1_TYPE_GENERALIZED_TIME
Definition: asn1.h:88
@ ASN1_TYPE_VIDEOTEX_STRING
Definition: asn1.h:85
@ ASN1_TYPE_OBJECT_IDENTIFIER
Definition: asn1.h:74
@ ASN1_TYPE_NUMERIC_STRING
Definition: asn1.h:82
@ ASN1_TYPE_BOOLEAN
Definition: asn1.h:69
@ ASN1_TYPE_UNIVERSAL_STRING
Definition: asn1.h:92
@ ASN1_TYPE_SET
Definition: asn1.h:81
@ ASN1_TYPE_UTC_TIME
Definition: asn1.h:87
@ ASN1_TYPE_REAL
Definition: asn1.h:77
@ ASN1_TYPE_BIT_STRING
Definition: asn1.h:71
@ ASN1_TYPE_PRINTABLE_STRING
Definition: asn1.h:83
@ ASN1_TYPE_TELETEX_STRING
Definition: asn1.h:84
@ ASN1_TYPE_NULL
Definition: asn1.h:73
@ ASN1_TYPE_OCTET_STRING
Definition: asn1.h:72
@ ASN1_TYPE_IA5_STRING
Definition: asn1.h:86
@ ASN1_TYPE_UTF8_STRING
Definition: asn1.h:79
@ ASN1_TYPE_EXTERNAL
Definition: asn1.h:76
@ ASN1_TYPE_INTEGER
Definition: asn1.h:70
@ ASN1_TYPE_SEQUENCE
Definition: asn1.h:80
@ ASN1_TYPE_GRAPHIC_STRING
Definition: asn1.h:89
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:591
error_t asn1WriteTag(Asn1Tag *tag, bool_t reverse, uint8_t *data, size_t *written)
Write an ASN.1 tag.
Definition: asn1.c:334
error_t asn1CheckOid(const Asn1Tag *tag, const uint8_t *oid, size_t length)
Check ASN.1 tag against a specified OID.
Definition: asn1.c:679
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:653
error_t asn1ReadOid(const uint8_t *data, size_t length, Asn1Tag *tag)
Read an object identifier from the input stream.
Definition: asn1.c:218
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
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:553
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
error_t asn1DumpObject(const uint8_t *data, size_t length, uint_t level)
Display an ASN.1 data object.
Definition: asn1.c:706
unsigned int uint_t
Definition: compiler_port.h:45
int bool_t
Definition: compiler_port.h:49
General definitions for cryptographic algorithms.
uint8_t value[]
Definition: dtls_misc.h:150
error_t
Error codes.
Definition: error.h:43
uint8_t oid[1]
Definition: mib_common.h:186
MPI (Multiple Precision Integer Arithmetic)
uint8_t data[]
Definition: sftp_common.h:228
uint32_t length
Definition: sftp_common.h:214
ASN.1 tag.
Definition: asn1.h:102
size_t totalLength
Definition: asn1.h:108
const uint8_t * value
Definition: asn1.h:107
uint_t objClass
Definition: asn1.h:104
uint_t objType
Definition: asn1.h:105
bool_t constructed
Definition: asn1.h:103
size_t length
Definition: asn1.h:106
Arbitrary precision integer.
Definition: mpi.h:70
uint8_t level
Definition: tls.h:1747