From 12997b56eebe279aa3b05adb25937801652ba4e5 Mon Sep 17 00:00:00 2001 From: ying-css <71699179+ying-css@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:30:05 +0800 Subject: [PATCH] Minor code modification (#76) 1)Minor code modification --- trustm_provider/trustm_provider_signature.c | 44 ++++++++++--------- .../trustm_provider_store_object.c | 33 ++++++-------- 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/trustm_provider/trustm_provider_signature.c b/trustm_provider/trustm_provider_signature.c index 797a611..ec3ec01 100644 --- a/trustm_provider/trustm_provider_signature.c +++ b/trustm_provider/trustm_provider_signature.c @@ -39,8 +39,8 @@ static OSSL_FUNC_signature_gettable_ctx_params_fn trustm_signature_gettable_ctx_ static OSSL_FUNC_signature_sign_init_fn trustm_rsa_signature_sign_init; static OSSL_FUNC_signature_sign_fn trustm_rsa_signature_sign; -static OSSL_FUNC_signature_digest_sign_init_fn trustm_rsa_signature_digest_sign_init; -static OSSL_FUNC_signature_digest_sign_update_fn trustm_rsa_signature_digest_sign_update; +static OSSL_FUNC_signature_digest_sign_init_fn trustm_rsa_signature_digest_init; +static OSSL_FUNC_signature_digest_sign_update_fn trustm_rsa_signature_digest_update; static OSSL_FUNC_signature_digest_sign_final_fn trustm_rsa_signature_digest_sign_final; static OSSL_FUNC_signature_digest_sign_fn trustm_rsa_signature_digest_sign; @@ -53,8 +53,8 @@ static OSSL_FUNC_signature_settable_ctx_params_fn trustm_rsa_signature_settable_ static OSSL_FUNC_signature_sign_init_fn trustm_ecdsa_signature_sign_init; static OSSL_FUNC_signature_sign_fn trustm_ecdsa_signature_sign; -static OSSL_FUNC_signature_digest_sign_init_fn trustm_ecdsa_signature_digest_sign_init; -static OSSL_FUNC_signature_digest_sign_update_fn trustm_ecdsa_signature_digest_sign_update; +static OSSL_FUNC_signature_digest_sign_init_fn trustm_ecdsa_signature_digest_init; +static OSSL_FUNC_signature_digest_sign_update_fn trustm_ecdsa_signature_digest_update; static OSSL_FUNC_signature_digest_sign_final_fn trustm_ecdsa_signature_digest_sign_final; static OSSL_FUNC_signature_digest_sign_fn trustm_ecdsa_signature_digest_sign; @@ -337,7 +337,7 @@ static int trustm_ecdsa_signature_sign(void *ctx, unsigned char *sig, size_t *si } // basically digest sign, can be used for both sign and verify operations -static int trustm_rsa_signature_digest_sign_init(void *ctx, const char *mdname, void *provkey, const OSSL_PARAM params[]) +static int trustm_rsa_signature_digest_init(void *ctx, const char *mdname, void *provkey, const OSSL_PARAM params[]) { trustm_signature_ctx_t *trustm_signature_ctx = ctx; trustm_signature_ctx->trustm_rsa_key = provkey; @@ -362,7 +362,7 @@ static int trustm_rsa_signature_digest_sign_init(void *ctx, const char *mdname, return_status = optiga_lib_status; if (return_status != OPTIGA_LIB_SUCCESS) { - TRUSTM_PROVIDER_ERRFN("Error in trustm_rsa_signature_digest_sign_init\n"); + TRUSTM_PROVIDER_ERRFN("Error in trustm_rsa_signature_digest_init\n"); return 0; } @@ -374,7 +374,7 @@ static int trustm_rsa_signature_digest_sign_init(void *ctx, const char *mdname, } // basically digest sign, can be used for both sign and verify operations -static int trustm_ecdsa_signature_digest_sign_init(void *ctx, const char *mdname, void *provkey, const OSSL_PARAM params[]) +static int trustm_ecdsa_signature_digest_init(void *ctx, const char *mdname, void *provkey, const OSSL_PARAM params[]) { trustm_signature_ctx_t *trustm_signature_ctx = ctx; trustm_signature_ctx->trustm_ec_key = provkey; @@ -399,7 +399,7 @@ static int trustm_ecdsa_signature_digest_sign_init(void *ctx, const char *mdname return_status = optiga_lib_status; if (return_status != OPTIGA_LIB_SUCCESS) { - TRUSTM_PROVIDER_ERRFN("Error in trustm_ecdsa_signature_digest_sign_init\n"); + TRUSTM_PROVIDER_ERRFN("Error in trustm_ecdsa_signature_digest_init\n"); return 0; } @@ -411,7 +411,7 @@ static int trustm_ecdsa_signature_digest_sign_init(void *ctx, const char *mdname } // basically digest update, can be used for both sign and verify operations -static int trustm_rsa_signature_digest_sign_update(void *ctx, const unsigned char *data, size_t datalen) +static int trustm_rsa_signature_digest_update(void *ctx, const unsigned char *data, size_t datalen) { trustm_signature_ctx_t *trustm_signature_ctx = ctx; optiga_lib_status_t return_status; @@ -440,7 +440,7 @@ static int trustm_rsa_signature_digest_sign_update(void *ctx, const unsigned cha return_status = optiga_lib_status; if (return_status != OPTIGA_LIB_SUCCESS) { - TRUSTM_PROVIDER_ERRFN("Error in trustm_ecdsa_signature_digest_sign_update\n"); + TRUSTM_PROVIDER_ERRFN("Error in trustm_ecdsa_signature_digest_update\n"); return 0; } optiga_lib_status = OPTIGA_LIB_BUSY; @@ -460,7 +460,7 @@ static int trustm_rsa_signature_digest_sign_update(void *ctx, const unsigned cha return_status = optiga_lib_status; if (return_status != OPTIGA_LIB_SUCCESS) { - TRUSTM_PROVIDER_ERRFN("Error in trustm_ecdsa_signature_digest_sign_update\n"); + TRUSTM_PROVIDER_ERRFN("Error in trustm_ecdsa_signature_digest_update\n"); return 0; } @@ -470,7 +470,7 @@ static int trustm_rsa_signature_digest_sign_update(void *ctx, const unsigned cha } // basically digest update, can be used for both sign and verify operations -static int trustm_ecdsa_signature_digest_sign_update(void *ctx, const unsigned char *data, size_t datalen) +static int trustm_ecdsa_signature_digest_update(void *ctx, const unsigned char *data, size_t datalen) { trustm_signature_ctx_t *trustm_signature_ctx = ctx; optiga_lib_status_t return_status; @@ -895,7 +895,9 @@ static int trustm_rsa_signature_digest_verify_final(void *ctx, const unsigned ch TRUSTM_PROVIDER_DBGFN(">"); TRUSTM_PROVIDER_SSL_MUTEX_ACQUIRE trustm_signature_ctx->me_crypt = me_crypt; - + printf("siglen : %d\n", siglen); + if (sig == NULL) + printf("sig is NULL"); // convert public key to trustm's public key format public_key_buffer[0] = 0x03; if (trustm_signature_ctx->trustm_rsa_key->key_size == OPTIGA_RSA_KEY_2048_BIT_EXPONENTIAL) @@ -1312,11 +1314,11 @@ const OSSL_DISPATCH trustm_rsa_signature_functions[] = { { OSSL_FUNC_SIGNATURE_DUPCTX, (void(*)(void))trustm_signature_dupctx }, { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void(*)(void))trustm_rsa_signature_sign_init }, { OSSL_FUNC_SIGNATURE_SIGN, (void(*)(void))trustm_rsa_signature_sign }, - { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, (void(*)(void))trustm_rsa_signature_digest_sign_init }, - { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, (void(*)(void))trustm_rsa_signature_digest_sign_update }, + { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, (void(*)(void))trustm_rsa_signature_digest_init }, + { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, (void(*)(void))trustm_rsa_signature_digest_update }, { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, (void(*)(void))trustm_rsa_signature_digest_sign_final }, - { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, (void(*)(void))trustm_rsa_signature_digest_sign_init }, - { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, (void(*)(void))trustm_rsa_signature_digest_sign_update }, + { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, (void(*)(void))trustm_rsa_signature_digest_init }, + { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, (void(*)(void))trustm_rsa_signature_digest_update }, { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, (void(*)(void))trustm_rsa_signature_digest_verify_final }, { OSSL_FUNC_SIGNATURE_DIGEST_SIGN, (void(*)(void))trustm_rsa_signature_digest_sign }, { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void(*)(void))trustm_signature_get_ctx_params }, @@ -1333,11 +1335,11 @@ const OSSL_DISPATCH trustm_ecdsa_signature_functions[] = { { OSSL_FUNC_SIGNATURE_DUPCTX, (void(*)(void))trustm_signature_dupctx }, { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void(*)(void))trustm_ecdsa_signature_sign_init }, { OSSL_FUNC_SIGNATURE_SIGN, (void(*)(void))trustm_ecdsa_signature_sign }, - { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, (void(*)(void))trustm_ecdsa_signature_digest_sign_init }, - { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, (void(*)(void))trustm_ecdsa_signature_digest_sign_update }, + { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, (void(*)(void))trustm_ecdsa_signature_digest_init }, + { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, (void(*)(void))trustm_ecdsa_signature_digest_update }, { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, (void(*)(void))trustm_ecdsa_signature_digest_sign_final }, - { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, (void(*)(void))trustm_ecdsa_signature_digest_sign_init }, - { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, (void(*)(void))trustm_ecdsa_signature_digest_sign_update }, + { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, (void(*)(void))trustm_ecdsa_signature_digest_init }, + { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, (void(*)(void))trustm_ecdsa_signature_digest_update }, { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, (void(*)(void))trustm_ecdsa_signature_digest_verify_final }, { OSSL_FUNC_SIGNATURE_DIGEST_SIGN, (void(*)(void))trustm_ecdsa_signature_digest_sign }, { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void(*)(void))trustm_signature_get_ctx_params }, diff --git a/trustm_provider/trustm_provider_store_object.c b/trustm_provider/trustm_provider_store_object.c index 1226377..4a6e170 100644 --- a/trustm_provider/trustm_provider_store_object.c +++ b/trustm_provider/trustm_provider_store_object.c @@ -63,15 +63,13 @@ static void *trustm_object_open(void *provctx, const char *uri) if ((baseuri = OPENSSL_strdup(uri)) == NULL) { - OPENSSL_clear_free(trustm_object_ctx, sizeof(trustm_object_ctx_t)); - return NULL; + goto error; } char *ptr = strstr(baseuri, needle); if (ptr == NULL) { - OPENSSL_clear_free(trustm_object_ctx, sizeof(trustm_object_ctx_t)); - return NULL; + goto error; } // extract arguments @@ -79,8 +77,7 @@ static void *trustm_object_open(void *provctx, const char *uri) if (opts[0] == NULL) { TRUSTM_PROVIDER_ERRFN("No OID input. Abortting...\n"); - OPENSSL_clear_free(trustm_object_ctx, sizeof(trustm_object_ctx_t)); - return NULL; + goto error; } while (opts[i] != NULL) @@ -91,8 +88,7 @@ static void *trustm_object_open(void *provctx, const char *uri) if (i > 6) { - OPENSSL_clear_free(trustm_object_ctx, sizeof(trustm_object_ctx_t)); - return NULL; + goto error; } // processing opts[0] @@ -109,8 +105,7 @@ static void *trustm_object_open(void *provctx, const char *uri) ((key_id < 0xE0FC) || (key_id > 0xE0FD))) { TRUSTM_PROVIDER_ERRFN("Invalid key OID\n"); - OPENSSL_clear_free(trustm_object_ctx, sizeof(trustm_object_ctx_t)); - return NULL; + goto error; } // assign primary key object id @@ -121,8 +116,7 @@ static void *trustm_object_open(void *provctx, const char *uri) if (opts[1] == NULL) { TRUSTM_PROVIDER_ERRFN("Specify public key input\n"); - OPENSSL_clear_free(trustm_object_ctx, sizeof(trustm_object_ctx_t)); - return NULL; + goto error; } // if just reading out the public key @@ -155,16 +149,14 @@ static void *trustm_object_open(void *provctx, const char *uri) if (opts[3] == NULL) { TRUSTM_PROVIDER_ERRFN("Specify key size\n"); - OPENSSL_clear_free(trustm_object_ctx, sizeof(trustm_object_ctx_t)); - return NULL; + goto error; } sscanf(opts[3],"%x", &(trustm_object_ctx->key_size)); if (opts[4] == NULL) { TRUSTM_PROVIDER_ERRFN("Specify key usage\n"); - OPENSSL_clear_free(trustm_object_ctx, sizeof(trustm_object_ctx_t)); - return NULL; + goto error; } sscanf(opts[4], "%x", &(trustm_object_ctx->key_usage)); } @@ -175,16 +167,14 @@ static void *trustm_object_open(void *provctx, const char *uri) if (opts[3] == NULL) { TRUSTM_PROVIDER_ERRFN("Specify key curve\n"); - OPENSSL_clear_free(trustm_object_ctx, sizeof(trustm_object_ctx_t)); - return NULL; + goto error; } sscanf(opts[3],"%x", &(trustm_object_ctx->key_curve)); if (opts[4] == NULL) { TRUSTM_PROVIDER_ERRFN("Specify key usage\n"); - OPENSSL_clear_free(trustm_object_ctx, sizeof(trustm_object_ctx_t)); - return NULL; + goto error; } sscanf(opts[4], "%x", &(trustm_object_ctx->key_usage)); } @@ -194,6 +184,9 @@ static void *trustm_object_open(void *provctx, const char *uri) OPENSSL_free(baseuri); TRUSTM_PROVIDER_DBGFN("<"); return trustm_object_ctx; +error: + OPENSSL_clear_free(trustm_object_ctx, sizeof(trustm_object_ctx_t)); + return NULL; } static void *trustm_object_attach(void *provctx, OSSL_CORE_BIO *cin)