Skip to content

Commit 20180ca

Browse files
Add ASN.1 ENUMERATED tag support
Add ASN.1 ENUMERATED [1] tag to supported tag list. 1. https://tools.ietf.org/html/rfc3641#page-8 Signed-off-by: Mykhailo Sopiha <[email protected]>
1 parent 0eaf49c commit 20180ca

File tree

4 files changed

+71
-11
lines changed

4 files changed

+71
-11
lines changed

include/mbedtls/asn1.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#define MBEDTLS_ASN1_OCTET_STRING 0x04
7676
#define MBEDTLS_ASN1_NULL 0x05
7777
#define MBEDTLS_ASN1_OID 0x06
78+
#define MBEDTLS_ASN1_ENUMERATED 0x0A
7879
#define MBEDTLS_ASN1_UTF8_STRING 0x0C
7980
#define MBEDTLS_ASN1_SEQUENCE 0x10
8081
#define MBEDTLS_ASN1_SET 0x11
@@ -254,13 +255,32 @@ int mbedtls_asn1_get_bool( unsigned char **p,
254255
* a valid ASN.1 INTEGER.
255256
* \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
256257
* not fit in an \c int.
257-
* \return An ASN.1 error code if the input does not start with
258-
* a valid ASN.1 INTEGER.
259258
*/
260259
int mbedtls_asn1_get_int( unsigned char **p,
261260
const unsigned char *end,
262261
int *val );
263262

263+
/**
264+
* \brief Retrieve an enumerated ASN.1 tag and its value.
265+
* Updates the pointer to immediately behind the full tag.
266+
*
267+
* \param p On entry, \c *p points to the start of the ASN.1 element.
268+
* On successful completion, \c *p points to the first byte
269+
* beyond the ASN.1 element.
270+
* On error, the value of \c *p is undefined.
271+
* \param end End of data.
272+
* \param val On success, the parsed value.
273+
*
274+
* \return 0 if successful.
275+
* \return An ASN.1 error code if the input does not start with
276+
* a valid ASN.1 ENUMERATED.
277+
* \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
278+
* not fit in an \c int.
279+
*/
280+
int mbedtls_asn1_get_enum( unsigned char **p,
281+
const unsigned char *end,
282+
int *val );
283+
264284
/**
265285
* \brief Retrieve a bitstring ASN.1 tag and its value.
266286
* Updates the pointer to immediately behind the full tag.
@@ -367,8 +387,6 @@ int mbedtls_asn1_get_sequence_of( unsigned char **p,
367387
* \return #MBEDTLS_ERR_ASN1_INVALID_LENGTH if the parsed value does
368388
* not fit in an \c int.
369389
* \return An MPI error code if the parsed value is too large.
370-
* \return An ASN.1 error code if the input does not start with
371-
* a valid ASN.1 INTEGER.
372390
*/
373391
int mbedtls_asn1_get_mpi( unsigned char **p,
374392
const unsigned char *end,

include/mbedtls/asn1write.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start,
192192
*/
193193
int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val );
194194

195+
/**
196+
* \brief Write an enum tag (#MBEDTLS_ASN1_ENUMERATED) and value
197+
* in ASN.1 format.
198+
*
199+
* \note This function works backwards in data buffer.
200+
*
201+
* \param p The reference to the current position pointer.
202+
* \param start The start of the buffer, for bounds-checking.
203+
* \param val The integer value to write.
204+
*
205+
* \return The number of bytes written to \p p on success.
206+
* \return A negative \c MBEDTLS_ERR_ASN1_XXX error code on failure.
207+
*/
208+
int mbedtls_asn1_write_enum( unsigned char **p, unsigned char *start, int val );
209+
195210
/**
196211
* \brief Write a string in ASN.1 format using a specific
197212
* string encoding tag.

library/asn1parse.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,20 @@ int mbedtls_asn1_get_bool( unsigned char **p,
139139
return( 0 );
140140
}
141141

142-
int mbedtls_asn1_get_int( unsigned char **p,
143-
const unsigned char *end,
144-
int *val )
142+
static int asn1_get_tagged_int( unsigned char **p,
143+
const unsigned char *end,
144+
int tag, int *val )
145145
{
146146
int ret;
147147
size_t len;
148148

149-
if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
149+
if( ( ret = mbedtls_asn1_get_tag( p, end, &len, tag ) ) != 0 )
150150
return( ret );
151151

152-
/* len==0 is malformed (0 must be represented as 020100). */
152+
/*
153+
* len==0 is malformed (0 must be represented as 020100 for INTEGER,
154+
* or 0A0100 for ENUMERATED tags
155+
*/
153156
if( len == 0 )
154157
return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
155158
/* This is a cryptography library. Reject negative integers. */
@@ -180,6 +183,20 @@ int mbedtls_asn1_get_int( unsigned char **p,
180183
return( 0 );
181184
}
182185

186+
int mbedtls_asn1_get_int( unsigned char **p,
187+
const unsigned char *end,
188+
int *val )
189+
{
190+
return( asn1_get_tagged_int( p, end, MBEDTLS_ASN1_INTEGER, val) );
191+
}
192+
193+
int mbedtls_asn1_get_enum( unsigned char **p,
194+
const unsigned char *end,
195+
int *val )
196+
{
197+
return( asn1_get_tagged_int( p, end, MBEDTLS_ASN1_ENUMERATED, val) );
198+
}
199+
183200
#if defined(MBEDTLS_BIGNUM_C)
184201
int mbedtls_asn1_get_mpi( unsigned char **p,
185202
const unsigned char *end,

library/asn1write.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolea
231231
return( (int) len );
232232
}
233233

234-
int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
234+
static int asn1_write_tagged_int( unsigned char **p, unsigned char *start, int val, int tag )
235235
{
236236
int ret;
237237
size_t len = 0;
@@ -255,11 +255,21 @@ int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
255255
}
256256

257257
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
258-
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
258+
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
259259

260260
return( (int) len );
261261
}
262262

263+
int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
264+
{
265+
return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_INTEGER ) );
266+
}
267+
268+
int mbedtls_asn1_write_enum( unsigned char **p, unsigned char *start, int val )
269+
{
270+
return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_ENUMERATED ) );
271+
}
272+
263273
int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
264274
const char *text, size_t text_len )
265275
{

0 commit comments

Comments
 (0)