Skip to content

Commit 994b6a1

Browse files
committed
move default to der_attr_flags_t
which means that the default value is in attr_flags
1 parent 3a57d57 commit 994b6a1

File tree

10 files changed

+42
-39
lines changed

10 files changed

+42
-39
lines changed

share/dictionary/der/dictionary.common

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ END DirectoryName
6161
DEFINE GeneralSubtree sequence
6262
BEGIN GeneralSubtree
6363
DEFINE base sequence clone=@.GeneralName
64-
ATTRIBUTE minimum 0 integer has_default,option,optional
65-
VALUE minimum DEFAULT 0
64+
ATTRIBUTE minimum 0 integer option,optional,default=0
6665
ATTRIBUTE maximum 1 integer option,optional
6766
END GeneralSubtree
6867

share/dictionary/der/dictionary.extensions

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ ATTRIBUTE issuerAltName 2.5.29.18 group der_type=sequence,sequence_of=choice,
4444

4545
ATTRIBUTE basicConstraints 2.5.29.19 sequence is_oid_leaf
4646
BEGIN 2.5.29.19
47-
DEFINE cA boolean has_default
48-
VALUE cA DEFAULT false
47+
DEFINE cA boolean default=false
4948
DEFINE pathLenConstraint integer optional
5049
END 2.5.29.19
5150

share/dictionary/der/dictionary.oids

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ ATTRIBUTE ecPublicKey 1.2.840.10045.2.1 oid is_oid_leaf
1313

1414
ATTRIBUTE signatures 1.2.840.10045.4 sequence
1515
ATTRIBUTE ecdsa-with-SHA2 1.2.840.10045.4.3 sequence
16-
ATTRIBUTE ecdsa-with-SHA384 1.2.840.10045.4.3.3 bool is_oid_leaf,has_default
17-
VALUE 1.2.840.10045.4.3.3 DEFAULT false
16+
ATTRIBUTE ecdsa-with-SHA384 1.2.840.10045.4.3.3 bool is_oid_leaf,default=false
1817

1918
ATTRIBUTE rsadsi 1.2.840.113549 sequence
2019
ATTRIBUTE pkcs 1.2.840.113549.1 sequence

src/protocols/der/base.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,30 @@ static int dict_flag_class(fr_dict_attr_t **da_p, char const *value, UNUSED fr_d
251251
return 0;
252252
}
253253

254-
static int dict_flag_has_default(fr_dict_attr_t **da_p, UNUSED char const *value, UNUSED fr_dict_flag_parser_rule_t const *rules)
254+
static int dict_flag_default_value(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rules)
255255
{
256256
fr_der_attr_flags_t *flags = fr_dict_attr_ext(*da_p, FR_DICT_ATTR_EXT_PROTOCOL_SPECIFIC);
257257

258-
flags->has_default = true;
258+
if (!fr_type_is_leaf((*da_p)->type)) {
259+
fr_strerror_printf("Cannot set 'default=...' for attribute %s DER type %s",
260+
(*da_p)->name, fr_der_tag_to_str(flags->der_type));
261+
return -1;
262+
}
263+
264+
/*
265+
* The default values are parented from the dict root. That way we don't need to copy the values
266+
* when we clone the attribute, we can just copy the pointer.
267+
*/
268+
flags->default_value = fr_value_box_alloc(fr_dict_unconst((*da_p)->dict), (*da_p)->type, NULL);
269+
if (!flags->default_value) return -1;
270+
271+
if (fr_value_box_from_str(flags->default_value, flags->default_value, (*da_p)->type, NULL,
272+
value, strlen(value), NULL, false) < 0) {
273+
fr_strerror_printf("Failed parsing 'value=...' - %s", fr_strerror());
274+
return -1;
275+
}
276+
277+
flags->has_default_value = true;
259278

260279
return 0;
261280
}
@@ -587,8 +606,8 @@ static int dict_flag_optional(fr_dict_attr_t **da_p, UNUSED char const *value, U
587606

588607
static const fr_dict_flag_parser_t der_flags[] = {
589608
{ L("class"), { .func = dict_flag_class } },
609+
{ L("default"), { .func = dict_flag_default_value,.needs_value = true } },
590610
{ L("der_type"), { .func = dict_flag_der_type, .needs_value = true } },
591-
{ L("has_default"), { .func = dict_flag_has_default } },
592611
{ L("is_extensions"), { .func = dict_flag_is_extensions } },
593612
{ L("is_oid_leaf"), { .func = dict_flag_is_oid_leaf } },
594613
{ L("max"), { .func = dict_flag_max, .needs_value = true } },

src/protocols/der/decode.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,9 +2428,8 @@ static ssize_t fr_der_decode_pair_dbuff(TALLOC_CTX *ctx, fr_pair_list_t *out, fr
24282428
*/
24292429
if (unlikely(slen == 0)) {
24302430
fr_pair_t *vp;
2431-
fr_dict_enum_value_t *ev;
24322431

2433-
if (likely(!flags->has_default)) return 0;
2432+
if (likely(!flags->has_default_value)) return 0;
24342433

24352434
create_default:
24362435
vp = fr_pair_afrom_da(ctx, parent);
@@ -2439,16 +2438,11 @@ static ssize_t fr_der_decode_pair_dbuff(TALLOC_CTX *ctx, fr_pair_list_t *out, fr
24392438
return -1;
24402439
}
24412440

2442-
ev = fr_dict_enum_by_name(parent, "DEFAULT", strlen("DEFAULT"));
2443-
if (unlikely(ev == NULL)) {
2444-
fr_strerror_printf_push("No DEFAULT value for attribute %s", parent->name);
2445-
error:
2441+
if (fr_value_box_copy(vp, &vp->data, flags->default_value) < 0) {
24462442
talloc_free(vp);
24472443
return -1;
24482444
}
24492445

2450-
if (fr_value_box_copy(vp, &vp->data, ev->value) < 0) goto error;
2451-
24522446
vp->data.enumv = vp->da;
24532447

24542448
fr_pair_append(out, vp);
@@ -2497,7 +2491,7 @@ static ssize_t fr_der_decode_pair_dbuff(TALLOC_CTX *ctx, fr_pair_list_t *out, fr
24972491
* NULL.
24982492
*/
24992493
if (unlikely(fr_dbuff_remaining(&our_in) == 0)) {
2500-
if (flags->has_default) goto create_default;
2494+
if (flags->has_default_value) goto create_default;
25012495

25022496
if (tag == FR_DER_TAG_NULL) {
25032497
func = &tag_funcs[FR_DER_TAG_NULL];
@@ -2531,7 +2525,7 @@ static ssize_t fr_der_decode_pair_dbuff(TALLOC_CTX *ctx, fr_pair_list_t *out, fr
25312525
/*
25322526
* Optional or not, if we can create a default value, then do so.
25332527
*/
2534-
if (flags->has_default) goto create_default;
2528+
if (flags->has_default_value) goto create_default;
25352529

25362530
/*
25372531
* Optional means "decoded nothing". Otherwise it's a hard failure.

src/protocols/der/der.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include <freeradius-devel/build.h>
2929
#include <freeradius-devel/util/dict.h>
30-
#include <freeradius-devel/util/types.h>
30+
#include <freeradius-devel/util/value.h>
3131

3232
extern HIDDEN fr_dict_t const *dict_der;
3333

@@ -98,6 +98,7 @@ typedef struct {
9898
union {
9999
fr_der_tag_t sequence_of;
100100
fr_der_tag_t set_of;
101+
fr_value_box_t *default_value;
101102
};
102103
uint64_t max; //!< maximum count of items in a sequence, set, or string.
103104
uint32_t restrictions; //!< for choice of options and tags - no dups allowed
@@ -107,13 +108,14 @@ typedef struct {
107108
bool optional : 1; //!< optional, we MUST already have set 'option'
108109
bool is_sequence_of : 1; //!< sequence_of has been defined
109110
bool is_set_of : 1; //!< set_of has been defined
110-
bool is_oid_and_value : 1; //!< is OID+value
111+
bool is_oid_and_value : 1; //!< is OID+value
111112
bool is_extensions : 1; //!< a list of X.509 extensions
112-
bool has_default : 1; //!< a default value exists
113+
bool has_default_value : 1; //!< a default value exists
113114
bool is_oid_leaf : 1;
114115
bool is_choice : 1; //!< DER name "choice".
115116
} fr_der_attr_flags_t;
116117

118+
117119
static inline fr_der_attr_flags_t const *fr_der_attr_flags(fr_dict_attr_t const *da)
118120
{
119121
return fr_dict_attr_ext(da, FR_DICT_ATTR_EXT_PROTOCOL_SPECIFIC);
@@ -130,7 +132,7 @@ static inline fr_der_attr_flags_t const *fr_der_attr_flags(fr_dict_attr_t const
130132
#define fr_der_flag_max(_da) (fr_der_attr_flags(_da)->max)
131133
#define fr_der_flag_is_oid_and_value(_da) (fr_der_attr_flags(_da)->is_oid_and_value)
132134
#define fr_der_flag_is_extensions(_da) (fr_der_attr_flags(_da)->is_extensions)
133-
#define fr_der_flag_has_default(_da) (fr_der_attr_flags(_da)->has_default)
135+
#define fr_der_flag_has_default_value(_da) ((fr_der_attr_flags(_da)->has_default_value) != NULL);
134136
#define fr_der_flag_is_oid_leaf(_da) (fr_der_attr_flags(_da)->is_oid_leaf)
135137
#define fr_der_flag_is_choice(_da) (fr_der_attr_flags(_da)->is_choice)
136138

src/protocols/der/encode.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,19 +1865,11 @@ static ssize_t encode_value(fr_dbuff_t *dbuff, UNUSED fr_da_stack_t *da_stack, U
18651865
*
18661866
*/
18671867

1868-
if (flags->has_default) {
1868+
if (flags->has_default_value) {
18691869
/*
18701870
* Skip encoding the default value, as per ISO/IEC 8825-1:2021 11.5
18711871
*/
1872-
fr_dict_enum_value_t const *evp;
1873-
1874-
evp = fr_dict_enum_by_name(vp->da, "DEFAULT", strlen("DEFAULT"));
1875-
if (unlikely(!evp)) {
1876-
fr_strerror_printf("No default value for %s", vp->da->name);
1877-
return -1;
1878-
}
1879-
1880-
if (fr_value_box_cmp(&vp->data, evp->value) == 0) {
1872+
if (fr_value_box_cmp(&vp->data, flags->default_value) == 0) {
18811873
FR_PROTO_TRACE("Skipping default value");
18821874
fr_dcursor_next(cursor);
18831875
return 0;

src/tests/unit/protocols/der/base.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ decode-pair 30 06 02 01 09 01 01 FF
195195
match Foo-Bar = { Test-Integer = 9, Test-Boolean = yes }
196196

197197
decode-pair 30 06 02 01 01 01 01 FF
198-
match Foo-Bar = { Test-Integer = ::DEFAULT, Test-Boolean = yes }
198+
match Foo-Bar = { Test-Integer = 1, Test-Boolean = yes }
199199

200200
decode-pair 30 03 01 01 FF
201-
match Foo-Bar = { Test-Integer = ::DEFAULT, Test-Boolean = yes }
201+
match Foo-Bar = { Test-Integer = 1, Test-Boolean = yes }
202202

203203
decode-pair 30 06 02 01 09 01 01 01
204204
match Boolean is not correctly DER encoded (0x00 or 0xff).: Failed decoding Foo-Bar

src/tests/unit/protocols/der/dictionary.test

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ END Bar
4747

4848
DEFINE Foo-Bar sequence
4949
BEGIN Foo-Bar
50-
DEFINE Test-Integer integer has_default
51-
VALUE Test-Integer DEFAULT 1
50+
DEFINE Test-Integer integer default=1
5251
DEFINE Test-Boolean bool
5352
END Foo-Bar
5453

0 commit comments

Comments
 (0)