|
|
97823b |
From e877c21f5e2394c8325955c645f214b8868317c8 Mon Sep 17 00:00:00 2001
|
|
|
97823b |
From: Jeff Layton <jlayton@samba.org>
|
|
|
97823b |
Date: Wed, 9 Oct 2013 08:17:49 -0400
|
|
|
97823b |
Subject: [PATCH] asn1: remove some usused functions
|
|
|
97823b |
|
|
|
97823b |
This cuts 30k out of the cifs.upcall binary on my x86_64 box.
|
|
|
97823b |
|
|
|
97823b |
Signed-off-by: Jeff Layton <jlayton@samba.org>
|
|
|
97823b |
---
|
|
|
97823b |
asn1.c | 638 -----------------------------------------------------------------
|
|
|
97823b |
asn1.h | 37 ----
|
|
|
97823b |
2 files changed, 675 deletions(-)
|
|
|
97823b |
|
|
|
97823b |
diff --git a/asn1.c b/asn1.c
|
|
|
97823b |
index a00c474..ea50a23 100644
|
|
|
97823b |
--- a/asn1.c
|
|
|
97823b |
+++ b/asn1.c
|
|
|
97823b |
@@ -140,83 +140,6 @@ bool asn1_pop_tag(struct asn1_data *data)
|
|
|
97823b |
return true;
|
|
|
97823b |
}
|
|
|
97823b |
|
|
|
97823b |
-/* "i" is the one's complement representation, as is the normal result of an
|
|
|
97823b |
- * implicit signed->unsigned conversion */
|
|
|
97823b |
-
|
|
|
97823b |
-static bool push_int_bigendian(struct asn1_data *data, unsigned int i, bool negative)
|
|
|
97823b |
-{
|
|
|
97823b |
- uint8_t lowest = i & 0xFF;
|
|
|
97823b |
-
|
|
|
97823b |
- i = i >> 8;
|
|
|
97823b |
- if (i != 0)
|
|
|
97823b |
- if (!push_int_bigendian(data, i, negative))
|
|
|
97823b |
- return false;
|
|
|
97823b |
-
|
|
|
97823b |
- if (data->nesting->start+1 == data->ofs) {
|
|
|
97823b |
-
|
|
|
97823b |
- /* We did not write anything yet, looking at the highest
|
|
|
97823b |
- * valued byte */
|
|
|
97823b |
-
|
|
|
97823b |
- if (negative) {
|
|
|
97823b |
- /* Don't write leading 0xff's */
|
|
|
97823b |
- if (lowest == 0xFF)
|
|
|
97823b |
- return true;
|
|
|
97823b |
-
|
|
|
97823b |
- if ((lowest & 0x80) == 0) {
|
|
|
97823b |
- /* The only exception for a leading 0xff is if
|
|
|
97823b |
- * the highest bit is 0, which would indicate
|
|
|
97823b |
- * a positive value */
|
|
|
97823b |
- if (!asn1_write_uint8(data, 0xff))
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- } else {
|
|
|
97823b |
- if (lowest & 0x80) {
|
|
|
97823b |
- /* The highest bit of a positive integer is 1,
|
|
|
97823b |
- * this would indicate a negative number. Push
|
|
|
97823b |
- * a 0 to indicate a positive one */
|
|
|
97823b |
- if (!asn1_write_uint8(data, 0))
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- }
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- return asn1_write_uint8(data, lowest);
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* write an Integer without the tag framing. Needed for example for the LDAP
|
|
|
97823b |
- * Abandon Operation */
|
|
|
97823b |
-
|
|
|
97823b |
-bool asn1_write_implicit_Integer(struct asn1_data *data, int i)
|
|
|
97823b |
-{
|
|
|
97823b |
- if (i == -1) {
|
|
|
97823b |
- /* -1 is special as it consists of all-0xff bytes. In
|
|
|
97823b |
- push_int_bigendian this is the only case that is not
|
|
|
97823b |
- properly handled, as all 0xff bytes would be handled as
|
|
|
97823b |
- leading ones to be ignored. */
|
|
|
97823b |
- return asn1_write_uint8(data, 0xff);
|
|
|
97823b |
- } else {
|
|
|
97823b |
- return push_int_bigendian(data, i, i<0);
|
|
|
97823b |
- }
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-
|
|
|
97823b |
-/* write an integer */
|
|
|
97823b |
-bool asn1_write_Integer(struct asn1_data *data, int i)
|
|
|
97823b |
-{
|
|
|
97823b |
- if (!asn1_push_tag(data, ASN1_INTEGER)) return false;
|
|
|
97823b |
- if (!asn1_write_implicit_Integer(data, i)) return false;
|
|
|
97823b |
- return asn1_pop_tag(data);
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* write a BIT STRING */
|
|
|
97823b |
-bool asn1_write_BitString(struct asn1_data *data, const void *p, size_t length, uint8_t padding)
|
|
|
97823b |
-{
|
|
|
97823b |
- if (!asn1_push_tag(data, ASN1_BIT_STRING)) return false;
|
|
|
97823b |
- if (!asn1_write_uint8(data, padding)) return false;
|
|
|
97823b |
- if (!asn1_write(data, p, length)) return false;
|
|
|
97823b |
- return asn1_pop_tag(data);
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
bool ber_write_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *OID)
|
|
|
97823b |
{
|
|
|
97823b |
unsigned int v, v2;
|
|
|
97823b |
@@ -291,564 +214,3 @@ bool asn1_write_OctetString(struct asn1_data *data, const void *p, size_t length
|
|
|
97823b |
return !data->has_error;
|
|
|
97823b |
}
|
|
|
97823b |
|
|
|
97823b |
-/* write a LDAP string */
|
|
|
97823b |
-bool asn1_write_LDAPString(struct asn1_data *data, const char *s)
|
|
|
97823b |
-{
|
|
|
97823b |
- asn1_write(data, s, strlen(s));
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* write a LDAP string from a DATA_BLOB */
|
|
|
97823b |
-bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data *data, const DATA_BLOB *s)
|
|
|
97823b |
-{
|
|
|
97823b |
- asn1_write(data, s->data, s->length);
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* write a general string */
|
|
|
97823b |
-bool asn1_write_GeneralString(struct asn1_data *data, const char *s)
|
|
|
97823b |
-{
|
|
|
97823b |
- asn1_push_tag(data, ASN1_GENERAL_STRING);
|
|
|
97823b |
- asn1_write_LDAPString(data, s);
|
|
|
97823b |
- asn1_pop_tag(data);
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-bool asn1_write_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
|
|
|
97823b |
-{
|
|
|
97823b |
- asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(num));
|
|
|
97823b |
- asn1_write(data, blob->data, blob->length);
|
|
|
97823b |
- asn1_pop_tag(data);
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* write a BOOLEAN */
|
|
|
97823b |
-bool asn1_write_BOOLEAN(struct asn1_data *data, bool v)
|
|
|
97823b |
-{
|
|
|
97823b |
- asn1_push_tag(data, ASN1_BOOLEAN);
|
|
|
97823b |
- asn1_write_uint8(data, v ? 0xFF : 0);
|
|
|
97823b |
- asn1_pop_tag(data);
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-bool asn1_read_BOOLEAN(struct asn1_data *data, bool *v)
|
|
|
97823b |
-{
|
|
|
97823b |
- uint8_t tmp = 0;
|
|
|
97823b |
- asn1_start_tag(data, ASN1_BOOLEAN);
|
|
|
97823b |
- asn1_read_uint8(data, &tmp);
|
|
|
97823b |
- if (tmp == 0xFF) {
|
|
|
97823b |
- *v = true;
|
|
|
97823b |
- } else {
|
|
|
97823b |
- *v = false;
|
|
|
97823b |
- }
|
|
|
97823b |
- asn1_end_tag(data);
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* write a BOOLEAN in a simple context */
|
|
|
97823b |
-bool asn1_write_BOOLEAN_context(struct asn1_data *data, bool v, int context)
|
|
|
97823b |
-{
|
|
|
97823b |
- asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(context));
|
|
|
97823b |
- asn1_write_uint8(data, v ? 0xFF : 0);
|
|
|
97823b |
- asn1_pop_tag(data);
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-bool asn1_read_BOOLEAN_context(struct asn1_data *data, bool *v, int context)
|
|
|
97823b |
-{
|
|
|
97823b |
- uint8_t tmp = 0;
|
|
|
97823b |
- asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(context));
|
|
|
97823b |
- asn1_read_uint8(data, &tmp);
|
|
|
97823b |
- if (tmp == 0xFF) {
|
|
|
97823b |
- *v = true;
|
|
|
97823b |
- } else {
|
|
|
97823b |
- *v = false;
|
|
|
97823b |
- }
|
|
|
97823b |
- asn1_end_tag(data);
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* check a BOOLEAN */
|
|
|
97823b |
-bool asn1_check_BOOLEAN(struct asn1_data *data, bool v)
|
|
|
97823b |
-{
|
|
|
97823b |
- uint8_t b = 0;
|
|
|
97823b |
-
|
|
|
97823b |
- asn1_read_uint8(data, &b);
|
|
|
97823b |
- if (b != ASN1_BOOLEAN) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- asn1_read_uint8(data, &b);
|
|
|
97823b |
- if (b != v) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-
|
|
|
97823b |
-/* load a struct asn1_data structure with a lump of data, ready to be parsed */
|
|
|
97823b |
-bool asn1_load(struct asn1_data *data, DATA_BLOB blob)
|
|
|
97823b |
-{
|
|
|
97823b |
- ZERO_STRUCTP(data);
|
|
|
97823b |
- data->data = (uint8_t *)talloc_memdup(data, blob.data, blob.length);
|
|
|
97823b |
- if (!data->data) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- data->length = blob.length;
|
|
|
97823b |
- return true;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* Peek into an ASN1 buffer, not advancing the pointer */
|
|
|
97823b |
-bool asn1_peek(struct asn1_data *data, void *p, int len)
|
|
|
97823b |
-{
|
|
|
97823b |
- if (data->has_error)
|
|
|
97823b |
- return false;
|
|
|
97823b |
-
|
|
|
97823b |
- if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len)
|
|
|
97823b |
- return false;
|
|
|
97823b |
-
|
|
|
97823b |
- if ((size_t)data->ofs + len > data->length) {
|
|
|
97823b |
- /* we need to mark the buffer as consumed, so the caller knows
|
|
|
97823b |
- this was an out of data error, and not a decode error */
|
|
|
97823b |
- data->ofs = data->length;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- memcpy(p, data->data + data->ofs, len);
|
|
|
97823b |
- return true;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* read from a ASN1 buffer, advancing the buffer pointer */
|
|
|
97823b |
-bool asn1_read(struct asn1_data *data, void *p, int len)
|
|
|
97823b |
-{
|
|
|
97823b |
- if (!asn1_peek(data, p, len)) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- data->ofs += len;
|
|
|
97823b |
- return true;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* read a uint8_t from a ASN1 buffer */
|
|
|
97823b |
-bool asn1_read_uint8(struct asn1_data *data, uint8_t *v)
|
|
|
97823b |
-{
|
|
|
97823b |
- return asn1_read(data, v, 1);
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-bool asn1_peek_uint8(struct asn1_data *data, uint8_t *v)
|
|
|
97823b |
-{
|
|
|
97823b |
- return asn1_peek(data, v, 1);
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-bool asn1_peek_tag(struct asn1_data *data, uint8_t tag)
|
|
|
97823b |
-{
|
|
|
97823b |
- uint8_t b;
|
|
|
97823b |
-
|
|
|
97823b |
- if (asn1_tag_remaining(data) <= 0) {
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- if (!asn1_peek_uint8(data, &b))
|
|
|
97823b |
- return false;
|
|
|
97823b |
-
|
|
|
97823b |
- return (b == tag);
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* start reading a nested asn1 structure */
|
|
|
97823b |
-bool asn1_start_tag(struct asn1_data *data, uint8_t tag)
|
|
|
97823b |
-{
|
|
|
97823b |
- uint8_t b;
|
|
|
97823b |
- struct nesting *nesting;
|
|
|
97823b |
-
|
|
|
97823b |
- if (!asn1_read_uint8(data, &b))
|
|
|
97823b |
- return false;
|
|
|
97823b |
-
|
|
|
97823b |
- if (b != tag) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- nesting = talloc(data, struct nesting);
|
|
|
97823b |
- if (!nesting) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- if (!asn1_read_uint8(data, &b)) {
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- if (b & 0x80) {
|
|
|
97823b |
- int n = b & 0x7f;
|
|
|
97823b |
- if (!asn1_read_uint8(data, &b))
|
|
|
97823b |
- return false;
|
|
|
97823b |
- nesting->taglen = b;
|
|
|
97823b |
- while (n > 1) {
|
|
|
97823b |
- if (!asn1_read_uint8(data, &b))
|
|
|
97823b |
- return false;
|
|
|
97823b |
- nesting->taglen = (nesting->taglen << 8) | b;
|
|
|
97823b |
- n--;
|
|
|
97823b |
- }
|
|
|
97823b |
- } else {
|
|
|
97823b |
- nesting->taglen = b;
|
|
|
97823b |
- }
|
|
|
97823b |
- nesting->start = data->ofs;
|
|
|
97823b |
- nesting->next = data->nesting;
|
|
|
97823b |
- data->nesting = nesting;
|
|
|
97823b |
- if (asn1_tag_remaining(data) == -1) {
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* stop reading a tag */
|
|
|
97823b |
-bool asn1_end_tag(struct asn1_data *data)
|
|
|
97823b |
-{
|
|
|
97823b |
- struct nesting *nesting;
|
|
|
97823b |
-
|
|
|
97823b |
- /* make sure we read it all */
|
|
|
97823b |
- if (asn1_tag_remaining(data) != 0) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- nesting = data->nesting;
|
|
|
97823b |
-
|
|
|
97823b |
- if (!nesting) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- data->nesting = nesting->next;
|
|
|
97823b |
- talloc_free(nesting);
|
|
|
97823b |
- return true;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* work out how many bytes are left in this nested tag */
|
|
|
97823b |
-int asn1_tag_remaining(struct asn1_data *data)
|
|
|
97823b |
-{
|
|
|
97823b |
- int remaining;
|
|
|
97823b |
- if (data->has_error) {
|
|
|
97823b |
- return -1;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- if (!data->nesting) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return -1;
|
|
|
97823b |
- }
|
|
|
97823b |
- remaining = data->nesting->taglen - (data->ofs - data->nesting->start);
|
|
|
97823b |
- if (remaining < 0) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return -1;
|
|
|
97823b |
- }
|
|
|
97823b |
- if ((size_t)remaining > data->length - data->ofs) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return -1;
|
|
|
97823b |
- }
|
|
|
97823b |
- return remaining;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/**
|
|
|
97823b |
- * Internal implementation for reading binary OIDs
|
|
|
97823b |
- * Reading is done as far in the buffer as valid OID
|
|
|
97823b |
- * till buffer ends or not valid sub-identifier is found.
|
|
|
97823b |
- */
|
|
|
97823b |
-static bool _ber_read_OID_String_impl(TALLOC_CTX *mem_ctx, DATA_BLOB blob,
|
|
|
97823b |
- const char **OID, size_t *bytes_eaten)
|
|
|
97823b |
-{
|
|
|
97823b |
- unsigned int i;
|
|
|
97823b |
- uint8_t *b;
|
|
|
97823b |
- unsigned int v;
|
|
|
97823b |
- char *tmp_oid = NULL;
|
|
|
97823b |
-
|
|
|
97823b |
- if (blob.length < 2) return false;
|
|
|
97823b |
-
|
|
|
97823b |
- b = blob.data;
|
|
|
97823b |
-
|
|
|
97823b |
- tmp_oid = talloc_asprintf(mem_ctx, "%u", b[0]/40);
|
|
|
97823b |
- if (!tmp_oid) goto nomem;
|
|
|
97823b |
- tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", b[0]%40);
|
|
|
97823b |
- if (!tmp_oid) goto nomem;
|
|
|
97823b |
-
|
|
|
97823b |
- for(i = 1, v = 0; i < blob.length; i++) {
|
|
|
97823b |
- v = (v<<7) | (b[i]&0x7f);
|
|
|
97823b |
- if ( ! (b[i] & 0x80)) {
|
|
|
97823b |
- tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", v);
|
|
|
97823b |
- v = 0;
|
|
|
97823b |
- if (bytes_eaten)
|
|
|
97823b |
- *bytes_eaten = i+1;
|
|
|
97823b |
- }
|
|
|
97823b |
- if (!tmp_oid) goto nomem;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- *OID = tmp_oid;
|
|
|
97823b |
- return true;
|
|
|
97823b |
-
|
|
|
97823b |
-nomem:
|
|
|
97823b |
- return false;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* read an object ID from a data blob */
|
|
|
97823b |
-bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **OID)
|
|
|
97823b |
-{
|
|
|
97823b |
- size_t bytes_eaten = 0;
|
|
|
97823b |
-
|
|
|
97823b |
- if (!_ber_read_OID_String_impl(mem_ctx, blob, OID, &bytes_eaten))
|
|
|
97823b |
- return false;
|
|
|
97823b |
-
|
|
|
97823b |
- return (bytes_eaten == blob.length);
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* read an object ID from a ASN1 buffer */
|
|
|
97823b |
-bool asn1_read_OID(struct asn1_data *data, TALLOC_CTX *mem_ctx, const char **OID)
|
|
|
97823b |
-{
|
|
|
97823b |
- DATA_BLOB blob;
|
|
|
97823b |
- int len;
|
|
|
97823b |
-
|
|
|
97823b |
- if (!asn1_start_tag(data, ASN1_OID)) return false;
|
|
|
97823b |
-
|
|
|
97823b |
- len = asn1_tag_remaining(data);
|
|
|
97823b |
- if (len < 0) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- blob = data_blob(NULL, len);
|
|
|
97823b |
- if (!blob.data) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- asn1_read(data, blob.data, len);
|
|
|
97823b |
- asn1_end_tag(data);
|
|
|
97823b |
- if (data->has_error) {
|
|
|
97823b |
- data_blob_free(&blob;;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- if (!ber_read_OID_String(mem_ctx, blob, OID)) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- data_blob_free(&blob;;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- data_blob_free(&blob;;
|
|
|
97823b |
- return true;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* check that the next object ID is correct */
|
|
|
97823b |
-bool asn1_check_OID(struct asn1_data *data, const char *OID)
|
|
|
97823b |
-{
|
|
|
97823b |
- const char *id;
|
|
|
97823b |
-
|
|
|
97823b |
- if (!asn1_read_OID(data, data, &id)) return false;
|
|
|
97823b |
-
|
|
|
97823b |
- if (strcmp(id, OID) != 0) {
|
|
|
97823b |
- talloc_free(discard_const(id));
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- talloc_free(discard_const(id));
|
|
|
97823b |
- return true;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* read a LDAPString from a ASN1 buffer */
|
|
|
97823b |
-bool asn1_read_LDAPString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
|
|
|
97823b |
-{
|
|
|
97823b |
- int len;
|
|
|
97823b |
- len = asn1_tag_remaining(data);
|
|
|
97823b |
- if (len < 0) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- *s = talloc_array(mem_ctx, char, len+1);
|
|
|
97823b |
- if (! *s) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- asn1_read(data, *s, len);
|
|
|
97823b |
- (*s)[len] = 0;
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-
|
|
|
97823b |
-/* read a GeneralString from a ASN1 buffer */
|
|
|
97823b |
-bool asn1_read_GeneralString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
|
|
|
97823b |
-{
|
|
|
97823b |
- if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return false;
|
|
|
97823b |
- if (!asn1_read_LDAPString(data, mem_ctx, s)) return false;
|
|
|
97823b |
- return asn1_end_tag(data);
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-
|
|
|
97823b |
-/* read a octet string blob */
|
|
|
97823b |
-bool asn1_read_OctetString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
|
|
|
97823b |
-{
|
|
|
97823b |
- int len;
|
|
|
97823b |
- ZERO_STRUCTP(blob);
|
|
|
97823b |
- if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return false;
|
|
|
97823b |
- len = asn1_tag_remaining(data);
|
|
|
97823b |
- if (len < 0) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- *blob = data_blob_talloc(mem_ctx, NULL, len+1);
|
|
|
97823b |
- if (!blob->data) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- asn1_read(data, blob->data, len);
|
|
|
97823b |
- asn1_end_tag(data);
|
|
|
97823b |
- blob->length--;
|
|
|
97823b |
- blob->data[len] = 0;
|
|
|
97823b |
-
|
|
|
97823b |
- if (data->has_error) {
|
|
|
97823b |
- data_blob_free(blob);
|
|
|
97823b |
- *blob = data_blob_null;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- return true;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-bool asn1_read_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
|
|
|
97823b |
-{
|
|
|
97823b |
- int len;
|
|
|
97823b |
- ZERO_STRUCTP(blob);
|
|
|
97823b |
- if (!asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(num))) return false;
|
|
|
97823b |
- len = asn1_tag_remaining(data);
|
|
|
97823b |
- if (len < 0) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- *blob = data_blob(NULL, len);
|
|
|
97823b |
- if ((len != 0) && (!blob->data)) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- asn1_read(data, blob->data, len);
|
|
|
97823b |
- asn1_end_tag(data);
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* read an integer without tag*/
|
|
|
97823b |
-bool asn1_read_implicit_Integer(struct asn1_data *data, int *i)
|
|
|
97823b |
-{
|
|
|
97823b |
- uint8_t b;
|
|
|
97823b |
- *i = 0;
|
|
|
97823b |
-
|
|
|
97823b |
- while (!data->has_error && asn1_tag_remaining(data)>0) {
|
|
|
97823b |
- if (!asn1_read_uint8(data, &b)) return false;
|
|
|
97823b |
- *i = (*i << 8) + b;
|
|
|
97823b |
- }
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* read an integer */
|
|
|
97823b |
-bool asn1_read_Integer(struct asn1_data *data, int *i)
|
|
|
97823b |
-{
|
|
|
97823b |
- *i = 0;
|
|
|
97823b |
-
|
|
|
97823b |
- if (!asn1_start_tag(data, ASN1_INTEGER)) return false;
|
|
|
97823b |
- if (!asn1_read_implicit_Integer(data, i)) return false;
|
|
|
97823b |
- return asn1_end_tag(data);
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* read a BIT STRING */
|
|
|
97823b |
-bool asn1_read_BitString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob, uint8_t *padding)
|
|
|
97823b |
-{
|
|
|
97823b |
- int len;
|
|
|
97823b |
- ZERO_STRUCTP(blob);
|
|
|
97823b |
- if (!asn1_start_tag(data, ASN1_BIT_STRING)) return false;
|
|
|
97823b |
- len = asn1_tag_remaining(data);
|
|
|
97823b |
- if (len < 0) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- if (!asn1_read_uint8(data, padding)) return false;
|
|
|
97823b |
-
|
|
|
97823b |
- *blob = data_blob_talloc(mem_ctx, NULL, len);
|
|
|
97823b |
- if (!blob->data) {
|
|
|
97823b |
- data->has_error = true;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- if (asn1_read(data, blob->data, len - 1)) {
|
|
|
97823b |
- blob->length--;
|
|
|
97823b |
- blob->data[len] = 0;
|
|
|
97823b |
- asn1_end_tag(data);
|
|
|
97823b |
- }
|
|
|
97823b |
-
|
|
|
97823b |
- if (data->has_error) {
|
|
|
97823b |
- data_blob_free(blob);
|
|
|
97823b |
- *blob = data_blob_null;
|
|
|
97823b |
- *padding = 0;
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- return true;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* read an integer */
|
|
|
97823b |
-bool asn1_read_enumerated(struct asn1_data *data, int *v)
|
|
|
97823b |
-{
|
|
|
97823b |
- *v = 0;
|
|
|
97823b |
-
|
|
|
97823b |
- if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
|
|
|
97823b |
- while (!data->has_error && asn1_tag_remaining(data)>0) {
|
|
|
97823b |
- uint8_t b;
|
|
|
97823b |
- asn1_read_uint8(data, &b);
|
|
|
97823b |
- *v = (*v << 8) + b;
|
|
|
97823b |
- }
|
|
|
97823b |
- return asn1_end_tag(data);
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* check a enumerated value is correct */
|
|
|
97823b |
-bool asn1_check_enumerated(struct asn1_data *data, int v)
|
|
|
97823b |
-{
|
|
|
97823b |
- uint8_t b;
|
|
|
97823b |
- if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
|
|
|
97823b |
- asn1_read_uint8(data, &b);
|
|
|
97823b |
- asn1_end_tag(data);
|
|
|
97823b |
-
|
|
|
97823b |
- if (v != b)
|
|
|
97823b |
- data->has_error = false;
|
|
|
97823b |
-
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/* write an enumerated value to the stream */
|
|
|
97823b |
-bool asn1_write_enumerated(struct asn1_data *data, uint8_t v)
|
|
|
97823b |
-{
|
|
|
97823b |
- if (!asn1_push_tag(data, ASN1_ENUMERATED)) return false;
|
|
|
97823b |
- asn1_write_uint8(data, v);
|
|
|
97823b |
- asn1_pop_tag(data);
|
|
|
97823b |
- return !data->has_error;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/*
|
|
|
97823b |
- Get us the data just written without copying
|
|
|
97823b |
-*/
|
|
|
97823b |
-bool asn1_blob(const struct asn1_data *asn1, DATA_BLOB *blob)
|
|
|
97823b |
-{
|
|
|
97823b |
- if (asn1->has_error) {
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- if (asn1->nesting != NULL) {
|
|
|
97823b |
- return false;
|
|
|
97823b |
- }
|
|
|
97823b |
- blob->data = asn1->data;
|
|
|
97823b |
- blob->length = asn1->length;
|
|
|
97823b |
- return true;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
-/*
|
|
|
97823b |
- Fill in an asn1 struct without making a copy
|
|
|
97823b |
-*/
|
|
|
97823b |
-void asn1_load_nocopy(struct asn1_data *data, uint8_t *buf, size_t len)
|
|
|
97823b |
-{
|
|
|
97823b |
- ZERO_STRUCTP(data);
|
|
|
97823b |
- data->data = buf;
|
|
|
97823b |
- data->length = len;
|
|
|
97823b |
-}
|
|
|
97823b |
-
|
|
|
97823b |
diff --git a/asn1.h b/asn1.h
|
|
|
97823b |
index 615041f..a773885 100644
|
|
|
97823b |
--- a/asn1.h
|
|
|
97823b |
+++ b/asn1.h
|
|
|
97823b |
@@ -58,44 +58,7 @@ bool asn1_write(struct asn1_data *data, const void *p, int len);
|
|
|
97823b |
bool asn1_write_uint8(struct asn1_data *data, uint8_t v);
|
|
|
97823b |
bool asn1_push_tag(struct asn1_data *data, uint8_t tag);
|
|
|
97823b |
bool asn1_pop_tag(struct asn1_data *data);
|
|
|
97823b |
-bool asn1_write_implicit_Integer(struct asn1_data *data, int i);
|
|
|
97823b |
-bool asn1_write_Integer(struct asn1_data *data, int i);
|
|
|
97823b |
-bool asn1_write_BitString(struct asn1_data *data, const void *p, size_t length, uint8_t padding);
|
|
|
97823b |
bool ber_write_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *OID);
|
|
|
97823b |
bool asn1_write_OID(struct asn1_data *data, const char *OID);
|
|
|
97823b |
bool asn1_write_OctetString(struct asn1_data *data, const void *p, size_t length);
|
|
|
97823b |
-bool asn1_write_LDAPString(struct asn1_data *data, const char *s);
|
|
|
97823b |
-bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data *data, const DATA_BLOB *s);
|
|
|
97823b |
-bool asn1_write_GeneralString(struct asn1_data *data, const char *s);
|
|
|
97823b |
-bool asn1_write_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob);
|
|
|
97823b |
-bool asn1_write_BOOLEAN(struct asn1_data *data, bool v);
|
|
|
97823b |
-bool asn1_read_BOOLEAN(struct asn1_data *data, bool *v);
|
|
|
97823b |
-bool asn1_check_BOOLEAN(struct asn1_data *data, bool v);
|
|
|
97823b |
-bool asn1_write_BOOLEAN_context(struct asn1_data *data, bool v, int context);
|
|
|
97823b |
-bool asn1_read_BOOLEAN_context(struct asn1_data *data, bool *v, int context);
|
|
|
97823b |
-bool asn1_load(struct asn1_data *data, DATA_BLOB blob);
|
|
|
97823b |
-bool asn1_peek(struct asn1_data *data, void *p, int len);
|
|
|
97823b |
-bool asn1_read(struct asn1_data *data, void *p, int len);
|
|
|
97823b |
-bool asn1_read_uint8(struct asn1_data *data, uint8_t *v);
|
|
|
97823b |
-bool asn1_peek_uint8(struct asn1_data *data, uint8_t *v);
|
|
|
97823b |
-bool asn1_peek_tag(struct asn1_data *data, uint8_t tag);
|
|
|
97823b |
-bool asn1_start_tag(struct asn1_data *data, uint8_t tag);
|
|
|
97823b |
-bool asn1_end_tag(struct asn1_data *data);
|
|
|
97823b |
-int asn1_tag_remaining(struct asn1_data *data);
|
|
|
97823b |
-bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **OID);
|
|
|
97823b |
-bool asn1_read_OID(struct asn1_data *data, TALLOC_CTX *mem_ctx, const char **OID);
|
|
|
97823b |
-bool asn1_check_OID(struct asn1_data *data, const char *OID);
|
|
|
97823b |
-bool asn1_read_LDAPString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s);
|
|
|
97823b |
-bool asn1_read_GeneralString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s);
|
|
|
97823b |
-bool asn1_read_OctetString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob);
|
|
|
97823b |
-bool asn1_read_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob);
|
|
|
97823b |
-bool asn1_read_implicit_Integer(struct asn1_data *data, int *i);
|
|
|
97823b |
-bool asn1_read_Integer(struct asn1_data *data, int *i);
|
|
|
97823b |
-bool asn1_read_BitString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob, uint8_t *padding);
|
|
|
97823b |
-bool asn1_read_enumerated(struct asn1_data *data, int *v);
|
|
|
97823b |
-bool asn1_check_enumerated(struct asn1_data *data, int v);
|
|
|
97823b |
-bool asn1_write_enumerated(struct asn1_data *data, uint8_t v);
|
|
|
97823b |
-bool asn1_blob(const struct asn1_data *asn1, DATA_BLOB *blob);
|
|
|
97823b |
-void asn1_load_nocopy(struct asn1_data *data, uint8_t *buf, size_t len);
|
|
|
97823b |
-
|
|
|
97823b |
#endif /* _ASN_1_H */
|
|
|
97823b |
--
|
|
|
97823b |
1.8.3.1
|
|
|
97823b |
|