feat: define and use ossl_bio_print_hex

Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/25429)
This commit is contained in:
Jonathan M. Wilbur 2024-09-12 23:22:42 +00:00 committed by Tomas Mraz
parent 80b0a33b38
commit 1f7d2a2887
5 changed files with 58 additions and 43 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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;

View File

@ -0,0 +1,32 @@
=pod
=head1 NAME
ossl_bio_print_hex
- Print a hexdump of a binary input
=head1 SYNOPSIS
#include <crypto/x509.h>
int ossl_bio_print_hex(BIO *out, unsigned char *buf, int len);
=head1 DESCRIPTION
This function prints a hexdump-like hexadecimal output to I<out> from a binary
input I<buf> that is I<len> 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<https://www.openssl.org/source/license.html>.
=cut

View File

@ -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 */