errstr.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "apps.h"
  13. #include "progs.h"
  14. #include <openssl/bio.h>
  15. #include <openssl/err.h>
  16. #include <openssl/ssl.h>
  17. typedef enum OPTION_choice {
  18. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP
  19. } OPTION_CHOICE;
  20. const OPTIONS errstr_options[] = {
  21. {OPT_HELP_STR, 1, '-', "Usage: %s [options] errnum...\n"},
  22. OPT_SECTION("General"),
  23. {"help", OPT_HELP, '-', "Display this summary"},
  24. OPT_PARAMETERS(),
  25. {"errnum", 0, 0, "Error number(s) to decode"},
  26. {NULL}
  27. };
  28. int errstr_main(int argc, char **argv)
  29. {
  30. OPTION_CHOICE o;
  31. char buf[256], *prog;
  32. int ret = 1;
  33. unsigned long l;
  34. prog = opt_init(argc, argv, errstr_options);
  35. while ((o = opt_next()) != OPT_EOF) {
  36. switch (o) {
  37. case OPT_EOF:
  38. case OPT_ERR:
  39. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  40. goto end;
  41. case OPT_HELP:
  42. opt_help(errstr_options);
  43. ret = 0;
  44. goto end;
  45. }
  46. }
  47. ret = 0;
  48. for (argv = opt_rest(); *argv; argv++) {
  49. if (sscanf(*argv, "%lx", &l) == 0) {
  50. ret++;
  51. } else {
  52. /* We're not really an SSL application so this won't auto-init, but
  53. * we're still interested in SSL error strings
  54. */
  55. OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
  56. | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  57. ERR_error_string_n(l, buf, sizeof(buf));
  58. BIO_printf(bio_out, "%s\n", buf);
  59. }
  60. }
  61. end:
  62. return ret;
  63. }