The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
der.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/**
19 * $Id: 9e8b99447f54ba482b708a4d92ce2471e631dc11 $
20 *
21 * @file protocols/der/der.c
22 * @brief Structures and prototypes for base DER functionality.
23 *
24 * @author Ethan Thompson (ethan.thompson@inkbridge.io)
25 *
26 * @copyright 2025 Network RADIUS SAS (legal@networkradius.com)
27 */
28
29#include <freeradius-devel/build.h>
30#include <freeradius-devel/util/value.h>
31
32/** Enumeration describing the data types in a DER encoded structure
33 */
34typedef enum {
35 FR_DER_TAG_INVALID = 0x00, //!< Invalid tag.
36 FR_DER_TAG_BOOLEAN = 0x01, //!< Boolean true/false
37 FR_DER_TAG_INTEGER = 0x02, //!< Arbitrary width signed integer.
38 FR_DER_TAG_BITSTRING = 0x03, //!< String of bits (length field specifies bits).
39 FR_DER_TAG_OCTETSTRING = 0x04, //!< String of octets (length field specifies bytes).
40 FR_DER_TAG_NULL = 0x05, //!< An empty value.
41 FR_DER_TAG_OID = 0x06, //!< Reference to an OID based attribute.
42 FR_DER_TAG_ENUMERATED = 0x0a, //!< An enumerated value.
43 FR_DER_TAG_UTF8_STRING = 0x0c, //!< String of UTF8 chars.
44 FR_DER_TAG_SEQUENCE = 0x10, //!< A sequence of DER encoded data (a structure).
45 FR_DER_TAG_SET = 0x11, //!< A set of DER encoded data (a structure).
46 FR_DER_TAG_PRINTABLE_STRING = 0x13, //!< String of printable chars.
47 FR_DER_TAG_T61_STRING = 0x14, //!< String of T61 (8bit) chars.
48 FR_DER_TAG_IA5_STRING = 0x16, //!< String of IA5 (7bit) chars.
49 FR_DER_TAG_UTC_TIME = 0x17, //!< A time in UTC "YYMMDDhhmmssZ" format.
50 FR_DER_TAG_GENERALIZED_TIME = 0x18, //!< A time in "YYYYMMDDHHMMSS[.fff]Z" format.
51 FR_DER_TAG_VISIBLE_STRING = 0x1a, //!< String of visible chars.
52 FR_DER_TAG_GENERAL_STRING = 0x1b, //!< String of general chars.
53 FR_DER_TAG_UNIVERSAL_STRING = 0x1c, //!< String of universal chars.
54 FR_DER_TAG_BMP_STRING = 0x1e, //!< String of BMP chars.
55
56 FR_DER_TAG_CHOICE = 0x23, //!< A choice of types. Techically not a DER tag, but used to represent a choice.
57
58 FR_DER_TAG_MAX = 0x24
60
61#define FR_DER_TAG_VALUE_MAX (0x1f) //!< tags >=max can't exist
62
63typedef enum {
64 FR_DER_TAG_PRIMITIVE = 0x00, //!< This is a leaf value, it contains no children.
65 FR_DER_TAG_CONSTRUCTED = 0x20 //!< This is a sequence or set, it contains children.
67
75
76#define DER_MAX_STR 16384
77
78#define DER_UTC_TIME_LEN 13 //!< Length of the UTC time string.
79#define DER_GENERALIZED_TIME_LEN_MIN 15 //!< Minimum length of the generalized time string.
80#define DER_GENERALIZED_TIME_PRECISION_MAX 4 //!< Maximum precision of the generalized time string.
81
82#define DER_TAG_CLASS_MASK 0xc0 //!< Mask to extract the class from the tag.
83#define DER_TAG_CONSTRUCTED_MASK 0x20 //!< Mask to check if the tag is constructed.
84#define DER_TAG_NUM_MASK 0x1f //!< Mask to extract the tag number from the tag.
85
86#define DER_TAG_CONTINUATION 0x1f //!< Mask to check if the tag is a continuation.
87
88#define DER_LEN_MULTI_BYTE 0x80 //!< Mask to check if the length is multi-byte.
89
90#define DER_BOOLEAN_FALSE 0x00 //!< DER encoded boolean false value.
91#define DER_BOOLEAN_TRUE 0xff //!< DER encoded boolean true value.
92
93typedef struct {
94 fr_der_tag_class_t class; //!< tag Class
95 fr_der_tag_t der_type; //!< the DER type, which is different from the FreeRADIUS type
96 union {
97 fr_der_tag_t sequence_of;
98 fr_der_tag_t set_of;
99 fr_value_box_t *default_value;
100 };
101 uint64_t max; //!< maximum count of items in a sequence, set, or string.
102 uint32_t restrictions; //!< for choice of options and tags - no dups allowed
103 uint8_t min; //!< mininum count
104 uint8_t option; //!< an "attribute number" encoded in the tag field.
105 bool is_option : 1; //!< has an option defined
106 bool optional : 1; //!< optional, we MUST already have set 'option'
107 bool is_sequence_of : 1; //!< sequence_of has been defined
108 bool is_set_of : 1; //!< set_of has been defined
109 bool is_oid_and_value : 1; //!< is OID+value
110 bool is_extensions : 1; //!< a list of X.509 extensions
111 bool has_default_value : 1; //!< a default value exists
112 bool is_oid_leaf : 1;
113 bool is_choice : 1; //!< DER name "choice".
115
116typedef struct {
117 TALLOC_CTX *tmp_ctx; //!< ctx under which temporary data will be allocated
119
124
125#define fr_der_flag_option(_da) (fr_der_attr_flags(_da)->option)
126#define fr_der_flag_optional(_da) (fr_der_attr_flags(_da)->optional)
127#define fr_der_flag_class(_da) (fr_der_attr_flags(_da)->class)
128#define fr_der_flag_der_type(_da) (fr_der_attr_flags(_da)->der_type)
129#define fr_der_flag_sequence_of(_da) (fr_der_attr_flags(_da)->sequence_of)
130#define fr_der_flag_is_sequence_of(_da) (fr_der_attr_flags(_da)->is_sequence_of)
131#define fr_der_flag_set_of(_da) (fr_der_attr_flags(_da)->set_of)
132#define fr_der_flag_is_set_of(_da) (fr_der_attr_flags(_da)->is_set_of)
133#define fr_der_flag_max(_da) (fr_der_attr_flags(_da)->max)
134#define fr_der_flag_is_oid_and_value(_da) (fr_der_attr_flags(_da)->is_oid_and_value)
135#define fr_der_flag_is_extensions(_da) (fr_der_attr_flags(_da)->is_extensions)
136#define fr_der_flag_has_default_value(_da) ((fr_der_attr_flags(_da)->has_default_value) != NULL);
137#define fr_der_flag_is_oid_leaf(_da) (fr_der_attr_flags(_da)->is_oid_leaf)
138#define fr_der_flag_is_choice(_da) (fr_der_attr_flags(_da)->is_choice)
139
140/*
141 * base.c
142 */
146char const *fr_der_tag_to_str(fr_der_tag_t tag);
147
148int fr_der_global_init(void);
149void fr_der_global_free(void);
150
151/*
152 * decode.c
153 */
155 fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx);
fr_der_tag_t
Enumeration describing the data types in a DER encoded structure.
Definition der.h:34
@ FR_DER_TAG_IA5_STRING
String of IA5 (7bit) chars.
Definition der.h:48
@ FR_DER_TAG_SEQUENCE
A sequence of DER encoded data (a structure).
Definition der.h:44
@ FR_DER_TAG_SET
A set of DER encoded data (a structure).
Definition der.h:45
@ FR_DER_TAG_BMP_STRING
String of BMP chars.
Definition der.h:54
@ FR_DER_TAG_INTEGER
Arbitrary width signed integer.
Definition der.h:37
@ FR_DER_TAG_BOOLEAN
Boolean true/false.
Definition der.h:36
@ FR_DER_TAG_CHOICE
A choice of types. Techically not a DER tag, but used to represent a choice.
Definition der.h:56
@ FR_DER_TAG_UTF8_STRING
String of UTF8 chars.
Definition der.h:43
@ FR_DER_TAG_UTC_TIME
A time in UTC "YYMMDDhhmmssZ" format.
Definition der.h:49
@ FR_DER_TAG_GENERALIZED_TIME
A time in "YYYYMMDDHHMMSS[.fff]Z" format.
Definition der.h:50
@ FR_DER_TAG_INVALID
Invalid tag.
Definition der.h:35
@ FR_DER_TAG_NULL
An empty value.
Definition der.h:40
@ FR_DER_TAG_OCTETSTRING
String of octets (length field specifies bytes).
Definition der.h:39
@ FR_DER_TAG_VISIBLE_STRING
String of visible chars.
Definition der.h:51
@ FR_DER_TAG_BITSTRING
String of bits (length field specifies bits).
Definition der.h:38
@ FR_DER_TAG_T61_STRING
String of T61 (8bit) chars.
Definition der.h:47
@ FR_DER_TAG_ENUMERATED
An enumerated value.
Definition der.h:42
@ FR_DER_TAG_UNIVERSAL_STRING
String of universal chars.
Definition der.h:53
@ FR_DER_TAG_PRINTABLE_STRING
String of printable chars.
Definition der.h:46
@ FR_DER_TAG_GENERAL_STRING
String of general chars.
Definition der.h:52
@ FR_DER_TAG_OID
Reference to an OID based attribute.
Definition der.h:41
@ FR_DER_TAG_MAX
Definition der.h:58
bool fr_der_tags_compatible(fr_der_tag_t tag1, fr_der_tag_t tag2)
Definition base.c:99
bool optional
optional, we MUST already have set 'option'
Definition der.h:106
bool is_extensions
a list of X.509 extensions
Definition der.h:110
void fr_der_global_free(void)
Definition base.c:204
fr_der_tag_t der_type
the DER type, which is different from the FreeRADIUS type
Definition der.h:95
bool is_option
has an option defined
Definition der.h:105
bool is_sequence_of
sequence_of has been defined
Definition der.h:107
TALLOC_CTX * tmp_ctx
ctx under which temporary data will be allocated
Definition der.h:117
bool is_set_of
set_of has been defined
Definition der.h:108
int fr_der_global_init(void)
Definition base.c:181
uint32_t restrictions
for choice of options and tags - no dups allowed
Definition der.h:102
bool fr_type_to_der_tag_valid(fr_type_t type, fr_der_tag_t tag)
Definition base.c:173
fr_der_tag_t fr_type_to_der_tag_default(fr_type_t type)
Definition base.c:805
char const * fr_der_tag_to_str(fr_der_tag_t tag)
Definition base.c:76
fr_der_tag_constructed_t
Definition der.h:63
@ FR_DER_TAG_CONSTRUCTED
This is a sequence or set, it contains children.
Definition der.h:65
@ FR_DER_TAG_PRIMITIVE
This is a leaf value, it contains no children.
Definition der.h:64
uint8_t min
mininum count
Definition der.h:103
ssize_t fr_der_decode_pair_dbuff(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
Definition decode.c:2362
bool is_oid_and_value
is OID+value
Definition der.h:109
static fr_der_attr_flags_t const * fr_der_attr_flags(fr_dict_attr_t const *da)
Definition der.h:120
bool is_choice
DER name "choice".
Definition der.h:113
uint8_t option
an "attribute number" encoded in the tag field.
Definition der.h:104
bool has_default_value
a default value exists
Definition der.h:111
uint64_t max
maximum count of items in a sequence, set, or string.
Definition der.h:101
fr_der_tag_class_t
Definition der.h:68
@ FR_DER_CLASS_APPLICATION
Definition der.h:70
@ FR_DER_CLASS_CONTEXT
Definition der.h:71
@ FR_DER_CLASS_UNIVERSAL
Definition der.h:69
@ FR_DER_CLASS_INVALID
Definition der.h:73
@ FR_DER_CLASS_PRIVATE
Definition der.h:72
@ FR_DICT_ATTR_EXT_PROTOCOL_SPECIFIC
Protocol specific extensions.
Definition dict.h:177
static fr_slen_t in
Definition dict.h:841
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition dict_ext.h:140
fr_type_t
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
fr_aka_sim_id_type_t type
static fr_slen_t parent
Definition pair.h:839
static size_t char ** out
Definition value.h:1020