err_prn.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 1995-2021 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. #define OSSL_FORCE_ERR_STATE
  10. #include <stdio.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/crypto.h>
  13. #include <openssl/buffer.h>
  14. #include <openssl/err.h>
  15. #include "err_local.h"
  16. #define ERR_PRINT_BUF_SIZE 4096
  17. void ERR_print_errors_cb(int (*cb) (const char *str, size_t len, void *u),
  18. void *u)
  19. {
  20. CRYPTO_THREAD_ID tid = CRYPTO_THREAD_get_current_id();
  21. unsigned long l;
  22. const char *file, *data, *func;
  23. int line, flags;
  24. while ((l = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
  25. char buf[ERR_PRINT_BUF_SIZE] = "";
  26. char *hex = NULL;
  27. int offset;
  28. if ((flags & ERR_TXT_STRING) == 0)
  29. data = "";
  30. hex = ossl_buf2hexstr_sep((const unsigned char *)&tid, sizeof(tid), '\0');
  31. BIO_snprintf(buf, sizeof(buf), "%s:", hex == NULL ? "<null>" : hex);
  32. offset = strlen(buf);
  33. ossl_err_string_int(l, func, buf + offset, sizeof(buf) - offset);
  34. offset += strlen(buf + offset);
  35. BIO_snprintf(buf + offset, sizeof(buf) - offset, ":%s:%d:%s\n",
  36. file, line, data);
  37. OPENSSL_free(hex);
  38. if (cb(buf, strlen(buf), u) <= 0)
  39. break; /* abort outputting the error report */
  40. }
  41. }
  42. /* auxiliary function for incrementally reporting texts via the error queue */
  43. static void put_error(int lib, const char *func, int reason,
  44. const char *file, int line)
  45. {
  46. ERR_new();
  47. ERR_set_debug(file, line, func);
  48. ERR_set_error(lib, reason, NULL /* no data here, so fmt is NULL */);
  49. }
  50. #define TYPICAL_MAX_OUTPUT_BEFORE_DATA 100
  51. #define MAX_DATA_LEN (ERR_PRINT_BUF_SIZE - TYPICAL_MAX_OUTPUT_BEFORE_DATA)
  52. void ERR_add_error_txt(const char *separator, const char *txt)
  53. {
  54. const char *file = NULL;
  55. int line;
  56. const char *func = NULL;
  57. const char *data = NULL;
  58. int flags;
  59. unsigned long err = ERR_peek_last_error();
  60. if (separator == NULL)
  61. separator = "";
  62. if (err == 0)
  63. put_error(ERR_LIB_NONE, NULL, 0, "", 0);
  64. do {
  65. size_t available_len, data_len;
  66. const char *curr = txt, *next = txt;
  67. const char *leading_separator = separator;
  68. int trailing_separator = 0;
  69. char *tmp;
  70. ERR_peek_last_error_all(&file, &line, &func, &data, &flags);
  71. if ((flags & ERR_TXT_STRING) == 0) {
  72. data = "";
  73. leading_separator = "";
  74. }
  75. data_len = strlen(data);
  76. /* workaround for limit of ERR_print_errors_cb() */
  77. if (data_len >= MAX_DATA_LEN
  78. || strlen(separator) >= (size_t)(MAX_DATA_LEN - data_len))
  79. available_len = 0;
  80. else
  81. available_len = MAX_DATA_LEN - data_len - strlen(separator) - 1;
  82. /* MAX_DATA_LEN > available_len >= 0 */
  83. if (*separator == '\0') {
  84. const size_t len_next = strlen(next);
  85. if (len_next <= available_len) {
  86. next += len_next;
  87. curr = NULL; /* no need to split */
  88. } else {
  89. next += available_len;
  90. curr = next; /* will split at this point */
  91. }
  92. } else {
  93. while (*next != '\0' && (size_t)(next - txt) <= available_len) {
  94. curr = next;
  95. next = strstr(curr, separator);
  96. if (next != NULL) {
  97. next += strlen(separator);
  98. trailing_separator = *next == '\0';
  99. } else {
  100. next = curr + strlen(curr);
  101. }
  102. }
  103. if ((size_t)(next - txt) <= available_len)
  104. curr = NULL; /* the above loop implies *next == '\0' */
  105. }
  106. if (curr != NULL) {
  107. /* split error msg at curr since error data would get too long */
  108. if (curr != txt) {
  109. tmp = OPENSSL_strndup(txt, curr - txt);
  110. if (tmp == NULL)
  111. return;
  112. ERR_add_error_data(2, separator, tmp);
  113. OPENSSL_free(tmp);
  114. }
  115. put_error(ERR_GET_LIB(err), func, err, file, line);
  116. txt = curr;
  117. } else {
  118. if (trailing_separator) {
  119. tmp = OPENSSL_strndup(txt, next - strlen(separator) - txt);
  120. if (tmp == NULL)
  121. return;
  122. /* output txt without the trailing separator */
  123. ERR_add_error_data(2, leading_separator, tmp);
  124. OPENSSL_free(tmp);
  125. } else {
  126. ERR_add_error_data(2, leading_separator, txt);
  127. }
  128. txt = next; /* finished */
  129. }
  130. } while (*txt != '\0');
  131. }
  132. void ERR_add_error_mem_bio(const char *separator, BIO *bio)
  133. {
  134. if (bio != NULL) {
  135. char *str;
  136. long len = BIO_get_mem_data(bio, &str);
  137. if (len > 0) {
  138. if (str[len - 1] != '\0') {
  139. if (BIO_write(bio, "", 1) <= 0)
  140. return;
  141. len = BIO_get_mem_data(bio, &str);
  142. }
  143. if (len > 1)
  144. ERR_add_error_txt(separator, str);
  145. }
  146. }
  147. }
  148. static int print_bio(const char *str, size_t len, void *bp)
  149. {
  150. return BIO_write((BIO *)bp, str, len);
  151. }
  152. void ERR_print_errors(BIO *bp)
  153. {
  154. ERR_print_errors_cb(print_bio, bp);
  155. }
  156. #ifndef OPENSSL_NO_STDIO
  157. void ERR_print_errors_fp(FILE *fp)
  158. {
  159. BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);
  160. if (bio == NULL)
  161. return;
  162. ERR_print_errors_cb(print_bio, bio);
  163. BIO_free(bio);
  164. }
  165. #endif