Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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