Better error handling for SSL

This commit is contained in:
2022-05-06 18:25:15 -04:00
parent c3e4588afc
commit d6aa216505
4 changed files with 33 additions and 41 deletions

View File

@@ -61,46 +61,36 @@ const char *dummy_key =
"HcKc9a4WXhC7yu79e5BnKWltHXY=\n"
"-----END PRIVATE KEY-----\n";
std::string errors_string(bool lastonly) {
std::string err;
const char *file, *data;
int line, flags;
// const char *func;
std::string error_string() {
// Get the last code.
int code = 0;
while (true) {
// Newer versions of the SSL API support this.
// unsigned long code =
// ERR_get_error_all(&file, &line, &func, &data, &flags);
// Older versions of the SSL API support this.
unsigned long code =
ERR_get_error_line_data(&file, &line, &data, &flags);
if (code == 0) break;
std::string reason;
int icode = ERR_get_error();
if (icode == 0) break;
code = icode;
}
// Fetch and clear errno.
int terrno = errno;
errno = 0;
if (code != 0) {
const char *rc = ERR_reason_error_string(code);
if (rc != nullptr) {
reason = rc;
return rc;
} else {
reason = "sys:" + strerror_str(ERR_GET_REASON(code));
}
if (err.empty() || lastonly) {
err = reason;
} else {
err = err + ", " + reason;
}
if ((data != nullptr) && (data[0] != 0)) {
err = err + " " + data;
return strerror_str(ERR_GET_REASON(code));
}
} else if (terrno != 0) {
return strerror_str(terrno);
} else {
return "";
}
return err;
}
void assert_errors_empty() {
int code = ERR_peek_error();
if (code != 0) {
std::cerr << "SSL should not have errors at this point." << std::endl;
ERR_print_errors_fp(stderr);
exit(1);
}
void clear_all_errors() {
ERR_clear_error();
errno = 0;
}
SSL_CTX *new_context(int verify) {