xref: /openbmc/linux/scripts/ssl-common.h (revision d699090510c3223641a23834b4710e2d4309a6ad)
11e2d849eSJan Stancek /* SPDX-License-Identifier: LGPL-2.1+ */
21e2d849eSJan Stancek /*
31e2d849eSJan Stancek  * SSL helper functions shared by sign-file and extract-cert.
41e2d849eSJan Stancek  */
51e2d849eSJan Stancek 
drain_openssl_errors(int l,int silent)6*f8dafdafSJan Stancek static void drain_openssl_errors(int l, int silent)
71e2d849eSJan Stancek {
81e2d849eSJan Stancek 	const char *file;
91e2d849eSJan Stancek 	char buf[120];
101e2d849eSJan Stancek 	int e, line;
111e2d849eSJan Stancek 
121e2d849eSJan Stancek 	if (ERR_peek_error() == 0)
131e2d849eSJan Stancek 		return;
14*f8dafdafSJan Stancek 	if (!silent)
151e2d849eSJan Stancek 		fprintf(stderr, "At main.c:%d:\n", l);
161e2d849eSJan Stancek 
17*f8dafdafSJan Stancek 	while ((e = ERR_peek_error_line(&file, &line))) {
181e2d849eSJan Stancek 		ERR_error_string(e, buf);
19*f8dafdafSJan Stancek 		if (!silent)
201e2d849eSJan Stancek 			fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line);
21*f8dafdafSJan Stancek 		ERR_get_error();
221e2d849eSJan Stancek 	}
231e2d849eSJan Stancek }
241e2d849eSJan Stancek 
251e2d849eSJan Stancek #define ERR(cond, fmt, ...)				\
261e2d849eSJan Stancek 	do {						\
271e2d849eSJan Stancek 		bool __cond = (cond);			\
28*f8dafdafSJan Stancek 		drain_openssl_errors(__LINE__, 0);	\
291e2d849eSJan Stancek 		if (__cond) {				\
301e2d849eSJan Stancek 			errx(1, fmt, ## __VA_ARGS__);	\
311e2d849eSJan Stancek 		}					\
321e2d849eSJan Stancek 	} while (0)
33