From 715c790600477b9b1ebdae7aa98779fc06be3bd1 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 25 Sep 2009 16:38:54 +0300 Subject: s4/drsuapi: ber_write_partial_OID_String() implementation --- lib/util/asn1.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'lib/util/asn1.c') diff --git a/lib/util/asn1.c b/lib/util/asn1.c index 70c2c57450..61fb9555a7 100644 --- a/lib/util/asn1.c +++ b/lib/util/asn1.c @@ -258,6 +258,41 @@ bool ber_write_OID_String(DATA_BLOB *blob, const char *OID) return true; } +/** + * Serialize partial OID string. + * Partial OIDs are in the form: + * 1:2.5.6:0x81 + * 1:2.5.6:0x8182 + */ +bool ber_write_partial_OID_String(DATA_BLOB *blob, const char *partial_oid) +{ + TALLOC_CTX *mem_ctx = talloc_new(NULL); + char *oid = talloc_strdup(mem_ctx, partial_oid); + char *p; + + /* truncate partial part so ber_write_OID_String() works */ + p = strchr(oid, ':'); + if (p) { + *p = '\0'; + p++; + } + + if (!ber_write_OID_String(blob, oid)) { + talloc_free(mem_ctx); + return false; + } + + /* Add partially endcoded subidentifier */ + if (p) { + DATA_BLOB tmp_blob = strhex_to_data_blob(mem_ctx, p); + data_blob_append(NULL, blob, tmp_blob.data, tmp_blob.length); + } + + talloc_free(mem_ctx); + + return true; +} + /* write an object ID to a ASN1 buffer */ bool asn1_write_OID(struct asn1_data *data, const char *OID) { -- cgit From 55dfc116f47b7c7242567f596c82bfd8f81d7b98 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 25 Sep 2009 17:28:33 +0300 Subject: s4/drsuapi: Internal implementation for ber_read_OID_String Modified implementation _ber_read_OID_String_impl() returns how much bytes are converted. The intentation is to use this implementation both for reading OIDs and partial-OIDs in the future --- lib/util/asn1.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'lib/util/asn1.c') diff --git a/lib/util/asn1.c b/lib/util/asn1.c index 61fb9555a7..0d08027a07 100644 --- a/lib/util/asn1.c +++ b/lib/util/asn1.c @@ -578,6 +578,46 @@ int asn1_tag_remaining(struct asn1_data *data) return remaining; } +/** + * Internal implementation for reading binary OIDs + * Reading is done as far in the buffer as valid OID + * till buffer ends or not valid sub-identifier is found. + */ +static bool _ber_read_OID_String_impl(TALLOC_CTX *mem_ctx, DATA_BLOB blob, + const char **OID, size_t *bytes_eaten) +{ + int i; + uint8_t *b; + uint_t v; + char *tmp_oid = NULL; + + if (blob.length < 2) return false; + + b = blob.data; + + tmp_oid = talloc_asprintf(mem_ctx, "%u", b[0]/40); + if (!tmp_oid) goto nomem; + tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", b[0]%40); + if (!tmp_oid) goto nomem; + + for(i = 1, v = 0; i < blob.length; i++) { + v = (v<<7) | (b[i]&0x7f); + if ( ! (b[i] & 0x80)) { + tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", v); + v = 0; + if (bytes_eaten) + *bytes_eaten = i+1; + } + if (!tmp_oid) goto nomem; + } + + *OID = tmp_oid; + return true; + +nomem: + return false; +} + /* read an object ID from a data blob */ bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **OID) { -- cgit From 540759ec4d0ea432ad0cd3eb10ed2f977f7b6d29 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Fri, 25 Sep 2009 17:29:05 +0300 Subject: s4/drsuapi: ber_read_partial_OID_String() implementation --- lib/util/asn1.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lib/util/asn1.c') diff --git a/lib/util/asn1.c b/lib/util/asn1.c index 0d08027a07..93d96c2bdf 100644 --- a/lib/util/asn1.c +++ b/lib/util/asn1.c @@ -656,6 +656,42 @@ nomem: return false; } +/** + * Deserialize partial OID string. + * Partial OIDs are in the form: + * 1:2.5.6:0x81 + * 1:2.5.6:0x8182 + */ +bool ber_read_partial_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **partial_oid) +{ + size_t bytes_left; + size_t bytes_eaten; + char *identifier = NULL; + char *tmp_oid = NULL; + + if (!_ber_read_OID_String_impl(mem_ctx, blob, (const char **)&tmp_oid, &bytes_eaten)) + return false; + + if (bytes_eaten < blob.length) { + bytes_left = blob.length - bytes_eaten; + identifier = hex_encode_talloc(mem_ctx, &blob.data[bytes_eaten], bytes_left); + if (!identifier) goto nomem; + + *partial_oid = talloc_asprintf_append_buffer(tmp_oid, ":0x%s", identifier); + if (!*partial_oid) goto nomem; + TALLOC_FREE(identifier); + } else { + *partial_oid = tmp_oid; + } + + return true; + +nomem: + TALLOC_FREE(identifier); + TALLOC_FREE(tmp_oid); + return false; +} + /* read an object ID from a ASN1 buffer */ bool asn1_read_OID(struct asn1_data *data, TALLOC_CTX *mem_ctx, const char **OID) { -- cgit From a58bc2c9a93597f3625dc8b64221c601b6f59833 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Sat, 26 Sep 2009 01:41:18 +0300 Subject: s4/asn1: ber_read_OID_String() to be based on _ber_read_OID_String_impl() --- lib/util/asn1.c | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-) (limited to 'lib/util/asn1.c') diff --git a/lib/util/asn1.c b/lib/util/asn1.c index 93d96c2bdf..ec8ef3f28f 100644 --- a/lib/util/asn1.c +++ b/lib/util/asn1.c @@ -621,39 +621,12 @@ nomem: /* read an object ID from a data blob */ bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, const char **OID) { - int i; - uint8_t *b; - uint_t v; - char *tmp_oid = NULL; - - if (blob.length < 2) return false; - - b = blob.data; - - tmp_oid = talloc_asprintf(mem_ctx, "%u", b[0]/40); - if (!tmp_oid) goto nomem; - tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", b[0]%40); - if (!tmp_oid) goto nomem; - - for(i = 1, v = 0; i < blob.length; i++) { - v = (v<<7) | (b[i]&0x7f); - if ( ! (b[i] & 0x80)) { - tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", v); - v = 0; - } - if (!tmp_oid) goto nomem; - } + size_t bytes_eaten; - if (v != 0) { - talloc_free(tmp_oid); + if (!_ber_read_OID_String_impl(mem_ctx, blob, OID, &bytes_eaten)) return false; - } - *OID = tmp_oid; - return true; - -nomem: - return false; + return (bytes_eaten == blob.length); } /** -- cgit From f11cdc19d1123ca0f7ac909e7c5307a7a3ad5fdb Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Mon, 5 Oct 2009 04:46:20 +0300 Subject: s4/asn1: Use explicite TALLOC_CTX in ber_write_OID functions --- lib/util/asn1.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'lib/util/asn1.c') diff --git a/lib/util/asn1.c b/lib/util/asn1.c index ec8ef3f28f..946f71359c 100644 --- a/lib/util/asn1.c +++ b/lib/util/asn1.c @@ -214,7 +214,7 @@ bool asn1_write_BitString(struct asn1_data *data, const void *p, size_t length, 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; const char *p = (const char *)OID; @@ -230,7 +230,7 @@ bool ber_write_OID_String(DATA_BLOB *blob, const char *OID) 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; @@ -264,10 +264,10 @@ bool ber_write_OID_String(DATA_BLOB *blob, const char *OID) * 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 */ @@ -277,18 +277,18 @@ bool ber_write_partial_OID_String(DATA_BLOB *blob, const char *partial_oid) 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 */ 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; } @@ -300,7 +300,7 @@ bool asn1_write_OID(struct asn1_data *data, const char *OID) 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; } -- cgit