]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
feat: define and use ossl_bio_print_hex
authorJonathan M. Wilbur <jonathan@wilbur.space>
Thu, 12 Sep 2024 23:22:42 +0000 (23:22 +0000)
committerTomas Mraz <tomas@openssl.org>
Wed, 13 Nov 2024 10:53:34 +0000 (11:53 +0100)
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/25429)

crypto/x509/v3_attrdesc.c
crypto/x509/v3_utl.c
crypto/x509/x_attrib.c
doc/internal/man3/ossl_bio_print_hex.pod [new file with mode: 0644]
include/crypto/x509.h

index 85509fc402ef4adc0b55fb854659fcc5bb53a5f2..45958e9affdc708e19568aa8e4a6e45b53938d6a 100644 (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);
index 40eef9eb3177173d2a43b31d591991afbe47af17..60aa31a7c7587c0e30c42ee331746405607b1579 100644 (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;
+}
index 310bef258053e5a72b2c22921a5afe1f92567d28..0bae13dd7377f23fd2868fa4db2ac98fd0ee4dfc 100644 (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;
diff --git a/doc/internal/man3/ossl_bio_print_hex.pod b/doc/internal/man3/ossl_bio_print_hex.pod
new file mode 100644 (file)
index 0000000..6607e6d
--- /dev/null
@@ -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
index 616265f5b27f79fb22756f6e663950dc59f56c2e..e6ebae39145ac72a5aa46fdc47e210105f0c86fc 100644 (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 */