APPS & TEST: Adapt to use the new BIO_f_prefix()
authorRichard Levitte <levitte@openssl.org>
Wed, 27 Nov 2019 15:13:12 +0000 (16:13 +0100)
committerRichard Levitte <levitte@openssl.org>
Wed, 18 Dec 2019 18:42:44 +0000 (19:42 +0100)
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/10531)

apps/include/apps.h
apps/lib/apps.c
apps/lib/bf_prefix.c [deleted file]
apps/lib/build.info
apps/openssl.c
test/build.info

index 21a2a905449135e9a0e40696f58f4fce099d66eb..c33a98772b94f579e1e9f5f49709c1749c7bb4da 100644 (file)
@@ -58,20 +58,6 @@ extern const unsigned char tls13_aes128gcmsha256_id[];
 extern const unsigned char tls13_aes256gcmsha384_id[];
 extern BIO_ADDR *ourpeer;
 
-BIO_METHOD *apps_bf_prefix(void);
-/*
- * The control used to set the prefix with BIO_ctrl()
- * We make it high enough so the chance of ever clashing with the BIO library
- * remains unlikely for the foreseeable future and beyond.
- */
-#define PREFIX_CTRL_SET_PREFIX  (1 << 15)
-/*
- * apps_bf_prefix() returns a dynamically created BIO_METHOD, which we
- * need to destroy at some point.  When created internally, it's stored
- * in an internal pointer which can be freed with the following function
- */
-void destroy_prefix_method(void);
-
 BIO *dup_bio_in(int format);
 BIO *dup_bio_out(int format);
 BIO *dup_bio_err(int format);
index 8b840bb2a1b37ceaaae697afbdd4ee61608518e7..3a18cd007c49a8fb7f04c94a66a02639005bcfb2 100644 (file)
@@ -2324,8 +2324,8 @@ BIO *dup_bio_out(int format)
 
     if (FMT_istext(format)
         && (prefix = getenv("HARNESS_OSSL_PREFIX")) != NULL) {
-        b = BIO_push(BIO_new(apps_bf_prefix()), b);
-        BIO_ctrl(b, PREFIX_CTRL_SET_PREFIX, 0, prefix);
+        b = BIO_push(BIO_new(BIO_f_prefix()), b);
+        BIO_set_prefix(b, prefix);
     }
 
     return b;
@@ -2342,17 +2342,6 @@ BIO *dup_bio_err(int format)
     return b;
 }
 
-/*
- * Because the prefix method is created dynamically, we must also be able
- * to destroy it.
- */
-void destroy_prefix_method(void)
-{
-    BIO_METHOD *prefix_method = apps_bf_prefix();
-    BIO_meth_free(prefix_method);
-    prefix_method = NULL;
-}
-
 void unbuffer(FILE *fp)
 {
 /*
diff --git a/apps/lib/bf_prefix.c b/apps/lib/bf_prefix.c
deleted file mode 100644 (file)
index 8cedca9..0000000
+++ /dev/null
@@ -1,177 +0,0 @@
-/*
- * Copyright 2018 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
- * https://www.openssl.org/source/license.html
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#include <openssl/bio.h>
-#include "apps.h"
-
-static int prefix_write(BIO *b, const char *out, size_t outl,
-                        size_t *numwritten);
-static int prefix_read(BIO *b, char *buf, size_t size, size_t *numread);
-static int prefix_puts(BIO *b, const char *str);
-static int prefix_gets(BIO *b, char *str, int size);
-static long prefix_ctrl(BIO *b, int cmd, long arg1, void *arg2);
-static int prefix_create(BIO *b);
-static int prefix_destroy(BIO *b);
-static long prefix_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
-
-static BIO_METHOD *prefix_meth = NULL;
-
-BIO_METHOD *apps_bf_prefix(void)
-{
-    if (prefix_meth == NULL) {
-        if ((prefix_meth =
-             BIO_meth_new(BIO_TYPE_FILTER, "Prefix filter")) == NULL
-            || !BIO_meth_set_create(prefix_meth, prefix_create)
-            || !BIO_meth_set_destroy(prefix_meth, prefix_destroy)
-            || !BIO_meth_set_write_ex(prefix_meth, prefix_write)
-            || !BIO_meth_set_read_ex(prefix_meth, prefix_read)
-            || !BIO_meth_set_puts(prefix_meth, prefix_puts)
-            || !BIO_meth_set_gets(prefix_meth, prefix_gets)
-            || !BIO_meth_set_ctrl(prefix_meth, prefix_ctrl)
-            || !BIO_meth_set_callback_ctrl(prefix_meth, prefix_callback_ctrl)) {
-            BIO_meth_free(prefix_meth);
-            prefix_meth = NULL;
-        }
-    }
-    return prefix_meth;
-}
-
-typedef struct prefix_ctx_st {
-    char *prefix;
-    int linestart;               /* flag to indicate we're at the line start */
-} PREFIX_CTX;
-
-static int prefix_create(BIO *b)
-{
-    PREFIX_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
-
-    if (ctx == NULL)
-        return 0;
-
-    ctx->prefix = NULL;
-    ctx->linestart = 1;
-    BIO_set_data(b, ctx);
-    BIO_set_init(b, 1);
-    return 1;
-}
-
-static int prefix_destroy(BIO *b)
-{
-    PREFIX_CTX *ctx = BIO_get_data(b);
-
-    OPENSSL_free(ctx->prefix);
-    OPENSSL_free(ctx);
-    return 1;
-}
-
-static int prefix_read(BIO *b, char *in, size_t size, size_t *numread)
-{
-    return BIO_read_ex(BIO_next(b), in, size, numread);
-}
-
-static int prefix_write(BIO *b, const char *out, size_t outl,
-                        size_t *numwritten)
-{
-    PREFIX_CTX *ctx = BIO_get_data(b);
-
-    if (ctx == NULL)
-        return 0;
-
-    /* If no prefix is set or if it's empty, we've got nothing to do here */
-    if (ctx->prefix == NULL || *ctx->prefix == '\0') {
-        /* We do note if what comes next will be a new line, though */
-        if (outl > 0)
-            ctx->linestart = (out[outl-1] == '\n');
-        return BIO_write_ex(BIO_next(b), out, outl, numwritten);
-    }
-
-    *numwritten = 0;
-
-    while (outl > 0) {
-        size_t i;
-        char c;
-
-        /* If we know that we're at the start of the line, output the prefix */
-        if (ctx->linestart) {
-            size_t dontcare;
-
-            if (!BIO_write_ex(BIO_next(b), ctx->prefix, strlen(ctx->prefix),
-                              &dontcare))
-                return 0;
-            ctx->linestart = 0;
-        }
-
-        /* Now, go look for the next LF, or the end of the string */
-        for (i = 0, c = '\0'; i < outl && (c = out[i]) != '\n'; i++)
-            continue;
-        if (c == '\n')
-            i++;
-
-        /* Output what we found so far */
-        while (i > 0) {
-            size_t num = 0;
-
-            if (!BIO_write_ex(BIO_next(b), out, i, &num))
-                return 0;
-            out += num;
-            outl -= num;
-            *numwritten += num;
-            i -= num;
-        }
-
-        /* If we found a LF, what follows is a new line, so take note */
-        if (c == '\n')
-            ctx->linestart = 1;
-    }
-
-    return 1;
-}
-
-static long prefix_ctrl(BIO *b, int cmd, long num, void *ptr)
-{
-    long ret = 0;
-
-    switch (cmd) {
-    case PREFIX_CTRL_SET_PREFIX:
-        {
-            PREFIX_CTX *ctx = BIO_get_data(b);
-
-            if (ctx == NULL)
-                break;
-
-            OPENSSL_free(ctx->prefix);
-            ctx->prefix = OPENSSL_strdup((const char *)ptr);
-            ret = ctx->prefix != NULL;
-        }
-        break;
-    default:
-        if (BIO_next(b) != NULL)
-            ret = BIO_ctrl(BIO_next(b), cmd, num, ptr);
-        break;
-    }
-    return ret;
-}
-
-static long prefix_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
-{
-    return BIO_callback_ctrl(BIO_next(b), cmd, fp);
-}
-
-static int prefix_gets(BIO *b, char *buf, int size)
-{
-    return BIO_gets(BIO_next(b), buf, size);
-}
-
-static int prefix_puts(BIO *b, const char *str)
-{
-    return BIO_write(b, str, strlen(str));
-}
index 7a2536af2453be927325d4632f2c5692e9e1f952..3f68a2ed354a973f54029096749451c8941c35ad 100644 (file)
@@ -9,7 +9,7 @@ ENDIF
 
 # Source for libapps
 $LIBAPPSSRC=apps.c apps_ui.c opt.c fmt.c s_cb.c s_socket.c app_rand.c \
-        bf_prefix.c columns.c app_params.c names.c
+        columns.c app_params.c names.c
 
 IF[{- !$disabled{apps} -}]
   LIBS{noinst}=../libapps.a
index d60267d742f9382f8429d133ea381cc0ad0bec7e..00ad9ca0bdcddbb44122a68d94071dbfc1624943 100644 (file)
@@ -116,8 +116,7 @@ static size_t internal_trace_cb(const char *buf, size_t cnt,
                      hex == NULL ? "<null>" : hex,
                      OSSL_trace_get_category_name(category));
         OPENSSL_free(hex);
-        BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX,
-                 strlen(buffer), buffer);
+        BIO_set_prefix(trace_data->bio, buffer);
         break;
     case OSSL_TRACE_CTRL_WRITE:
         if (!ossl_assert(trace_data->ingroup))
@@ -130,7 +129,7 @@ static size_t internal_trace_cb(const char *buf, size_t cnt,
             return 0;
         trace_data->ingroup = 0;
 
-        BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX, 0, NULL);
+        BIO_set_prefix(trace_data->bio, NULL);
 
         break;
     }
@@ -162,8 +161,7 @@ static void setup_trace_category(int category)
     if (OSSL_trace_enabled(category))
         return;
 
-    channel = BIO_push(BIO_new(apps_bf_prefix()),
-                       dup_bio_err(FORMAT_TEXT));
+    channel = BIO_push(BIO_new(BIO_f_prefix()), dup_bio_err(FORMAT_TEXT));
     trace_data = OPENSSL_zalloc(sizeof(*trace_data));
 
     if (trace_data == NULL
@@ -247,13 +245,6 @@ int main(int argc, char *argv[])
     win32_utf8argv(&argc, &argv);
 #endif
 
-    /*
-     * We use the prefix method to get the trace output we want.  Since some
-     * trace outputs happen with OPENSSL_cleanup(), which is run automatically
-     * after exit(), we need to destroy the prefix method as late as possible.
-     */
-    atexit(destroy_prefix_method);
-
 #ifndef OPENSSL_NO_TRACE
     setup_trace(getenv("OPENSSL_TRACE"));
 #endif
index 0be3ee078c7b86e61cf4e2f1cffedec7abeadfa0..fc7007ed934ae28975760ec6692ccf11c681d687 100644 (file)
@@ -14,7 +14,7 @@ ENDIF
 IF[{- $config{target} =~ /^vms-/ -}]
   $AUXLIBAPPSSRC=../apps/lib/vms_term_sock.c ../apps/lib/vms_decc_argv.c
 ENDIF
-$LIBAPPSSRC=../apps/lib/opt.c ../apps/lib/bf_prefix.c $AUXLIBAPPSSRC
+$LIBAPPSSRC=../apps/lib/opt.c $AUXLIBAPPSSRC
 
 IF[{- !$disabled{tests} -}]
   LIBS{noinst,has_main}=libtestutil.a