|
| 1 | +/* |
| 2 | + * AWS IoT Device SDK for Embedded C 202211.00 |
| 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 | + |
| 23 | +#ifndef HTTP_DEMO_S3_UTILS_H_ |
| 24 | +#define HTTP_DEMO_S3_UTILS_H_ |
| 25 | + |
| 26 | +/* Standard includes. */ |
| 27 | +#include <stdlib.h> |
| 28 | +#include <stdbool.h> |
| 29 | + |
| 30 | +/* *INDENT-OFF* */ |
| 31 | +#ifdef __cplusplus |
| 32 | + extern "C" { |
| 33 | +#endif |
| 34 | +/* *INDENT-ON* */ |
| 35 | + |
| 36 | +/* Other HTTP utils header. */ |
| 37 | +#include "http_demo_utils.h" |
| 38 | + |
| 39 | +/* JSON API header. */ |
| 40 | +#include "core_json.h" |
| 41 | + |
| 42 | +/* SIGV4 API header. */ |
| 43 | +#include "sigv4.h" |
| 44 | + |
| 45 | +/** |
| 46 | + * @brief Length in bytes of hex encoded hash digest. |
| 47 | + */ |
| 48 | +#define HEX_ENCODED_SHA256_HASH_DIGEST_LENGTH ( ( ( uint16_t ) 64 ) ) |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief Length in bytes of SHA256 hash digest. |
| 52 | + */ |
| 53 | +#define SHA256_HASH_DIGEST_LENGTH ( HEX_ENCODED_SHA256_HASH_DIGEST_LENGTH / 2 ) |
| 54 | + |
| 55 | +/** |
| 56 | + * @brief Maximum Length for AWS IOT Credential provider server host name. |
| 57 | + * |
| 58 | + * @note length of the AWS IOT Credential provider server host name string |
| 59 | + * cannot exceed this value. |
| 60 | + */ |
| 61 | +#define SERVER_HOST_NAME_MAX_LENGTH 65U |
| 62 | + |
| 63 | +/** |
| 64 | + * @brief The host address string extracted from the AWS IOT CREDENTIAL PROVIDER URL. |
| 65 | + * |
| 66 | + * @note SERVER_HOST_NAME_MAX_LENGTH is set as the array length here as the |
| 67 | + * length of the host name string cannot exceed this value. |
| 68 | + */ |
| 69 | +extern char serverHost[ SERVER_HOST_NAME_MAX_LENGTH ]; |
| 70 | + |
| 71 | +/** |
| 72 | + * @brief The length of the host address found in the the AWS IOT CREDENTIAL PROVIDER URL. |
| 73 | + */ |
| 74 | +extern size_t serverHostLength; |
| 75 | + |
| 76 | +/** |
| 77 | + * @brief The security token retrieved from AWS IoT credential provider |
| 78 | + * required for making HTTP requests to AWS S3. |
| 79 | + */ |
| 80 | +extern const char * pSecurityToken; |
| 81 | + |
| 82 | +/** |
| 83 | + * @brief Length of security token retrieved from AWS IoT credential provider |
| 84 | + * required for making HTTP requests to AWS S3. |
| 85 | + */ |
| 86 | +extern size_t securityTokenLen; |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief The expiration time for the temporary credentials retrieved |
| 90 | + * from AWS IoT credential provider service. |
| 91 | + */ |
| 92 | +extern const char * pExpiration; |
| 93 | + |
| 94 | +/** |
| 95 | + * @brief Length of expiration time for the temporary credentials retrieved |
| 96 | + * from AWS IoT credential provider service. |
| 97 | + */ |
| 98 | +size_t expirationLen; |
| 99 | + |
| 100 | +/** |
| 101 | + * @brief Retrieve the temporary credentials from AWS IOT Credential Provider. |
| 102 | + * |
| 103 | + * @param[in] pTransportInterface The transport interface for performing network send/recv operations. |
| 104 | + * @param[out] pDateISO8601 Buffer to store the ISO8601 formatted date. |
| 105 | + * @param[in] pDateISO8601Len Length of the buffer provided to store ISO8601 formatted date. |
| 106 | + * @param[in,out] response Response buffer to store the HTTP response received. |
| 107 | + * @param[out] sigvCreds Buffer to store the parsed credentials. |
| 108 | + * |
| 109 | + * @return `true` if credentials are retrieved successfully otherwise 'false`. |
| 110 | + */ |
| 111 | +bool getTemporaryCredentials( TransportInterface_t * transportInterface, |
| 112 | + char * pDateISO8601, |
| 113 | + size_t pDateISO8601Len, |
| 114 | + HTTPResponse_t * response, |
| 115 | + SigV4Credentials_t * sigvCreds ); |
| 116 | + |
| 117 | +/** |
| 118 | + * @brief Calculate SHA256 digest. |
| 119 | + * |
| 120 | + * @param[in] pInput Input string to hash. |
| 121 | + * @param[in] ilen Length of input string. |
| 122 | + * @param[out] pOutput Buffer to store the generated hash. |
| 123 | + */ |
| 124 | +int32_t sha256( const char * pInput, |
| 125 | + size_t ilen, |
| 126 | + char * pOutput ); |
| 127 | + |
| 128 | +/** |
| 129 | + * @brief Application-defined Hash Initialization function provided |
| 130 | + * to the SigV4 library. |
| 131 | + * |
| 132 | + * @note Refer to SigV4CryptoInterface_t interface documentation for this function. |
| 133 | + */ |
| 134 | +int32_t sha256Init( void * hashContext ); |
| 135 | + |
| 136 | +/** |
| 137 | + * @brief Application-defined Hash Update function provided to the SigV4 library. |
| 138 | + * |
| 139 | + * @note Refer to SigV4CryptoInterface_t interface documentation for this function. |
| 140 | + */ |
| 141 | +int32_t sha256Update( void * hashContext, |
| 142 | + const uint8_t * pInput, |
| 143 | + size_t inputLen ); |
| 144 | + |
| 145 | +/** |
| 146 | + * @brief Application-defined Hash Final function provided to the SigV4 library. |
| 147 | + * |
| 148 | + * @note Refer to SigV4CryptoInterface_t interface documentation for this function. |
| 149 | + */ |
| 150 | +int32_t sha256Final( void * hashContext, |
| 151 | + uint8_t * pOutput, |
| 152 | + size_t outputLen ); |
| 153 | + |
| 154 | +/** |
| 155 | + * @brief Connect to AWS IOT Credential Provider server with reconnection retries. |
| 156 | + * |
| 157 | + * @param[out] pNetworkContext The output parameter to return the created |
| 158 | + * network context. |
| 159 | + * |
| 160 | + * @return EXIT_FAILURE on failure; EXIT_SUCCESS on successful connection. |
| 161 | + */ |
| 162 | +int32_t connectToIotServer( NetworkContext_t * pNetworkContext ); |
| 163 | + |
| 164 | +/* *INDENT-OFF* */ |
| 165 | +#ifdef __cplusplus |
| 166 | + } |
| 167 | +#endif |
| 168 | +/* *INDENT-ON* */ |
| 169 | + |
| 170 | +#endif /* ifndef HTTP_DEMO_S3_UTILS_H_ */ |
0 commit comments