From dbb8882971aab46ba24a0c940197016b2475881b Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Wed, 24 Feb 2021 22:11:41 -0600 Subject: [PATCH] asn1: Fix some of the primitive comparators The comparators for BIT STRING and unconstrained INTEGER need help too. --- lib/asn1/der_cmp.c | 70 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/lib/asn1/der_cmp.c b/lib/asn1/der_cmp.c index f4612c469..94a124b89 100644 --- a/lib/asn1/der_cmp.c +++ b/lib/asn1/der_cmp.c @@ -36,20 +36,44 @@ int der_heim_oid_cmp(const heim_oid *p, const heim_oid *q) { - if (p->length != q->length) - return (int)(p->length - q->length); - return memcmp(p->components, - q->components, - p->length * sizeof(*p->components)); + int c; + + if (p->length == q->length) + return memcmp(p->components, + q->components, + p->length * sizeof(*p->components)); + if (p->length < q->length) { + c = memcmp(p->components, + q->components, + p->length * sizeof(*p->components)); + if (c == 0) + return -1; + return c; + } + c = memcmp(p->components, + q->components, + q->length * sizeof(*p->components)); + if (c == 0) + return 1; + return c; } int der_heim_octet_string_cmp(const heim_octet_string *p, const heim_octet_string *q) { - if (p->length != q->length) - return (int)(p->length - q->length); - return memcmp(p->data, q->data, p->length); + int c; + + if (p->length == q->length) + return memcmp(p->data, q->data, p->length); + if (p->length < q->length) { + if ((c = memcmp(p->data, q->data, p->length)) == 0) + return -1; + return c; + } + if ((c = memcmp(p->data, q->data, q->length)) == 0) + return 1; + return c; } int @@ -102,16 +126,34 @@ der_heim_integer_cmp(const heim_integer *p, int der_heim_bmp_string_cmp(const heim_bmp_string *p, const heim_bmp_string *q) { - if (p->length != q->length) - return (int)(p->length - q->length); - return memcmp(p->data, q->data, q->length * sizeof(q->data[0])); + int c; + + if (p->length == q->length) + return memcmp(p->data, q->data, p->length * sizeof(q->data[0])); + if (p->length < q->length) { + if ((c = memcmp(p->data, q->data, p->length * sizeof(q->data[0]))) == 0) + return -1; + return c; + } + if ((c = memcmp(p->data, q->data, q->length * sizeof(q->data[0]))) == 0) + return 1; + return c; } int der_heim_universal_string_cmp(const heim_universal_string *p, const heim_universal_string *q) { - if (p->length != q->length) - return (int)(p->length - q->length); - return memcmp(p->data, q->data, q->length * sizeof(q->data[0])); + int c; + + if (p->length == q->length) + return memcmp(p->data, q->data, p->length * sizeof(q->data[0])); + if (p->length < q->length) { + if ((c = memcmp(p->data, q->data, p->length * sizeof(q->data[0]))) == 0) + return -1; + return c; + } + if ((c = memcmp(p->data, q->data, q->length * sizeof(q->data[0]))) == 0) + return 1; + return c; }