2016-05-18 03:38:09 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:39:21 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 03:38:09 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
2019-07-26 03:57:48 +08:00
|
|
|
/* TODO: When ERR_STATE becomes opaque, this musts be removed */
|
|
|
|
#define OSSL_FORCE_ERR_STATE
|
|
|
|
|
1998-12-21 18:52:47 +08:00
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/crypto.h>
|
|
|
|
#include <openssl/buffer.h>
|
|
|
|
#include <openssl/err.h>
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "err_local.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-02-15 21:57:32 +08:00
|
|
|
#define ERR_PRINT_BUF_SIZE 4096
|
2015-01-22 11:40:55 +08:00
|
|
|
void ERR_print_errors_cb(int (*cb) (const char *str, size_t len, void *u),
|
|
|
|
void *u)
|
|
|
|
{
|
2019-06-20 09:24:17 +08:00
|
|
|
CRYPTO_THREAD_ID tid = CRYPTO_THREAD_get_current_id();
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned long l;
|
2019-09-05 04:14:21 +08:00
|
|
|
const char *file, *data, *func;
|
2015-01-22 11:40:55 +08:00
|
|
|
int line, flags;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2019-09-05 04:14:21 +08:00
|
|
|
while ((l = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
|
2020-06-29 18:18:24 +08:00
|
|
|
char buf[ERR_PRINT_BUF_SIZE], *hex;
|
|
|
|
const char *lib, *reason = NULL;
|
|
|
|
char rsbuf[256];
|
|
|
|
unsigned long r = ERR_GET_REASON(l);
|
|
|
|
|
2019-09-05 04:14:21 +08:00
|
|
|
lib = ERR_lib_error_string(l);
|
2020-06-29 18:18:24 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ERR_reason_error_string() can't safely return system error strings,
|
|
|
|
* since it would call openssl_strerror_r(), which needs a buffer for
|
|
|
|
* thread safety. So for system errors, we call openssl_strerror_r()
|
|
|
|
* directly instead.
|
|
|
|
*/
|
|
|
|
if (ERR_SYSTEM_ERROR(l)) {
|
|
|
|
if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf)))
|
|
|
|
reason = rsbuf;
|
|
|
|
} else {
|
|
|
|
reason = ERR_reason_error_string(l);
|
|
|
|
}
|
|
|
|
|
2019-09-05 04:14:21 +08:00
|
|
|
if (func == NULL)
|
|
|
|
func = "unknown function";
|
2020-06-29 18:18:24 +08:00
|
|
|
if (reason == NULL) {
|
|
|
|
BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
|
|
|
|
reason = rsbuf;
|
|
|
|
}
|
2019-09-05 04:14:21 +08:00
|
|
|
if ((flags & ERR_TXT_STRING) == 0)
|
|
|
|
data = "";
|
2020-05-26 10:44:36 +08:00
|
|
|
hex = openssl_buf2hexstr_sep((const unsigned char *)&tid, sizeof(tid),
|
|
|
|
'\0');
|
|
|
|
BIO_snprintf(buf, sizeof(buf), "%s:error::%s:%s:%s:%s:%d:%s\n",
|
2019-11-27 01:15:20 +08:00
|
|
|
hex == NULL ? "<null>" : hex, lib, func, reason, file,
|
|
|
|
line, data);
|
2019-06-20 09:24:17 +08:00
|
|
|
OPENSSL_free(hex);
|
2019-09-05 04:14:21 +08:00
|
|
|
if (cb(buf, strlen(buf), u) <= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
break; /* abort outputting the error report */
|
|
|
|
}
|
|
|
|
}
|
2001-06-23 23:06:17 +08:00
|
|
|
|
2020-02-15 21:57:32 +08:00
|
|
|
/* auxiliary function for incrementally reporting texts via the error queue */
|
|
|
|
static void put_error(int lib, const char *func, int reason,
|
|
|
|
const char *file, int line)
|
|
|
|
{
|
|
|
|
ERR_new();
|
|
|
|
ERR_set_debug(file, line, func);
|
|
|
|
ERR_set_error(lib, reason, NULL /* no data here, so fmt is NULL */);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TYPICAL_MAX_OUTPUT_BEFORE_DATA 100
|
|
|
|
#define MAX_DATA_LEN (ERR_PRINT_BUF_SIZE - TYPICAL_MAX_OUTPUT_BEFORE_DATA)
|
|
|
|
void ERR_add_error_txt(const char *separator, const char *txt)
|
|
|
|
{
|
|
|
|
const char *file = NULL;
|
|
|
|
int line;
|
|
|
|
const char *func = NULL;
|
|
|
|
const char *data = NULL;
|
|
|
|
int flags;
|
|
|
|
unsigned long err = ERR_peek_last_error();
|
|
|
|
|
|
|
|
if (separator == NULL)
|
|
|
|
separator = "";
|
|
|
|
if (err == 0)
|
2020-03-10 17:39:22 +08:00
|
|
|
put_error(ERR_LIB_NONE, NULL, 0, "", 0);
|
2020-02-15 21:57:32 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
size_t available_len, data_len;
|
|
|
|
const char *curr = txt, *next = txt;
|
|
|
|
const char *leading_separator = separator;
|
|
|
|
int trailing_separator = 0;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
ERR_peek_last_error_all(&file, &line, &func, &data, &flags);
|
|
|
|
if ((flags & ERR_TXT_STRING) == 0) {
|
|
|
|
data = "";
|
|
|
|
leading_separator = "";
|
|
|
|
}
|
|
|
|
data_len = strlen(data);
|
|
|
|
|
|
|
|
/* workaround for limit of ERR_print_errors_cb() */
|
|
|
|
if (data_len >= MAX_DATA_LEN
|
|
|
|
|| strlen(separator) >= (size_t)(MAX_DATA_LEN - data_len))
|
|
|
|
available_len = 0;
|
|
|
|
else
|
|
|
|
available_len = MAX_DATA_LEN - data_len - strlen(separator) - 1;
|
|
|
|
/* MAX_DATA_LEN > available_len >= 0 */
|
|
|
|
|
|
|
|
if (*separator == '\0') {
|
|
|
|
const size_t len_next = strlen(next);
|
|
|
|
|
|
|
|
if (len_next <= available_len) {
|
|
|
|
next += len_next;
|
|
|
|
curr = NULL; /* no need to split */
|
|
|
|
} else {
|
|
|
|
next += available_len;
|
|
|
|
curr = next; /* will split at this point */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (*next != '\0' && (size_t)(next - txt) <= available_len) {
|
|
|
|
curr = next;
|
|
|
|
next = strstr(curr, separator);
|
|
|
|
if (next != NULL) {
|
|
|
|
next += strlen(separator);
|
|
|
|
trailing_separator = *next == '\0';
|
|
|
|
} else {
|
|
|
|
next = curr + strlen(curr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((size_t)(next - txt) <= available_len)
|
|
|
|
curr = NULL; /* the above loop implies *next == '\0' */
|
|
|
|
}
|
|
|
|
if (curr != NULL) {
|
|
|
|
/* split error msg at curr since error data would get too long */
|
|
|
|
if (curr != txt) {
|
|
|
|
tmp = OPENSSL_strndup(txt, curr - txt);
|
|
|
|
if (tmp == NULL)
|
|
|
|
return;
|
|
|
|
ERR_add_error_data(2, separator, tmp);
|
|
|
|
OPENSSL_free(tmp);
|
|
|
|
}
|
2020-03-10 17:39:22 +08:00
|
|
|
put_error(ERR_GET_LIB(err), func, err, file, line);
|
2020-02-15 21:57:32 +08:00
|
|
|
txt = curr;
|
|
|
|
} else {
|
|
|
|
if (trailing_separator) {
|
|
|
|
tmp = OPENSSL_strndup(txt, next - strlen(separator) - txt);
|
|
|
|
if (tmp == NULL)
|
|
|
|
return;
|
|
|
|
/* output txt without the trailing separator */
|
|
|
|
ERR_add_error_data(2, leading_separator, tmp);
|
|
|
|
OPENSSL_free(tmp);
|
|
|
|
} else {
|
|
|
|
ERR_add_error_data(2, leading_separator, txt);
|
|
|
|
}
|
|
|
|
txt = next; /* finished */
|
|
|
|
}
|
|
|
|
} while (*txt != '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
void ERR_add_error_mem_bio(const char *separator, BIO *bio)
|
|
|
|
{
|
|
|
|
if (bio != NULL) {
|
|
|
|
char *str;
|
|
|
|
long len = BIO_get_mem_data(bio, &str);
|
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
if (str[len - 1] != '\0') {
|
|
|
|
if (BIO_write(bio, "", 1) <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
len = BIO_get_mem_data(bio, &str);
|
|
|
|
}
|
|
|
|
if (len > 1)
|
|
|
|
ERR_add_error_txt(separator, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-25 18:09:55 +08:00
|
|
|
static int print_bio(const char *str, size_t len, void *bp)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return BIO_write((BIO *)bp, str, len);
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
void ERR_print_errors(BIO *bp)
|
|
|
|
{
|
|
|
|
ERR_print_errors_cb(print_bio, bp);
|
|
|
|
}
|
2016-03-19 07:40:11 +08:00
|
|
|
|
|
|
|
#ifndef OPENSSL_NO_STDIO
|
|
|
|
void ERR_print_errors_fp(FILE *fp)
|
|
|
|
{
|
|
|
|
BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);
|
|
|
|
if (bio == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ERR_print_errors_cb(print_bio, bio);
|
|
|
|
BIO_free(bio);
|
|
|
|
}
|
|
|
|
#endif
|