From df2b42f33dd72da8832ef41f1c88350eacd909b3 Mon Sep 17 00:00:00 2001 From: Paul Bartell Date: Tue, 1 Nov 2022 18:18:08 -0700 Subject: [PATCH] Update Fleet Provisioning WinSim Demo build files --- .../FleetProvisioningDemoExample.c | 7 - .../fleet_provisioning_demo.sln | 79 ++++- .../fleet_provisioning_demo.vcxproj | 235 ++++++++++++++ .../fleet_provisioning_demo.vcxproj.filters | 131 ++++++++ .../pkcs11_operations.c | 307 +----------------- 5 files changed, 444 insertions(+), 315 deletions(-) rename FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/{DemoTasks => }/FleetProvisioningDemoExample.c (98%) create mode 100644 FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.vcxproj create mode 100644 FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.vcxproj.filters diff --git a/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/DemoTasks/FleetProvisioningDemoExample.c b/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/FleetProvisioningDemoExample.c similarity index 98% rename from FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/DemoTasks/FleetProvisioningDemoExample.c rename to FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/FleetProvisioningDemoExample.c index 87fb6df0da..e10906f71b 100644 --- a/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/DemoTasks/FleetProvisioningDemoExample.c +++ b/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/FleetProvisioningDemoExample.c @@ -81,7 +81,6 @@ #include "mqtt_pkcs11_demo_helpers.h" #include "pkcs11_operations.h" #include "tinycbor_serializer.h" -#include "using_mbedtls_pkcs11.h" /** * These configurations are required. Throw compilation error if it is not @@ -520,12 +519,6 @@ static bool prvUnsubscribeFromRegisterThingResponseTopics( void ) */ void vStartFleetProvisioningDemo() { - /* Configure mbedTLS to use FreeRTOS specific threading function. */ - mbedtls_threading_set_alt( mbedtls_platform_mutex_init, - mbedtls_platform_mutex_free, - mbedtls_platform_mutex_lock, - mbedtls_platform_mutex_unlock ); - /* This example uses a single application task, which shows that how to use * Fleet Provisioning library to generate and sign certificates with AWS IoT * and create new IoT Things using the AWS IoT Fleet Provisioning API */ diff --git a/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.sln b/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.sln index 49642528ef..be0cf9a4c2 100644 --- a/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.sln +++ b/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.sln @@ -1,25 +1,84 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31729.503 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32819.101 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RTOSDemo", "WIN32.vcxproj", "{C686325E-3261-42F7-AEB1-DDE5280E1CEB}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FreeRTOS-Kernel", "..\..\..\..\WindowsSimulator\FreeRTOS-Kernel\FreeRTOS-Kernel.vcxproj", "{72C209C4-49A4-4942-A201-44706C9D77EC}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FreeRTOS+TCP", "..\..\..\..\WindowsSimulator\FreeRTOS+TCP\FreeRTOS+TCP.vcxproj", "{C90E6CC5-818B-4C97-8876-0986D989387C}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Fleet Provisioning Demo", "fleet_provisioning_demo.vcxproj", "{382DC80F-E278-4BC9-9DB6-59014486DA0F}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Logging", "..\..\..\..\WindowsSimulator\Logging\Logging.vcxproj", "{BE362AC0-B10B-4276-B84E-6304652BA228}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MbedTLS", "..\..\..\..\WindowsSimulator\MbedTLS\MbedTLS.vcxproj", "{E1016F3E-94E9-4864-9FD8-1D7C1FEFBFD7}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "corePKCS11", "..\..\..\..\WindowsSimulator\corePKCS11\corePKCS11.vcxproj", "{19F0FF1A-3368-491A-9932-A2F089508F51}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x86 = Debug|x86 - Release|x86 = Release|x86 + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Debug|x86.ActiveCfg = Debug|Win32 - {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Debug|x86.Build.0 = Debug|Win32 - {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Release|x86.ActiveCfg = Release|Win32 - {C686325E-3261-42F7-AEB1-DDE5280E1CEB}.Release|x86.Build.0 = Release|Win32 + {72C209C4-49A4-4942-A201-44706C9D77EC}.Debug|Win32.ActiveCfg = Debug|Win32 + {72C209C4-49A4-4942-A201-44706C9D77EC}.Debug|Win32.Build.0 = Debug|Win32 + {72C209C4-49A4-4942-A201-44706C9D77EC}.Debug|x64.ActiveCfg = Debug|x64 + {72C209C4-49A4-4942-A201-44706C9D77EC}.Debug|x64.Build.0 = Debug|x64 + {72C209C4-49A4-4942-A201-44706C9D77EC}.Release|Win32.ActiveCfg = Release|Win32 + {72C209C4-49A4-4942-A201-44706C9D77EC}.Release|Win32.Build.0 = Release|Win32 + {72C209C4-49A4-4942-A201-44706C9D77EC}.Release|x64.ActiveCfg = Release|x64 + {72C209C4-49A4-4942-A201-44706C9D77EC}.Release|x64.Build.0 = Release|x64 + {C90E6CC5-818B-4C97-8876-0986D989387C}.Debug|Win32.ActiveCfg = Debug|Win32 + {C90E6CC5-818B-4C97-8876-0986D989387C}.Debug|Win32.Build.0 = Debug|Win32 + {C90E6CC5-818B-4C97-8876-0986D989387C}.Debug|x64.ActiveCfg = Debug|x64 + {C90E6CC5-818B-4C97-8876-0986D989387C}.Debug|x64.Build.0 = Debug|x64 + {C90E6CC5-818B-4C97-8876-0986D989387C}.Release|Win32.ActiveCfg = Release|Win32 + {C90E6CC5-818B-4C97-8876-0986D989387C}.Release|Win32.Build.0 = Release|Win32 + {C90E6CC5-818B-4C97-8876-0986D989387C}.Release|x64.ActiveCfg = Release|x64 + {C90E6CC5-818B-4C97-8876-0986D989387C}.Release|x64.Build.0 = Release|x64 + {382DC80F-E278-4BC9-9DB6-59014486DA0F}.Debug|Win32.ActiveCfg = Debug|Win32 + {382DC80F-E278-4BC9-9DB6-59014486DA0F}.Debug|Win32.Build.0 = Debug|Win32 + {382DC80F-E278-4BC9-9DB6-59014486DA0F}.Debug|x64.ActiveCfg = Debug|x64 + {382DC80F-E278-4BC9-9DB6-59014486DA0F}.Debug|x64.Build.0 = Debug|x64 + {382DC80F-E278-4BC9-9DB6-59014486DA0F}.Release|Win32.ActiveCfg = Release|Win32 + {382DC80F-E278-4BC9-9DB6-59014486DA0F}.Release|Win32.Build.0 = Release|Win32 + {382DC80F-E278-4BC9-9DB6-59014486DA0F}.Release|x64.ActiveCfg = Release|x64 + {382DC80F-E278-4BC9-9DB6-59014486DA0F}.Release|x64.Build.0 = Release|x64 + {BE362AC0-B10B-4276-B84E-6304652BA228}.Debug|Win32.ActiveCfg = Debug|Win32 + {BE362AC0-B10B-4276-B84E-6304652BA228}.Debug|Win32.Build.0 = Debug|Win32 + {BE362AC0-B10B-4276-B84E-6304652BA228}.Debug|x64.ActiveCfg = Debug|x64 + {BE362AC0-B10B-4276-B84E-6304652BA228}.Debug|x64.Build.0 = Debug|x64 + {BE362AC0-B10B-4276-B84E-6304652BA228}.Release|Win32.ActiveCfg = Release|Win32 + {BE362AC0-B10B-4276-B84E-6304652BA228}.Release|Win32.Build.0 = Release|Win32 + {BE362AC0-B10B-4276-B84E-6304652BA228}.Release|x64.ActiveCfg = Release|x64 + {BE362AC0-B10B-4276-B84E-6304652BA228}.Release|x64.Build.0 = Release|x64 + {E1016F3E-94E9-4864-9FD8-1D7C1FEFBFD7}.Debug|Win32.ActiveCfg = Debug|Win32 + {E1016F3E-94E9-4864-9FD8-1D7C1FEFBFD7}.Debug|Win32.Build.0 = Debug|Win32 + {E1016F3E-94E9-4864-9FD8-1D7C1FEFBFD7}.Debug|x64.ActiveCfg = Debug|x64 + {E1016F3E-94E9-4864-9FD8-1D7C1FEFBFD7}.Debug|x64.Build.0 = Debug|x64 + {E1016F3E-94E9-4864-9FD8-1D7C1FEFBFD7}.Release|Win32.ActiveCfg = Release|Win32 + {E1016F3E-94E9-4864-9FD8-1D7C1FEFBFD7}.Release|Win32.Build.0 = Release|Win32 + {E1016F3E-94E9-4864-9FD8-1D7C1FEFBFD7}.Release|x64.ActiveCfg = Release|x64 + {E1016F3E-94E9-4864-9FD8-1D7C1FEFBFD7}.Release|x64.Build.0 = Release|x64 + {19F0FF1A-3368-491A-9932-A2F089508F51}.Debug|Win32.ActiveCfg = Debug|Win32 + {19F0FF1A-3368-491A-9932-A2F089508F51}.Debug|Win32.Build.0 = Debug|Win32 + {19F0FF1A-3368-491A-9932-A2F089508F51}.Debug|x64.ActiveCfg = Debug|x64 + {19F0FF1A-3368-491A-9932-A2F089508F51}.Debug|x64.Build.0 = Debug|x64 + {19F0FF1A-3368-491A-9932-A2F089508F51}.Release|Win32.ActiveCfg = Release|Win32 + {19F0FF1A-3368-491A-9932-A2F089508F51}.Release|Win32.Build.0 = Release|Win32 + {19F0FF1A-3368-491A-9932-A2F089508F51}.Release|x64.ActiveCfg = Release|x64 + {19F0FF1A-3368-491A-9932-A2F089508F51}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {573E92F2-7674-4EE8-A7FD-8128A495333D} + SolutionGuid = {3876F3F5-8086-4F8E-BB5E-D8840E7D926C} + EndGlobalSection + GlobalSection(TestCaseManagementSettings) = postSolution + CategoryFile = FreeRTOS_Plus_TCP_Minimal.vsmdi EndGlobalSection EndGlobal diff --git a/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.vcxproj b/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.vcxproj new file mode 100644 index 0000000000..a1d11e1f1c --- /dev/null +++ b/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.vcxproj @@ -0,0 +1,235 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {382dc80f-e278-4bc9-9db6-59014486da0f} + corePKCS11_MQTT_Mutual_Auth + 10.0 + Fleet Provisioning Demo + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + $(VC_IncludePath);$(WindowsSDK_IncludePath);.;..\..\..\..\Source\Application-Protocols\network_transport\sockets_wrapper\freertos_plus_tcp;..\..\..\..\Source\Application-Protocols\network_transport;..\..\..\..\Source\Utilities\backoff_algorithm\source\include;..\..\..\..\Source\Application-Protocols\coreMQTT\source\include;..\..\..\..\Source\Application-Protocols\coreMQTT\source\interface;..\..\..\..\ThirdParty\tinycbor\src;..\..\..\..\Source\AWS\fleet-provisioning\source\include;..\..\Mqtt_Demo_Helpers;$(IncludePath) + + + $(VC_IncludePath);$(WindowsSDK_IncludePath);.;..\..\..\..\Source\Application-Protocols\network_transport\sockets_wrapper\freertos_plus_tcp;..\..\..\..\Source\Application-Protocols\network_transport;..\..\..\..\Source\Utilities\backoff_algorithm\source\include;..\..\..\..\Source\Application-Protocols\coreMQTT\source\include;..\..\..\..\Source\Application-Protocols\coreMQTT\source\interface;..\..\..\..\ThirdParty\tinycbor\src;..\..\..\..\Source\AWS\fleet-provisioning\source\include;..\..\Mqtt_Demo_Helpers;$(IncludePath) + + + $(VC_IncludePath);$(WindowsSDK_IncludePath);.;..\..\..\..\Source\Application-Protocols\network_transport\sockets_wrapper\freertos_plus_tcp;..\..\..\..\Source\Application-Protocols\network_transport;..\..\..\..\Source\Utilities\backoff_algorithm\source\include;..\..\..\..\Source\Application-Protocols\coreMQTT\source\include;..\..\..\..\Source\Application-Protocols\coreMQTT\source\interface;..\..\..\..\ThirdParty\tinycbor\src;..\..\..\..\Source\AWS\fleet-provisioning\source\include;..\..\Mqtt_Demo_Helpers;$(IncludePath) + + + $(VC_IncludePath);$(WindowsSDK_IncludePath);.;..\..\..\..\Source\Application-Protocols\network_transport\sockets_wrapper\freertos_plus_tcp;..\..\..\..\Source\Application-Protocols\network_transport;..\..\..\..\Source\Utilities\backoff_algorithm\source\include;..\..\..\..\Source\Application-Protocols\coreMQTT\source\include;..\..\..\..\Source\Application-Protocols\coreMQTT\source\interface;..\..\..\..\ThirdParty\tinycbor\src;..\..\..\..\Source\AWS\fleet-provisioning\source\include;..\..\Mqtt_Demo_Helpers;$(IncludePath) + + + false + + + + Level3 + true + MBEDTLS_CONFIG_FILE="mbedtls_config_v3.2.1.h";_CRT_SECURE_NO_WARNINGS;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + true + + + Console + true + %(AdditionalDependencies) + + + + + Level3 + true + true + true + MBEDTLS_CONFIG_FILE="mbedtls_config_v3.2.1.h";_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + true + + + Console + true + true + true + %(AdditionalDependencies) + + + + + Level3 + true + MBEDTLS_CONFIG_FILE="mbedtls_config_v3.2.1.h";_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + true + + + Console + true + %(AdditionalDependencies) + + + + + Level3 + true + true + true + MBEDTLS_CONFIG_FILE="mbedtls_config_v3.2.1.h";_CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + true + + + Console + true + true + true + %(AdditionalDependencies) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {19f0ff1a-3368-491a-9932-a2f089508f51} + + + {c90e6cc5-818b-4c97-8876-0986d989387c} + false + false + + + {72c209c4-49a4-4942-a201-44706c9d77ec} + false + false + + + {be362ac0-b10b-4276-b84e-6304652ba228} + false + + + {e1016f3e-94e9-4864-9fd8-1d7c1fefbfd7} + + + + + + \ No newline at end of file diff --git a/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.vcxproj.filters b/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.vcxproj.filters new file mode 100644 index 0000000000..35b9a58877 --- /dev/null +++ b/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/fleet_provisioning_demo.vcxproj.filters @@ -0,0 +1,131 @@ + + + + + {a442e1bd-e141-45a4-860d-a8eccab1b8c2} + + + {ec612a95-3f21-4e0a-b9cd-0ba89d62067e} + + + {c69eabd6-7eed-4959-9351-41b8c3b731b8} + + + {64a78119-97f6-4252-b793-87cb6e7902d1} + + + {58f51daa-dd72-4e9c-939d-3113f97234ca} + + + + + network_transport + + + backoff_algorithm + + + network_transport + + + + coreMQTT + + + coreMQTT + + + coreMQTT + + + network_transport + + + network_transport + + + + + + tinycbor + + + tinycbor + + + tinycbor + + + tinycbor + + + tinycbor + + + tinycbor + + + tinycbor + + + tinycbor + + + + + + + network_transport + + + backoff_algorithm + + + + + coreMQTT\include + + + coreMQTT\include + + + coreMQTT\include + + + coreMQTT\include + + + coreMQTT\include + + + network_transport + + + + + + tinycbor + + + tinycbor + + + tinycbor + + + tinycbor + + + tinycbor + + + tinycbor + + + network_transport + + + + + + \ No newline at end of file diff --git a/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/pkcs11_operations.c b/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/pkcs11_operations.c index c397d3aef6..ff141528ad 100644 --- a/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/pkcs11_operations.c +++ b/FreeRTOS-Plus/Demo/AWS/Fleet_Provisioning_Windows_Simulator/Fleet_Provisioning_With_CSR_Demo/pkcs11_operations.c @@ -44,67 +44,16 @@ #include "core_pkcs11_config.h" #include "core_pki_utils.h" #include "mbedtls_utils.h" +#include "mbedtls_pk_pkcs11.h" /* MbedTLS include. */ -#include "mbedtls/ctr_drbg.h" -#include "mbedtls/entropy.h" -#include "mbedtls/entropy_poll.h" #include "mbedtls/error.h" #include "mbedtls/oid.h" #include "mbedtls/pk.h" -#include "mbedtls/pk_internal.h" #include "mbedtls/sha256.h" #include "mbedtls/x509_crt.h" #include "mbedtls/x509_csr.h" -/** - * @brief Represents string to be logged when mbedTLS returned error - * does not contain a high-level code. - */ -static const char * pcNoHighLevelMbedTlsCodeStr = ""; - -/** - * @brief Represents string to be logged when mbedTLS returned error - * does not contain a low-level code. - */ -static const char * pcNoLowLevelMbedTlsCodeStr = ""; - -/** - * @brief Utility for converting the high-level code in an mbedTLS error to - * string, if the code-contains a high-level code; otherwise, using a default - * string. - */ -#define mbedtlsHighLevelCodeOrDefault( mbedTlsCode ) \ - ( mbedtls_high_level_strerr( mbedTlsCode ) != NULL ) \ - ? mbedtls_high_level_strerr( mbedTlsCode ) \ - : pcNoHighLevelMbedTlsCodeStr - -/** - * @brief Utility for converting the level-level code in an mbedTLS error to - * string, if the code-contains a level-level code; otherwise, using a default - * string. - */ -#define mbedtlsLowLevelCodeOrDefault( mbedTlsCode ) \ - ( mbedtls_low_level_strerr( mbedTlsCode ) != NULL ) \ - ? mbedtls_low_level_strerr( mbedTlsCode ) \ - : pcNoLowLevelMbedTlsCodeStr - -/** - * @brief Struct containing parameters needed by the signing callback. - */ -typedef struct SigningCallbackContext -{ - CK_SESSION_HANDLE p11Session; - CK_OBJECT_HANDLE p11PrivateKey; -} SigningCallbackContext_t; - -/** - * @brief Parameters for the signing callback. This needs to be global as - * MbedTLS passes the key context to the signing function, so we cannot pass - * our own. - */ -static SigningCallbackContext_t xSigningContext = { 0 }; - /*-----------------------------------------------------------*/ /** @@ -120,51 +69,6 @@ static CK_RV prvDestroyProvidedObjects( CK_SESSION_HANDLE xSession, CK_OBJECT_CLASS * pxClass, CK_ULONG xCount ); -/** - * @brief Read the specified ECDSA public key into the MbedTLS ECDSA context. - * - * @param[in] xP11Session The PKCS #11 session. - * @param[in] pxEcdsaContext The context in which to store the key. - * @param[in] xPublicKey The public key to read. - */ -static int prvExtractEcPublicKey( CK_SESSION_HANDLE xP11Session, - mbedtls_ecdsa_context * pxEcdsaContext, - CK_OBJECT_HANDLE xPublicKey ); - -/** - * @brief MbedTLS callback for signing using the provisioned private key. Used for - * signing the CSR. - * - * @param[in] pxContext Unused. - * @param[in] xMdAlg Unused. - * @param[in] pucHash Data to sign. - * @param[in] xHashLen Length of #pucHash. - * @param[out] pucSig The signature - * @param[out] pxSigLen The length of the signature. - * @param[in] pxRng Unused. - * @param[in] pxRngContext Unused. - */ -static int32_t prvPrivateKeySigningCallback( void * pxContext, - mbedtls_md_type_t xMdAlg, - const unsigned char * pucHash, - size_t xHashLen, - unsigned char * pucSig, - size_t * pxSigLen, - int ( * pxRng )( void *, unsigned char *, size_t ), - void * pxRngContext ); - -/** - * @brief MbedTLS random generation callback to generate random values with - * PKCS #11. - * - * @param[in] pxCtx Pointer to the PKCS #11 session handle. - * @param[out] pucRandom Buffer to write random data to. - * @param[in] xRandomLength Length of random data to write. - */ -static int prvRandomCallback( void * pxCtx, - unsigned char * pucRandom, - size_t xRandomLength ); - /** * @brief Generate a new ECDSA key pair using PKCS #11. * @@ -236,184 +140,6 @@ static CK_RV prvDestroyProvidedObjects( CK_SESSION_HANDLE xSession, /*-----------------------------------------------------------*/ -static int prvExtractEcPublicKey( CK_SESSION_HANDLE xP11Session, - mbedtls_ecdsa_context * pxEcdsaContext, - CK_OBJECT_HANDLE xPublicKey ) -{ - CK_ATTRIBUTE xEcTemplate = { 0 }; - int xMbedtlsRet = -1; - CK_RV xPkcs11ret = CKR_OK; - CK_BYTE pxEcPoint[ 67 ] = { 0 }; - CK_FUNCTION_LIST_PTR xP11FunctionList; - - mbedtls_ecdsa_init( pxEcdsaContext ); - mbedtls_ecp_group_init( &( pxEcdsaContext->grp ) ); - - xPkcs11ret = C_GetFunctionList( &xP11FunctionList ); - - if( xPkcs11ret != CKR_OK ) - { - LogError( ( "Could not get a PKCS #11 function pointer." ) ); - } - else - { - xEcTemplate.type = CKA_EC_POINT; - xEcTemplate.pValue = pxEcPoint; - xEcTemplate.ulValueLen = sizeof( pxEcPoint ); - xPkcs11ret = xP11FunctionList->C_GetAttributeValue( xP11Session, xPublicKey, &xEcTemplate, 1 ); - - if( xPkcs11ret != CKR_OK ) - { - LogError( ( "Failed to extract EC public key. Could not get attribute value. " - "C_GetAttributeValue failed with %lu.", xPkcs11ret ) ); - } - } - - if( xPkcs11ret == CKR_OK ) - { - xMbedtlsRet = mbedtls_ecp_group_load( &( pxEcdsaContext->grp ), MBEDTLS_ECP_DP_SECP256R1 ); - - if( xMbedtlsRet != 0 ) - { - LogError( ( "Failed creating an EC key. " - "mbedtls_ecp_group_load failed: MbedTLS" - "error = %s : %s.", - mbedtlsHighLevelCodeOrDefault( xMbedtlsRet ), - mbedtlsLowLevelCodeOrDefault( xMbedtlsRet ) ) ); - xPkcs11ret = CKR_FUNCTION_FAILED; - } - else - { - xMbedtlsRet = mbedtls_ecp_point_read_binary( &( pxEcdsaContext->grp ), &( pxEcdsaContext->Q ), &pxEcPoint[ 2 ], xEcTemplate.ulValueLen - 2 ); - - if( xMbedtlsRet != 0 ) - { - LogError( ( "Failed creating an EC key. " - "mbedtls_ecp_group_load failed: MbedTLS" - "error = %s : %s.", - mbedtlsHighLevelCodeOrDefault( xMbedtlsRet ), - mbedtlsLowLevelCodeOrDefault( xMbedtlsRet ) ) ); - xPkcs11ret = CKR_FUNCTION_FAILED; - } - } - } - - return xMbedtlsRet; -} - -/*-----------------------------------------------------------*/ - -static int32_t prvPrivateKeySigningCallback( void * pxContext, - mbedtls_md_type_t xMdAlg, - const unsigned char * pucHash, - size_t xHashLen, - unsigned char * pucSig, - size_t * pxSigLen, - int ( * pxRng )( void *, unsigned char *, size_t ), - void * pxRngContext ) -{ - CK_RV xRet = CKR_OK; - int32_t usResult = 0; - CK_MECHANISM xMech = { 0 }; - CK_BYTE pxToBeSigned[ 256 ]; - CK_ULONG xToBeSignedLen = sizeof( pxToBeSigned ); - CK_FUNCTION_LIST_PTR xFunctionList = NULL; - - /* Unreferenced parameters. */ - ( void ) ( pxContext ); - ( void ) ( pxRng ); - ( void ) ( pxRngContext ); - ( void ) ( xMdAlg ); - - /* Sanity check buffer length. */ - if( xHashLen > sizeof( pxToBeSigned ) ) - { - xRet = CKR_ARGUMENTS_BAD; - } - - xMech.mechanism = CKM_ECDSA; - memcpy( pxToBeSigned, pucHash, xHashLen ); - xToBeSignedLen = xHashLen; - - if( xRet == CKR_OK ) - { - xRet = C_GetFunctionList( &xFunctionList ); - } - - if( xRet == CKR_OK ) - { - xRet = xFunctionList->C_SignInit( xSigningContext.p11Session, &xMech, - xSigningContext.p11PrivateKey ); - } - - if( xRet == CKR_OK ) - { - *pxSigLen = sizeof( pxToBeSigned ); - xRet = xFunctionList->C_Sign( xSigningContext.p11Session, pxToBeSigned, - xToBeSignedLen, pucSig, ( CK_ULONG_PTR ) pxSigLen ); - } - - if( xRet == CKR_OK ) - { - /* PKCS #11 for P256 returns a 64-byte signature with 32 bytes for R and 32 - * bytes for S. This must be converted to an ASN.1 encoded array. */ - if( *pxSigLen != pkcs11ECDSA_P256_SIGNATURE_LENGTH ) - { - xRet = CKR_FUNCTION_FAILED; - LogError( ( "Failed to sign message using PKCS #11. Expected signature " - "length of %lu, but received %lu.", - ( unsigned long ) pkcs11ECDSA_P256_SIGNATURE_LENGTH, - ( unsigned long ) *pxSigLen ) ); - } - - if( xRet == CKR_OK ) - { - PKI_pkcs11SignatureTombedTLSSignature( pucSig, pxSigLen ); - } - } - - if( xRet != CKR_OK ) - { - LogError( ( "Failed to sign message using PKCS #11 with error code %lu.", xRet ) ); - usResult = -1; - } - - return usResult; -} - -/*-----------------------------------------------------------*/ - -static int prvRandomCallback( void * pxCtx, - unsigned char * pucRandom, - size_t xRandomLength ) -{ - CK_SESSION_HANDLE * pxP11Session = ( CK_SESSION_HANDLE * ) pxCtx; - CK_RV xRes; - CK_FUNCTION_LIST_PTR xP11FunctionList; - - xRes = C_GetFunctionList( &xP11FunctionList ); - - if( xRes != CKR_OK ) - { - LogError( ( "Failed to generate a random number in RNG callback. Could not get a " - "PKCS #11 function pointer." ) ); - } - else - { - xRes = xP11FunctionList->C_GenerateRandom( *pxP11Session, pucRandom, xRandomLength ); - - if( xRes != CKR_OK ) - { - LogError( ( "Failed to generate a random number in RNG callback. " - "C_GenerateRandom failed with %lu.", ( unsigned long ) xRes ) ); - } - } - - return ( int ) xRes; -} - -/*-----------------------------------------------------------*/ - static CK_RV prvGenerateKeyPairEC( CK_SESSION_HANDLE xSession, const char * pcPrivateKeyLabel, const char * pcPublicKeyLabel, @@ -488,7 +214,6 @@ bool xGenerateKeyAndCsr( CK_SESSION_HANDLE xP11Session, CK_OBJECT_HANDLE xPubKeyHandle; CK_RV xPkcs11Ret = CKR_OK; mbedtls_pk_context xPrivKey; - mbedtls_pk_info_t xPrivKeyInfo; mbedtls_ecdsa_context xEcdsaContext; mbedtls_x509write_csr xReq; int32_t ulMbedtlsRet = -1; @@ -505,6 +230,11 @@ bool xGenerateKeyAndCsr( CK_SESSION_HANDLE xP11Session, &xPrivKeyHandle, &xPubKeyHandle ); + if( xPkcs11Ret == CKR_OK ) + { + xPkcs11Ret = xPKCS11_initMbedtlsPkContext( &xPrivKey, xP11Session, xPrivKeyHandle ); + } + if( xPkcs11Ret == CKR_OK ) { mbedtls_x509write_csr_init( &xReq ); @@ -524,35 +254,16 @@ bool xGenerateKeyAndCsr( CK_SESSION_HANDLE xP11Session, if( ulMbedtlsRet == 0 ) { - mbedtls_pk_init( &xPrivKey ); - } - - if( ulMbedtlsRet == 0 ) - { - ulMbedtlsRet = prvExtractEcPublicKey( xP11Session, &xEcdsaContext, xPubKeyHandle ); - } - - if( ulMbedtlsRet == 0 ) - { - xSigningContext.p11Session = xP11Session; - xSigningContext.p11PrivateKey = xPrivKeyHandle; - - memcpy( &xPrivKeyInfo, pxHeader, sizeof( mbedtls_pk_info_t ) ); - - xPrivKeyInfo.sign_func = prvPrivateKeySigningCallback; - xPrivKey.pk_info = &xPrivKeyInfo; - xPrivKey.pk_ctx = &xEcdsaContext; - mbedtls_x509write_csr_set_key( &xReq, &xPrivKey ); ulMbedtlsRet = mbedtls_x509write_csr_pem( &xReq, ( unsigned char * ) pcCsrBuffer, - xCsrBufferLength, &prvRandomCallback, + xCsrBufferLength, &lPKCS11RandomCallback, &xP11Session ); } mbedtls_x509write_csr_free( &xReq ); - mbedtls_ecdsa_free( &xEcdsaContext ); - mbedtls_ecp_group_free( &( xEcdsaContext.grp ) ); + + lPKCS11PkMbedtlsCloseSessionAndFree( &xPrivKey ); } *pxOutCsrLength = strlen( pcCsrBuffer );