asn1: Add function to generate an ASN.1 integer from an uint64_t

This commit is contained in:
Tobias Brunner
2017-11-08 16:48:10 +01:00
parent bfe1cb3a94
commit ffd0eeecf0
3 changed files with 63 additions and 6 deletions
+20
View File
@@ -609,6 +609,26 @@ uint64_t asn1_parse_integer_uint64(chunk_t blob)
return val;
}
/*
* Described in header
*/
chunk_t asn1_integer_from_uint64(uint64_t val)
{
u_char buf[sizeof(val)];
chunk_t enc = chunk_empty;
if (val < 0x100)
{
buf[0] = (u_char)val;
return chunk_clone(chunk_create(buf, 1));
}
for (enc.ptr = buf + sizeof(val); val; enc.len++, val >>= 8)
{ /* fill the buffer from the end */
*(--enc.ptr) = val & 0xff;
}
return chunk_clone(enc);
}
/**
* ASN.1 definition of an algorithmIdentifier
*/
+8
View File
@@ -180,6 +180,14 @@ bool asn1_parse_simple_object(chunk_t *object, asn1_t type, u_int level0,
*/
uint64_t asn1_parse_integer_uint64(chunk_t blob);
/**
* Converts an uint64_t to an ASN.1 INTEGER object.
*
* @param val integer to convert
* @return body of an ASN.1 coded integer object
*/
chunk_t asn1_integer_from_uint64(uint64_t val);
/**
* Print the value of an ASN.1 simple object
*