tool.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <string>
  15. #include <vector>
  16. #include <openssl/crypto.h>
  17. #include <openssl/err.h>
  18. #include <openssl/ssl.h>
  19. #if defined(OPENSSL_WINDOWS)
  20. #include <fcntl.h>
  21. #include <io.h>
  22. #else
  23. #include <libgen.h>
  24. #include <signal.h>
  25. #endif
  26. #include "internal.h"
  27. static bool IsFIPS(const std::vector<std::string> &args) {
  28. printf("%d\n", FIPS_mode());
  29. return true;
  30. }
  31. typedef bool (*tool_func_t)(const std::vector<std::string> &args);
  32. struct Tool {
  33. const char *name;
  34. tool_func_t func;
  35. };
  36. static const Tool kTools[] = {
  37. { "ciphers", Ciphers },
  38. { "client", Client },
  39. { "isfips", IsFIPS },
  40. { "generate-ech", GenerateECH},
  41. { "generate-ed25519", GenerateEd25519Key },
  42. { "genrsa", GenerateRSAKey },
  43. { "md5sum", MD5Sum },
  44. { "pkcs12", DoPKCS12 },
  45. { "rand", Rand },
  46. { "s_client", Client },
  47. { "s_server", Server },
  48. { "server", Server },
  49. { "sha1sum", SHA1Sum },
  50. { "sha224sum", SHA224Sum },
  51. { "sha256sum", SHA256Sum },
  52. { "sha384sum", SHA384Sum },
  53. { "sha512sum", SHA512Sum },
  54. { "sha512256sum", SHA512256Sum },
  55. { "sign", Sign },
  56. { "speed", Speed },
  57. { "", nullptr },
  58. };
  59. static void usage(const char *name) {
  60. printf("Usage: %s COMMAND\n", name);
  61. printf("\n");
  62. printf("Available commands:\n");
  63. for (size_t i = 0;; i++) {
  64. const Tool &tool = kTools[i];
  65. if (tool.func == nullptr) {
  66. break;
  67. }
  68. printf(" %s\n", tool.name);
  69. }
  70. }
  71. static tool_func_t FindTool(const std::string &name) {
  72. for (size_t i = 0;; i++) {
  73. const Tool &tool = kTools[i];
  74. if (tool.func == nullptr || name == tool.name) {
  75. return tool.func;
  76. }
  77. }
  78. }
  79. int main(int argc, char **argv) {
  80. #if defined(OPENSSL_WINDOWS)
  81. // Read and write in binary mode. This makes bssl on Windows consistent with
  82. // bssl on other platforms, and also makes it consistent with MSYS's commands
  83. // like diff(1) and md5sum(1). This is especially important for the digest
  84. // commands.
  85. if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
  86. perror("_setmode(_fileno(stdin), O_BINARY)");
  87. return 1;
  88. }
  89. if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
  90. perror("_setmode(_fileno(stdout), O_BINARY)");
  91. return 1;
  92. }
  93. if (_setmode(_fileno(stderr), _O_BINARY) == -1) {
  94. perror("_setmode(_fileno(stderr), O_BINARY)");
  95. return 1;
  96. }
  97. #else
  98. signal(SIGPIPE, SIG_IGN);
  99. #endif
  100. CRYPTO_library_init();
  101. int starting_arg = 1;
  102. tool_func_t tool = nullptr;
  103. #if !defined(OPENSSL_WINDOWS)
  104. tool = FindTool(basename(argv[0]));
  105. #endif
  106. if (tool == nullptr) {
  107. starting_arg++;
  108. if (argc > 1) {
  109. tool = FindTool(argv[1]);
  110. }
  111. }
  112. if (tool == nullptr) {
  113. usage(argv[0]);
  114. return 1;
  115. }
  116. std::vector<std::string> args;
  117. for (int i = starting_arg; i < argc; i++) {
  118. args.push_back(argv[i]);
  119. }
  120. if (!tool(args)) {
  121. ERR_print_errors_fp(stderr);
  122. return 1;
  123. }
  124. return 0;
  125. }