xref: /openbsd-src/usr.bin/openssl/errstr.c (revision 3228b0c4b8598ac2f799f997d457a8ba24307bec)
1*3228b0c4Stb /* $OpenBSD: errstr.c,v 1.11 2023/07/23 11:20:11 tb Exp $ */
2dab3f910Sjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3dab3f910Sjsing  * All rights reserved.
4dab3f910Sjsing  *
5dab3f910Sjsing  * This package is an SSL implementation written
6dab3f910Sjsing  * by Eric Young (eay@cryptsoft.com).
7dab3f910Sjsing  * The implementation was written so as to conform with Netscapes SSL.
8dab3f910Sjsing  *
9dab3f910Sjsing  * This library is free for commercial and non-commercial use as long as
10dab3f910Sjsing  * the following conditions are aheared to.  The following conditions
11dab3f910Sjsing  * apply to all code found in this distribution, be it the RC4, RSA,
12dab3f910Sjsing  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13dab3f910Sjsing  * included with this distribution is covered by the same copyright terms
14dab3f910Sjsing  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15dab3f910Sjsing  *
16dab3f910Sjsing  * Copyright remains Eric Young's, and as such any Copyright notices in
17dab3f910Sjsing  * the code are not to be removed.
18dab3f910Sjsing  * If this package is used in a product, Eric Young should be given attribution
19dab3f910Sjsing  * as the author of the parts of the library used.
20dab3f910Sjsing  * This can be in the form of a textual message at program startup or
21dab3f910Sjsing  * in documentation (online or textual) provided with the package.
22dab3f910Sjsing  *
23dab3f910Sjsing  * Redistribution and use in source and binary forms, with or without
24dab3f910Sjsing  * modification, are permitted provided that the following conditions
25dab3f910Sjsing  * are met:
26dab3f910Sjsing  * 1. Redistributions of source code must retain the copyright
27dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer.
28dab3f910Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
29dab3f910Sjsing  *    notice, this list of conditions and the following disclaimer in the
30dab3f910Sjsing  *    documentation and/or other materials provided with the distribution.
31dab3f910Sjsing  * 3. All advertising materials mentioning features or use of this software
32dab3f910Sjsing  *    must display the following acknowledgement:
33dab3f910Sjsing  *    "This product includes cryptographic software written by
34dab3f910Sjsing  *     Eric Young (eay@cryptsoft.com)"
35dab3f910Sjsing  *    The word 'cryptographic' can be left out if the rouines from the library
36dab3f910Sjsing  *    being used are not cryptographic related :-).
37dab3f910Sjsing  * 4. If you include any Windows specific code (or a derivative thereof) from
38dab3f910Sjsing  *    the apps directory (application code) you must include an acknowledgement:
39dab3f910Sjsing  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40dab3f910Sjsing  *
41dab3f910Sjsing  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42dab3f910Sjsing  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43dab3f910Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44dab3f910Sjsing  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45dab3f910Sjsing  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46dab3f910Sjsing  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47dab3f910Sjsing  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48dab3f910Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49dab3f910Sjsing  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50dab3f910Sjsing  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51dab3f910Sjsing  * SUCH DAMAGE.
52dab3f910Sjsing  *
53dab3f910Sjsing  * The licence and distribution terms for any publically available version or
54dab3f910Sjsing  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55dab3f910Sjsing  * copied and put under another distribution licence
56dab3f910Sjsing  * [including the GNU Public Licence.]
57dab3f910Sjsing  */
58dab3f910Sjsing 
59f6e54465Sjsing #include <limits.h>
60dab3f910Sjsing #include <stdio.h>
61dab3f910Sjsing #include <stdlib.h>
62dab3f910Sjsing #include <string.h>
63dab3f910Sjsing 
64dab3f910Sjsing #include "apps.h"
65dab3f910Sjsing 
66dab3f910Sjsing #include <openssl/bio.h>
67dab3f910Sjsing #include <openssl/err.h>
68dab3f910Sjsing #include <openssl/lhash.h>
69dab3f910Sjsing #include <openssl/ssl.h>
70dab3f910Sjsing 
71ea149709Sguenther static const struct option errstr_options[] = {
72dcff9825Sjsing 	{ NULL },
73dcff9825Sjsing };
74dcff9825Sjsing 
75dcff9825Sjsing static void
errstr_usage(void)76*3228b0c4Stb errstr_usage(void)
77dcff9825Sjsing {
78*3228b0c4Stb 	fprintf(stderr, "usage: errstr errno ...\n");
79dcff9825Sjsing }
80dcff9825Sjsing 
81dab3f910Sjsing int
errstr_main(int argc,char ** argv)82dab3f910Sjsing errstr_main(int argc, char **argv)
83dab3f910Sjsing {
84f6e54465Sjsing 	unsigned long ulval;
85f6e54465Sjsing 	char *ularg, *ep;
86f6e54465Sjsing 	int argsused, i;
87dab3f910Sjsing 	char buf[256];
88f6e54465Sjsing 	int ret = 0;
89dab3f910Sjsing 
90e370f0eeSdoug 	if (pledge("stdio rpath", NULL) == -1) {
919bc487adSdoug 		perror("pledge");
92e370f0eeSdoug 		exit(1);
93e370f0eeSdoug 	}
949bc487adSdoug 
95dcff9825Sjsing 	if (options_parse(argc, argv, errstr_options, NULL, &argsused) != 0) {
96dcff9825Sjsing 		errstr_usage();
97dcff9825Sjsing 		return (1);
98dcff9825Sjsing 	}
99dcff9825Sjsing 
100dcff9825Sjsing 	for (i = argsused; i < argc; i++) {
101f6e54465Sjsing 		errno = 0;
102f6e54465Sjsing 		ularg = argv[i];
103f6e54465Sjsing 		ulval = strtoul(ularg, &ep, 16);
104f6e54465Sjsing 		if (strchr(ularg, '-') != NULL ||
105f6e54465Sjsing 		    (ularg[0] == '\0' || *ep != '\0') ||
106f6e54465Sjsing 		    (errno == ERANGE && ulval == ULONG_MAX)) {
107f6e54465Sjsing 			printf("%s: bad error code\n", ularg);
108dab3f910Sjsing 			ret++;
109f6e54465Sjsing 			continue;
110dab3f910Sjsing 		}
111f6e54465Sjsing 
112f6e54465Sjsing 		ERR_error_string_n(ulval, buf, sizeof(buf));
113f6e54465Sjsing 		printf("%s\n", buf);
114dab3f910Sjsing 	}
115dab3f910Sjsing 
116dab3f910Sjsing 	return (ret);
117dab3f910Sjsing }
118