testutil_init.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/opensslconf.h>
  11. #include <openssl/trace.h>
  12. #include "apps.h"
  13. #include "../testutil.h"
  14. #ifndef OPENSSL_NO_TRACE
  15. typedef struct tracedata_st {
  16. BIO *bio;
  17. unsigned int ingroup:1;
  18. } tracedata;
  19. static size_t internal_trace_cb(const char *buf, size_t cnt,
  20. int category, int cmd, void *vdata)
  21. {
  22. int ret = 0;
  23. tracedata *trace_data = vdata;
  24. char buffer[256], *hex;
  25. CRYPTO_THREAD_ID tid;
  26. switch (cmd) {
  27. case OSSL_TRACE_CTRL_BEGIN:
  28. trace_data->ingroup = 1;
  29. tid = CRYPTO_THREAD_get_current_id();
  30. hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
  31. BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ",
  32. hex, OSSL_trace_get_category_name(category));
  33. OPENSSL_free(hex);
  34. BIO_set_prefix(trace_data->bio, buffer);
  35. break;
  36. case OSSL_TRACE_CTRL_WRITE:
  37. ret = BIO_write(trace_data->bio, buf, cnt);
  38. break;
  39. case OSSL_TRACE_CTRL_END:
  40. trace_data->ingroup = 0;
  41. BIO_set_prefix(trace_data->bio, NULL);
  42. break;
  43. }
  44. return ret < 0 ? 0 : ret;
  45. }
  46. DEFINE_STACK_OF(tracedata)
  47. static STACK_OF(tracedata) *trace_data_stack;
  48. static void tracedata_free(tracedata *data)
  49. {
  50. BIO_free_all(data->bio);
  51. OPENSSL_free(data);
  52. }
  53. static STACK_OF(tracedata) *trace_data_stack;
  54. static void cleanup_trace(void)
  55. {
  56. sk_tracedata_pop_free(trace_data_stack, tracedata_free);
  57. }
  58. static void setup_trace_category(int category)
  59. {
  60. BIO *channel;
  61. tracedata *trace_data;
  62. if (OSSL_trace_enabled(category))
  63. return;
  64. channel = BIO_push(BIO_new(BIO_f_prefix()),
  65. BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT));
  66. trace_data = OPENSSL_zalloc(sizeof(*trace_data));
  67. if (trace_data == NULL
  68. || (trace_data->bio = channel) == NULL
  69. || OSSL_trace_set_callback(category, internal_trace_cb,
  70. trace_data) == 0
  71. || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
  72. fprintf(stderr,
  73. "warning: unable to setup trace callback for category '%s'.\n",
  74. OSSL_trace_get_category_name(category));
  75. OSSL_trace_set_callback(category, NULL, NULL);
  76. BIO_free_all(channel);
  77. }
  78. }
  79. static void setup_trace(const char *str)
  80. {
  81. char *val;
  82. /*
  83. * We add this handler as early as possible to ensure it's executed
  84. * as late as possible, i.e. after the TRACE code has done its cleanup
  85. * (which happens last in OPENSSL_cleanup).
  86. */
  87. atexit(cleanup_trace);
  88. trace_data_stack = sk_tracedata_new_null();
  89. val = OPENSSL_strdup(str);
  90. if (val != NULL) {
  91. char *valp = val;
  92. char *item;
  93. for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
  94. int category = OSSL_trace_get_category_num(item);
  95. if (category == OSSL_TRACE_CATEGORY_ALL) {
  96. while (++category < OSSL_TRACE_CATEGORY_NUM)
  97. setup_trace_category(category);
  98. break;
  99. } else if (category > 0) {
  100. setup_trace_category(category);
  101. } else {
  102. fprintf(stderr,
  103. "warning: unknown trace category: '%s'.\n", item);
  104. }
  105. }
  106. }
  107. OPENSSL_free(val);
  108. }
  109. #endif /* OPENSSL_NO_TRACE */
  110. int global_init(void)
  111. {
  112. #ifndef OPENSSL_NO_TRACE
  113. setup_trace(getenv("OPENSSL_TRACE"));
  114. #endif
  115. return 1;
  116. }