asn1: Try to fill the available binary OID buffer if possible

This commit is contained in:
Tobias Brunner
2014-09-09 12:31:00 +02:00
parent c6f886ce10
commit 92b76384a0
+24 -11
View File
@@ -123,6 +123,24 @@ chunk_t asn1_build_known_oid(int n)
return oid; return oid;
} }
/**
* Returns the number of bytes required to encode the given OID node
*/
static int bytes_required(u_int val)
{
int shift, required = 1;
/* sufficient to handle 32 bit node numbers */
for (shift = 28; shift; shift -= 7)
{
if (val >> shift)
{ /* do not encode leading zeroes */
required++;
}
}
return required;
}
/* /*
* Defined in header. * Defined in header.
*/ */
@@ -132,14 +150,15 @@ chunk_t asn1_oid_from_string(char *str)
size_t buf_len = 64; size_t buf_len = 64;
u_char buf[buf_len]; u_char buf[buf_len];
char *end; char *end;
int i = 0, pos = 0, shift; int i = 0, pos = 0, req, shift;
u_int val, shifted_val, first = 0; u_int val, first = 0;
enumerator = enumerator_create_token(str, ".", ""); enumerator = enumerator_create_token(str, ".", "");
while (enumerator->enumerate(enumerator, &str)) while (enumerator->enumerate(enumerator, &str))
{ {
val = strtoul(str, &end, 10); val = strtoul(str, &end, 10);
if (end == str || pos > buf_len-5) req = bytes_required(val);
if (end == str || pos + req > buf_len)
{ {
pos = 0; pos = 0;
break; break;
@@ -153,15 +172,9 @@ chunk_t asn1_oid_from_string(char *str)
buf[pos++] = first * 40 + val; buf[pos++] = first * 40 + val;
break; break;
default: default:
shift = 28; /* sufficient to handle 32 bit node numbers */ for (shift = (req - 1) * 7; shift; shift -= 7)
while (shift)
{ {
shifted_val = val >> shift; buf[pos++] = 0x80 | ((val >> shift) & 0x7F);
shift -= 7;
if (shifted_val) /* do not encode leading zeroes */
{
buf[pos++] = 0x80 | (shifted_val & 0x7F);
}
} }
buf[pos++] = val & 0x7F; buf[pos++] = val & 0x7F;
} }