diff mbox series

[3/8] libcommon: add bin2hex and tmp2_get_hexname

Message ID 20240802202606.12767-4-James.Bottomley@HansenPartnership.com (mailing list archive)
State New
Headers show
Series openssl_tpm2_engine: Add attestation functions for primary keys | expand

Commit Message

James Bottomley Aug. 2, 2024, 8:26 p.m. UTC
Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
---
 src/include/tpm2-common.h   |  5 +++++
 src/libcommon/tpm2-common.c | 16 ++++++++++++++++
 2 files changed, 21 insertions(+)

Comments

Jarkko Sakkinen Aug. 3, 2024, 5:21 p.m. UTC | #1
On Fri Aug 2, 2024 at 11:26 PM EEST, James Bottomley wrote:
> Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
> ---
>  src/include/tpm2-common.h   |  5 +++++
>  src/libcommon/tpm2-common.c | 16 ++++++++++++++++
>  2 files changed, 21 insertions(+)

Would not hurt to introduce them in the commit message.

>
> diff --git a/src/include/tpm2-common.h b/src/include/tpm2-common.h
> index 97b60f2..0e0f28a 100644
> --- a/src/include/tpm2-common.h
> +++ b/src/include/tpm2-common.h
> @@ -9,6 +9,9 @@
>   * not a TPM error, so don't process the rc as one */
>  #define NOT_TPM_ERROR (0xffffffff)
>  
> +/* maximum space for a sha256 name in ascii */
> +#define MAX_HEXNAME 132
> +
>  extern TPM_ALG_ID name_alg;
>  
>  struct policy_command {
> @@ -141,4 +144,6 @@ int tpm2_rsa_decrypt(const struct app_data *ad, PUBLIC_KEY_RSA_2B *cipherText,
>  		     char *srk_auth);
>  int tpm2_rm_signed_policy(char *tpmkey, int rmnum);
>  int tpm2_get_signed_policy(char *tpmkey, STACK_OF(TSSAUTHPOLICY) **sk);
> +void bin2hex(char *dst, const unsigned char *src, size_t count);
> +void tpm2_get_hexname(char hexname[MAX_HEXNAME], TPM2B_PUBLIC *pub);
>  #endif
> diff --git a/src/libcommon/tpm2-common.c b/src/libcommon/tpm2-common.c
> index b70ac27..3ffa773 100644
> --- a/src/libcommon/tpm2-common.c
> +++ b/src/libcommon/tpm2-common.c
> @@ -2320,6 +2320,14 @@ int hex2bin(unsigned char *dst, const char *src, size_t count)
>  	return 0;
>  }
>  
> +void bin2hex(char *dst, const unsigned char *src, size_t count)
> +{
> +	int i;
> +
> +	for (i = 0; i < count; i++)
> +		sprintf(&dst[i<<1], "%02x", src[i]);
> +}
> +
>  TPM_RC tpm2_parse_policy_file(const char *policy_file,
>  			      STACK_OF(TSSOPTPOLICY) *sk,
>  			      char *auth, TPMT_HA *digest)
> @@ -3376,6 +3384,14 @@ openssl_print_errors()
>  	ERR_print_errors_fp(stderr);
>  }
>  
> +void tpm2_get_hexname(char hexname[MAX_HEXNAME], TPM2B_PUBLIC *pub)
> +{
> +	NAME_2B n;
> +
> +	tpm2_ObjectPublic_GetName(&n, &pub->publicArea);
> +	bin2hex(hexname, (unsigned char *)n.name, n.size);
> +}
> +
>  IMPLEMENT_ASN1_FUNCTIONS(TSSOPTPOLICY)
>  IMPLEMENT_ASN1_FUNCTIONS(TSSAUTHPOLICY)
>  IMPLEMENT_ASN1_FUNCTIONS(TSSLOADABLE)


BR, Jarkko
diff mbox series

Patch

diff --git a/src/include/tpm2-common.h b/src/include/tpm2-common.h
index 97b60f2..0e0f28a 100644
--- a/src/include/tpm2-common.h
+++ b/src/include/tpm2-common.h
@@ -9,6 +9,9 @@ 
  * not a TPM error, so don't process the rc as one */
 #define NOT_TPM_ERROR (0xffffffff)
 
+/* maximum space for a sha256 name in ascii */
+#define MAX_HEXNAME 132
+
 extern TPM_ALG_ID name_alg;
 
 struct policy_command {
@@ -141,4 +144,6 @@  int tpm2_rsa_decrypt(const struct app_data *ad, PUBLIC_KEY_RSA_2B *cipherText,
 		     char *srk_auth);
 int tpm2_rm_signed_policy(char *tpmkey, int rmnum);
 int tpm2_get_signed_policy(char *tpmkey, STACK_OF(TSSAUTHPOLICY) **sk);
+void bin2hex(char *dst, const unsigned char *src, size_t count);
+void tpm2_get_hexname(char hexname[MAX_HEXNAME], TPM2B_PUBLIC *pub);
 #endif
diff --git a/src/libcommon/tpm2-common.c b/src/libcommon/tpm2-common.c
index b70ac27..3ffa773 100644
--- a/src/libcommon/tpm2-common.c
+++ b/src/libcommon/tpm2-common.c
@@ -2320,6 +2320,14 @@  int hex2bin(unsigned char *dst, const char *src, size_t count)
 	return 0;
 }
 
+void bin2hex(char *dst, const unsigned char *src, size_t count)
+{
+	int i;
+
+	for (i = 0; i < count; i++)
+		sprintf(&dst[i<<1], "%02x", src[i]);
+}
+
 TPM_RC tpm2_parse_policy_file(const char *policy_file,
 			      STACK_OF(TSSOPTPOLICY) *sk,
 			      char *auth, TPMT_HA *digest)
@@ -3376,6 +3384,14 @@  openssl_print_errors()
 	ERR_print_errors_fp(stderr);
 }
 
+void tpm2_get_hexname(char hexname[MAX_HEXNAME], TPM2B_PUBLIC *pub)
+{
+	NAME_2B n;
+
+	tpm2_ObjectPublic_GetName(&n, &pub->publicArea);
+	bin2hex(hexname, (unsigned char *)n.name, n.size);
+}
+
 IMPLEMENT_ASN1_FUNCTIONS(TSSOPTPOLICY)
 IMPLEMENT_ASN1_FUNCTIONS(TSSAUTHPOLICY)
 IMPLEMENT_ASN1_FUNCTIONS(TSSLOADABLE)