Skip to content

Commit

Permalink
Fix build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aggarw13 committed Aug 10, 2021
1 parent dea0cc9 commit 098f715
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 176 deletions.
2 changes: 1 addition & 1 deletion source/include/sigv4.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ typedef enum SigV4Status
* Functions that may return this value:
* - #SigV4_GenerateHTTPAuthorization
*/
SigV4HashError,
SigV4HashError
} SigV4Status_t;

/**
Expand Down
38 changes: 11 additions & 27 deletions source/sigv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@

#if ( SIGV4_USE_CANONICAL_SUPPORT == 1 )

/**
* @brief Verifies if a SigV4 string value is empty.
*
* @param[in] pInput The SigV4 string value struct to verify.
*
* @return Returns 'true' if @pInput is empty, and 'false' otherwise.
*/
static bool emptySigV4String( SigV4ConstString_t * pInput );

/**
* @brief Normalize a URI string according to RFC 3986 and fill destination
* buffer with the formatted string.
Expand Down Expand Up @@ -1050,17 +1041,6 @@ static SigV4Status_t generateCredentialScope( const SigV4Parameters_t * pSigV4Pa

#if ( SIGV4_USE_CANONICAL_SUPPORT == 1 )

static bool emptySigV4String( SigV4ConstString_t * pInput )
{
bool returnVal = true;

assert( pInput != NULL );

return ( pInput->pData == NULL || pInput->dataLen == 0 ) ? returnVal : !returnVal;
}

/*-----------------------------------------------------------*/

static int cmpHeaderField( const void * pFirstVal,
const void * pSecondVal )
{
Expand All @@ -1073,8 +1053,8 @@ static SigV4Status_t generateCredentialScope( const SigV4Parameters_t * pSigV4Pa
pFirst = ( SigV4KeyValuePair_t * ) pFirstVal;
pSecond = ( SigV4KeyValuePair_t * ) pSecondVal;

assert( !emptySigV4String( &pFirst->key ) );
assert( !emptySigV4String( &pSecond->key ) );
assert( ( pFirst->key.pData != NULL ) && ( pFirst->key.dataLen != 0U ) );
assert( ( pSecond->key.pData != NULL ) && ( pSecond->key.dataLen != 0U ) );

if( pFirst->key.dataLen <= pSecond->key.dataLen )
{
Expand Down Expand Up @@ -1105,8 +1085,8 @@ static SigV4Status_t generateCredentialScope( const SigV4Parameters_t * pSigV4Pa
pFirst = ( SigV4KeyValuePair_t * ) pFirstVal;
pSecond = ( SigV4KeyValuePair_t * ) pSecondVal;

assert( !emptySigV4String( &pFirst->key ) );
assert( !emptySigV4String( &pSecond->key ) );
assert( ( pFirst->key.pData != NULL ) && ( pFirst->key.dataLen != 0U ) );
assert( ( pSecond->key.pData != NULL ) && ( pSecond->key.dataLen != 0U ) );

lenSmall = ( pFirst->key.dataLen < pSecond->key.dataLen ) ? pFirst->key.dataLen : pSecond->key.dataLen;
compResult = ( int32_t ) strncmp( ( char * ) pFirst->key.pData,
Expand Down Expand Up @@ -1168,6 +1148,9 @@ static SigV4Status_t generateCredentialScope( const SigV4Parameters_t * pSigV4Pa
assert( pBuffer != NULL );
assert( bufferLen >= URI_ENCODED_SPECIAL_CHAR_SIZE );

/* Suppress unused warning in when asserts are disabled. */
( void ) bufferLen;

*pBuffer = '%';
*( pBuffer + 1U ) = toUpperHexChar( code >> 4 );
*( pBuffer + 2U ) = toUpperHexChar( code & 0x0F );
Expand All @@ -1183,6 +1166,9 @@ static SigV4Status_t generateCredentialScope( const SigV4Parameters_t * pSigV4Pa
assert( pBuffer != NULL );
assert( bufferLen > URI_DOUBLE_ENCODED_EQUALS_CHAR_SIZE );

/* Suppress unused warning in when asserts are disabled. */
( void ) bufferLen;

*pBuffer = '%';
*( pBuffer + 1U ) = '2';
*( pBuffer + 2U ) = '5';
Expand Down Expand Up @@ -1761,7 +1747,7 @@ static SigV4Status_t generateCredentialScope( const SigV4Parameters_t * pSigV4Pa
{
returnStatus = SigV4MaxQueryPairCountExceeded;
LogError( ( "Failed to parse query string: Number of query parameters exceeds max threshold defined in config. "
"SIGV4_MAX_QUERY_PAIR_COUNT=%lu", SIGV4_MAX_QUERY_PAIR_COUNT ) );
"SIGV4_MAX_QUERY_PAIR_COUNT=%lu", ( unsigned long ) SIGV4_MAX_QUERY_PAIR_COUNT ) );
break;
}
}
Expand Down Expand Up @@ -2602,8 +2588,6 @@ static SigV4Status_t generateAuthorizationValuePrefix( const SigV4Parameters_t *
LogError( ( "Insufficient memory provided to write the Authorization header value, bytesExceeded=%lu",
( unsigned long ) ( authPrefixLen + encodedSignatureLen - *pAuthPrefixLen ) ) );
returnStatus = SigV4InsufficientMemory;
LOG_INSUFFICIENT_MEMORY_ERROR( "string to sign",
sizeNeededBeforeHash + encodedSignatureLen - SIGV4_PROCESSING_BUFFER_LENGTH );
}
}

Expand Down
166 changes: 33 additions & 133 deletions test/unit-test/sigv4_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,38 @@
* must NOT be set.
*/

#ifndef SIGV4_CONFIG_DEFAULTS_H_
#define SIGV4_CONFIG_DEFAULTS_H_
#ifndef SIGV4_CONFIG_H_
#define SIGV4_CONFIG_H_


#define LOGGING_LEVEL_DEBUG 1

/* @[code_example_loggingmacros] */
/************* Define Logging Macros using printf function ***********/

#define PrintfError( ... ) printf( "Error: "__VA_ARGS__ ); printf( "\n" )
#define PrintfWarn( ... ) printf( "Warn: "__VA_ARGS__ ); printf( "\n" )
#define PrintfInfo( ... ) printf( "Info: " __VA_ARGS__ ); printf( "\n" )
#define PrintfDebug( ... ) printf( "Debug: " __VA_ARGS__ ); printf( "\n" )

#ifdef LOGGING_LEVEL_ERROR
#define LogError( message ) PrintfError message
#elif defined( LOGGING_LEVEL_WARNING )
#define LogError( message ) PrintfError message
#define LogWarn( message ) PrintfWarn message
#elif defined( LOGGING_LEVEL_INFO )
#define LogError( message ) PrintfError message
#define LogWarn( message ) PrintfWarn message
#define LogInfo( message ) PrintfInfo message
#elif defined( LOGGING_LEVEL_DEBUG )
#define LogError( message ) PrintfError message
#define LogWarn( message ) PrintfWarn message
#define LogInfo( message ) PrintfInfo message
#define LogDebug( message ) PrintfDebug message
#endif /* ifdef LOGGING_LEVEL_ERROR */

/**************************************************/
/* @[code_example_loggingmacros] */

/**
* @brief Macro defining the size of the internal buffer used for incremental
Expand Down Expand Up @@ -77,134 +107,4 @@
#define SIGV4_MAX_QUERY_PAIR_COUNT 5U
#endif

/**
* @brief Macro indicating the largest block size of any hashing
* algorithm used for SigV4 authentication i.e. the maximum of all
* values specified for #SigV4CryptoInterface.hashBlockLen. For example,
* using SHA-512 would make this value must be at least 128.
*
* <b>Possible values:</b> Any positive 32 bit integer. <br>
* <b>Default value:</b> `64`
*/
#ifndef SIGV4_HASH_MAX_BLOCK_LENGTH
#define SIGV4_HASH_MAX_BLOCK_LENGTH 1024U
#endif

/**
* @brief Macro defining the maximum digest length of the specified hash function,
* used to determine the length of the output buffer.
*
* This macro should be updated if using a hashing algorithm other than SHA256
* (32 byte digest length). For example, using SHA512 would make this
* value must be at least 64.
*
* <b>Possible values:</b> Any positive 32 bit integer. <br>
* <b>Default value:</b> `32`
*/
#ifndef SIGV4_HASH_MAX_DIGEST_LENGTH
#define SIGV4_HASH_MAX_DIGEST_LENGTH 1024U
#endif

/**
* @brief Macro to statically enable support for canonicalizing the URI,
* headers, and query in this utility.
*
* Set this to one to enable the encoding functions used to create the canonical
* request.
*
* <b>Possible values:</b> 0 or 1 <br>
* <b>Default value:</b> `1`
*/
#ifndef SIGV4_USE_CANONICAL_SUPPORT
#define SIGV4_USE_CANONICAL_SUPPORT 1
#endif

/**
* @brief Macro called by the SigV4 Utility library for logging "Error" level
* messages.
*
* To enable error level logging in the SigV4 Utility library, this macro should
* be mapped to the application-specific logging implementation that supports
* error logging.
*
* @note This logging macro is called in the SigV4 Utility library with
* parameters wrapped in double parentheses to be ISO C89/C90 standard
* compliant. For a reference POSIX implementation of the logging macros, refer
* to sigv4_config.h files, and the logging-stack in demos folder of the [AWS
* IoT Embedded C SDK
* repository](https://github.com/aws/aws-iot-device-sdk-embedded-C).
*
* <b>Default value</b>: Error logging is turned off, and no code is generated
* for calls to the macro in the SigV4 Utility library on compilation.
*/
#ifndef LogError
#define LogError( message )
#endif

/**
* @brief Macro called by the the SigV4 Utility library for logging "Warning"
* level messages.
*
* To enable warning level logging in the SigV4 Utility library, this macro
* should be mapped to the application-specific logging implementation that
* supports warning logging.
*
* @note This logging macro is called in the SigV4 Utility library with
* parameters wrapped in double parentheses to be ISO C89/C90 standard
* compliant. For a reference POSIX implementation of the logging macros, refer
* to sigv4_config.h files, and the logging-stack in demos folder of the [AWS
* IoT Embedded C SDK
* repository](https://github.com/aws/aws-iot-device-sdk-embedded-C).
*
* <b>Default value</b>: Warning logs are turned off, and no code is generated
* for calls to the macro in the SigV4 Utility library on compilation.
*/
#ifndef LogWarn
#define LogWarn( message )
#endif

/**
* @brief Macro called by the the SigV4 Utility library for logging "Info" level
* messages.
*
* To enable info level logging in the SigV4 Utility library, this macro should
* be mapped to the application-specific logging implementation that supports
* info logging.
*
* @note This logging macro is called in the SigV4 Utility library with
* parameters wrapped in double parentheses to be ISO C89/C90 standard
* compliant. For a reference POSIX implementation of the logging macros, refer
* to sigv4_config.h files, and the logging-stack in demos folder of the [AWS
* IoT Embedded C SDK
* repository](https://github.com/aws/aws-iot-device-sdk-embedded-C).
*
* <b>Default value</b>: Info logging is turned off, and no code is generated
* for calls to the macro in the SigV4 Utility library on compilation.
*/
#ifndef LogInfo
#define LogInfo( message )
#endif

/**
* @brief Macro called by the the SigV4 Utility library for logging "Debug"
* level messages.
*
* To enable debug level logging from SigV4 Utility library, this macro should
* be mapped to the application-specific logging implementation that supports
* debug logging.
*
* @note This logging macro is called in the SigV4 Utility library with
* parameters wrapped in double parentheses to be ISO C89/C90 standard
* compliant. For a reference POSIX implementation of the logging macros, refer
* to sigv4_config.h files, and the logging-stack in demos folder of the [AWS
* IoT Embedded C SDK
* repository](https://github.com/aws/aws-iot-device-sdk-embedded-C).
*
* <b>Default value</b>: Debug logging is turned off, and no code is generated
* for calls to the macro in the SigV4 Utility library on compilation.
*/
#ifndef LogDebug
#define LogDebug( message )
#endif

#endif /* ifndef SIGV4_CONFIG_DEFAULTS_H_ */
#endif /* ifndef SIGV4_CONFIG_H_ */
40 changes: 25 additions & 15 deletions test/unit-test/sigv4_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#define DATE "20150830T123600Z"
#define REGION "us-east-1"
#define SERVICE "iam"
#define HEADERS "Host: iam.amazonaws.com\r\nContent-Type: application/x-www-form-urlencoded; charset=utf-8\r\nX-Amz-Date: 20150830T123600Z\r\n\r\n"
#define HEADERS "Host: iam.amazonaws.com\r\nContent-Type: application/x-www-form-urlencoded; charset=utf-8\r\nX-Amz-Date: "DATE "\r\n\r\n"
#define PRECANON_HEADER "content-type:application/json;host:iam.amazonaws.com"
#define HEADERS_LENGTH ( sizeof( HEADERS ) - 1U )
#define SECURITY_TOKEN "security-token"
Expand All @@ -65,7 +65,7 @@
#define EXPIRATION_LENGTH ( sizeof( EXPIRATION ) - 1U )

/* Insufficient memory parameters for SIGV4_PROCESSING_BUFFER_LENGTH=350. In the comments below,
* + means concatenation. */
* + means concatenation, OOM means "Out of Memory", LF means newline character */

/* HTTP method + URI-encoded variant of this string must be greater than SIGV4_PROCESSING_BUFFER_LENGTH. */
#define PATH_FIRST_ENCODE_OOM \
Expand Down Expand Up @@ -167,7 +167,9 @@ void formatAndVerifyInputDate( const char * pInputDate,
tearDown();
}

static int32_t sha256_init( void * pHashContext )
/*==================== OpenSSL Based implementation of Crypto Interface ===================== */

static int32_t valid_sha256_init( void * pHashContext )
{
if( SHA256_Init( ( SHA256_CTX * ) pHashContext ) == 1 )
{
Expand All @@ -177,9 +179,9 @@ static int32_t sha256_init( void * pHashContext )
return -1;
}

static int32_t sha256_update( void * pHashContext,
const char * pInput,
size_t inputLen )
static int32_t valid_sha256_update( void * pHashContext,
const char * pInput,
size_t inputLen )
{
if( SHA256_Update( ( SHA256_CTX * ) pHashContext, pInput, inputLen ) )
{
Expand All @@ -189,18 +191,20 @@ static int32_t sha256_update( void * pHashContext,
return -1;
}

static int32_t sha256_final( void * pHashContext,
char * pOutput,
size_t outputLen )
static int32_t valid_sha256_final( void * pHashContext,
char * pOutput,
size_t outputLen )
{
if( SHA256_Final( pOutput, ( SHA256_CTX * ) pHashContext ) )
if( SHA256_Final( ( uint8_t * ) pOutput, ( SHA256_CTX * ) pHashContext ) )
{
return 0;
}

return -1;
}

/*==================== Echo Implementation of Crypto Interface ===================== */

static hashEchoBuffer[ SIGV4_HASH_MAX_BLOCK_LENGTH ];
static size_t hashInputLen;

Expand All @@ -227,6 +231,8 @@ static int32_t echo_hash_final( void * pHashContext,
( void ) memcpy( pOutput, hashEchoBuffer, hashInputLen );
}

/*==================== Failable Implementation of Crypto Interface ===================== */

/*
#define FAIL_HASH_INIT 1U,
#define FAIL_HASH_UPDATE 2U
Expand Down Expand Up @@ -280,6 +286,8 @@ static int32_t hash_final_failable( void * pHashContext,
return ret;
}

/*============================ Test Helpers ========================== */

static void resetFailableHashParams()
{
initHashCalledCount = 0U;
Expand Down Expand Up @@ -334,11 +342,11 @@ static void resetInputParams()
params.pService = SERVICE;
params.serviceLen = sizeof( SERVICE ) - 1U;
cryptoInterface.pHashContext = &sha256;
cryptoInterface.hashInit = sha256_init;
cryptoInterface.hashUpdate = sha256_update;
cryptoInterface.hashFinal = sha256_final;
cryptoInterface.hashBlockLen = SIGV4_SHA256_BLOCK_LENGTH;
cryptoInterface.hashDigestLen = SIGV4_SHA256_DIGEST_LENGTH;
cryptoInterface.hashInit = valid_sha256_init;
cryptoInterface.hashUpdate = valid_sha256_update;
cryptoInterface.hashFinal = valid_sha256_final;
cryptoInterface.hashBlockLen = SIGV4_HASH_MAX_BLOCK_LENGTH;
cryptoInterface.hashDigestLen = SIGV4_HASH_MAX_DIGEST_LENGTH;
params.pCryptoInterface = &cryptoInterface;
}

Expand Down Expand Up @@ -494,6 +502,8 @@ void test_SigV4_AwsIotDateToIso8601_Formatting_Error()
}
}

/* ======================= Testing SigV4_GenerateHTTPAuthorization =========================== */
/* TODO - Verify the generated signatures. */
void test_SigV4_GenerateHTTPAuthorization_Happy_Paths()
{
SigV4Status_t returnStatus;
Expand Down

0 comments on commit 098f715

Please sign in to comment.