| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 |
| 18 | #include <openssl/crypto.h> |
| 19 | #include <openssl/err.h> |
| 20 | #include <openssl/ssl.h> |
| 21 |
| 22 | #if defined(OPENSSL_WINDOWS) |
| 23 | #include <fcntl.h> |
| 24 | #include <io.h> |
| 25 | #else |
| 26 | #include <libgen.h> |
| 27 | #include <signal.h> |
| 28 | #endif |
| 29 |
| 30 | #include "internal.h" |
| 31 |
| 32 |
| 33 | static bool IsFIPS(const std::vector<std::string> &args) { |
| 34 | printf("%d\n", FIPS_mode()); |
| 35 | return true; |
| 36 | } |
| 37 |
| 38 | typedef bool (*tool_func_t)(const std::vector<std::string> &args); |
| 39 |
| 40 | struct Tool { |
| 41 | const char *name; |
| 42 | tool_func_t func; |
| 43 | }; |
| 44 |
| 45 | static const Tool kTools[] = { |
| 46 | { "ciphers", Ciphers }, |
| 47 | { "client", Client }, |
| 48 | { "isfips", IsFIPS }, |
| 49 | { "generate-ech", GenerateECH}, |
| 50 | { "generate-ed25519", GenerateEd25519Key }, |
| 51 | { "genrsa", GenerateRSAKey }, |
| 52 | { "md5sum", MD5Sum }, |
| 53 | { "pkcs12", DoPKCS12 }, |
| 54 | { "rand", Rand }, |
| 55 | { "s_client", Client }, |
| 56 | { "s_server", Server }, |
| 57 | { "server", Server }, |
| 58 | { "sha1sum", SHA1Sum }, |
| 59 | { "sha224sum", SHA224Sum }, |
| 60 | { "sha256sum", SHA256Sum }, |
| 61 | { "sha384sum", SHA384Sum }, |
| 62 | { "sha512sum", SHA512Sum }, |
| 63 | { "sha512256sum", SHA512256Sum }, |
| 64 | { "sign", Sign }, |
| 65 | { "speed", Speed }, |
| 66 | { "", nullptr }, |
| 67 | }; |
| 68 |
| 69 | static void usage(const char *name) { |
| 70 | printf("Usage: %s COMMAND\n", name); |
| 71 | printf("\n"); |
| 72 | printf("Available commands:\n"); |
| 73 |
| 74 | for (size_t i = 0;; i++) { |
| 75 | const Tool &tool = kTools[i]; |
| 76 | if (tool.func == nullptr) { |
| 77 | break; |
| 78 | } |
| 79 | printf(" %s\n", tool.name); |
| 80 | } |
| 81 | } |
| 82 |
| 83 | static tool_func_t FindTool(const std::string &name) { |
| 84 | for (size_t i = 0;; i++) { |
| 85 | const Tool &tool = kTools[i]; |
| 86 | if (tool.func == nullptr || name == tool.name) { |
| 87 | return tool.func; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 |
| 92 | int main(int argc, char **argv) { |
| 93 | #if defined(OPENSSL_WINDOWS) |
| 94 | // Read and write in binary mode. This makes bssl on Windows consistent with |
| 95 | // bssl on other platforms, and also makes it consistent with MSYS's commands |
| 96 | // like diff(1) and md5sum(1). This is especially important for the digest |
| 97 | // commands. |
| 98 | if (_setmode(_fileno(stdin), _O_BINARY) == -1) { |
| 99 | perror("_setmode(_fileno(stdin), O_BINARY)"); |
| 100 | return 1; |
| 101 | } |
| 102 | if (_setmode(_fileno(stdout), _O_BINARY) == -1) { |
| 103 | perror("_setmode(_fileno(stdout), O_BINARY)"); |
| 104 | return 1; |
| 105 | } |
| 106 | if (_setmode(_fileno(stderr), _O_BINARY) == -1) { |
| 107 | perror("_setmode(_fileno(stderr), O_BINARY)"); |
| 108 | return 1; |
| 109 | } |
| 110 | #else |
| 111 | signal(SIGPIPE, SIG_IGN); |
| 112 | #endif |
| 113 |
| 114 | int starting_arg = 1; |
| 115 | tool_func_t tool = nullptr; |
| 116 | #if !defined(OPENSSL_WINDOWS) |
| 117 | tool = FindTool(basename(argv[0])); |
| 118 | #endif |
| 119 | if (tool == nullptr) { |
| 120 | starting_arg++; |
| 121 | if (argc > 1) { |
| 122 | tool = FindTool(argv[1]); |
| 123 | } |
| 124 | } |
| 125 | if (tool == nullptr) { |
| 126 | usage(argv[0]); |
| 127 | return 1; |
| 128 | } |
| 129 |
| 130 | std::vector<std::string> args; |
| 131 | for (int i = starting_arg; i < argc; i++) { |
| 132 | args.push_back(argv[i]); |
| 133 | } |
| 134 |
| 135 | if (!tool(args)) { |
| 136 | ERR_print_errors_fp(stderr); |
| 137 | return 1; |
| 138 | } |
| 139 |
| 140 | return 0; |
| 141 | } |
| 142 |