errstr.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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_HELP_STR, 1, '-', " errnum Error number\n"},
  23. {"help", OPT_HELP, '-', "Display this summary"},
  24. {NULL}
  25. };
  26. int errstr_main(int argc, char **argv)
  27. {
  28. OPTION_CHOICE o;
  29. char buf[256], *prog;
  30. int ret = 1;
  31. unsigned long l;
  32. prog = opt_init(argc, argv, errstr_options);
  33. while ((o = opt_next()) != OPT_EOF) {
  34. switch (o) {
  35. case OPT_EOF:
  36. case OPT_ERR:
  37. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  38. goto end;
  39. case OPT_HELP:
  40. opt_help(errstr_options);
  41. ret = 0;
  42. goto end;
  43. }
  44. }
  45. ret = 0;
  46. for (argv = opt_rest(); *argv; argv++) {
  47. if (sscanf(*argv, "%lx", &l) == 0) {
  48. ret++;
  49. } else {
  50. /* We're not really an SSL application so this won't auto-init, but
  51. * we're still interested in SSL error strings
  52. */
  53. OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
  54. | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  55. ERR_error_string_n(l, buf, sizeof(buf));
  56. BIO_printf(bio_out, "%s\n", buf);
  57. }
  58. }
  59. end:
  60. return ret;
  61. }