return asn1_pop_tag(data);
}
-bool ber_write_OID_String(DATA_BLOB *blob, const char *OID)
+bool ber_write_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *OID)
{
- uint_t v, v2;
+ unsigned int v, v2;
const char *p = (const char *)OID;
char *newp;
int i;
p = newp + 1;
/*the ber representation can't use more space then the string one */
- *blob = data_blob(NULL, strlen(OID));
+ *blob = data_blob_talloc(mem_ctx, NULL, strlen(OID));
if (!blob->data) return false;
blob->data[0] = 40*v + v2;
* 1:2.5.6:0x81
* 1:2.5.6:0x8182
*/
-bool ber_write_partial_OID_String(DATA_BLOB *blob, const char *partial_oid)
+bool ber_write_partial_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *partial_oid)
{
- TALLOC_CTX *mem_ctx = talloc_new(NULL);
- char *oid = talloc_strdup(mem_ctx, partial_oid);
+ TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
+ char *oid = talloc_strdup(tmp_ctx, partial_oid);
char *p;
/* truncate partial part so ber_write_OID_String() works */
p++;
}
- if (!ber_write_OID_String(blob, oid)) {
- talloc_free(mem_ctx);
+ if (!ber_write_OID_String(mem_ctx, blob, oid)) {
+ talloc_free(tmp_ctx);
return false;
}
- /* Add partially endcoded subidentifier */
+ /* Add partially encoded sub-identifier */
if (p) {
- DATA_BLOB tmp_blob = strhex_to_data_blob(mem_ctx, p);
- data_blob_append(NULL, blob, tmp_blob.data, tmp_blob.length);
+ DATA_BLOB tmp_blob = strhex_to_data_blob(tmp_ctx, p);
+ data_blob_append(mem_ctx, blob, tmp_blob.data, tmp_blob.length);
}
- talloc_free(mem_ctx);
+ talloc_free(tmp_ctx);
return true;
}
if (!asn1_push_tag(data, ASN1_OID)) return false;
- if (!ber_write_OID_String(&blob, OID)) {
+ if (!ber_write_OID_String(NULL, &blob, OID)) {
data->has_error = true;
return false;
}
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;
+ }
+ if (n > 4) {
+ /*
+ * We should not allow more than 4 bytes
+ * for the encoding of the tag length.
+ *
+ * Otherwise we'd overflow the taglen
+ * variable on 32 bit systems.
+ */
+ 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)
{
{
int i;
uint8_t *b;
- uint_t v;
+ unsigned int v;
char *tmp_oid = NULL;
if (blob.length < 2) return false;
tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", b[0]%40);
if (!tmp_oid) goto nomem;
+ if (bytes_eaten != NULL) {
+ *bytes_eaten = 0;
+ }
+
for(i = 1, v = 0; i < blob.length; i++) {
v = (v<<7) | (b[i]&0x7f);
if ( ! (b[i] & 0x80)) {
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;
+ size_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 NT_STATUS_INVALID_BUFFER_SIZE;
+ }
+
+ if (size > blob.length) {
+ *packet_size = size;
+ return STATUS_MORE_ENTRIES;
}
*packet_size = size;