diff options
author | Stefan Metzmacher <metze@samba.org> | 2010-09-23 18:10:28 +0200 |
---|---|---|
committer | Stefan Metzmacher <metze@samba.org> | 2010-09-26 06:45:39 +0200 |
commit | 182a69c5be7706fbb542694c7be51d499b61c98d (patch) | |
tree | 16e0d4c7b1bd34ba5a11915ffc8565d3d56bed82 /lib/util/asn1.c | |
parent | e628bf1081929684d888353101296cc17d9f3ae4 (diff) | |
download | samba-182a69c5be7706fbb542694c7be51d499b61c98d.tar.gz |
lib/util/asn1: add asn1_peek_tag_needed_size() and asn1_peek_full_tag()
We need a way to ask for the length of a tag without having the full
buffer yet.
metze
Diffstat (limited to 'lib/util/asn1.c')
-rw-r--r-- | lib/util/asn1.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/util/asn1.c b/lib/util/asn1.c index 8f30cfe2c67..c4923343e25 100644 --- a/lib/util/asn1.c +++ b/lib/util/asn1.c @@ -489,6 +489,65 @@ bool asn1_peek_tag(struct asn1_data *data, uint8_t tag) return (b == tag); } +/* + * just get the needed size the tag would consume + */ +bool asn1_peek_tag_needed_size(struct asn1_data *data, uint8_t tag, size_t *size) +{ + off_t start_ofs = data->ofs; + uint8_t b; + size_t taglen = 0; + + if (data->has_error) { + return false; + } + + if (!asn1_read_uint8(data, &b)) { + data->ofs = start_ofs; + data->has_error = false; + return false; + } + + if (b != tag) { + data->ofs = start_ofs; + data->has_error = false; + return false; + } + + if (!asn1_read_uint8(data, &b)) { + data->ofs = start_ofs; + data->has_error = false; + return false; + } + + if (b & 0x80) { + int n = b & 0x7f; + if (!asn1_read_uint8(data, &b)) { + data->ofs = start_ofs; + data->has_error = false; + return false; + } + taglen = b; + while (n > 1) { + if (!asn1_read_uint8(data, &b)) { + data->ofs = start_ofs; + data->has_error = false; + return false; + } + taglen = (taglen << 8) | b; + n--; + } + } else { + taglen = b; + } + + *size = (data->ofs - start_ofs) + taglen; + + data->ofs = start_ofs; + data->has_error = false; + return true; +} + /* start reading a nested asn1 structure */ bool asn1_start_tag(struct asn1_data *data, uint8_t tag) { @@ -943,6 +1002,30 @@ NTSTATUS asn1_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size) if (size > blob.length) { return STATUS_MORE_ENTRIES; + } + + *packet_size = size; + return NT_STATUS_OK; +} + +NTSTATUS asn1_peek_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size) +{ + struct asn1_data asn1; + uint32_t size; + bool ok; + + ZERO_STRUCT(asn1); + asn1.data = blob.data; + asn1.length = blob.length; + + ok = asn1_peek_tag_needed_size(&asn1, tag, &size); + if (!ok) { + return STATUS_MORE_ENTRIES; + } + + if (size > blob.length) { + *packet_size = size; + return STATUS_MORE_ENTRIES; } *packet_size = size; |