From 1f7d2a28877dffdaf607a6fdcc5fcc5f5e030b1e Mon Sep 17 00:00:00 2001 From: "Jonathan M. Wilbur" Date: Thu, 12 Sep 2024 23:22:42 +0000 Subject: [PATCH] feat: define and use ossl_bio_print_hex Reviewed-by: Neil Horman Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/25429) --- crypto/x509/v3_attrdesc.c | 24 +++--------------- crypto/x509/v3_utl.c | 17 +++++++++++++ crypto/x509/x_attrib.c | 27 ++++---------------- doc/internal/man3/ossl_bio_print_hex.pod | 32 ++++++++++++++++++++++++ include/crypto/x509.h | 1 + 5 files changed, 58 insertions(+), 43 deletions(-) create mode 100644 doc/internal/man3/ossl_bio_print_hex.pod diff --git a/crypto/x509/v3_attrdesc.c b/crypto/x509/v3_attrdesc.c index 85509fc402e..45958e9affd 100644 --- a/crypto/x509/v3_attrdesc.c +++ b/crypto/x509/v3_attrdesc.c @@ -46,24 +46,6 @@ IMPLEMENT_ASN1_FUNCTIONS(OSSL_INFO_SYNTAX_POINTER) IMPLEMENT_ASN1_FUNCTIONS(OSSL_PRIVILEGE_POLICY_ID) IMPLEMENT_ASN1_FUNCTIONS(OSSL_ATTRIBUTE_DESCRIPTOR) -/* Copied from x_attrib.c */ -static int print_hex(BIO *out, unsigned char *buf, int len) -{ - int result = 1; - char *hexbuf; - - if (len == 0) - return 1; - - hexbuf = OPENSSL_buf2hexstr(buf, len); - if (hexbuf == NULL) - return 0; - result = BIO_puts(out, hexbuf) > 0; - - OPENSSL_free(hexbuf); - return result; -} - static int i2r_HASH(X509V3_EXT_METHOD *method, OSSL_HASH *hash, BIO *out, int indent) @@ -85,7 +67,7 @@ static int i2r_HASH(X509V3_EXT_METHOD *method, } if (BIO_printf(out, "%*sHash Value: ", indent, "") <= 0) return 0; - return print_hex(out, hash->hashValue->data, hash->hashValue->length); + return ossl_bio_print_hex(out, hash->hashValue->data, hash->hashValue->length); } static int i2r_INFO_SYNTAX_POINTER(X509V3_EXT_METHOD *method, @@ -112,7 +94,7 @@ static int i2r_OSSL_INFO_SYNTAX(X509V3_EXT_METHOD *method, BIO *out, int indent) { switch (info->type) { - case (OSSL_INFO_SYNTAX_TYPE_CONTENT): + case OSSL_INFO_SYNTAX_TYPE_CONTENT: if (BIO_printf(out, "%*sContent: ", indent, "") <= 0) return 0; if (BIO_printf(out, "%.*s", info->choice.content->length, info->choice.content->data) <= 0) @@ -120,7 +102,7 @@ static int i2r_OSSL_INFO_SYNTAX(X509V3_EXT_METHOD *method, if (BIO_puts(out, "\n") <= 0) return 0; return 1; - case (OSSL_INFO_SYNTAX_TYPE_POINTER): + case OSSL_INFO_SYNTAX_TYPE_POINTER: if (BIO_printf(out, "%*sPointer:\n", indent, "") <= 0) return 0; return i2r_INFO_SYNTAX_POINTER(method, info->choice.pointer, out, indent + 4); diff --git a/crypto/x509/v3_utl.c b/crypto/x509/v3_utl.c index 40eef9eb317..60aa31a7c75 100644 --- a/crypto/x509/v3_utl.c +++ b/crypto/x509/v3_utl.c @@ -1434,3 +1434,20 @@ int OSSL_GENERAL_NAMES_print(BIO *out, GENERAL_NAMES *gens, int indent) } return 1; } + +int ossl_bio_print_hex(BIO *out, unsigned char *buf, int len) +{ + int result; + char *hexbuf; + + if (len == 0) + return 1; + + hexbuf = OPENSSL_buf2hexstr(buf, len); + if (hexbuf == NULL) + return 0; + result = BIO_puts(out, hexbuf) > 0; + + OPENSSL_free(hexbuf); + return result; +} diff --git a/crypto/x509/x_attrib.c b/crypto/x509/x_attrib.c index 310bef25805..0bae13dd737 100644 --- a/crypto/x509/x_attrib.c +++ b/crypto/x509/x_attrib.c @@ -58,23 +58,6 @@ X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value) return NULL; } -static int print_hex(BIO *out, unsigned char *buf, int len) -{ - int result = 1; - char *hexbuf; - - if (len == 0) - return 1; - - hexbuf = OPENSSL_buf2hexstr(buf, len); - if (hexbuf == NULL) - return 0; - result = BIO_puts(out, hexbuf) > 0; - - OPENSSL_free(hexbuf); - return result; -} - static int print_oid(BIO *out, const ASN1_OBJECT *oid) { const char *ln; char objbuf[80]; @@ -116,20 +99,20 @@ int ossl_print_attribute_value(BIO *out, return BIO_printf(out, "%lld", (long long int)int_val) > 0; } str = av->value.integer; - return print_hex(out, str->data, str->length); + return ossl_bio_print_hex(out, str->data, str->length); case V_ASN1_BIT_STRING: if (BIO_printf(out, "%*s", indent, "") < 0) return 0; - return print_hex(out, av->value.bit_string->data, - av->value.bit_string->length); + return ossl_bio_print_hex(out, av->value.bit_string->data, + av->value.bit_string->length); case V_ASN1_OCTET_STRING: case V_ASN1_VIDEOTEXSTRING: if (BIO_printf(out, "%*s", indent, "") < 0) return 0; - return print_hex(out, av->value.octet_string->data, - av->value.octet_string->length); + return ossl_bio_print_hex(out, av->value.octet_string->data, + av->value.octet_string->length); case V_ASN1_NULL: return BIO_printf(out, "%*sNULL", indent, "") >= 4; diff --git a/doc/internal/man3/ossl_bio_print_hex.pod b/doc/internal/man3/ossl_bio_print_hex.pod new file mode 100644 index 00000000000..6607e6d750d --- /dev/null +++ b/doc/internal/man3/ossl_bio_print_hex.pod @@ -0,0 +1,32 @@ +=pod + +=head1 NAME + +ossl_bio_print_hex +- Print a hexdump of a binary input + +=head1 SYNOPSIS + + #include + + int ossl_bio_print_hex(BIO *out, unsigned char *buf, int len); + +=head1 DESCRIPTION + +This function prints a hexdump-like hexadecimal output to I from a binary +input I that is I bytes in length. + +=head1 RETURN VALUES + +Returns 1 if it succeeds in printing, and 0 if it failed. + +=head1 COPYRIGHT + +Copyright 2024 The OpenSSL Project Authors. All Rights Reserved. + +Licensed under the Apache License 2.0 (the "License"). You may not use +this file except in compliance with the License. You can obtain a copy +in the file LICENSE in the source distribution or at +L. + +=cut diff --git a/include/crypto/x509.h b/include/crypto/x509.h index 616265f5b27..e6ebae39145 100644 --- a/include/crypto/x509.h +++ b/include/crypto/x509.h @@ -393,5 +393,6 @@ int ossl_print_attribute_value(BIO *out, int indent); int ossl_serial_number_print(BIO *out, const ASN1_INTEGER *bs, int indent); +int ossl_bio_print_hex(BIO *out, unsigned char *buf, int len); #endif /* OSSL_CRYPTO_X509_H */ -- 2.47.2