testutil_init.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2017-2020 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. BIO *bio = NULL;
  63. if (OSSL_trace_enabled(category))
  64. return;
  65. bio = BIO_new(BIO_f_prefix());
  66. channel = BIO_push(bio,
  67. BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT));
  68. trace_data = OPENSSL_zalloc(sizeof(*trace_data));
  69. if (trace_data == NULL
  70. || bio == NULL
  71. || (trace_data->bio = channel) == NULL
  72. || OSSL_trace_set_callback(category, internal_trace_cb,
  73. trace_data) == 0
  74. || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
  75. fprintf(stderr,
  76. "warning: unable to setup trace callback for category '%s'.\n",
  77. OSSL_trace_get_category_name(category));
  78. OSSL_trace_set_callback(category, NULL, NULL);
  79. BIO_free_all(channel);
  80. }
  81. }
  82. static void setup_trace(const char *str)
  83. {
  84. char *val;
  85. /*
  86. * We add this handler as early as possible to ensure it's executed
  87. * as late as possible, i.e. after the TRACE code has done its cleanup
  88. * (which happens last in OPENSSL_cleanup).
  89. */
  90. atexit(cleanup_trace);
  91. trace_data_stack = sk_tracedata_new_null();
  92. val = OPENSSL_strdup(str);
  93. if (val != NULL) {
  94. char *valp = val;
  95. char *item;
  96. for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
  97. int category = OSSL_trace_get_category_num(item);
  98. if (category == OSSL_TRACE_CATEGORY_ALL) {
  99. while (++category < OSSL_TRACE_CATEGORY_NUM)
  100. setup_trace_category(category);
  101. break;
  102. } else if (category > 0) {
  103. setup_trace_category(category);
  104. } else {
  105. fprintf(stderr,
  106. "warning: unknown trace category: '%s'.\n", item);
  107. }
  108. }
  109. }
  110. OPENSSL_free(val);
  111. }
  112. #endif /* OPENSSL_NO_TRACE */
  113. int global_init(void)
  114. {
  115. #ifndef OPENSSL_NO_TRACE
  116. setup_trace(getenv("OPENSSL_TRACE"));
  117. #endif
  118. return 1;
  119. }