Skip to content

Commit 5aa7013

Browse files
authored
Demo fleet provisioning with csr - support option to write key and certificate to disk (#1897)
* save private key and CSR generated during provisioning * write private key to GENERATED_PRIVATE_KEY_WRITE_PATH * update corePKCS11 submodule * updated manifest.yml for submodule corePKCS11
1 parent 2d5c869 commit 5aa7013

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

demos/fleet_provisioning/fleet_provisioning_keys_cert/fleet_provisioning_keys_cert_demo.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@
5757
/* POSIX includes. */
5858
#include <unistd.h>
5959
#include <errno.h>
60-
#include <fcntl.h>
60+
61+
#if defined( DOWNLOADED_CERT_WRITE_PATH ) || defined( DOWNLOADED_PRIVATE_KEY_WRITE_PATH )
62+
#include <fcntl.h>
63+
#endif
6164

6265
/* Demo config. */
6366
#include "demo_config.h"
@@ -125,7 +128,7 @@
125128
#define DELAY_BETWEEN_DEMO_RETRY_ITERATIONS_SECONDS ( 5 )
126129

127130
/**
128-
* @brief Size of buffer in which to hold the certificate signing request (CSR).
131+
* @brief Size of buffer in which to hold the private key.
129132
*/
130133
#define PRIV_KEY_BUFFER_LENGTH 2048
131134

@@ -605,7 +608,7 @@ int main( int argc,
605608

606609
if( status == true )
607610
{
608-
/* Save the certificate into PKCS #11. */
611+
/* Save the private key into PKCS #11. */
609612
status = loadPrivateKey( p11Session,
610613
privatekey,
611614
pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS,

demos/fleet_provisioning/fleet_provisioning_with_csr/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ target_include_directories( ${DEMO_NAME}
5656
"${CORE_PKCS11_3RDPARTY_LOCATION}/mbedtls_utils" )
5757

5858
set_macro_definitions(TARGETS ${DEMO_NAME}
59+
OPTIONAL
60+
"DOWNLOADED_CERT_WRITE_PATH"
61+
"GENERATED_PRIVATE_KEY_WRITE_PATH"
5962
REQUIRED
6063
"AWS_IOT_ENDPOINT"
6164
"ROOT_CA_CERT_PATH"

demos/fleet_provisioning/fleet_provisioning_with_csr/fleet_provisioning_with_csr_demo.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
#include <unistd.h>
5959
#include <errno.h>
6060

61+
#if defined( DOWNLOADED_CERT_WRITE_PATH )
62+
#include <fcntl.h>
63+
#endif // DOWNLOADED_CERT_WRITE_PATH
64+
6165
/* Demo config. */
6266
#include "demo_config.h"
6367

@@ -777,6 +781,34 @@ int main( int argc,
777781
if( status == true )
778782
{
779783
LogInfo( ( "Demo completed successfully." ) );
784+
785+
#if defined( DOWNLOADED_CERT_WRITE_PATH )
786+
{
787+
int fd = open( DOWNLOADED_CERT_WRITE_PATH, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR );
788+
789+
if( -1 != fd )
790+
{
791+
const ssize_t writtenBytes = write( fd, certificate, certificateLength );
792+
793+
if( writtenBytes == certificateLength )
794+
{
795+
LogInfo( ( "Written %s successfully.", DOWNLOADED_CERT_WRITE_PATH ) );
796+
}
797+
else
798+
{
799+
LogError( ( "Could not write to %s. Error: %s.", DOWNLOADED_CERT_WRITE_PATH, strerror( errno ) ) );
800+
}
801+
802+
close( fd );
803+
}
804+
else
805+
{
806+
LogError( ( "Could not open %s. Error: %s.", DOWNLOADED_CERT_WRITE_PATH, strerror( errno ) ) );
807+
}
808+
}
809+
#else /* if defined( DOWNLOADED_CERT_WRITE_PATH ) */
810+
LogInfo( ( "NOTE: define DOWNLOADED_CERT_WRITE_PATH in order to have the certificate written to disk." ) );
811+
#endif // DOWNLOADED_CERT_WRITE_PATH
780812
}
781813

782814
return ( status == true ) ? EXIT_SUCCESS : EXIT_FAILURE;

libraries/standard/corePKCS11

Submodule corePKCS11 updated 51 files

manifest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ dependencies:
5656
url: "https://github.com/FreeRTOS/backoffAlgorithm"
5757
path: "libraries/standard/backoffAlgorithm"
5858
- name: "corePKCS11"
59-
version: "db05642c6c1f97055128f17c7962d7daf725d3c5"
59+
version: "c671c11f2de13c31e8eb9563858fb513b4c5b678"
6060
repository:
6161
type: "git"
6262
url: "https://github.com/FreeRTOS/corePKCS11"

0 commit comments

Comments
 (0)