Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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