pkcs11_client: implement RSA sign/verify
Make mbedtls_rsa_emsa_pkcs1_v15_encode_digestinfo from rsa.c public,
and use it in the pkcs11_client module.
pkcs11_client: refactor pkcs11_sign and pkcs11_verify to simplify
memory managmement. Implement these functions for RSA.
diff --git a/tests/suites/test_suite_pkcs11_client.data b/tests/suites/test_suite_pkcs11_client.data
index 3f4d4cf..ba47f95 100644
--- a/tests/suites/test_suite_pkcs11_client.data
+++ b/tests/suites/test_suite_pkcs11_client.data
@@ -5,3 +5,15 @@
PKCS#11 RSA generate and sign
depends_on:MBEDTLS_PK_C:MBEDTLS_RSA_C
pk_generate_sign:MBEDTLS_PK_RSA
+
+PKCS#11 RSA import, sign and verify with Cryptoki
+depends_on:MBEDTLS_PK_C:MBEDTLS_RSA_C
+pk_import_sign_verify:"data_files/server1.key"
+
+PKCS#11 RSA import, sign with MbedTLS and verify with Cryptoki
+depends_on:MBEDTLS_PK_C:MBEDTLS_RSA_C
+pk_import_verify_signed:"data_files/server1.key"
+
+PKCS#11 RSA verify a hardcoded signature with Cryptoki
+depends_on:MBEDTLS_SHA1_C:MBEDTLS_PKCS1_V15
+pk_rsa_hardcoded_verify:"206ef4bf396c6087f8229ef196fd35f37ccb8de5efcdb238f20d556668f114257a11fbe038464a67830378e62ae9791453953dac1dbd7921837ba98e84e856eb80ed9487e656d0b20c28c8ba5e35db1abbed83ed1c7720a97701f709e3547a4bfcabca9c89c57ad15c3996577a0ae36d7c7b699035242f37954646c1cd5c08ac":MBEDTLS_MD_SHA1:1024:16:"e28a13548525e5f36dccb24ecb7cc332cc689dfd64012604c9c7816d72a16c3f5fcdc0e86e7c03280b1c69b586ce0cd8aec722cc73a5d3b730310bf7dfebdc77ce5d94bbc369dc18a2f7b07bd505ab0f82224aef09fdc1e5063234255e0b3c40a52e9e8ae60898eb88a766bdd788fe9493d8fd86bcdd2884d5c06216c65469e5":16:"3":"5abc01f5de25b70867ff0c24e222c61f53c88daf42586fddcd56f3c4588f074be3c328056c063388688b6385a8167957c6e5355a510e005b8a851d69c96b36ec6036644078210e5d7d326f96365ee0648882921492bc7b753eb9c26cdbab37555f210df2ca6fec1b25b463d38b81c0dcea202022b04af5da58aa03d77be949b7":0
diff --git a/tests/suites/test_suite_pkcs11_client.function b/tests/suites/test_suite_pkcs11_client.function
index 34800ef..d972336 100644
--- a/tests/suites/test_suite_pkcs11_client.function
+++ b/tests/suites/test_suite_pkcs11_client.function
@@ -294,3 +294,190 @@
mbedtls_pk_free( &transparent_ctx );
}
/* END_CASE */
+
+/* BEGIN_CASE depends_on:MBEDTLS_PK_C:MBEDTLS_SHA256_C */
+void pk_import_sign_verify( char *file )
+ {
+ /* Sign with cryptoki, convert to mbedTLS format and save,
+ verify by cryptoki with a conversion to a raw, concatenated
+ format by the engine. */
+ mbedtls_pk_context pkcs11_ctx;
+ mbedtls_pk_context transparent_ctx;
+ CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
+ CK_OBJECT_HANDLE hPublicKey = CK_INVALID_HANDLE;
+ CK_OBJECT_HANDLE hPrivateKey = CK_INVALID_HANDLE;
+ unsigned char hash_value[32] = "Fake hash, it doesn't matter....";
+ unsigned char sig_buffer[4096];
+ size_t sig_length = sizeof( sig_buffer );
+
+ mbedtls_pk_init( &pkcs11_ctx );
+ mbedtls_pk_init( &transparent_ctx );
+
+ /* Read a transparent key */
+ TEST_ASSERT( mbedtls_pk_parse_keyfile( &transparent_ctx, file, NULL ) == 0 );
+
+ /* Initialize cryptoki and import the key into the token */
+ hSession = pkcs11_init( );
+ TEST_ASSERT( hSession != CK_INVALID_HANDLE );
+
+ TEST_ASSERT( mbedtls_pk_import_to_pkcs11( &transparent_ctx,
+ MBEDTLS_PK_FLAG_SIGN |
+ MBEDTLS_PK_FLAG_VERIFY,
+ hSession,
+ &hPublicKey,
+ &hPrivateKey ) == 0 );
+ TEST_ASSERT( hPublicKey != CK_INVALID_HANDLE );
+ TEST_ASSERT( hPrivateKey != CK_INVALID_HANDLE );
+ TEST_ASSERT( mbedtls_pk_setup_pkcs11( &pkcs11_ctx,
+ hSession,
+ hPublicKey,
+ hPrivateKey ) == 0 );
+
+ /* Sign with the token and verify with cryptoki */
+ TEST_ASSERT( sizeof( sig_buffer ) >= mbedtls_pk_signature_size( &pkcs11_ctx ) );
+ TEST_ASSERT( mbedtls_pk_sign( &pkcs11_ctx, MBEDTLS_MD_SHA256,
+ hash_value, 32,
+ sig_buffer, &sig_length,
+ NULL, NULL ) == 0 );
+ TEST_ASSERT( mbedtls_pk_verify( &pkcs11_ctx, MBEDTLS_MD_SHA256,
+ hash_value, 32,
+ sig_buffer, sig_length ) == 0 );
+
+exit:
+ if( hPublicKey != CK_INVALID_HANDLE )
+ C_DestroyObject( hSession, hPublicKey );
+ if( hPrivateKey != CK_INVALID_HANDLE )
+ C_DestroyObject( hSession, hPrivateKey );
+ C_CloseSession( hSession );
+ C_Finalize( NULL_PTR );
+ mbedtls_pk_free( &pkcs11_ctx );
+ mbedtls_pk_free( &transparent_ctx );
+}
+/* END_CASE */
+
+/* BEGIN_CASE depends_on:MBEDTLS_PK_C:MBEDTLS_SHA256_C */
+void pk_import_verify_signed( char *file )
+{
+ /* Sign with mbedTLS, verify by cryptoki with a conversion
+ to a raw, concatenated format by the engine. */
+ mbedtls_pk_context pkcs11_ctx;
+ mbedtls_pk_context transparent_ctx;
+ CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
+ CK_OBJECT_HANDLE hPublicKey = CK_INVALID_HANDLE;
+ CK_OBJECT_HANDLE hPrivateKey = CK_INVALID_HANDLE;
+ unsigned char hash_value[32] = "Fake hash, it doesn't matter....";
+ unsigned char sig_buffer[4096];
+ size_t sig_length = sizeof( sig_buffer );
+
+ mbedtls_pk_init( &pkcs11_ctx );
+ mbedtls_pk_init( &transparent_ctx );
+
+ /* Read a transparent key */
+ TEST_ASSERT( mbedtls_pk_parse_keyfile( &transparent_ctx, file, NULL ) == 0 );
+
+ /* Initialize cryptoki and import the key into the token */
+ hSession = pkcs11_init( );
+ TEST_ASSERT( hSession != CK_INVALID_HANDLE );
+
+ TEST_ASSERT( mbedtls_pk_import_to_pkcs11( &transparent_ctx,
+ MBEDTLS_PK_FLAG_SIGN |
+ MBEDTLS_PK_FLAG_VERIFY,
+ hSession,
+ &hPublicKey,
+ NULL ) == 0 );
+ TEST_ASSERT( hPublicKey != CK_INVALID_HANDLE );
+ TEST_ASSERT( mbedtls_pk_setup_pkcs11( &pkcs11_ctx,
+ hSession,
+ hPublicKey,
+ CK_INVALID_HANDLE ) == 0 );
+
+ /* Sign with the token and verify with cryptoki */
+ TEST_ASSERT( sizeof( sig_buffer ) >= mbedtls_pk_signature_size( &pkcs11_ctx ) );
+ TEST_ASSERT( mbedtls_pk_sign( &transparent_ctx, MBEDTLS_MD_SHA256,
+ hash_value, 32,
+ sig_buffer, &sig_length,
+ NULL, NULL ) == 0 );
+ TEST_ASSERT( mbedtls_pk_verify( &pkcs11_ctx, MBEDTLS_MD_SHA256,
+ hash_value, 32,
+ sig_buffer, sig_length ) == 0 );
+
+exit:
+ if( hPublicKey != CK_INVALID_HANDLE )
+ C_DestroyObject( hSession, hPublicKey );
+ if( hPrivateKey != CK_INVALID_HANDLE )
+ C_DestroyObject( hSession, hPrivateKey );
+ C_CloseSession( hSession );
+ C_Finalize( NULL_PTR );
+ mbedtls_pk_free( &pkcs11_ctx );
+ mbedtls_pk_free( &transparent_ctx );
+}
+/* END_CASE */
+
+/* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
+void pk_rsa_hardcoded_verify( char *message_hex_string, int digest,
+ int mod, int radix_N, char *input_N, int radix_E,
+ char *input_E, char *result_hex_str, int result )
+{
+ unsigned char message_str[1000];
+ unsigned char hash_result[1000];
+ unsigned char result_str[1000];
+ mbedtls_rsa_context *rsa;
+ mbedtls_pk_context transparent_ctx;
+ int msg_len;
+
+ mbedtls_pk_context pkcs11_ctx;
+ CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
+ CK_OBJECT_HANDLE hPublicKey = CK_INVALID_HANDLE;
+ CK_OBJECT_HANDLE hPrivateKey = CK_INVALID_HANDLE;
+
+ mbedtls_pk_init( &transparent_ctx );
+
+ memset( message_str, 0x00, 1000 );
+ memset( hash_result, 0x00, 1000 );
+ memset( result_str, 0x00, 1000 );
+
+ TEST_ASSERT( mbedtls_pk_setup( &transparent_ctx, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
+ rsa = mbedtls_pk_rsa( transparent_ctx );
+
+ rsa->len = mod / 8;
+ TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
+ TEST_ASSERT( mbedtls_mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
+
+ msg_len = unhexify( message_str, message_hex_string );
+ unhexify( result_str, result_hex_str );
+
+ if( mbedtls_md_info_from_type( digest ) != NULL )
+ TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
+
+ // PKCS11 part
+ mbedtls_pk_init( &pkcs11_ctx );
+
+ /* Initialize cryptoki and import the key into the token */
+ hSession = pkcs11_init( );
+ TEST_ASSERT( hSession != CK_INVALID_HANDLE );
+ TEST_ASSERT( mbedtls_pk_import_to_pkcs11( &transparent_ctx,
+ MBEDTLS_PK_FLAG_SIGN |
+ MBEDTLS_PK_FLAG_VERIFY,
+ hSession,
+ &hPublicKey,
+ NULL ) == 0 );
+ TEST_ASSERT( hPublicKey != CK_INVALID_HANDLE );
+ TEST_ASSERT( mbedtls_pk_setup_pkcs11( &pkcs11_ctx,
+ hSession,
+ hPublicKey,
+ CK_INVALID_HANDLE ) == 0 );
+
+ TEST_ASSERT( mbedtls_pk_verify( &pkcs11_ctx, digest, hash_result, 0,
+ result_str, mbedtls_pk_get_len( &transparent_ctx ) ) == result );
+
+exit:
+ if( hPublicKey != CK_INVALID_HANDLE )
+ C_DestroyObject( hSession, hPublicKey );
+ if( hPrivateKey != CK_INVALID_HANDLE )
+ C_DestroyObject( hSession, hPrivateKey );
+ C_CloseSession( hSession );
+ C_Finalize( NULL_PTR );
+ mbedtls_pk_free( &pkcs11_ctx );
+ mbedtls_pk_free( &transparent_ctx );
+}
+/* END_CASE */