From 6bba3a2a7065286f79787fc4c98a719707cd28fe Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Sun, 24 Aug 2014 15:00:32 +0200 Subject: [PATCH] change the ASN1 type to be a typedef replace all 'default' cases in the switch statements --- src/headers/tomcrypt_pk.h | 6 +++--- src/pk/asn1/der/choice/der_decode_choice.c | 4 +++- .../der/sequence/der_decode_sequence_ex.c | 6 ++++-- .../der/sequence/der_decode_sequence_multi.c | 19 +++++++++---------- .../der/sequence/der_encode_sequence_ex.c | 13 ++++++++++--- .../der/sequence/der_encode_sequence_multi.c | 17 ++++++++++++----- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/headers/tomcrypt_pk.h b/src/headers/tomcrypt_pk.h index ef0a5f9..3621140 100644 --- a/src/headers/tomcrypt_pk.h +++ b/src/headers/tomcrypt_pk.h @@ -435,7 +435,7 @@ int dsa_shared_secret(void *private_key, void *base, #ifdef LTC_DER /* DER handling */ -enum { +typedef enum ltc_asn1_type_ { LTC_ASN1_EOL, LTC_ASN1_BOOLEAN, LTC_ASN1_INTEGER, @@ -455,12 +455,12 @@ enum { LTC_ASN1_RAW_BIT_STRING, LTC_ASN1_TELETEX_STRING, LTC_ASN1_CONSTRUCTED, -}; +} ltc_asn1_type; /** A LTC ASN.1 list type */ typedef struct ltc_asn1_list_ { /** The LTC ASN.1 enumerated type identifier */ - int type; + ltc_asn1_type type; /** The data to encode or place for decoding */ void *data; /** The size of the input or resulting output */ diff --git a/src/pk/asn1/der/choice/der_decode_choice.c b/src/pk/asn1/der/choice/der_decode_choice.c index 46c314c..17aa105 100644 --- a/src/pk/asn1/der/choice/der_decode_choice.c +++ b/src/pk/asn1/der/choice/der_decode_choice.c @@ -198,7 +198,9 @@ int der_decode_choice(const unsigned char *in, unsigned long *inlen, } break; - default: + case LTC_ASN1_CHOICE: + case LTC_ASN1_CONSTRUCTED: + case LTC_ASN1_EOL: return CRYPT_INVALID_ARG; } } diff --git a/src/pk/asn1/der/sequence/der_decode_sequence_ex.c b/src/pk/asn1/der/sequence/der_decode_sequence_ex.c index 4925041..b233641 100644 --- a/src/pk/asn1/der/sequence/der_decode_sequence_ex.c +++ b/src/pk/asn1/der/sequence/der_decode_sequence_ex.c @@ -31,7 +31,8 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen, ltc_asn1_list *list, unsigned long outlen, int ordered) { - int err, type, i; + int err, i; + ltc_asn1_type type; unsigned long size, x, y, z, blksize; void *data; @@ -282,7 +283,8 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen, } break; - default: + case LTC_ASN1_CONSTRUCTED: + case LTC_ASN1_EOL: err = CRYPT_INVALID_ARG; goto LBL_ERR; } diff --git a/src/pk/asn1/der/sequence/der_decode_sequence_multi.c b/src/pk/asn1/der/sequence/der_decode_sequence_multi.c index b8f2b3f..2169938 100644 --- a/src/pk/asn1/der/sequence/der_decode_sequence_multi.c +++ b/src/pk/asn1/der/sequence/der_decode_sequence_multi.c @@ -28,7 +28,8 @@ */ int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...) { - int err, type; + int err; + ltc_asn1_type type; unsigned long size, x; void *data; va_list args; @@ -40,7 +41,7 @@ int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...) va_start(args, inlen); x = 0; for (;;) { - type = va_arg(args, int); + type = va_arg(args, ltc_asn1_type); size = va_arg(args, unsigned long); data = va_arg(args, void*); @@ -69,7 +70,8 @@ int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...) ++x; break; - default: + case LTC_ASN1_EOL: + case LTC_ASN1_CONSTRUCTED: va_end(args); return CRYPT_INVALID_ARG; } @@ -90,7 +92,7 @@ int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...) va_start(args, inlen); x = 0; for (;;) { - type = va_arg(args, int); + type = va_arg(args, ltc_asn1_type); size = va_arg(args, unsigned long); data = va_arg(args, void*); @@ -118,17 +120,14 @@ int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...) case LTC_ASN1_TELETEX_STRING: LTC_SET_ASN1(list, x++, type, data, size); break; - - default: - va_end(args); - err = CRYPT_INVALID_ARG; - goto LBL_ERR; + case LTC_ASN1_EOL: + case LTC_ASN1_CONSTRUCTED: + break; } } va_end(args); err = der_decode_sequence(in, inlen, list, x); -LBL_ERR: XFREE(list); return err; } diff --git a/src/pk/asn1/der/sequence/der_encode_sequence_ex.c b/src/pk/asn1/der/sequence/der_encode_sequence_ex.c index e054840..10f4dbe 100644 --- a/src/pk/asn1/der/sequence/der_encode_sequence_ex.c +++ b/src/pk/asn1/der/sequence/der_encode_sequence_ex.c @@ -31,7 +31,8 @@ int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen, unsigned char *out, unsigned long *outlen, int type_of) { - int err, type; + int err; + ltc_asn1_type type; unsigned long size, x, y, z, i; void *data; @@ -135,7 +136,10 @@ int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen, y += x; break; - default: + case LTC_ASN1_CHOICE: + case LTC_ASN1_CONSTRUCTED: + case LTC_ASN1_EOL: + case LTC_ASN1_TELETEX_STRING: err = CRYPT_INVALID_ARG; goto LBL_ERR; } @@ -330,7 +334,10 @@ int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen, *outlen -= z; break; - default: + case LTC_ASN1_CHOICE: + case LTC_ASN1_CONSTRUCTED: + case LTC_ASN1_EOL: + case LTC_ASN1_TELETEX_STRING: err = CRYPT_INVALID_ARG; goto LBL_ERR; } diff --git a/src/pk/asn1/der/sequence/der_encode_sequence_multi.c b/src/pk/asn1/der/sequence/der_encode_sequence_multi.c index 618d59d..da5d371 100644 --- a/src/pk/asn1/der/sequence/der_encode_sequence_multi.c +++ b/src/pk/asn1/der/sequence/der_encode_sequence_multi.c @@ -28,7 +28,8 @@ */ int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...) { - int err, type; + int err; + ltc_asn1_type type; unsigned long size, x; void *data; va_list args; @@ -41,7 +42,7 @@ int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...) va_start(args, outlen); x = 0; for (;;) { - type = va_arg(args, int); + type = va_arg(args, ltc_asn1_type); size = va_arg(args, unsigned long); data = va_arg(args, void*); @@ -68,7 +69,10 @@ int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...) ++x; break; - default: + case LTC_ASN1_CHOICE: + case LTC_ASN1_CONSTRUCTED: + case LTC_ASN1_EOL: + case LTC_ASN1_TELETEX_STRING: va_end(args); return CRYPT_INVALID_ARG; } @@ -89,7 +93,7 @@ int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...) va_start(args, outlen); x = 0; for (;;) { - type = va_arg(args, int); + type = va_arg(args, ltc_asn1_type); size = va_arg(args, unsigned long); data = va_arg(args, void*); @@ -116,7 +120,10 @@ int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...) LTC_SET_ASN1(list, x++, type, data, size); break; - default: + case LTC_ASN1_CHOICE: + case LTC_ASN1_CONSTRUCTED: + case LTC_ASN1_EOL: + case LTC_ASN1_TELETEX_STRING: va_end(args); err = CRYPT_INVALID_ARG; goto LBL_ERR;