/* yes, this is ugly. We don't know in advance how many bytes the length
of a tag will take, so we assumed 1 byte. If we were wrong then we
need to correct our mistake */
- if (len > 255) {
+ if (len > 0xFFFF) {
+ data->data[nesting->start] = 0x83;
+ if (!asn1_write_uint8(data, 0)) return False;
+ if (!asn1_write_uint8(data, 0)) return False;
+ if (!asn1_write_uint8(data, 0)) return False;
+ memmove(data->data+nesting->start+4, data->data+nesting->start+1, len);
+ data->data[nesting->start+1] = (len>>16) & 0xFF;
+ data->data[nesting->start+2] = (len>>8) & 0xFF;
+ data->data[nesting->start+3] = len&0xff;
+ } else if (len > 255) {
data->data[nesting->start] = 0x82;
if (!asn1_write_uint8(data, 0)) return False;
if (!asn1_write_uint8(data, 0)) return False;
if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len)
return False;
- if (data->ofs + len > data->length)
+ if (data->ofs + len > data->length) {
+ /* we need to mark the buffer as consumed, so the caller knows
+ this was an out of data error, and not a decode error */
+ data->ofs = data->length;
return False;
+ }
memcpy(p, data->data + data->ofs, len);
return True;
}
-/* Get the length to be expected in buf */
-BOOL asn1_object_length(uint8_t *buf, size_t buf_length,
- uint8_t tag, size_t *result)
-{
- struct asn1_data data;
-
- /* Fake the asn1_load to avoid the memdup, this is just to be able to
- * re-use the length-reading in asn1_start_tag */
- ZERO_STRUCT(data);
- data.data = buf;
- data.length = buf_length;
- if (!asn1_start_tag(&data, tag))
- return False;
- *result = asn1_tag_remaining(&data)+data.ofs;
- /* We can't use asn1_end_tag here, as we did not consume the complete
- * tag, so asn1_end_tag would flag an error and not free nesting */
- talloc_free(data.nesting);
- return True;
-}
-
/* stop reading a tag */
BOOL asn1_end_tag(struct asn1_data *data)
{
do {
asn1_read_uint8(data, &b);
v = (v<<7) | (b&0x7f);
- } while (!data->has_error && b & 0x80);
+ } while (!data->has_error && (b & 0x80));
tmp_oid = talloc_asprintf_append(tmp_oid, " %u", v);
}
uint8_t b;
*i = 0;
- while (asn1_tag_remaining(data)>0) {
+ while (!data->has_error && asn1_tag_remaining(data)>0) {
if (!asn1_read_uint8(data, &b)) return False;
*i = (*i << 8) + b;
}
if (!asn1_start_tag(data, ASN1_INTEGER)) return False;
if (!asn1_read_implicit_Integer(data, i)) return False;
return asn1_end_tag(data);
-
}
/* read an interger */
*v = 0;
if (!asn1_start_tag(data, ASN1_ENUMERATED)) return False;
- while (asn1_tag_remaining(data)>0) {
+ while (!data->has_error && asn1_tag_remaining(data)>0) {
uint8_t b;
asn1_read_uint8(data, &b);
*v = (*v << 8) + b;