err_prn.c 5.8 KB

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