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

127 行
3.2 KiB

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