From 46d58bb58370ae088027597a2b5bffb78088ee83 Mon Sep 17 00:00:00 2001 From: Giuseppe Penone Date: Mon, 2 Oct 2023 09:08:25 +0100 Subject: [PATCH] encodeURI exposed to outside world --- source/include/sigv4.h | 25 +++++++++++++++ source/sigv4.c | 69 +++++++++++++++--------------------------- 2 files changed, 50 insertions(+), 44 deletions(-) diff --git a/source/include/sigv4.h b/source/include/sigv4.h index 023f4573..1a90de78 100644 --- a/source/include/sigv4.h +++ b/source/include/sigv4.h @@ -574,6 +574,31 @@ SigV4Status_t SigV4_AwsIotDateToIso8601( const char * pDate, size_t dateISO8601Len ); /* @[declare_sigV4_awsIotDateToIso8601_function] */ +#if ( SIGV4_USE_CANONICAL_SUPPORT == 1 ) + +/** + * @brief Normalize a URI string according to RFC 3986 and fill destination + * buffer with the formatted string. + * + * @param[in] pUri The URI string to encode. + * @param[in] uriLen Length of pUri. + * @param[out] pCanonicalURI The resulting canonicalized URI. + * @param[in, out] canonicalURILen input: the length of pCanonicalURI, + * output: the length of the generated canonical URI. + * @param[in] encodeSlash Option to indicate if slashes should be encoded. + * @param[in] doubleEncodeEquals Option to indicate if equals should be double-encoded. + */ +/* @[declare_sigV4_EncodeURI_function] */ +SigV4Status_t SigV4_EncodeURI( const char * pUri, + size_t uriLen, + char * pCanonicalURI, + size_t * canonicalURILen, + bool encodeSlash, + bool doubleEncodeEquals ); +/* @[declare_sigV4_encodeURI_function] */ + +#endif /* #if (SIGV4_USE_CANONICAL_SUPPORT == 1) */ + /* *INDENT-OFF* */ #ifdef __cplusplus } diff --git a/source/sigv4.c b/source/sigv4.c index b212a954..5f5ba607 100644 --- a/source/sigv4.c +++ b/source/sigv4.c @@ -41,25 +41,6 @@ #if ( SIGV4_USE_CANONICAL_SUPPORT == 1 ) -/** - * @brief Normalize a URI string according to RFC 3986 and fill destination - * buffer with the formatted string. - * - * @param[in] pUri The URI string to encode. - * @param[in] uriLen Length of pUri. - * @param[out] pCanonicalURI The resulting canonicalized URI. - * @param[in, out] canonicalURILen input: the length of pCanonicalURI, - * output: the length of the generated canonical URI. - * @param[in] encodeSlash Option to indicate if slashes should be encoded. - * @param[in] doubleEncodeEquals Option to indicate if equals should be double-encoded. - */ - static SigV4Status_t encodeURI( const char * pUri, - size_t uriLen, - char * pCanonicalURI, - size_t * canonicalURILen, - bool encodeSlash, - bool doubleEncodeEquals ); - /** * @brief Canonicalize the full URI path. The input URI starts after the * HTTP host and ends at the question mark character ("?") that begins the @@ -1316,12 +1297,12 @@ static void generateCredentialScope( const SigV4Parameters_t * pSigV4Params, /*-----------------------------------------------------------*/ - static SigV4Status_t encodeURI( const char * pUri, - size_t uriLen, - char * pCanonicalURI, - size_t * canonicalURILen, - bool encodeSlash, - bool doubleEncodeEquals ) + SigV4Status_t SigV4_EncodeURI( const char * pUri, + size_t uriLen, + char * pCanonicalURI, + size_t * canonicalURILen, + bool encodeSlash, + bool doubleEncodeEquals ) { size_t uriIndex = 0U, bytesConsumed = 0U; size_t bufferLen = 0U; @@ -1416,7 +1397,7 @@ static void generateCredentialScope( const SigV4Parameters_t * pSigV4Params, /* If the canonical URI needs to be encoded twice, then we encode once here, * and again at the end of the buffer. Afterwards, the second encode is copied * to overwrite the first one. */ - returnStatus = encodeURI( pUri, uriLen, pBufLoc, &encodedLen, false, false ); + returnStatus = SigV4_EncodeURI( pUri, uriLen, pBufLoc, &encodedLen, false, false ); if( returnStatus == SigV4Success ) { @@ -1428,12 +1409,12 @@ static void generateCredentialScope( const SigV4Parameters_t * pSigV4Params, * written to a different position in the buffer. It should not be done * at an overlapping position of the single-encoded URI. Once written, * the double-encoded URI is moved to the starting location of the single-encoded URI. */ - returnStatus = encodeURI( pBufLoc, - encodedLen, - pBufLoc + encodedLen, - &doubleEncodedLen, - false, - false ); + returnStatus = SigV4_EncodeURI( pBufLoc, + encodedLen, + pBufLoc + encodedLen, + &doubleEncodedLen, + false, + false ); if( returnStatus == SigV4Success ) { @@ -2078,12 +2059,12 @@ static void generateCredentialScope( const SigV4Parameters_t * pSigV4Params, /* Encode parameter value if non-empty. Query parameters can have empty values. */ if( valueLen > 0U ) { - returnStatus = encodeURI( pValue, - valueLen, - pBufCur + 1U, - &valueBytesWritten, - true, - false ); + returnStatus = SigV4_EncodeURI( pValue, + valueLen, + pBufCur + 1U, + &valueBytesWritten, + true, + false ); if( returnStatus == SigV4Success ) { @@ -2117,12 +2098,12 @@ static void generateCredentialScope( const SigV4Parameters_t * pSigV4Params, assert( pCanonicalRequest->pQueryLoc[ paramsIndex ].key.dataLen > 0U ); encodedLen = remainingLen; - returnStatus = encodeURI( pCanonicalRequest->pQueryLoc[ paramsIndex ].key.pData, - pCanonicalRequest->pQueryLoc[ paramsIndex ].key.dataLen, - pBufLoc, - &encodedLen, - true /* Encode slash (/) */, - false /* Do not encode '='. */ ); + returnStatus = SigV4_EncodeURI( pCanonicalRequest->pQueryLoc[ paramsIndex ].key.pData, + pCanonicalRequest->pQueryLoc[ paramsIndex ].key.dataLen, + pBufLoc, + &encodedLen, + true /* Encode slash (/) */, + false /* Do not double encode '='. */ ); if( returnStatus == SigV4Success ) {