Skip to content

Commit

Permalink
Refactor header attribute string functions to use context object
Browse files Browse the repository at this point in the history
This allows all header attribute string functions to consider the
available optional blocks when determining the description.
  • Loading branch information
leonlynch committed Dec 1, 2023
1 parent 2f7ab32 commit fa3f94d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/tr31-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
28 changes: 20 additions & 8 deletions src/tr31_strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,27 @@ 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";
} else {
// 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";
Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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;

Expand Down
41 changes: 26 additions & 15 deletions src/tr31_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,48 +42,59 @@ 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.
*/
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.
*/
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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fa3f94d

Please sign in to comment.