Skip to content

Commit a604be4

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 <s@jaeckel.eu>
1 parent e16a7ba commit a604be4

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
@@ -598,17 +598,29 @@ int der_printable_value_decode(int v);
598598

599599
unsigned long der_utf8_charsize(const wchar_t c);
600600

601-
typedef struct {
601+
typedef int (*der_flexi_handler)(const ltc_asn1_list*, void*);
602+
603+
typedef struct der_flexi_check {
602604
ltc_asn1_type t;
605+
int optional;
603606
ltc_asn1_list **pp;
607+
der_flexi_handler handler;
608+
void *userdata;
604609
} der_flexi_check;
605610

606-
#define LTC_SET_DER_FLEXI_CHECK(list, index, Type, P) \
607-
do { \
608-
int LTC_SDFC_temp##__LINE__ = (index); \
609-
list[LTC_SDFC_temp##__LINE__].t = Type; \
610-
list[LTC_SDFC_temp##__LINE__].pp = P; \
611+
#define LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, P, Opt, Hndl, Udata) \
612+
do { \
613+
int LTC_SDFC_temp##__LINE__ = (index); \
614+
list[LTC_SDFC_temp##__LINE__].t = Type; \
615+
list[LTC_SDFC_temp##__LINE__].pp = P; \
616+
list[LTC_SDFC_temp##__LINE__].optional = Opt; \
617+
list[LTC_SDFC_temp##__LINE__].handler = (der_flexi_handler)Hndl; \
618+
list[LTC_SDFC_temp##__LINE__].userdata = Udata; \
611619
} while (0)
620+
#define LTC_SET_DER_FLEXI_CHECK(list, index, Type, P) LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, P, 0, NULL, NULL)
621+
#define LTC_SET_DER_FLEXI_CHECK_OPT(list, index, Type, P) LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, P, 1, NULL, NULL)
622+
#define LTC_SET_DER_FLEXI_HANDLER(list, index, Type, Hndl, Udata) LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, NULL, 0, Hndl, Udata)
623+
#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)
612624

613625

614626
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)