xref: /freebsd/crypto/openssh/ssherr-libcrypto.c (revision f9bc005b8ef3b507a5ecbc2d7fe4411ef70eff38)
1 /* $OpenBSD: ssherr-libcrypto.c,v 1.2 2026/06/30 03:42:53 tb Exp $ */
2 /*
3  * Copyright (c) 2026 Darren Tucker
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "includes.h"
19 
20 #include <sys/types.h>
21 
22 #include <errno.h>
23 #include <string.h>
24 
25 #include "log.h"
26 
27 #ifdef WITH_OPENSSL
28 #include <openssl/err.h>
29 
30 const char *
31 ssherr_libcrypto(void)
32 {
33 	unsigned long e;
34 	static char buf[512];
35 	char msg[4096];
36 	const char *reason = NULL, *file, *data;
37 	int ln, fl;
38 
39 	OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
40 	while ((e = ERR_get_error_line_data(&file, &ln, &data, &fl)) != 0) {
41 		ERR_error_string_n(e, buf, sizeof(buf));
42 		snprintf(msg, sizeof(msg), "%s:%s:%d:%s", buf, file, ln,
43 		    (fl & ERR_TXT_STRING) ? data : "");
44 		debug("libcrypto: '%s'", msg);
45 		if ((reason = ERR_reason_error_string(e)) != NULL)
46 			snprintf(buf, sizeof(buf), "error in libcrypto: %s",
47 			    reason);
48 	}
49 	if (reason == NULL)
50 		return NULL;
51 	return buf;
52 }
53 #else
54 const char *
55 ssherr_libcrypto(void)
56 {
57 	return NULL;
58 }
59 #endif
60