Skip to content

Commit c9b609a

Browse files
committed
Extend der_flexi_sequence_cmp()
To be able to do a bit more, add an optional handler callback function. Additional to that, also make it possible to mark elements as optional. Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent 7e78899 commit c9b609a

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/headers/tomcrypt_private.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -651,17 +651,29 @@ int der_printable_value_decode(int v);
651651

652652
unsigned long der_utf8_charsize(const wchar_t c);
653653

654-
typedef struct {
654+
typedef int (*der_flexi_handler)(const ltc_asn1_list*, void*);
655+
656+
typedef struct der_flexi_check {
655657
ltc_asn1_type t;
658+
int optional;
656659
ltc_asn1_list **pp;
660+
der_flexi_handler handler;
661+
void *userdata;
657662
} der_flexi_check;
658663

659-
#define LTC_SET_DER_FLEXI_CHECK(list, index, Type, P) \
660-
do { \
661-
int LTC_SDFC_temp##__LINE__ = (index); \
662-
list[LTC_SDFC_temp##__LINE__].t = Type; \
663-
list[LTC_SDFC_temp##__LINE__].pp = P; \
664+
#define LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, P, Opt, Hndl, Udata) \
665+
do { \
666+
int LTC_SDFC_temp##__LINE__ = (index); \
667+
list[LTC_SDFC_temp##__LINE__].t = Type; \
668+
list[LTC_SDFC_temp##__LINE__].pp = P; \
669+
list[LTC_SDFC_temp##__LINE__].optional = Opt; \
670+
list[LTC_SDFC_temp##__LINE__].handler = (der_flexi_handler)Hndl; \
671+
list[LTC_SDFC_temp##__LINE__].userdata = Udata; \
664672
} while (0)
673+
#define LTC_SET_DER_FLEXI_CHECK(list, index, Type, P) LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, P, 0, NULL, NULL)
674+
#define LTC_SET_DER_FLEXI_CHECK_OPT(list, index, Type, P) LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, P, 1, NULL, NULL)
675+
#define LTC_SET_DER_FLEXI_HANDLER(list, index, Type, Hndl, Udata) LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, NULL, 0, Hndl, Udata)
676+
#define LTC_SET_DER_FLEXI_HANDLER_OPT(list, index, Type, Hndl, Udata) LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, NULL, 1, Hndl, Udata)
665677

666678

667679
extern const ltc_asn1_type der_asn1_tag_to_type_map[];

src/pk/asn1/der/sequence/der_flexi_sequence_cmp.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@ int der_flexi_sequence_cmp(const ltc_asn1_list *flexi, der_flexi_check *check)
2424
return CRYPT_INVALID_PACKET;
2525
}
2626
cur = flexi->child;
27-
while(check->t != LTC_ASN1_EOL) {
27+
while(check->t != LTC_ASN1_EOL && cur) {
2828
if (!LTC_ASN1_IS_TYPE(cur, check->t)) {
29+
if (check->optional) {
30+
check++;
31+
continue;
32+
}
2933
return CRYPT_INVALID_PACKET;
3034
}
3135
if (check->pp != NULL) *check->pp = cur;
36+
else if (check->handler) {
37+
int err = check->handler(cur, check->userdata);
38+
if (err != CRYPT_OK)
39+
return err;
40+
}
3241
cur = cur->next;
3342
check++;
3443
}

0 commit comments

Comments
 (0)