From f799f30eb9dbbfd59ff6c3f0a13a47294dc286a0 Mon Sep 17 00:00:00 2001 From: jsing Date: Thu, 10 Nov 2022 13:09:34 +0000 Subject: [PATCH] Port ASN1_buf_print() from OpenSSL 1.1. This is needed to print byte array based keys, such as Ed25519 and X25519. ok beck@ tb@ --- lib/libcrypto/asn1/asn1.h | 5 ++++- lib/libcrypto/asn1/t_pkey.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/libcrypto/asn1/asn1.h b/lib/libcrypto/asn1/asn1.h index ff42e45676d..24ba6a66685 100644 --- a/lib/libcrypto/asn1/asn1.h +++ b/lib/libcrypto/asn1/asn1.h @@ -1,4 +1,4 @@ -/* $OpenBSD: asn1.h,v 1.70 2022/09/11 17:22:52 tb Exp $ */ +/* $OpenBSD: asn1.h,v 1.71 2022/11/10 13:09:34 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -838,6 +838,9 @@ int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v); int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags); int ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num, unsigned char *buf, int off); +#if defined(LIBRESSL_NEXT_API) || defined(LIBRESSL_INTERNAL) +int ASN1_buf_print(BIO *bp, const unsigned char *buf, size_t buflen, int indent); +#endif int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent); int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent, int dump); #endif diff --git a/lib/libcrypto/asn1/t_pkey.c b/lib/libcrypto/asn1/t_pkey.c index a3073812313..d1f77219ea2 100644 --- a/lib/libcrypto/asn1/t_pkey.c +++ b/lib/libcrypto/asn1/t_pkey.c @@ -1,4 +1,4 @@ -/* $OpenBSD: t_pkey.c,v 1.17 2021/12/04 16:08:32 tb Exp $ */ +/* $OpenBSD: t_pkey.c,v 1.18 2022/11/10 13:09:34 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -114,3 +114,32 @@ ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num, } return (1); } + +#define ASN1_BUF_PRINT_WIDTH 15 +#define ASN1_BUF_PRINT_MAX_INDENT 64 + +int +ASN1_buf_print(BIO *bp, const unsigned char *buf, size_t buflen, int indent) +{ + size_t i; + + for (i = 0; i < buflen; i++) { + if ((i % ASN1_BUF_PRINT_WIDTH) == 0) { + if (i > 0 && BIO_puts(bp, "\n") <= 0) + return 0; + if (!BIO_indent(bp, indent, ASN1_BUF_PRINT_MAX_INDENT)) + return 0; + } + /* + * Use colon separators for each octet for compatibility as + * this function is used to print out key components. + */ + if (BIO_printf(bp, "%02x%s", buf[i], + (i == buflen - 1) ? "" : ":") <= 0) + return 0; + } + if (BIO_write(bp, "\n", 1) <= 0) + return 0; + + return 1; +} -- 2.20.1