From 1ea635f9ae29f39c919a7c443c826c1cc08ff9d8 Mon Sep 17 00:00:00 2001 From: Francisco Paisana Date: Wed, 16 Dec 2020 17:47:01 +0000 Subject: [PATCH] asn1 utils fix: avoid unpacking an unconstrained integer length twice --- lib/src/asn1/asn1_utils.cc | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/src/asn1/asn1_utils.cc b/lib/src/asn1/asn1_utils.cc index 55af5c2ce..d463817ec 100644 --- a/lib/src/asn1/asn1_utils.cc +++ b/lib/src/asn1/asn1_utils.cc @@ -689,7 +689,6 @@ IntType unconstrained_whole_number_length(IntType n) template SRSASN_CODE pack_unconstrained_whole_number(bit_ref& bref, IntType n, bool aligned) { - // TODO: Test uint32_t len = unconstrained_whole_number_length(n); if (aligned) { HANDLE_CODE(bref.align_bytes_zero()); @@ -699,11 +698,8 @@ SRSASN_CODE pack_unconstrained_whole_number(bit_ref& bref, IntType n, bool align return SRSASN_SUCCESS; } template -SRSASN_CODE unpack_unconstrained_whole_number(IntType& n, cbit_ref& bref, bool aligned) +SRSASN_CODE unpack_unconstrained_whole_number(IntType& n, cbit_ref& bref, uint32_t len, bool aligned) { - // TODO: Test - uint32_t len; - HANDLE_CODE(unpack_length(len, bref, aligned)); if (aligned) { HANDLE_CODE(bref.align_bytes()); } @@ -715,18 +711,21 @@ template SRSASN_CODE pack_unconstrained_whole_number(bit_ref& bref, int8 template SRSASN_CODE pack_unconstrained_whole_number(bit_ref& bref, int16_t n, bool aligned); template SRSASN_CODE pack_unconstrained_whole_number(bit_ref& bref, int32_t n, bool aligned); template SRSASN_CODE pack_unconstrained_whole_number(bit_ref& bref, int64_t n, bool aligned); -template SRSASN_CODE unpack_unconstrained_whole_number(int8_t& n, cbit_ref& bref, bool aligned); -template SRSASN_CODE unpack_unconstrained_whole_number(int16_t& n, cbit_ref& bref, bool aligned); -template SRSASN_CODE unpack_unconstrained_whole_number(int32_t& n, cbit_ref& bref, bool aligned); -template SRSASN_CODE unpack_unconstrained_whole_number(int64_t& n, cbit_ref& bref, bool aligned); +template SRSASN_CODE unpack_unconstrained_whole_number(int8_t& n, cbit_ref& bref, uint32_t len, bool aligned); +template SRSASN_CODE unpack_unconstrained_whole_number(int16_t& n, cbit_ref& bref, uint32_t len, bool aligned); +template SRSASN_CODE unpack_unconstrained_whole_number(int32_t& n, cbit_ref& bref, uint32_t len, bool aligned); +template SRSASN_CODE unpack_unconstrained_whole_number(int64_t& n, cbit_ref& bref, uint32_t len, bool aligned); template SRSASN_CODE pack_unconstrained_whole_number(bit_ref& bref, uint8_t n, bool aligned); template SRSASN_CODE pack_unconstrained_whole_number(bit_ref& bref, uint16_t n, bool aligned); template SRSASN_CODE pack_unconstrained_whole_number(bit_ref& bref, uint32_t n, bool aligned); template SRSASN_CODE pack_unconstrained_whole_number(bit_ref& bref, uint64_t n, bool aligned); -template SRSASN_CODE unpack_unconstrained_whole_number(uint8_t& n, cbit_ref& bref, bool aligned); -template SRSASN_CODE unpack_unconstrained_whole_number(uint16_t& n, cbit_ref& bref, bool aligned); -template SRSASN_CODE unpack_unconstrained_whole_number(uint32_t& n, cbit_ref& bref, bool aligned); -template SRSASN_CODE unpack_unconstrained_whole_number(uint64_t& n, cbit_ref& bref, bool aligned); +template SRSASN_CODE unpack_unconstrained_whole_number(uint8_t& n, cbit_ref& bref, uint32_t len, bool aligned); +template SRSASN_CODE +unpack_unconstrained_whole_number(uint16_t& n, cbit_ref& bref, uint32_t len, bool aligned); +template SRSASN_CODE +unpack_unconstrained_whole_number(uint32_t& n, cbit_ref& bref, uint32_t len, bool aligned); +template SRSASN_CODE +unpack_unconstrained_whole_number(uint64_t& n, cbit_ref& bref, uint32_t len, bool aligned); /********************* varlength_packing @@ -882,6 +881,7 @@ SRSASN_CODE pack_integer(bit_ref& bref, IntType n, IntType lb, IntType ub, bool // } HANDLE_CODE(pack_constrained_whole_number(bref, n, (IntType)lb, (IntType)ub, aligned)); } else { + // See X.691 - 12.2.6 if (not within_bounds or (not lower_bounded and not upper_bounded)) { HANDLE_CODE(pack_length(bref, unconstrained_whole_number_length(n), aligned)); HANDLE_CODE(pack_unconstrained_whole_number(bref, n, aligned)); @@ -939,10 +939,11 @@ SRSASN_CODE unpack_integer(IntType& n, cbit_ref& bref, IntType lb, IntType ub, b // TODO: Check if we are in the indefinite length case, and pack length prefix if needed HANDLE_CODE(unpack_constrained_whole_number(n, bref, (IntType)lb, (IntType)ub, aligned)); } else { + // See X.691 - 12.2.6 if (not within_bounds or (not lower_bounded and not upper_bounded)) { uint32_t len; HANDLE_CODE(unpack_length(len, bref, aligned)); - HANDLE_CODE(unpack_unconstrained_whole_number(n, bref, aligned)); // TODO + HANDLE_CODE(unpack_unconstrained_whole_number(n, bref, len, aligned)); } else { // pack as semi-constrained // TODO