Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished object demo. #12

Merged
merged 1 commit into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Finished object demo.
  • Loading branch information
lundinc2 committed Jun 25, 2020
commit cb5c3dbbb7fd0c6767cdf5dde0d6f1085927f1cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</Midl>
<ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\..\..\FreeRTOS-Plus\Source\FreeRTOS-Plus-PKCS11\include;..\..\..\FreeRTOS-Plus\Source\FreeRTOS-Plus-PKCS11\3rdparty\pkcs11;..\..\..\FreeRTOS-Plus\Source\FreeRTOS-Plus-PKCS11\3rdparty\mbedtls\include;..\..\..\FreeRTOS-Plus\Source\FreeRTOS-Plus-PKCS11\3rdparty\mbedtls_utils;..\..\..\FreeRTOS\Source\include;..\..\..\FreeRTOS\Source\portable\MSVC-MingW;.;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\..\..\FreeRTOS-Plus\Source\FreeRTOS-Plus-PKCS11\include;..\..\..\FreeRTOS-Plus\Source\FreeRTOS-Plus-PKCS11\3rdparty\pkcs11;..\..\..\FreeRTOS-Plus\Source\FreeRTOS-Plus-PKCS11\3rdparty\mbedtls\include;..\..\..\FreeRTOS-Plus\Source\FreeRTOS-Plus-PKCS11\3rdparty\mbedtls_utils;..\..\..\FreeRTOS\Source\include;..\..\..\FreeRTOS\Source\portable\MSVC-MingW;examples;.;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_WIN32_WINNT=0x0500;WINVER=0x400;_CRT_SECURE_NO_WARNINGS;MBEDTLS_CONFIG_FILE="aws_mbedtls_config.h";CONFIG_MEDTLS_USE_AFR_MEMORY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>false</MinimalRebuild>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
Expand All @@ -76,6 +76,7 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<ExceptionHandling>false</ExceptionHandling>
<CompileAs>CompileAsC</CompileAs>
<DisableSpecificWarnings>4204;4221;%(DisableSpecificWarnings)</DisableSpecificWarnings>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
Expand Down Expand Up @@ -234,7 +235,7 @@
<ClCompile Include="..\..\Source\FreeRTOS-Plus-PKCS11\iot_pkcs11_mbedtls.c" />
<ClCompile Include="..\..\Source\FreeRTOS-Plus-PKCS11\iot_pki_utils.c" />
<ClCompile Include="..\..\Source\FreeRTOS-Plus-PKCS11\portable\iot_pkcs11_pal.c" />
<ClCompile Include="examples\common.c" />
<ClCompile Include="examples\demo_helpers.c" />
<ClCompile Include="examples\management_and_rng.c" />
<ClCompile Include="examples\objects.c" />
<ClCompile Include="examples\mechanisms_and_digests.c" />
Expand Down Expand Up @@ -339,6 +340,8 @@
<ClInclude Include="..\..\Source\FreeRTOS-Plus-PKCS11\include\iot_pki_utils.h" />
<ClInclude Include="..\..\Source\FreeRTOS-Plus-PKCS11\include\threading_alt.h" />
<ClInclude Include="aws_mbedtls_config.h" />
<ClInclude Include="examples\demo_helpers.h" />
<ClInclude Include="examples\pkcs11_demos.h" />
<ClInclude Include="FreeRTOSConfig.h" />
<ClInclude Include="pkcs11_demo_config.h" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@
<ClCompile Include="examples\mechanisms_and_digests.c">
<Filter>examples</Filter>
</ClCompile>
<ClCompile Include="examples\common.c">
<ClCompile Include="examples\demo_helpers.c">
<Filter>examples</Filter>
</ClCompile>
</ItemGroup>
Expand Down Expand Up @@ -634,6 +634,12 @@
<Filter>3rdparty\mbedtls_utils</Filter>
</ClInclude>
<ClInclude Include="pkcs11_demo_config.h" />
<ClInclude Include="examples\pkcs11_demos.h">
<Filter>examples</Filter>
</ClInclude>
<ClInclude Include="examples\demo_helpers.h">
<Filter>examples</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\Source\FreeRTOS-Plus-PKCS11\3rdparty\mbedtls\library\CMakeLists.txt">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@
#include "iot_pkcs11.h"
#include "pkcs11.h"

/* This function contains standard setup code for PKCS #11. See the
* "management_and_rng.c" file for the demo code explaining this section
* of cryptoki.
*/
void prvStart( CK_SESSION_HANDLE * pxSession,
/* mbed TLS includes. */
#include "mbedtls/pk.h"
#include "mbedtls/oid.h"

/* Helpers include. */
#include "demo_helpers.h"

void vStart( CK_SESSION_HANDLE * pxSession,
CK_SLOT_ID ** ppxSlotId )
{
CK_RV xResult = CKR_OK;
Expand Down Expand Up @@ -96,11 +99,7 @@ void prvStart( CK_SESSION_HANDLE * pxSession,
}
/*-----------------------------------------------------------*/

/* This function contains standard tear down code for PKCS #11. See the
* "management_and_rng.c" file for the demo code explaining this section
* of cryptoki.
*/
void prvEnd( CK_SESSION_HANDLE xSession,
void vEnd( CK_SESSION_HANDLE xSession,
CK_SLOT_ID * pxSlotId )
{
C_CloseSession( xSession );
Expand All @@ -109,11 +108,13 @@ void prvEnd( CK_SESSION_HANDLE xSession,
}
/*-----------------------------------------------------------*/

/* Write the ASN.1 encoded bytes of the device public key to the console. */
void vWriteHexBytesToConsole( char * pcDescription,
CK_BYTE * pucData,
CK_ULONG ulDataLength )
{
/* This function is simply a helper function to print the raw hex values
* of an EC public key. It's explanation is not within the scope of the demos
* and is sparsely commented. */
#define BYTES_TO_DISPLAY_PER_ROW 16
char pcByteRow[ 1 + ( BYTES_TO_DISPLAY_PER_ROW * 2 ) + ( BYTES_TO_DISPLAY_PER_ROW / 2 ) ];
char * pcNextChar = pcByteRow;
Expand All @@ -124,7 +125,7 @@ void vWriteHexBytesToConsole( char * pcDescription,
configPRINTF( ( "%s, %d bytes:\r\n", pcDescription, ulDataLength ) );

/* Iterate over the bytes of the encoded public key. */
for( ; ulIndex < ulDataLength; ulIndex++ )
for( ulIndex = 0; ulIndex < ulDataLength; ulIndex++ )
{
/* Convert one byte to ASCII hex. */
ucByteValue = *( pucData + ulIndex );
Expand Down Expand Up @@ -167,6 +168,9 @@ CK_RV vExportPublicKey( CK_SESSION_HANDLE xSession,
CK_BYTE ** ppucDerPublicKey,
CK_ULONG * pulDerPublicKeyLength )
{
/* This function is simply a helper function to export the raw hex values
* of an EC public key into a buffer. It's explanation is not within the
* scope of the demos and is sparsely commented. */
CK_RV xResult;
CK_FUNCTION_LIST_PTR pxFunctionList;
CK_KEY_TYPE xKeyType = 0;
Expand Down Expand Up @@ -255,3 +259,4 @@ CK_RV vExportPublicKey( CK_SESSION_HANDLE xSession,
return xResult;
}
/*-----------------------------------------------------------*/

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* FreeRTOS PKCS #11 V1.0.3
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
#ifndef _DEMO_HELPER_FUNCTIONS_
#define _DEMO_HELPER_FUNCTIONS_

#include "mbedtls/pk.h"

/* This function contains standard setup code for PKCS #11. See the
* "management_and_rng.c" file for the demo code explaining this section
* of cryptoki.
*/
void vStart( CK_SESSION_HANDLE * pxSession,
CK_SLOT_ID ** ppxSlotId );
/*-----------------------------------------------------------*/

/* This function contains standard tear down code for PKCS #11. See the
* "management_and_rng.c" file for the demo code explaining this section
* of cryptoki.
*/
void vEnd( CK_SESSION_HANDLE xSession,
CK_SLOT_ID * pxSlotId );
/*-----------------------------------------------------------*/

/* This function is simply a helper function to print the raw hex values
* of an EC public key. It's explanation is not within the scope of the demos
* and is sparsely commented. */
void vWriteHexBytesToConsole( char * pcDescription,
CK_BYTE * pucData,
CK_ULONG ulDataLength );
/*-----------------------------------------------------------*/

/* This function is simply a helper function to export the raw hex values
* of an EC public key into a buffer. It's explanation is not within the
* scope of the demos and is sparsely commented. */
CK_RV vExportPublicKey( CK_SESSION_HANDLE xSession,
CK_OBJECT_HANDLE xPublicKeyHandle,
CK_BYTE ** ppucDerPublicKey,
CK_ULONG * pulDerPublicKeyLength );
/*-----------------------------------------------------------*/
#endif /* _DEMO_HELPER_FUNCTIONS_ */
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#include "iot_pkcs11.h"
#include "pkcs11.h"

/* Demo include. */
#include "pkcs11_demos.h"

/**
* This function details how to use the PKCS #11 "Management" functions to
* manage the internal state machine of the PKCS #11 implementation. These
Expand Down Expand Up @@ -185,7 +188,7 @@ void vPKCS11ManagementAndRNGDemo( void )
* the length of the byte buffer. Then it will fill this buffer with random
* bytes. */
xResult = pxFunctionList->C_GenerateRandom( hSession,
&xRandomData,
xRandomData,
sizeof( xRandomData ) );
configASSERT( xResult == CKR_OK );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,9 @@
#include "iot_pkcs11.h"
#include "pkcs11.h"

/* This function contains standard setup code for PKCS #11. See the
* "management_and_rng.c" file for the demo code explaining this section
* of cryptoki.
*/
extern void prvStart( CK_SESSION_HANDLE * pxSession,
CK_SLOT_ID ** ppxSlotId );


/* This function contains standard tear down code for PKCS #11. See the
* "management_and_rng.c" file for the demo code explaining this section
* of cryptoki.
*/
extern void prvEnd( CK_SESSION_HANDLE xSession,
CK_SLOT_ID * pxSlotId );
/* Demo includes. */
#include "demo_helpers.h"
#include "pkcs11_demos.h"

/**
* This function details what Cryptoki mechanisms are, how to query a slot's
Expand Down Expand Up @@ -86,6 +75,11 @@ void vPKCS11MechanismsAndDigestDemo( void )
*/
CK_MECHANISM_TYPE xMechanismType = 0;

/* This variable is not directly used, but is instantiated for demonstration
* purposes.
*/
( void ) xMechanismType;

/* The CK_MECHANISM_INFO allows the application to retrieve the minimum and
* maximum key sizes supported by the mechanism (could be in bits or bytes).
* The structure also has a flags field, that is populated with bit flags
Expand All @@ -108,7 +102,7 @@ void vPKCS11MechanismsAndDigestDemo( void )

CK_BYTE pxKownMessage[] = "Hello world!";

prvStart( &hSession, &pxSlotId );
vStart( &hSession, &pxSlotId );
xResult = C_GetFunctionList( &pxFunctionList );
configASSERT( CKR_OK == xResult );
configASSERT( pxFunctionList->C_GetMechanismInfo != NULL );
Expand Down Expand Up @@ -259,6 +253,6 @@ void vPKCS11MechanismsAndDigestDemo( void )
configPRINTF( ( "\r\n" ) );

configPRINTF( ( "Finished PKCS #11 Mechanisms and Digest Demo.\r\n" ) );
prvEnd( hSession, pxSlotId );
vEnd( hSession, pxSlotId );
}

Loading