AWS IoT Device SDK C: Fleet Provisioning
AWS IoT Fleet Provisioning
Return to main page ↑
aws_iot_provisioning_internal.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy of
5  * this software and associated documentation files (the "Software"), to deal in
6  * the Software without restriction, including without limitation the rights to
7  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8  * the Software, and to permit persons to whom the Software is furnished to do so,
9  * subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  */
21 
28 #ifndef AWS_IOT_PROVISIONING_INTERNAL_H_
29 #define AWS_IOT_PROVISIONING_INTERNAL_H_
30 
31 /* The config header is always included first. */
32 #include "iot_config.h"
33 
34 /* AWS IoT common header include. */
35 #include "aws_iot.h"
36 
37 /* Provisioning include. */
38 #include "aws_iot_provisioning.h"
39 
40 /* Serializer include. */
41 #include "iot_serializer.h"
42 
52 #if AWS_IOT_PROVISIONING_ENABLE_ASSERTS == 1
53  #ifndef AwsIotProvisioning_Assert
54  #ifdef Iot_DefaultAssert
55  #define AwsIotProvisioning_Assert( expression ) Iot_DefaultAssert( expression )
56  #else
57  #error "Asserts are enabled for MQTT, but AwsIotProvisioning_Assert is not defined"
58  #endif
59  #endif
60 #else
61  #define AwsIotProvisioning_Assert( expression )
62 #endif
63 
64 /* Configure logs for PROVISIONING functions. */
65 #ifdef AWS_IOT_LOG_LEVEL_PROVISIONING
66  #define LIBRARY_LOG_LEVEL AWS_IOT_LOG_LEVEL_PROVISIONING
67 #else
68  #ifdef IOT_LOG_LEVEL_GLOBAL
69  #define LIBRARY_LOG_LEVEL IOT_LOG_LEVEL_GLOBAL
70  #else
71  #define LIBRARY_LOG_LEVEL IOT_LOG_NONE
72  #endif
73 #endif
74 
75 #define LIBRARY_LOG_NAME ( "Provisioning" )
76 #include "iot_logging_setup.h"
77 
82 #define CREATE_KEYS_AND_CERTIFICATE_OPERATION_LOG "CREATE KEYS AND CERTIFICATE"
83 #define CREATE_CERT_FROM_CSR_OPERATION_LOG "CREATE CERTIFICATE FROM CSR"
84 #define REGISTER_THING_OPERATION_LOG "REGISTER DEVICE"
85 
94 #ifndef AWS_IOT_PROVISIONING_FORMAT
95  #define AWS_IOT_PROVISIONING_FORMAT AWS_IOT_PROVISIONING_FORMAT_CBOR
96 #endif
97 
98 /* In current release, JSON format is not supported. */
99 #if AWS_IOT_PROVISIONING_FORMAT == AWS_IOT_PROVISIONING_FORMAT_JSON
100  #error "AWS_IOT_PROVISIONING_FORMAT_JSON is not supported."
101 #endif
102 
106 #if AWS_IOT_PROVISIONING_FORMAT == AWS_IOT_PROVISIONING_FORMAT_CBOR
107  #define PROVISIONING_FORMAT "cbor"
108 #else /* if AWS_IOT_PROVISIONING_FORMAT == AWS_IOT_PROVISIONING_FORMAT_CBOR */
109  #error "AWS_IOT_PROVISIONING_FORMAT must be AWS_IOT_PROVISIONING_FORMAT_CBOR."
110 #endif /* if AWS_IOT_PROVISIONING_FORMAT == AWS_IOT_PROVISIONING_FORMAT_CBOR */
111 
112 /*
113  * Provide default values for undefined memory allocation functions based on
114  * the usage of dynamic memory allocation.
115  */
116 #if IOT_STATIC_MEMORY_ONLY == 1
117  #include "iot_static_memory.h"
118 
124  #define AwsIotProvisioning_MallocPayload Iot_MallocMessageBuffer
125 
130  #define AwsIotProvisioning_FreePayload Iot_FreeMessageBuffer
131 
138  #define AwsIotProvisioning_MallocString Iot_MallocMessageBuffer
139 
145  #define AwsIotProvisioning_FreeString Iot_FreeMessageBuffer
146 
147 
153  #define AwsIotProvisioning_MallocDeviceConfigurationList Iot_MallocMessageBuffer
154 
160  #define AwsIotProvisioning_FreeDeviceConfigurationList Iot_FreeMessageBuffer
161 
162 
163 #else /* if IOT_STATIC_MEMORY_ONLY == 1 */
164  #ifndef AwsIotProvisioning_MallocPayload
165  #ifdef Iot_DefaultMalloc
166  #define AwsIotProvisioning_MallocPayload Iot_DefaultMalloc
167  #else
168  #error "No malloc function defined for AwsIotProvisioning_MallocPayload"
169  #endif
170  #endif
171 
172  #ifndef AwsIotProvisioning_FreePayload
173  #ifdef Iot_DefaultFree
174  #define AwsIotProvisioning_FreePayload Iot_DefaultFree
175  #else
176  #error "No Free function defined for AwsIotProvisioning_FreePayload"
177  #endif
178  #endif
179 
180  #ifndef AwsIotProvisioning_MallocString
181  #ifdef Iot_DefaultMalloc
182  #define AwsIotProvisioning_MallocString Iot_DefaultMalloc
183  #else
184  #error "No malloc function defined for AwsIotProvisioning_MallocString"
185  #endif
186  #endif
187 
188  #ifndef AwsIotProvisioning_FreeString
189  #ifdef Iot_DefaultFree
190  #define AwsIotProvisioning_FreeString Iot_DefaultFree
191  #else
192  #error "No Free function defined for AwsIotProvisioning_FreeString"
193  #endif
194  #endif
195 
196  #ifndef AwsIotProvisioning_MallocDeviceConfigurationList
197  #ifdef Iot_DefaultMalloc
198  #define AwsIotProvisioning_MallocDeviceConfigurationList Iot_DefaultMalloc
199  #else
200  #error "No malloc function defined for AwsIotProvisioning_MallocDeviceConfigurationList"
201  #endif
202  #endif
203 
204  #ifndef AwsIotProvisioning_FreeDeviceConfigurationList
205  #ifdef Iot_DefaultFree
206  #define AwsIotProvisioning_FreeDeviceConfigurationList Iot_DefaultFree
207  #else
208  #error "No Free function defined for AwsIotProvisioning_FreeDeviceConfigurationList"
209  #endif
210  #endif
211 
212 #endif /* if IOT_STATIC_MEMORY_ONLY == 1 */
213 
220 #ifndef AWS_IOT_PROVISIONING_DEFAULT_MQTT_TIMEOUT_MS
221  #define AWS_IOT_PROVISIONING_DEFAULT_MQTT_TIMEOUT_MS ( 5000 )
222 #endif
223 
231 #define PROVISIONING_CREATE_KEYS_AND_CERTIFICATE_RESPONSE_TOPIC_FILTER \
232  "$aws/certificates/create/"PROVISIONING_FORMAT
233 
237 #define PROVISIONING_CREATE_KEYS_AND_CERTIFICATE_RESPONSE_TOPIC_FILTER_LENGTH \
238  ( ( uint16_t ) ( sizeof( PROVISIONING_CREATE_KEYS_AND_CERTIFICATE_RESPONSE_TOPIC_FILTER ) - 1 ) )
239 
244 #define PROVISIONING_CREATE_KEYS_AND_CERTIFICATE_RESPONSE_MAX_TOPIC_LENGTH \
245  ( PROVISIONING_CREATE_KEYS_AND_CERTIFICATE_RESPONSE_TOPIC_FILTER_LENGTH + sizeof( AWS_IOT_REJECTED_SUFFIX ) )
246 
252 #define PROVISIONING_CREATE_KEYS_AND_CERTIFICATE_REQUEST_TOPIC \
253  "$aws/certificates/create/"PROVISIONING_FORMAT
254 
258 #define PROVISIONING_CREATE_KEYS_AND_CERTIFICATE_REQUEST_TOPIC_LENGTH \
259  ( ( uint16_t ) ( sizeof( PROVISIONING_CREATE_KEYS_AND_CERTIFICATE_REQUEST_TOPIC ) - 1 ) )
260 
267 #define PROVISIONING_CREATE_CERT_FROM_CSR_RESPONSE_TOPIC_FILTER \
268  "$aws/certificates/create-from-csr/"PROVISIONING_FORMAT
269 
273 #define PROVISIONING_CREATE_CERT_FROM_CSR_RESPONSE_TOPIC_FILTER_LENGTH \
274  ( ( uint16_t ) ( sizeof( PROVISIONING_CREATE_CERT_FROM_CSR_RESPONSE_TOPIC_FILTER ) - 1 ) )
275 
280 #define PROVISIONING_CREATE_CERT_FROM_CSR_RESPONSE_MAX_TOPIC_LENGTH \
281  ( PROVISIONING_CREATE_CERT_FROM_CSR_RESPONSE_TOPIC_FILTER_LENGTH + sizeof( AWS_IOT_REJECTED_SUFFIX ) )
282 
288 #define PROVISIONING_CREATE_CERT_FROM_CSR_REQUEST_TOPIC \
289  "$aws/certificates/create-from-csr/"PROVISIONING_FORMAT
290 
294 #define PROVISIONING_CREATE_CERT_FROM_CSR_REQUEST_TOPIC_LENGTH \
295  ( ( uint16_t ) ( sizeof( PROVISIONING_CREATE_CERT_FROM_CSR_REQUEST_TOPIC ) - 1 ) )
296 
301 #define PROVISIONING_CREATE_CERT_FROM_CSR_REQUEST_PAYLOAD_PEM_STRING "certificateSigningRequest"
302 
307 #define PROVISIONING_SERVER_RESPONSE_PAYLOAD_CERTIFICATE_PEM_STRING "certificatePem"
308 
313 #define PROVISIONING_SERVER_RESPONSE_PAYLOAD_CERTIFICATE_ID_STRING "certificateId"
314 
319 #define PROVISIONING_CREATE_KEYS_AND_CERTIFICATE_RESPONSE_PAYLOAD_PRIVATE_KEY_STRING "privateKey"
320 
325 #define PROVISIONING_SERVER_RESPONSE_PAYLOAD_CERTIFICATE_TOKEN_KEY_STRING "certificateOwnershipToken"
326 
330 #define PROVISIONING_REGISTER_THING_TOPICS_COMMON_PREFIX \
331  "$aws/provisioning-templates/"
332 
337 #define PROVISIONING_REGISTER_THING_TOPICS_COMMON_PREFIX_LENGTH \
338  ( ( uint16_t ) ( sizeof( PROVISIONING_REGISTER_THING_TOPICS_COMMON_PREFIX ) - 1 ) )
339 
344 #define PROVISIONING_MAX_TEMPLATE_NAME_LENGTH ( 36 )
345 
349 #define PROVISIONING_REGISTER_THING_TOPICS_COMMON_SUFFIX "/provision/"PROVISIONING_FORMAT
350 
354 #define PROVISIONING_REGISTER_THING_TOPICS_COMMON_SUFFIX_LENGTH \
355  ( ( uint16_t ) ( sizeof( PROVISIONING_REGISTER_THING_TOPICS_COMMON_SUFFIX ) - 1 ) )
356 
357 
361 #define PROVISIONING_REGISTER_THING_REQUEST_TOPIC_LENGTH \
362  ( PROVISIONING_REGISTER_THING_TOPICS_COMMON_PREFIX_LENGTH + PROVISIONING_MAX_TEMPLATE_NAME_LENGTH + \
363  PROVISIONING_REGISTER_THING_TOPICS_COMMON_SUFFIX_LENGTH )
364 
372 #define PROVISIONING_REGISTER_THING_REQUEST_PAYLOAD_CERTIFICATE_ID_STRING "certificateId"
373 
380 #define PROVISIONING_REGISTER_THING_REQUEST_PAYLOAD_CERTIFICATE_TOKEN_STRING "certificateOwnershipToken"
381 
389 #define PROVISIONING_REGISTER_THING_REQUEST_PAYLOAD_PARAMETERS_STRING "parameters"
390 
394 #define PROVISIONING_REGISTER_THING_RESPONSE_TOPIC_FILTER_LENGTH \
395  ( PROVISIONING_REGISTER_THING_TOPICS_COMMON_PREFIX_LENGTH + PROVISIONING_MAX_TEMPLATE_NAME_LENGTH + \
396  PROVISIONING_REGISTER_THING_TOPICS_COMMON_SUFFIX_LENGTH )
397 
402 #define PROVISIONING_REGISTER_THING_RESPONSE_MAX_TOPIC_LENGTH \
403  ( PROVISIONING_REGISTER_THING_RESPONSE_TOPIC_FILTER_LENGTH + sizeof( AWS_IOT_REJECTED_SUFFIX ) )
404 
411 #define PROVISIONING_REGISTER_THING_RESPONSE_PAYLOAD_DEVICE_CONFIGURATION_STRING "deviceConfiguration"
412 
419 #define PROVISIONING_REGISTER_THING_RESPONSE_PAYLOAD_THING_NAME_STRING "thingName"
420 
426 #define PROVISIONING_REJECTED_RESPONSE_STATUS_CODE_STRING "statusCode"
427 
433 #define PROVISIONING_REJECTED_RESPONSE_ERROR_CODE_STRING "errorCode"
434 
440 #define PROVISIONING_REJECTED_RESPONSE_ERROR_MESSAGE_STRING "errorMessage"
441 
442 /*---------------------- Provisioning internal data structures ----------------------*/
443 
447 typedef enum _provisioningOperationType
448 {
452 
461 typedef union _provisioningCallbackInfo
462 {
465 
468 
472 
482 typedef AwsIotProvisioningError_t ( * _provisioningServerResponseParser)( AwsIotStatus_t responseType,
483  const uint8_t * responsePayload,
484  size_t responsePayloadLength,
485  const _provisioningCallbackInfo_t * userCallback );
486 
490 typedef struct _provisioningOperationInfo
491 {
496 
500 typedef struct _provisioningOperation
501 {
503  uint32_t semReferenceCount;
513 
514 /*----------------- Declaration of INTERNAL global variables --------------------*/
515 
516 extern uint32_t _AwsIotProvisioningMqttTimeoutMs;
517 extern const IotSerializerEncodeInterface_t * _pAwsIotProvisioningEncoder;
518 extern const IotSerializerDecodeInterface_t * _pAwsIotProvisioningDecoder;
519 
520 /*---------------------- Declaration of Provisioning INTERNAL functions ----------------------*/
521 
533 size_t _AwsIotProvisioning_GenerateRegisterThingTopicFilter( const char * pTemplateName,
534  size_t templateNameLength,
535  char * pTopicFilterBuffer );
536 
549  const uint8_t * pResponsePayload,
550  size_t payloadLength,
551  const _provisioningCallbackInfo_t * userCallbackInfo );
552 
568  const uint8_t * pResponsePayload,
569  size_t payloadLength,
570  const _provisioningCallbackInfo_t * userCallbackInfo );
571 
584  const uint8_t * pResponsePayload,
585  size_t responsePayloadLength,
586  const _provisioningCallbackInfo_t * userCallbackInfo );
587 
599  size_t * pBufferSize );
600 
617  size_t csrLength,
618  size_t * pPayloadSize );
619 
632  size_t csrLength,
633  uint8_t * pSerializationBuffer,
634  size_t pBufferSize );
635 
650  uint8_t ** pSerializationBuffer,
651  size_t * pBufferSize );
652 
653 #endif /* ifndef AWS_IOT_PROVISIONING_INTERNAL_H_ */
AwsIotProvisioningError_t(* _provisioningServerResponseParser)(AwsIotStatus_t responseType, const uint8_t *responsePayload, size_t responsePayloadLength, const _provisioningCallbackInfo_t *userCallback)
Functor for parsing response payload received from AWS IoT Core. Parser that will de-serialize the se...
Definition: aws_iot_provisioning_internal.h:482
AwsIotProvisioningError_t _AwsIotProvisioning_ParseKeysAndCertificateResponse(AwsIotStatus_t responseType, const uint8_t *pResponsePayload, size_t payloadLength, const _provisioningCallbackInfo_t *userCallbackInfo)
Parses the response received from the server for device credentials, and invokes the provided user-ca...
Definition: aws_iot_provisioning_parser.c:312
AwsIotProvisioningError_t _AwsIotProvisioning_SerializeCreateCertFromCsrRequestPayload(const char *pCertificateSigningRequest, size_t csrLength, uint8_t *pSerializationBuffer, size_t pBufferSize)
Serializes payload data for the request to the MQTT CreateCertificateFromCsr service API...
Definition: aws_iot_provisioning_serializer.c:527
AwsIotProvisioningError_t
Return codes of Provisioning functions.
Definition: aws_iot_provisioning_types.h:54
AwsIotProvisioningRegisterThingCallbackInfo_t registerThingCallback
The user-callback passed to AwsIotProvisioning_RegisterThing.
Definition: aws_iot_provisioning_internal.h:470
_provisioningOperationType_t
Enumerations representing each of the Provisioning library's API functions.
Definition: aws_iot_provisioning_internal.h:447
AwsIotProvisioningError_t _AwsIotProvisioning_ParseRegisterThingResponse(AwsIotStatus_t responseType, const uint8_t *pResponsePayload, size_t responsePayloadLength, const _provisioningCallbackInfo_t *userCallbackInfo)
Parses the response payload received from the server for device provisioning, and invokes the provide...
Definition: aws_iot_provisioning_parser.c:554
const IotSerializerDecodeInterface_t * _pAwsIotProvisioningDecoder
Pointer to the decoder utility that will be used for de-serialization of payload data in the library...
Definition: aws_iot_provisioning_api.c:72
uint32_t _AwsIotProvisioningMqttTimeoutMs
Timeout for MQTT operations that will be used for communicating with the fleet provisioning APIs of t...
Definition: aws_iot_provisioning_api.c:105
User-specific callback information for handling server response for the Provisioning CreateKeysAndCer...
Definition: aws_iot_provisioning_types.h:424
Union representing either of the 2 Provisioning operation APIs' callbacks.
Definition: aws_iot_provisioning_internal.h:461
const IotSerializerEncodeInterface_t * _pAwsIotProvisioningEncoder
Pointer to the encoder utility that will be used for serialization of payload data in the library...
Definition: aws_iot_provisioning_api.c:66
User-specific callback information for handling server response for the Provisioning RegisterThing se...
Definition: aws_iot_provisioning_types.h:498
AwsIotProvisioningError_t _AwsIotProvisioning_SerializeCreateKeysAndCertificateRequestPayload(uint8_t **pSerializationBuffer, size_t *pBufferSize)
Serializes payload data for MQTT request to the Fleet Provisioning CreateKeysAndCertificate API on AW...
Definition: aws_iot_provisioning_serializer.c:359
AwsIotProvisioningError_t _AwsIotProvisioning_CalculateCertFromCsrPayloadSize(const char *pCertificateSigningRequest, size_t csrLength, size_t *pPayloadSize)
Calculates the payload size of serializing the passed Certificate-Signing Request data for the MQTT C...
Definition: aws_iot_provisioning_serializer.c:480
_provisioningCallbackInfo_t userCallback
User-provided callback to be called on receiving a response from the server.
Definition: aws_iot_provisioning_internal.h:493
IotSemaphore_t responseReceivedSem
Binary sempahore used for notifying arrival of server response in the synchronous API functions AwsIo...
Definition: aws_iot_provisioning_internal.h:506
AwsIotProvisioningError_t _AwsIotProvisioning_SerializeRegisterThingRequestPayload(const AwsIotProvisioningRegisterThingRequestInfo_t *pRequestData, uint8_t **pSerializationBuffer, size_t *pBufferSize)
Serializes payload data for MQTT request to the Provisioning RegisterThing service API...
Definition: aws_iot_provisioning_serializer.c:567
Definition: aws_iot_provisioning_internal.h:449
Internal structure representing a single Provisioning operation.
Definition: aws_iot_provisioning_internal.h:500
Definition: aws_iot_provisioning_internal.h:450
_provisioningOperationInfo_t info
The Provisioning operation object.
Definition: aws_iot_provisioning_internal.h:502
User-facing functions of the Provisioning library.
_IotSystemSemaphore_t IotSemaphore_t
size_t _AwsIotProvisioning_GenerateRegisterThingTopicFilter(const char *pTemplateName, size_t templateNameLength, char *pTopicFilterBuffer)
Utility for generating the request/response MQTT topic filter string for the ProvisioningDevice servi...
Definition: aws_iot_provisioning_serializer.c:606
uint32_t semReferenceCount
An atomic reference counter for safeguarding semaphore access across thread contexts.
Definition: aws_iot_provisioning_internal.h:503
AwsIotProvisioningError_t status
Status of operation.
Definition: aws_iot_provisioning_internal.h:492
AwsIotProvisioningCreateCertFromCsrCallbackInfo_t createCertFromCsrCallback
The user-callback passed to AwsIotProvisioning_CreateCertificateFromCsr.
Definition: aws_iot_provisioning_internal.h:467
Internal structure representing the data of an Provisioning operation.
Definition: aws_iot_provisioning_internal.h:490
AwsIotProvisioningError_t _AwsIotProvisioning_ParseCsrResponse(AwsIotStatus_t responseType, const uint8_t *pResponsePayload, size_t payloadLength, const _provisioningCallbackInfo_t *userCallbackInfo)
Parses the response from the server received on a Certificate-Signing Request, and invokes the provid...
Definition: aws_iot_provisioning_parser.c:441
AwsIotProvisioningCreateKeysAndCertificateCallbackInfo_t createKeysAndCertificateCallback
The user-callback passed to AwsIotProvisioning_CreateKeysAndCertificate.
Definition: aws_iot_provisioning_internal.h:464
Aggregates information required for sending a request to the AWS IoT Core service for provisioning a ...
Definition: aws_iot_provisioning_types.h:238
User-specific callback information for handling server response of the Provisioning CreateCertificate...
Definition: aws_iot_provisioning_types.h:348