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 2d61d9d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 42 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
38 changes: 24 additions & 14 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,9 +191,9 @@ 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 ) )
{
Expand All @@ -201,6 +203,8 @@ static int32_t sha256_final( void * pHashContext,
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 2d61d9d

Please sign in to comment.