SigV4 v1.1.0
SigV4 Library for AWS Authentication
sigv4.h
Go to the documentation of this file.
1/*
2 * SigV4 Library v1.1.0
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
28#ifndef SIGV4_H_
29#define SIGV4_H_
30
31/* Standard includes. */
32#include <stdint.h>
33#include <stddef.h>
34
35/* *INDENT-OFF* */
36#ifdef __cplusplus
37 extern "C" {
38#endif
39/* *INDENT-ON* */
40
41/* SIGV4_DO_NOT_USE_CUSTOM_CONFIG allows building of the SigV4 library without a
42 * config file. If a config file is provided, the SIGV4_DO_NOT_USE_CUSTOM_CONFIG
43 * macro must not be defined.
44 */
45#ifndef SIGV4_DO_NOT_USE_CUSTOM_CONFIG
46 #include "sigv4_config.h"
47#endif
48
49/* Include config defaults header to get default values of configurations not
50 * defined in sigv4_config.h file. */
52
53/* Convenience macros for library optimization */
54
58#define SIGV4_AWS4_HMAC_SHA256 "AWS4-HMAC-SHA256"
59#define SIGV4_AWS4_HMAC_SHA256_LENGTH ( sizeof( SIGV4_AWS4_HMAC_SHA256 ) - 1U )
60#define SIGV4_HTTP_X_AMZ_DATE_HEADER "x-amz-date"
61#define SIGV4_HTTP_X_AMZ_SECURITY_TOKEN_HEADER "x-amz-security-token"
63#define SIGV4_STREAMING_AWS4_HMAC_SHA256_PAYLOAD "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"
64#define SIGV4_HTTP_X_AMZ_CONTENT_SHA256_HEADER "x-amz-content-sha256"
65#define SIGV4_HTTP_X_AMZ_CONTENT_SHA256_HEADER_LENGTH ( sizeof( SIGV4_HTTP_X_AMZ_CONTENT_SHA256_HEADER ) - 1U )
66#define SIGV4_HTTP_X_AMZ_STORAGE_CLASS_HEADER "x-amz-storage-class"
68#define SIGV4_ACCESS_KEY_ID_LENGTH 20U
69#define SIGV4_SECRET_ACCESS_KEY_LENGTH 40U
71#define SIGV4_ISO_STRING_LEN 16U
72#define SIGV4_EXPECTED_LEN_RFC_3339 20U
73#define SIGV4_EXPECTED_LEN_RFC_5322 29U
94#define SIGV4_HTTP_PATH_IS_CANONICAL_FLAG 0x1U
95
103#define SIGV4_HTTP_QUERY_IS_CANONICAL_FLAG 0x2U
104
112#define SIGV4_HTTP_HEADERS_ARE_CANONICAL_FLAG 0x4U
113
121#define SIGV4_HTTP_PAYLOAD_IS_HASH 0x8U
122
130#define SIGV4_HTTP_ALL_ARE_CANONICAL_FLAG 0x7U
131
136typedef enum SigV4Status
137{
146
156
165
173
185
197
205
214
220typedef struct SigV4CryptoInterface
221{
230 int32_t ( * hashInit )( void * pHashContext );
231
242 int32_t ( * hashUpdate )( void * pHashContext,
243 const uint8_t * pInput,
244 size_t inputLen );
245
259 int32_t ( * hashFinal )( void * pHashContext,
260 uint8_t * pOutput,
261 size_t outputLen );
262
267
272
278
284typedef struct SigV4HttpParameters
285{
286 const char * pHttpMethod;
300 uint32_t flags;
301
312 const char * pPath;
313 size_t pathLen;
324 const char * pQuery;
325 size_t queryLen;
336 const char * pHeaders;
337 size_t headersLen;
344 const char * pPayload;
345 size_t payloadLen;
347
353typedef struct SigV4Credentials
354{
359 const char * pAccessKeyId;
365 const char * pSecretAccessKey;
368
378typedef struct SigV4Parameters
379{
385
390 const char * pDateIso8601;
391
396 const char * pAlgorithm;
397
405 const char * pRegion;
406 size_t regionLen;
415 const char * pService;
416 size_t serviceLen;
422
428
485/* @[declare_sigV4_generateHTTPAuthorization_function] */
487 char * pAuthBuf,
488 size_t * authBufLen,
489 char ** pSignature,
490 size_t * signatureLen );
491/* @[declare_sigV4_generateHTTPAuthorization_function] */
492
556/* @[declare_sigV4_awsIotDateToIso8601_function] */
557SigV4Status_t SigV4_AwsIotDateToIso8601( const char * pDate,
558 size_t dateLen,
559 char * pDateISO8601,
560 size_t dateISO8601Len );
561/* @[declare_sigV4_awsIotDateToIso8601_function] */
562
563/* *INDENT-OFF* */
564#ifdef __cplusplus
565 }
566#endif
567/* *INDENT-ON* */
568
569#endif /* SIGV4_H_ */
SigV4Status_t
Return status of the SigV4 Library.
Definition: sigv4.h:137
@ SigV4Success
The SigV4 library function completed successfully.
Definition: sigv4.h:145
@ SigV4InsufficientMemory
The application buffer was not large enough for the specified hash function.
Definition: sigv4.h:164
@ SigV4HashError
An error occurred while performing a hash operation.
Definition: sigv4.h:204
@ SigV4ISOFormattingError
An error occurred while formatting the provided date header.
Definition: sigv4.h:172
@ SigV4MaxQueryPairCountExceeded
The maximum number of query parameters was exceeded while parsing the query string passed to the libr...
Definition: sigv4.h:196
@ SigV4MaxHeaderPairCountExceeded
The maximum number of header parameters was exceeded while parsing the http header string passed to t...
Definition: sigv4.h:184
@ SigV4InvalidHttpHeaders
HTTP headers parsed to the library are invalid.
Definition: sigv4.h:212
@ SigV4InvalidParameter
The SigV4 library function received an invalid input parameter.
Definition: sigv4.h:155
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:3181
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:3105
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:354
size_t secretAccessKeyLen
Length of pSecretAccessKey.
Definition: sigv4.h:366
const char * pSecretAccessKey
The pSecretAccessKey MUST be at least 40 characters long.
Definition: sigv4.h:365
const char * pAccessKeyId
The pAccessKeyId MUST be at least 16 characters long but not more than 128 characters long.
Definition: sigv4.h:359
size_t accessKeyIdLen
Length of pAccessKeyId.
Definition: sigv4.h:360
The cryptography interface used to supply the user-defined hash implementation.
Definition: sigv4.h:221
size_t hashBlockLen
The block length of the hash function.
Definition: sigv4.h:271
size_t hashDigestLen
The digest length of the hash function.
Definition: sigv4.h:276
void * pHashContext
Context for the hashInit, hashUpdate, and hashFinal interfaces.
Definition: sigv4.h:266
Configurations of the HTTP request used to create the Canonical Request.
Definition: sigv4.h:285
const char * pPath
The path in the HTTP request. This is the absolute request URI, which contains everything in the URI ...
Definition: sigv4.h:312
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:336
size_t queryLen
Length of pQuery.
Definition: sigv4.h:325
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:344
uint32_t flags
These flags are used to indicate if the path, query, or headers are already in the canonical form....
Definition: sigv4.h:300
size_t pathLen
Length of pPath.
Definition: sigv4.h:313
const char * pQuery
The HTTP request query from the URL, if it exists. This contains all characters following the questio...
Definition: sigv4.h:324
size_t headersLen
Length of pHeaders.
Definition: sigv4.h:337
size_t payloadLen
Length of pPayload.
Definition: sigv4.h:345
const char * pHttpMethod
The HTTP method: GET, POST, PUT, etc.
Definition: sigv4.h:286
size_t httpMethodLen
Length of pHttpMethod.
Definition: sigv4.h:287
Complete configurations required for generating "String to Sign" and "Signing Key" values.
Definition: sigv4.h:379
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:415
const char * pRegion
The target AWS region for the request. Please see https://docs.aws.amazon.com/general/latest/gr/rande...
Definition: sigv4.h:405
SigV4Credentials_t * pCredentials
The AccessKeyId, SecretAccessKey, and SecurityToken used to generate the Authorization header.
Definition: sigv4.h:384
size_t algorithmLen
Length of pAlgorithm.
Definition: sigv4.h:398
const char * pDateIso8601
The date in ISO 8601 format, e.g. "20150830T123600Z". This is always 16 characters long.
Definition: sigv4.h:390
SigV4HttpParameters_t * pHttpParameters
HTTP specific SigV4 parameters for canonical request calculation.
Definition: sigv4.h:426
SigV4CryptoInterface_t * pCryptoInterface
The cryptography interface.
Definition: sigv4.h:421
const char * pAlgorithm
The algorithm used for SigV4 authentication. If set to NULL, this will automatically be set to "AWS4-...
Definition: sigv4.h:396
size_t regionLen
Length of pRegion.
Definition: sigv4.h:406
size_t serviceLen
Length of pService.
Definition: sigv4.h:416