xref: /src/crypto/openssl/apps/errstr.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "apps.h"
14 #include "progs.h"
15 #include <openssl/bio.h>
16 #include <openssl/err.h>
17 #include <openssl/ssl.h>
18 
19 typedef enum OPTION_choice {
20     OPT_ERR = -1,
21     OPT_EOF = 0,
22     OPT_HELP
23 } OPTION_CHOICE;
24 
25 const OPTIONS errstr_options[] = {
26     { OPT_HELP_STR, 1, '-', "Usage: %s [options] errnum...\n" },
27 
28     OPT_SECTION("General"),
29     { "help", OPT_HELP, '-', "Display this summary" },
30 
31     OPT_PARAMETERS(),
32     { "errnum", 0, 0, "Error number(s) to decode" },
33     { NULL }
34 };
35 
errstr_main(int argc,char ** argv)36 int errstr_main(int argc, char **argv)
37 {
38     OPTION_CHOICE o;
39     char buf[256], *prog;
40     int ret = 1;
41     unsigned long l;
42 
43     prog = opt_init(argc, argv, errstr_options);
44     while ((o = opt_next()) != OPT_EOF) {
45         switch (o) {
46         case OPT_EOF:
47         case OPT_ERR:
48             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
49             goto end;
50         case OPT_HELP:
51             opt_help(errstr_options);
52             ret = 0;
53             goto end;
54         }
55     }
56 
57     /*
58      * We're not really an SSL application so this won't auto-init, but
59      * we're still interested in SSL error strings
60      */
61     OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
62             | OPENSSL_INIT_LOAD_CRYPTO_STRINGS,
63         NULL);
64 
65     /* All remaining arg are error code. */
66     ret = 0;
67     for (argv = opt_rest(); *argv != NULL; argv++) {
68         if (sscanf(*argv, "%lx", &l) <= 0) {
69             ret++;
70         } else {
71             ERR_error_string_n(l, buf, sizeof(buf));
72             BIO_printf(bio_out, "%s\n", buf);
73         }
74     }
75 end:
76     return ret;
77 }
78