X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/4271ab433cd55bbd2612292bcf39e4dc3d7274f1..HEAD:/libs/smux/der_encoder.c?ds=inline diff --git a/libs/smux/der_encoder.c b/libs/smux/der_encoder.c index 1c014802..2c6a6f76 100644 --- a/libs/smux/der_encoder.c +++ b/libs/smux/der_encoder.c @@ -12,19 +12,17 @@ static ssize_t der_write_TL(ber_tlv_tag_t tag, ber_tlv_len_t len, * The DER encoder of any type. */ asn_enc_rval_t -der_encode(asn_TYPE_descriptor_t *type_descriptor, void *struct_ptr, - asn_app_consume_bytes_f *consume_bytes, void *app_key) { - - ASN_DEBUG("DER encoder invoked for %s", +der_encode(const asn_TYPE_descriptor_t *type_descriptor, const void *struct_ptr, + asn_app_consume_bytes_f *consume_bytes, void *app_key) { + ASN_DEBUG("DER encoder invoked for %s", type_descriptor->name); /* * Invoke type-specific encoder. */ - return type_descriptor->der_encoder(type_descriptor, - struct_ptr, /* Pointer to the destination structure */ - 0, 0, - consume_bytes, app_key); + return type_descriptor->op->der_encoder( + type_descriptor, struct_ptr, /* Pointer to the destination structure */ + 0, 0, consume_bytes, app_key); } /* @@ -51,15 +49,15 @@ static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) { * A variant of the der_encode() which encodes the data into the provided buffer */ asn_enc_rval_t -der_encode_to_buffer(asn_TYPE_descriptor_t *type_descriptor, void *struct_ptr, - void *buffer, size_t buffer_size) { - enc_to_buf_arg arg; +der_encode_to_buffer(const asn_TYPE_descriptor_t *type_descriptor, + const void *struct_ptr, void *buffer, size_t buffer_size) { + enc_to_buf_arg arg; asn_enc_rval_t ec; arg.buffer = buffer; arg.left = buffer_size; - ec = type_descriptor->der_encoder(type_descriptor, + ec = type_descriptor->op->der_encoder(type_descriptor, struct_ptr, /* Pointer to the destination structure */ 0, 0, encode_to_buffer_cb, &arg); if(ec.encoded != -1) { @@ -74,19 +72,20 @@ der_encode_to_buffer(asn_TYPE_descriptor_t *type_descriptor, void *struct_ptr, * Write out leading TL[v] sequence according to the type definition. */ ssize_t -der_write_tags(asn_TYPE_descriptor_t *sd, - size_t struct_length, - int tag_mode, int last_tag_form, - ber_tlv_tag_t tag, /* EXPLICIT or IMPLICIT tag */ - asn_app_consume_bytes_f *cb, - void *app_key) { - const ber_tlv_tag_t *tags; /* Copy of tags stream */ - int tags_count; /* Number of tags */ - size_t overall_length; - ssize_t *lens; - int i; - - ASN_DEBUG("Writing tags (%s, tm=%d, tc=%d, tag=%s, mtc=%d)", +der_write_tags(const asn_TYPE_descriptor_t *sd, size_t struct_length, + int tag_mode, int last_tag_form, + ber_tlv_tag_t tag, /* EXPLICIT or IMPLICIT tag */ + asn_app_consume_bytes_f *cb, void *app_key) { +#define ASN1_DER_MAX_TAGS_COUNT 4 + ber_tlv_tag_t + tags_buf_scratch[ASN1_DER_MAX_TAGS_COUNT * sizeof(ber_tlv_tag_t)]; + ssize_t lens[ASN1_DER_MAX_TAGS_COUNT * sizeof(ssize_t)]; + const ber_tlv_tag_t *tags; /* Copy of tags stream */ + int tags_count; /* Number of tags */ + size_t overall_length; + int i; + + ASN_DEBUG("Writing tags (%s, tm=%d, tc=%d, tag=%s, mtc=%d)", sd->name, tag_mode, sd->tags_count, ber_tlv_tag_string(tag), tag_mode @@ -95,6 +94,11 @@ der_write_tags(asn_TYPE_descriptor_t *sd, :sd->tags_count ); + if(sd->tags_count + 1 > ASN1_DER_MAX_TAGS_COUNT) { + ASN_DEBUG("System limit %d on tags count", ASN1_DER_MAX_TAGS_COUNT); + return -1; + } + if(tag_mode) { /* * Instead of doing shaman dance like we do in ber_check_tags(), @@ -102,12 +106,7 @@ der_write_tags(asn_TYPE_descriptor_t *sd, * and initialize it appropriately. */ int stag_offset; - ber_tlv_tag_t *tags_buf; - tags_buf = (ber_tlv_tag_t *)alloca((sd->tags_count + 1) * sizeof(ber_tlv_tag_t)); - if(!tags_buf) { /* Can fail on !x86 */ - errno = ENOMEM; - return -1; - } + ber_tlv_tag_t *tags_buf = tags_buf_scratch; tags_count = sd->tags_count + 1 /* EXPLICIT or IMPLICIT tag is given */ - ((tag_mode == -1) && sd->tags_count); @@ -126,12 +125,6 @@ der_write_tags(asn_TYPE_descriptor_t *sd, if(tags_count == 0) return 0; - lens = (ssize_t *)alloca(tags_count * sizeof(lens[0])); - if(!lens) { - errno = ENOMEM; - return -1; - } - /* * Array of tags is initialized. * Now, compute the size of the TLV pairs, from right to left. @@ -146,8 +139,8 @@ der_write_tags(asn_TYPE_descriptor_t *sd, if(!cb) return overall_length - struct_length; - ASN_DEBUG("%s %s TL sequence (%d elements)", - cb?"Encoding":"Estimating", sd->name, tags_count); + ASN_DEBUG("Encoding %s TL sequence (%d elements)", sd->name, + tags_count); /* * Encode the TL sequence for real.