SigV4 v1.3.0
SigV4 Library for AWS Authentication
 
Loading...
Searching...
No Matches
sigv4.h
Go to the documentation of this file.
1/*
2 * SigV4 Library v1.3.0
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
30#ifndef SIGV4_H_
31#define SIGV4_H_
32
33/* Standard includes. */
34#include <stdint.h>
35#include <stdbool.h>
36#include <stddef.h>
37
38/* *INDENT-OFF* */
39#ifdef __cplusplus
40 extern "C" {
41#endif
42/* *INDENT-ON* */
43
44/* SIGV4_DO_NOT_USE_CUSTOM_CONFIG allows building of the SigV4 library without a
45 * config file. If a config file is provided, the SIGV4_DO_NOT_USE_CUSTOM_CONFIG
46 * macro must not be defined.
47 */
48#ifndef SIGV4_DO_NOT_USE_CUSTOM_CONFIG
49 #include "sigv4_config.h"
50#endif
51
52/* Include config defaults header to get default values of configurations not
53 * defined in sigv4_config.h file. */
55
56/* Convenience macros for library optimization */
57
61#define SIGV4_AWS4_HMAC_SHA256 "AWS4-HMAC-SHA256"
62#define SIGV4_AWS4_HMAC_SHA256_LENGTH ( sizeof( SIGV4_AWS4_HMAC_SHA256 ) - 1U )
63#define SIGV4_HTTP_X_AMZ_DATE_HEADER "x-amz-date"
64#define SIGV4_HTTP_X_AMZ_SECURITY_TOKEN_HEADER "x-amz-security-token"
66#define SIGV4_STREAMING_AWS4_HMAC_SHA256_PAYLOAD "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"
67/* MISRA Ref 5.4.1 [Macro identifiers] */
68/* More details at: https://github.com/aws/SigV4-for-AWS-IoT-embedded-sdk/blob/main/MISRA.md#rule-54 */
69/* coverity[other_declaration] */
70#define SIGV4_HTTP_X_AMZ_CONTENT_SHA256_HEADER "x-amz-content-sha256"
71#define SIGV4_HTTP_X_AMZ_CONTENT_SHA256_HEADER_LENGTH ( sizeof( SIGV4_HTTP_X_AMZ_CONTENT_SHA256_HEADER ) - 1U )
72#define SIGV4_HTTP_X_AMZ_STORAGE_CLASS_HEADER "x-amz-storage-class"
74#define SIGV4_ACCESS_KEY_ID_LENGTH 20U
75#define SIGV4_SECRET_ACCESS_KEY_LENGTH 40U
77#define SIGV4_ISO_STRING_LEN 16U
78#define SIGV4_EXPECTED_LEN_RFC_3339 20U
79#define SIGV4_EXPECTED_LEN_RFC_5322 29U
100#define SIGV4_HTTP_PATH_IS_CANONICAL_FLAG 0x1U
101
109#define SIGV4_HTTP_QUERY_IS_CANONICAL_FLAG 0x2U
110
118#define SIGV4_HTTP_HEADERS_ARE_CANONICAL_FLAG 0x4U
119
127#define SIGV4_HTTP_PAYLOAD_IS_HASH 0x8U
128
136#define SIGV4_HTTP_IS_PRESIGNED_URL 0x10U
137
145#define SIGV4_HTTP_ALL_ARE_CANONICAL_FLAG 0x7U
146
151typedef enum SigV4Status
152{
162
172
182
190
202
214
222
231
237typedef struct SigV4CryptoInterface
238{
247 int32_t ( * hashInit )( void * pHashContext );
248
259 int32_t ( * hashUpdate )( void * pHashContext,
260 const uint8_t * pInput,
261 size_t inputLen );
262
276 int32_t ( * hashFinal )( void * pHashContext,
277 uint8_t * pOutput,
278 size_t outputLen );
279
284
289
295
301typedef struct SigV4HttpParameters
302{
303 const char * pHttpMethod;
317 uint32_t flags;
318
329 const char * pPath;
330 size_t pathLen;
341 const char * pQuery;
342 size_t queryLen;
353 const char * pHeaders;
354 size_t headersLen;
361 const char * pPayload;
362 size_t payloadLen;
364
370typedef struct SigV4Credentials
371{
376 const char * pAccessKeyId;
382 const char * pSecretAccessKey;
385
395typedef struct SigV4Parameters
396{
402
407 const char * pDateIso8601;
408
413 const char * pAlgorithm;
414
422 const char * pRegion;
423 size_t regionLen;
432 const char * pService;
433 size_t serviceLen;
439
445
502/* @[declare_sigV4_generateHTTPAuthorization_function] */
504 char * pAuthBuf,
505 size_t * authBufLen,
506 char ** pSignature,
507 size_t * signatureLen );
508/* @[declare_sigV4_generateHTTPAuthorization_function] */
509
573/* @[declare_sigV4_awsIotDateToIso8601_function] */
574SigV4Status_t SigV4_AwsIotDateToIso8601( const char * pDate,
575 size_t dateLen,
576 char * pDateISO8601,
577 size_t dateISO8601Len );
578/* @[declare_sigV4_awsIotDateToIso8601_function] */
579
580#if ( SIGV4_USE_CANONICAL_SUPPORT == 1 )
581
596/* @[declare_sigV4_encodeURI_function] */
597 SigV4Status_t SigV4_EncodeURI( const char * pUri,
598 size_t uriLen,
599 char * pCanonicalURI,
600 size_t * canonicalURILen,
601 bool encodeSlash,
602 bool doubleEncodeEquals );
603/* @[declare_sigV4_encodeURI_function] */
604
605#endif /* #if (SIGV4_USE_CANONICAL_SUPPORT == 1) */
606
607/* *INDENT-OFF* */
608#ifdef __cplusplus
609 }
610#endif
611/* *INDENT-ON* */
612
613#endif /* SIGV4_H_ */
SigV4Status_t
Return status of the SigV4 Library.
Definition: sigv4.h:152
@ SigV4Success
The SigV4 library function completed successfully.
Definition: sigv4.h:161
@ SigV4InsufficientMemory
The application buffer was not large enough for the specified hash function.
Definition: sigv4.h:181
@ SigV4HashError
An error occurred while performing a hash operation.
Definition: sigv4.h:221
@ SigV4ISOFormattingError
An error occurred while formatting the provided date header.
Definition: sigv4.h:189
@ SigV4MaxQueryPairCountExceeded
The maximum number of query parameters was exceeded while parsing the query string passed to the libr...
Definition: sigv4.h:213
@ SigV4MaxHeaderPairCountExceeded
The maximum number of header parameters was exceeded while parsing the http header string passed to t...
Definition: sigv4.h:201
@ SigV4InvalidHttpHeaders
HTTP headers parsed to the library are invalid.
Definition: sigv4.h:229
@ SigV4InvalidParameter
The SigV4 library function received an invalid input parameter.
Definition: sigv4.h:171
SigV4Status_t SigV4_GenerateHTTPAuthorization(const SigV4Parameters_t *pParams, char *pAuthBuf, size_t *authBufLen, char **pSignature, size_t *signatureLen)
Generates the HTTP Authorization header value.
Definition: sigv4.c:3119
SigV4Status_t SigV4_AwsIotDateToIso8601(const char *pDate, size_t dateLen, char *pDateISO8601, size_t dateISO8601Len)
Parse the date header value from the AWS IoT response, and generate the formatted ISO 8601 date requi...
Definition: sigv4.c:3041
SigV4Status_t SigV4_EncodeURI(const char *pUri, size_t uriLen, char *pCanonicalURI, size_t *canonicalURILen, bool encodeSlash, bool doubleEncodeEquals)
Normalize a URI string according to RFC 3986 and fill destination buffer with the formatted string.
The default values for configuration macros used by the SigV4 Library.
Configurations for the AWS credentials used to generate the Signing Key.
Definition: sigv4.h:371
size_t secretAccessKeyLen
Length of pSecretAccessKey.
Definition: sigv4.h:383
const char * pSecretAccessKey
The pSecretAccessKey MUST be at least 40 characters long.
Definition: sigv4.h:382
const char * pAccessKeyId
The pAccessKeyId MUST be at least 16 characters long but not more than 128 characters long.
Definition: sigv4.h:376
size_t accessKeyIdLen
Length of pAccessKeyId.
Definition: sigv4.h:377
The cryptography interface used to supply the user-defined hash implementation.
Definition: sigv4.h:238
size_t hashBlockLen
The block length of the hash function.
Definition: sigv4.h:288
size_t hashDigestLen
The digest length of the hash function.
Definition: sigv4.h:293
void * pHashContext
Context for the hashInit, hashUpdate, and hashFinal interfaces.
Definition: sigv4.h:283
Configurations of the HTTP request used to create the Canonical Request.
Definition: sigv4.h:302
const char * pPath
The path in the HTTP request. This is the absolute request URI, which contains everything in the URI ...
Definition: sigv4.h:329
const char * pHeaders
The headers from the HTTP request that we want to sign. This should be the raw headers in HTTP reques...
Definition: sigv4.h:353
size_t queryLen
Length of pQuery.
Definition: sigv4.h:342
const char * pPayload
The HTTP response body, if one exists (ex. PUT request). If this body is chunked, then this field sho...
Definition: sigv4.h:361
uint32_t flags
These flags are used to indicate if the path, query, or headers are already in the canonical form....
Definition: sigv4.h:317
size_t pathLen
Length of pPath.
Definition: sigv4.h:330
const char * pQuery
The HTTP request query from the URL, if it exists. This contains all characters following the questio...
Definition: sigv4.h:341
size_t headersLen
Length of pHeaders.
Definition: sigv4.h:354
size_t payloadLen
Length of pPayload.
Definition: sigv4.h:362
const char * pHttpMethod
The HTTP method: GET, POST, PUT, etc.
Definition: sigv4.h:303
size_t httpMethodLen
Length of pHttpMethod.
Definition: sigv4.h:304
Complete configurations required for generating "String to Sign" and "Signing Key" values.
Definition: sigv4.h:396
const char * pService
The target AWS service for the request. The service name can be found as the first segment of the ser...
Definition: sigv4.h:432
const char * pRegion
The target AWS region for the request. Please see https://docs.aws.amazon.com/general/latest/gr/rande...
Definition: sigv4.h:422
SigV4Credentials_t * pCredentials
The AccessKeyId, SecretAccessKey, and SecurityToken used to generate the Authorization header.
Definition: sigv4.h:401
size_t algorithmLen
Length of pAlgorithm.
Definition: sigv4.h:415
const char * pDateIso8601
The date in ISO 8601 format, e.g. "20150830T123600Z". This is always 16 characters long.
Definition: sigv4.h:407
SigV4HttpParameters_t * pHttpParameters
HTTP specific SigV4 parameters for canonical request calculation.
Definition: sigv4.h:443
SigV4CryptoInterface_t * pCryptoInterface
The cryptography interface.
Definition: sigv4.h:438
const char * pAlgorithm
The algorithm used for SigV4 authentication. If set to NULL, this will automatically be set to "AWS4-...
Definition: sigv4.h:413
size_t regionLen
Length of pRegion.
Definition: sigv4.h:423
size_t serviceLen
Length of pService.
Definition: sigv4.h:433