check length size in default BMPString and UniversalString constraints

This commit is contained in:
Lev Walkin 2017-09-06 23:19:48 -07:00
parent 86526e3fe6
commit b7a202e567
5 changed files with 65 additions and 8 deletions

View File

@ -27,7 +27,7 @@ asn_TYPE_operation_t asn_OP_BMPString = {
OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
BMPString_print,
OCTET_STRING_compare,
asn_generic_no_constraint, /* No constraint by default */
BMPString_constraint,
OCTET_STRING_decode_ber,
OCTET_STRING_encode_der,
BMPString_decode_xer, /* Convert from UTF-8 */
@ -52,7 +52,7 @@ asn_TYPE_descriptor_t asn_DEF_BMPString = {
"BMPString",
"BMPString",
&asn_OP_BMPString,
asn_generic_no_constraint, /* No constraint by default */
BMPString_constraint,
asn_DEF_BMPString_tags,
sizeof(asn_DEF_BMPString_tags)
/ sizeof(asn_DEF_BMPString_tags[0]) - 1,
@ -65,6 +65,28 @@ asn_TYPE_descriptor_t asn_DEF_BMPString = {
&asn_SPC_BMPString_specs
};
int
BMPString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_constraint_failed_f *ctfailcb,
void *app_key) {
const BMPString_t *st = (const BMPString_t *)sptr;
if(st && st->buf) {
if(st->size & 1) {
ASN__CTFAIL(app_key, td, sptr,
"%s: invalid size %zu not divisible by 2 (%s:%d)",
td->name, st->size, __FILE__, __LINE__);
return -1;
}
} else {
ASN__CTFAIL(app_key, td, sptr, "%s: value not given (%s:%d)", td->name,
__FILE__, __LINE__);
return -1;
}
return 0;
}
/*
* BMPString specific contents printer.
*/

View File

@ -18,6 +18,7 @@ extern asn_TYPE_operation_t asn_OP_BMPString;
extern asn_OCTET_STRING_specifics_t asn_SPC_BMPString_specs;
asn_struct_print_f BMPString_print; /* Human-readable output */
asn_constr_check_f BMPString_constraint;
xer_type_decoder_f BMPString_decode_xer;
xer_type_encoder_f BMPString_encode_xer;

View File

@ -27,7 +27,7 @@ asn_TYPE_operation_t asn_OP_UniversalString = {
OCTET_STRING_free,
UniversalString_print, /* Convert into UTF8 and print */
OCTET_STRING_compare,
asn_generic_no_constraint,
UniversalString_constraint,
OCTET_STRING_decode_ber,
OCTET_STRING_encode_der,
UniversalString_decode_xer, /* Convert from UTF-8 */
@ -52,7 +52,7 @@ asn_TYPE_descriptor_t asn_DEF_UniversalString = {
"UniversalString",
"UniversalString",
&asn_OP_UniversalString,
asn_generic_no_constraint,
UniversalString_constraint,
asn_DEF_UniversalString_tags,
sizeof(asn_DEF_UniversalString_tags)
/ sizeof(asn_DEF_UniversalString_tags[0]) - 1,
@ -65,6 +65,27 @@ asn_TYPE_descriptor_t asn_DEF_UniversalString = {
&asn_SPC_UniversalString_specs
};
int
UniversalString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_constraint_failed_f *ctfailcb,
void *app_key) {
const UniversalString_t *st = (const UniversalString_t *)sptr;
if(st && st->buf) {
if(st->size & 3) {
ASN__CTFAIL(app_key, td, sptr,
"%s: invalid size %zu not divisible by 4 (%s:%d)",
td->name, st->size, __FILE__, __LINE__);
return -1;
}
} else {
ASN__CTFAIL(app_key, td, sptr, "%s: value not given (%s:%d)", td->name,
__FILE__, __LINE__);
return -1;
}
return 0;
}
static ssize_t
UniversalString__dump(const UniversalString_t *st,

View File

@ -18,6 +18,7 @@ extern asn_TYPE_operation_t asn_OP_UniversalString;
extern asn_OCTET_STRING_specifics_t asn_SPC_UniversalString_specs;
asn_struct_print_f UniversalString_print; /* Human-readable output */
asn_constr_check_f UniversalString_constraint;
xer_type_decoder_f UniversalString_decode_xer;
xer_type_encoder_f UniversalString_encode_xer;

View File

@ -9,13 +9,21 @@ static void
check_encode_failed(asn_TYPE_descriptor_t *td, const char *buf, size_t buflen) {
uint8_t uper_output_buffer[32];
UniversalString_t *st_in;
char error_buf[128];
size_t error_buf_len = sizeof(error_buf);
st_in = OCTET_STRING_new_fromBuf(td, buf, buflen);
assert(st_in);
assert(st_in->size == buflen);
asn_enc_rval_t enc =
uper_encode_to_buffer(&asn_DEF_UniversalString, st_in,
uper_output_buffer, sizeof(uper_output_buffer));
/* First signal that something is wrong with the length */
int st_in_ct = asn_check_constraints(td, st_in, error_buf, &error_buf_len);
assert(st_in_ct != 0);
fprintf(stderr, "%s\n", error_buf);
/* Second signal that something is wrong with the length */
asn_enc_rval_t enc = uper_encode_to_buffer(td, st_in, uper_output_buffer,
sizeof(uper_output_buffer));
assert(enc.encoded == -1);
ASN_STRUCT_FREE(*td, st_in);
@ -31,14 +39,18 @@ check_round_trip_OK(asn_TYPE_descriptor_t *td, const char *buf, size_t buflen) {
assert(st_in);
assert(st_in->size == buflen);
int st_in_ct = asn_check_constraints(td, st_in, NULL, NULL);
assert(st_in_ct == 0);
asn_enc_rval_t enc =
uper_encode_to_buffer(&asn_DEF_UniversalString, st_in,
uper_encode_to_buffer(td, st_in,
uper_output_buffer, sizeof(uper_output_buffer));
assert(enc.encoded > 0);
asn_dec_rval_t dec =
uper_decode(0, &asn_DEF_UniversalString, (void **)&st_out,
uper_output_buffer, (enc.encoded + 7) / 8, 0, 0);
int st_out_ct = asn_check_constraints(td, st_out, NULL, NULL);
assert(st_out_ct == 0);
assert(dec.consumed == enc.encoded);
assert(st_in->size == st_out->size);
assert(memcmp(st_in->buf, st_out->buf, st_in->size) == 0);