diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2012-11-23 21:00:50 +0100 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2012-11-23 21:00:50 +0100 |
commit | 1d02266a174c939e9ca1460641c8eaa9dc2a7843 (patch) | |
tree | cebb21db31dece4ebca130c04d6bfe8344b82ee0 /lib | |
parent | d23e8f3918d401fbaa25a1342b5db368f9f3adef (diff) | |
download | libtasn1-1d02266a174c939e9ca1460641c8eaa9dc2a7843.tar.gz libtasn1-1d02266a174c939e9ca1460641c8eaa9dc2a7843.tar.bz2 libtasn1-1d02266a174c939e9ca1460641c8eaa9dc2a7843.zip |
simplified and renamed asn1_encode_string_der() and asn1_decode_string_der()
Diffstat (limited to 'lib')
-rw-r--r-- | lib/coding.c | 41 | ||||
-rw-r--r-- | lib/decoding.c | 30 | ||||
-rw-r--r-- | lib/int.h | 1 | ||||
-rw-r--r-- | lib/libtasn1.h | 12 | ||||
-rw-r--r-- | lib/libtasn1.map | 4 |
5 files changed, 45 insertions, 43 deletions
diff --git a/lib/coding.c b/lib/coding.c index 007703c..aa777f3 100644 --- a/lib/coding.c +++ b/lib/coding.c @@ -184,32 +184,38 @@ asn1_octet_der (const unsigned char *str, int str_len, /** - * asn1_encode_string_der: + * asn1_encode_simple_der: * @etype: The type of the string to be encoded (ASN1_ETYPE_) * @str: the string data. * @str_len: the string length - * @der: the encoded string - * @der_len: the bytes of the encoded string + * @tl: the encoded tag and length + * @tl_len: the bytes of the @tl field * - * Creates the DER encoding for the various ASN.1 STRING types. - * The DER encoding of the input data will be placed in the @der variable. + * Creates the DER encoding for various simple ASN.1 types like strings etc. + * It stores the tag and length in @tl, which should have space for at least + * %ASN1_MAX_TL_SIZE bytes. Initially @tl_len should contain the size of @tl. + * + * The complete DER encoding should consist of the value in @tl appended + * with the provided @str. * * Returns: %ASN1_SUCCESS if successful or an error value. **/ int -asn1_encode_string_der (unsigned int etype, const unsigned char *str, unsigned int str_len, - unsigned char **der, unsigned int *der_len) +asn1_encode_simple_der (unsigned int etype, const unsigned char *str, unsigned int str_len, + unsigned char *tl, unsigned int *tl_len) { - int tag_len, len_len, tlen; + int tag_len, len_len; + unsigned tlen; unsigned char der_tag[ASN1_MAX_TAG_SIZE]; unsigned char der_length[ASN1_MAX_LENGTH_SIZE]; unsigned char* p; - if (der == NULL) + if (str == NULL) return ASN1_VALUE_NOT_VALID; if (ETYPE_OK(etype) == 0) return ASN1_VALUE_NOT_VALID; + _asn1_tag_der (ETYPE_CLASS(etype), ETYPE_TAG(etype), der_tag, &tag_len); @@ -219,20 +225,17 @@ asn1_encode_string_der (unsigned int etype, const unsigned char *str, unsigned i if (tag_len <= 0 || len_len <= 0) return ASN1_VALUE_NOT_VALID; - tlen = tag_len + len_len + str_len; - - p = malloc(tlen); - if (p == NULL) - return ASN1_MEM_ALLOC_ERROR; - - *der = p; - *der_len = tag_len + len_len + str_len; + tlen = tag_len + len_len; + if (*tl_len < tlen) + return ASN1_MEM_ERROR; + + p = tl; memcpy(p, der_tag, tag_len); p+=tag_len; memcpy(p, der_length, len_len); - p+=len_len; - memcpy(p, str, str_len); + + *tl_len = tlen; return ASN1_SUCCESS; } diff --git a/lib/decoding.c b/lib/decoding.c index 82e5f4e..5df1f36 100644 --- a/lib/decoding.c +++ b/lib/decoding.c @@ -2868,23 +2868,23 @@ asn1_expand_octet_string (asn1_node definitions, asn1_node * element, } /** - * asn1_decode_string_der: + * asn1_decode_simple_der: * @etype: The type of the string to be encoded (ASN1_ETYPE_) * @der: the encoded string * @der_len: the bytes of the encoded string - * @str: the decoded string data. - * @str_len: the string length + * @str: a pointer to the data + * @str_len: the length of the data * - * Creates the DER encoding for the various ASN.1 STRING types. - * The DER encoding of the input data will be placed in the @der variable. + * Decodes a simple DER encoded type (e.g. a string, which is not constructed). + * The output is a pointer inside the @der. * * Returns: %ASN1_SUCCESS if successful or an error value. **/ int -asn1_decode_string_der (unsigned int etype, const unsigned char *der, unsigned int der_len, - unsigned char **str, unsigned int *str_len) +asn1_decode_simple_der (unsigned int etype, const unsigned char *der, unsigned int der_len, + const unsigned char **str, unsigned int *str_len) { - int tag_len, len_len, tlen; + int tag_len, len_len; const unsigned char* p; unsigned char class; unsigned long tag; @@ -2900,6 +2900,9 @@ asn1_decode_string_der (unsigned int etype, const unsigned char *der, unsigned i ret = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag); if (ret != ASN1_SUCCESS) return ret; + + if (class != ETYPE_CLASS(etype) || tag != ETYPE_TAG(etype)) + return ASN1_DER_ERROR; p += tag_len; der_len -= tag_len; @@ -2910,14 +2913,9 @@ asn1_decode_string_der (unsigned int etype, const unsigned char *der, unsigned i p += len_len; der_len -= len_len; - tlen = ret; - - *str = malloc(tlen); - if (*str == NULL) - return ASN1_MEM_ALLOC_ERROR; - - memcpy(*str, p, tlen); - *str_len = tlen; + + *str_len = ret; + *str = p; return ASN1_SUCCESS; } @@ -39,7 +39,6 @@ #include <libtasn1.h> #define ASN1_SMALL_VALUE_SIZE 16 -#define ASN1_MAX_TAG_SIZE 4 /* This structure is also in libtasn1.h, but then contains less fields. You cannot make any modifications to these first fields diff --git a/lib/libtasn1.h b/lib/libtasn1.h index 8c68205..6a164b0 100644 --- a/lib/libtasn1.h +++ b/lib/libtasn1.h @@ -272,7 +272,9 @@ extern "C" extern ASN1_API void asn1_perror (int error); +#define ASN1_MAX_TAG_SIZE 4 #define ASN1_MAX_LENGTH_SIZE 9 +#define ASN1_MAX_TL_SIZE (ASN1_MAX_TAG_SIZE+ASN1_MAX_LENGTH_SIZE) extern ASN1_API long asn1_get_length_der (const unsigned char *der, int der_len, int *len); @@ -285,12 +287,12 @@ extern "C" /* Other utility functions. */ extern ASN1_API - int asn1_decode_string_der (unsigned int etype, const unsigned char *der, unsigned int der_len, - unsigned char **str, unsigned int *str_len); + int asn1_decode_simple_der (unsigned int etype, const unsigned char *der, unsigned int der_len, + const unsigned char **str, unsigned int *str_len); - extern ASN1_API - int asn1_encode_string_der (unsigned int etype, const unsigned char *str, unsigned int str_len, - unsigned char **der, unsigned int *der_len); + extern ASN1_API int + asn1_encode_simple_der (unsigned int etype, const unsigned char *str, unsigned int str_len, + unsigned char *tl, unsigned int *tl_len); extern ASN1_API asn1_node asn1_find_node (asn1_node pointer, const char *name); diff --git a/lib/libtasn1.map b/lib/libtasn1.map index 9ae741a..a2b9416 100644 --- a/lib/libtasn1.map +++ b/lib/libtasn1.map @@ -50,8 +50,8 @@ LIBTASN1_0_3 asn1_strerror; asn1_write_value; asn1_read_node_value; - asn1_encode_string_der; - asn1_decode_string_der; + asn1_encode_simple_der; + asn1_decode_simple_der; # Old symbols libtasn1_strerror; |