diff --git a/src/tr31-tool.c b/src/tr31-tool.c index d0d1579..2d195f0 100644 --- a/src/tr31-tool.c +++ b/src/tr31-tool.c @@ -791,7 +791,7 @@ static int do_tr31_import(const struct tr31_tool_options_t* options) ); printf("Key algorithm: [%c] %s\n", tr31_ctx.key.algorithm, - tr31_key_algorithm_get_desc(&tr31_ctx, tr31_ctx.key.algorithm) + tr31_key_algorithm_get_desc(&tr31_ctx) ); printf("Key mode of use: [%c] %s\n", tr31_ctx.key.mode_of_use, @@ -804,11 +804,11 @@ static int do_tr31_import(const struct tr31_tool_options_t* options) } printf("Key exportability: [%c] %s\n", tr31_ctx.key.exportability, - tr31_key_exportability_get_desc(tr31_ctx.key.exportability) + tr31_key_exportability_get_desc(&tr31_ctx) ); printf("Key context: [%c] %s\n", tr31_ctx.key.key_context, - tr31_key_context_get_desc(tr31_ctx.key.key_context) + tr31_key_context_get_desc(&tr31_ctx) ); // print optional blocks, if available diff --git a/src/tr31_strings.c b/src/tr31_strings.c index 81e9f8e..0bfad20 100644 --- a/src/tr31_strings.c +++ b/src/tr31_strings.c @@ -164,15 +164,19 @@ const char* tr31_key_usage_get_desc(const struct tr31_ctx_t* ctx) return "Unknown key usage value"; } -const char* tr31_key_algorithm_get_desc(const struct tr31_ctx_t* ctx, unsigned int algorithm) +const char* tr31_key_algorithm_get_desc(const struct tr31_ctx_t* ctx) { + if (!ctx) { + return NULL; + } + // See ANSI X9.143:2021, 6.3.2, table 3 // See ISO 20038:2017, Annex A.2.4, table A.4 - switch (algorithm) { + switch (ctx->key.algorithm) { case TR31_KEY_ALGORITHM_AES: return "AES"; case TR31_KEY_ALGORITHM_DES: return "DES"; case TR31_KEY_ALGORITHM_EC: return "Elliptic Curve"; - case TR31_KEY_ALGORITHM_HMAC: + case TR31_KEY_ALGORITHM_HMAC: { if (tr31_opt_block_find((struct tr31_ctx_t*)ctx, TR31_OPT_BLOCK_HM)) { // ANSI X9.143 requires optional block HM for key algorithm HMAC return "HMAC"; @@ -180,6 +184,7 @@ const char* tr31_key_algorithm_get_desc(const struct tr31_ctx_t* ctx, unsigned i // ISO 20038 associates the HMAC digest to the key algorithm return "HMAC-SHA-1 (ISO 20038)"; } + } case TR31_KEY_ALGORITHM_HMAC_SHA2: return "HMAC-SHA-2 (ISO 20038)"; case TR31_KEY_ALGORITHM_HMAC_SHA3: return "HMAC-SHA-3 (ISO 20038)"; case TR31_KEY_ALGORITHM_RSA: return "RSA"; @@ -220,10 +225,14 @@ const char* tr31_key_mode_of_use_get_desc(const struct tr31_ctx_t* ctx) return "Unknown key mode of use value"; } -const char* tr31_key_exportability_get_desc(unsigned int exportability) +const char* tr31_key_exportability_get_desc(const struct tr31_ctx_t* ctx) { + if (!ctx) { + return NULL; + } + // See ANSI X9.143:2021, 6.3.5, table 6 - switch (exportability) { + switch (ctx->key.exportability) { case TR31_KEY_EXPORT_TRUSTED: return "Exportable in a trusted key block only"; case TR31_KEY_EXPORT_NONE: return "Not exportable"; case TR31_KEY_EXPORT_SENSITIVE: return "Sensitive"; @@ -232,10 +241,14 @@ const char* tr31_key_exportability_get_desc(unsigned int exportability) return "Unknown key exportability value"; } -const char* tr31_key_context_get_desc(unsigned int key_context) +const char* tr31_key_context_get_desc(const struct tr31_ctx_t* ctx) { + if (!ctx) { + return NULL; + } + // See ANSI X9.143:2021, 6.2, table 1 - switch (key_context) { + switch (ctx->key.key_context) { case TR31_KEY_CONTEXT_NONE: return "Determined by wrapping key"; case TR31_KEY_CONTEXT_STORAGE: return "Storage context only"; case TR31_KEY_CONTEXT_EXCHANGE: return "Key exchange context only"; @@ -623,7 +636,6 @@ static int tr31_opt_block_iso8601_get_string(const struct tr31_opt_ctx_t* opt_bl static const char* tr31_opt_block_wrapping_pedigree_get_string(const struct tr31_opt_ctx_t* opt_block) { - int r; struct tr31_opt_blk_wp_data_t wp_data; diff --git a/src/tr31_strings.h b/src/tr31_strings.h index 39f70bf..e7dc57e 100644 --- a/src/tr31_strings.h +++ b/src/tr31_strings.h @@ -42,7 +42,10 @@ struct tr31_opt_ctx_t; const char* tr31_key_usage_get_ascii(unsigned int usage, char* ascii, size_t ascii_len); /** - * Retrieve human readable description associated with key usage + * Retrieve human readable description associated with key usage. + * + * This function may consider the available optional blocks when determining + * the description. * * @param ctx TR-31 context object * @return Pointer to null-terminated string. Do not free. @@ -50,19 +53,21 @@ const char* tr31_key_usage_get_ascii(unsigned int usage, char* ascii, size_t asc const char* tr31_key_usage_get_desc(const struct tr31_ctx_t* ctx); /** - * Retrieve human readable description associated with key algorithm value. + * Retrieve human readable description associated with key algorithm. * - * If the TR-31 context object is provided, this function may consider - * the optional blocks when determining the description. + * This function may consider the available optional blocks when determining + * the description. * - * @param ctx TR-31 context object. Optional and may be NULL. - * @param algorithm Key algorithm value + * @param ctx TR-31 context object * @return Pointer to null-terminated string. Do not free. */ -const char* tr31_key_algorithm_get_desc(const struct tr31_ctx_t* ctx, unsigned int algorithm); +const char* tr31_key_algorithm_get_desc(const struct tr31_ctx_t* ctx); /** - * Retrieve human readable description associated with key mode of use + * Retrieve human readable description associated with key mode of use. + * + * This function may consider the available optional blocks when determining + * the description. * * @param ctx TR-31 context object * @return Pointer to null-terminated string. Do not free. @@ -70,20 +75,26 @@ const char* tr31_key_algorithm_get_desc(const struct tr31_ctx_t* ctx, unsigned i const char* tr31_key_mode_of_use_get_desc(const struct tr31_ctx_t* ctx); /** - * Retrieve human readable description associated with key exportability value + * Retrieve human readable description associated with key exportability. + * + * This function may consider the available optional blocks when determining + * the description. * - * @param exportability Key exportability value + * @param ctx TR-31 context object * @return Pointer to null-terminated string. Do not free. */ -const char* tr31_key_exportability_get_desc(unsigned int exportability); +const char* tr31_key_exportability_get_desc(const struct tr31_ctx_t* ctx); /** - * Retrieve human readable description associated with key context value + * Retrieve human readable description associated with key context. + * + * This function may consider the available optional blocks when determining + * the description. * - * @param key_context Key context value + * @param ctx TR-31 context object * @return Pointer to null-terminated string. Do not free. */ -const char* tr31_key_context_get_desc(unsigned int key_context); +const char* tr31_key_context_get_desc(const struct tr31_ctx_t* ctx); /** * Create ASCII string associated with optional block ID value @@ -113,7 +124,7 @@ const char* tr31_opt_block_id_get_desc(const struct tr31_opt_ctx_t* opt_block); * terminated) if no description is available for the optional block data or if * the optional block ID is unknown. * - * @param opt_block Optional block + * @param opt_block Optional block context object * @param str String buffer output * @param str_len Length of string buffer in bytes * @return Zero for success. Less than zero for internal error. Greater than zero for parse error. See @ref tr31_error_t