tool.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #endif
  25. #include "internal.h"
  26. static bool IsFIPS(const std::vector<std::string> &args) {
  27. printf("%d\n", FIPS_mode());
  28. return true;
  29. }
  30. typedef bool (*tool_func_t)(const std::vector<std::string> &args);
  31. struct Tool {
  32. const char *name;
  33. tool_func_t func;
  34. };
  35. static const Tool kTools[] = {
  36. { "ciphers", Ciphers },
  37. { "client", Client },
  38. { "isfips", IsFIPS },
  39. { "generate-ed25519", GenerateEd25519Key },
  40. { "genrsa", GenerateRSAKey },
  41. { "md5sum", MD5Sum },
  42. { "pkcs12", DoPKCS12 },
  43. { "rand", Rand },
  44. { "s_client", Client },
  45. { "s_server", Server },
  46. { "server", Server },
  47. { "sha1sum", SHA1Sum },
  48. { "sha224sum", SHA224Sum },
  49. { "sha256sum", SHA256Sum },
  50. { "sha384sum", SHA384Sum },
  51. { "sha512sum", SHA512Sum },
  52. { "sign", Sign },
  53. { "speed", Speed },
  54. { "", nullptr },
  55. };
  56. static void usage(const char *name) {
  57. printf("Usage: %s COMMAND\n", name);
  58. printf("\n");
  59. printf("Available commands:\n");
  60. for (size_t i = 0;; i++) {
  61. const Tool &tool = kTools[i];
  62. if (tool.func == nullptr) {
  63. break;
  64. }
  65. printf(" %s\n", tool.name);
  66. }
  67. }
  68. static tool_func_t FindTool(const std::string &name) {
  69. for (size_t i = 0;; i++) {
  70. const Tool &tool = kTools[i];
  71. if (tool.func == nullptr || name == tool.name) {
  72. return tool.func;
  73. }
  74. }
  75. }
  76. int main(int argc, char **argv) {
  77. #if defined(OPENSSL_WINDOWS)
  78. // Read and write in binary mode. This makes bssl on Windows consistent with
  79. // bssl on other platforms, and also makes it consistent with MSYS's commands
  80. // like diff(1) and md5sum(1). This is especially important for the digest
  81. // commands.
  82. if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
  83. perror("_setmode(_fileno(stdin), O_BINARY)");
  84. return 1;
  85. }
  86. if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
  87. perror("_setmode(_fileno(stdout), O_BINARY)");
  88. return 1;
  89. }
  90. if (_setmode(_fileno(stderr), _O_BINARY) == -1) {
  91. perror("_setmode(_fileno(stderr), O_BINARY)");
  92. return 1;
  93. }
  94. #endif
  95. CRYPTO_library_init();
  96. int starting_arg = 1;
  97. tool_func_t tool = nullptr;
  98. #if !defined(OPENSSL_WINDOWS)
  99. tool = FindTool(basename(argv[0]));
  100. #endif
  101. if (tool == nullptr) {
  102. starting_arg++;
  103. if (argc > 1) {
  104. tool = FindTool(argv[1]);
  105. }
  106. }
  107. if (tool == nullptr) {
  108. usage(argv[0]);
  109. return 1;
  110. }
  111. std::vector<std::string> args;
  112. for (int i = starting_arg; i < argc; i++) {
  113. args.push_back(argv[i]);
  114. }
  115. if (!tool(args)) {
  116. ERR_print_errors_fp(stderr);
  117. return 1;
  118. }
  119. return 0;
  120. }