選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

tool.cc 3.9 KiB

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