init.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX,
  35. strlen(buffer), buffer);
  36. break;
  37. case OSSL_TRACE_CTRL_WRITE:
  38. ret = BIO_write(trace_data->bio, buf, cnt);
  39. break;
  40. case OSSL_TRACE_CTRL_END:
  41. trace_data->ingroup = 0;
  42. BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX, 0, NULL);
  43. break;
  44. }
  45. return ret < 0 ? 0 : ret;
  46. }
  47. DEFINE_STACK_OF(tracedata)
  48. static STACK_OF(tracedata) *trace_data_stack;
  49. static void tracedata_free(tracedata *data)
  50. {
  51. BIO_free_all(data->bio);
  52. OPENSSL_free(data);
  53. }
  54. static STACK_OF(tracedata) *trace_data_stack;
  55. static void cleanup_trace(void)
  56. {
  57. sk_tracedata_pop_free(trace_data_stack, tracedata_free);
  58. }
  59. static void setup_trace_category(int category)
  60. {
  61. BIO *channel;
  62. tracedata *trace_data;
  63. if (OSSL_trace_enabled(category))
  64. return;
  65. channel = BIO_push(BIO_new(apps_bf_prefix()),
  66. BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT));
  67. trace_data = OPENSSL_zalloc(sizeof(*trace_data));
  68. if (trace_data == NULL
  69. || (trace_data->bio = channel) == NULL
  70. || OSSL_trace_set_callback(category, internal_trace_cb,
  71. trace_data) == 0
  72. || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
  73. fprintf(stderr,
  74. "warning: unable to setup trace callback for category '%s'.\n",
  75. OSSL_trace_get_category_name(category));
  76. OSSL_trace_set_callback(category, NULL, NULL);
  77. BIO_free_all(channel);
  78. }
  79. }
  80. static void setup_trace(const char *str)
  81. {
  82. char *val;
  83. /*
  84. * We add this handler as early as possible to ensure it's executed
  85. * as late as possible, i.e. after the TRACE code has done its cleanup
  86. * (which happens last in OPENSSL_cleanup).
  87. */
  88. atexit(cleanup_trace);
  89. trace_data_stack = sk_tracedata_new_null();
  90. val = OPENSSL_strdup(str);
  91. if (val != NULL) {
  92. char *valp = val;
  93. char *item;
  94. for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
  95. int category = OSSL_trace_get_category_num(item);
  96. if (category == OSSL_TRACE_CATEGORY_ALL) {
  97. while (++category < OSSL_TRACE_CATEGORY_NUM)
  98. setup_trace_category(category);
  99. break;
  100. } else if (category > 0) {
  101. setup_trace_category(category);
  102. } else {
  103. fprintf(stderr,
  104. "warning: unknown trace category: '%s'.\n", item);
  105. }
  106. }
  107. }
  108. OPENSSL_free(val);
  109. }
  110. #endif /* OPENSSL_NO_TRACE */
  111. int global_init(void)
  112. {
  113. #ifndef OPENSSL_NO_TRACE
  114. setup_trace(getenv("OPENSSL_TRACE"));
  115. #endif
  116. return 1;
  117. }