static SigV4Status_t writeStringToSign()

in source/sigv4.c [2711:2787]


static SigV4Status_t writeStringToSign( const SigV4Parameters_t * pParams,
                                        const char * pAlgorithm,
                                        size_t algorithmLen,
                                        CanonicalContext_t * pCanonicalContext )
{
    SigV4Status_t returnStatus = SigV4Success;
    char * pBufStart = ( char * ) pCanonicalContext->pBufProcessing;
    ptrdiff_t bufferLen = pCanonicalContext->pBufCur - pBufStart;
    /* An overestimate but sufficient memory is checked before proceeding. */
    size_t encodedLen = SIGV4_PROCESSING_BUFFER_LENGTH;

    /* The string to sign is composed of (+ means string concatenation):
     * Algorithm + \n +
     * RequestDateTime + \n +
     * CredentialScope + \n +
     * HashedCanonicalRequest
     *
     * The processing buffer is verified beforehand that it has enough
     * space to hold this string. */
    size_t sizeNeededBeforeHash = algorithmLen + 1U + \
                                  SIGV4_ISO_STRING_LEN + 1U;

    assert( pParams != NULL );
    assert( ( pAlgorithm != NULL ) && ( algorithmLen > 0 ) );
    assert( pCanonicalContext != NULL );

    sizeNeededBeforeHash += sizeNeededForCredentialScope( pParams ) + 1U;

    /* Check if there is enough space for the string to sign. */
    if( ( sizeNeededBeforeHash + ( pParams->pCryptoInterface->hashDigestLen * 2U ) ) >
        SIGV4_PROCESSING_BUFFER_LENGTH )
    {
        returnStatus = SigV4InsufficientMemory;
        LOG_INSUFFICIENT_MEMORY_ERROR( "for string to sign",
                                       sizeNeededBeforeHash + ( pParams->pCryptoInterface->hashDigestLen * 2U ) - SIGV4_PROCESSING_BUFFER_LENGTH );
    }
    else
    {
        /* Hash the canonical request to its precalculated location in the string to sign. */
        returnStatus = completeHashAndHexEncode( pBufStart,
                                                 ( size_t ) bufferLen,
                                                 pBufStart + sizeNeededBeforeHash,
                                                 &encodedLen,
                                                 pParams->pCryptoInterface );
    }

    if( returnStatus == SigV4Success )
    {
        size_t bytesWritten = 0U;
        SigV4String_t credentialScope;

        pCanonicalContext->pBufCur = pBufStart + sizeNeededBeforeHash + encodedLen;
        pCanonicalContext->bufRemaining = SIGV4_PROCESSING_BUFFER_LENGTH - encodedLen - sizeNeededBeforeHash;

        bytesWritten = writeStringToSignPrefix( pBufStart,
                                                pAlgorithm,
                                                algorithmLen,
                                                pParams->pDateIso8601 );
        pBufStart += bytesWritten;
        credentialScope.pData = pBufStart;
        credentialScope.dataLen = sizeNeededForCredentialScope( pParams );
        /* Concatenate credential scope. */
        ( void ) generateCredentialScope( pParams, &credentialScope );
        pBufStart += credentialScope.dataLen;
        /* Concatenate linefeed character. */
        *pBufStart = LINEFEED_CHAR;
    }

    if( returnStatus == SigV4Success )
    {
        LogDebug( ( "Generated String To Sign Key: %.*s",
                    ( unsigned int ) ( pCanonicalContext->pBufCur - pBufStart ),
                    pBufStart ) );
    }

    return returnStatus;
}