Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
| 15 | #include <string> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
David Benjamin | 7a1eefd | 2015-10-17 23:39:22 -0400 | [diff] [blame] | 18 | #include <openssl/crypto.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 19 | #include <openssl/err.h> |
Adam Langley | aacec17 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 20 | #include <openssl/ssl.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 21 | |
Brian Smith | afdaeee | 2015-01-26 19:54:32 -0800 | [diff] [blame] | 22 | #if defined(OPENSSL_WINDOWS) |
| 23 | #include <fcntl.h> |
| 24 | #include <io.h> |
| 25 | #else |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 26 | #include <libgen.h> |
David Benjamin | 41adb34 | 2021-09-15 10:19:47 -0400 | [diff] [blame] | 27 | #include <signal.h> |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 28 | #endif |
| 29 | |
Piotr Sikora | f932894 | 2016-03-18 18:24:50 -0700 | [diff] [blame] | 30 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 31 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 32 | |
Adam Langley | fd49993 | 2017-04-04 14:21:43 -0700 | [diff] [blame] | 33 | static bool IsFIPS(const std::vector<std::string> &args) { |
| 34 | printf("%d\n", FIPS_mode()); |
| 35 | return true; |
| 36 | } |
| 37 | |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 38 | typedef bool (*tool_func_t)(const std::vector<std::string> &args); |
| 39 | |
| 40 | struct Tool { |
David Benjamin | afe57cb | 2015-12-18 18:22:27 -0500 | [diff] [blame] | 41 | const char *name; |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 42 | tool_func_t func; |
| 43 | }; |
| 44 | |
| 45 | static const Tool kTools[] = { |
Adam Langley | eb8be01 | 2015-10-27 16:03:52 -0700 | [diff] [blame] | 46 | { "ciphers", Ciphers }, |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 47 | { "client", Client }, |
Adam Langley | fd49993 | 2017-04-04 14:21:43 -0700 | [diff] [blame] | 48 | { "isfips", IsFIPS }, |
Dan McArdle | 7a817f4 | 2021-07-14 17:19:16 -0400 | [diff] [blame] | 49 | { "generate-ech", GenerateECH}, |
David Benjamin | afe57cb | 2015-12-18 18:22:27 -0500 | [diff] [blame] | 50 | { "generate-ed25519", GenerateEd25519Key }, |
Adam Langley | 839b881 | 2015-05-26 11:36:46 -0700 | [diff] [blame] | 51 | { "genrsa", GenerateRSAKey }, |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 52 | { "md5sum", MD5Sum }, |
Adam Langley | 839b881 | 2015-05-26 11:36:46 -0700 | [diff] [blame] | 53 | { "pkcs12", DoPKCS12 }, |
| 54 | { "rand", Rand }, |
| 55 | { "s_client", Client }, |
| 56 | { "s_server", Server }, |
| 57 | { "server", Server }, |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 58 | { "sha1sum", SHA1Sum }, |
| 59 | { "sha224sum", SHA224Sum }, |
| 60 | { "sha256sum", SHA256Sum }, |
| 61 | { "sha384sum", SHA384Sum }, |
| 62 | { "sha512sum", SHA512Sum }, |
Adam Langley | 3c11bf5 | 2020-04-15 09:10:04 -0700 | [diff] [blame] | 63 | { "sha512256sum", SHA512256Sum }, |
David Benjamin | 4e78e30 | 2017-03-30 10:16:07 -0500 | [diff] [blame] | 64 | { "sign", Sign }, |
Adam Langley | 839b881 | 2015-05-26 11:36:46 -0700 | [diff] [blame] | 65 | { "speed", Speed }, |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 66 | { "", nullptr }, |
| 67 | }; |
| 68 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 69 | static void usage(const char *name) { |
David Benjamin | afe57cb | 2015-12-18 18:22:27 -0500 | [diff] [blame] | 70 | printf("Usage: %s COMMAND\n", name); |
| 71 | printf("\n"); |
| 72 | printf("Available commands:\n"); |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 73 | |
| 74 | for (size_t i = 0;; i++) { |
| 75 | const Tool &tool = kTools[i]; |
| 76 | if (tool.func == nullptr) { |
| 77 | break; |
| 78 | } |
David Benjamin | afe57cb | 2015-12-18 18:22:27 -0500 | [diff] [blame] | 79 | printf(" %s\n", tool.name); |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 80 | } |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Piotr Sikora | 9bb8ba6 | 2016-03-18 18:19:04 -0700 | [diff] [blame] | 83 | static tool_func_t FindTool(const std::string &name) { |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 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 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Adam Langley | 10f97f3 | 2016-07-12 08:09:33 -0700 | [diff] [blame] | 92 | int main(int argc, char **argv) { |
Brian Smith | afdaeee | 2015-01-26 19:54:32 -0800 | [diff] [blame] | 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 | } |
David Benjamin | 41adb34 | 2021-09-15 10:19:47 -0400 | [diff] [blame] | 110 | #else |
| 111 | signal(SIGPIPE, SIG_IGN); |
Brian Smith | afdaeee | 2015-01-26 19:54:32 -0800 | [diff] [blame] | 112 | #endif |
| 113 | |
David Benjamin | 7a1eefd | 2015-10-17 23:39:22 -0400 | [diff] [blame] | 114 | CRYPTO_library_init(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 115 | |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 116 | int starting_arg = 1; |
| 117 | tool_func_t tool = nullptr; |
David Benjamin | eee7306 | 2014-10-31 16:01:29 -0400 | [diff] [blame] | 118 | #if !defined(OPENSSL_WINDOWS) |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 119 | tool = FindTool(basename(argv[0])); |
David Benjamin | eee7306 | 2014-10-31 16:01:29 -0400 | [diff] [blame] | 120 | #endif |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 121 | if (tool == nullptr) { |
| 122 | starting_arg++; |
| 123 | if (argc > 1) { |
| 124 | tool = FindTool(argv[1]); |
| 125 | } |
| 126 | } |
| 127 | if (tool == nullptr) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | usage(argv[0]); |
| 129 | return 1; |
| 130 | } |
Adam Langley | cca4d59 | 2015-01-12 12:01:23 -0800 | [diff] [blame] | 131 | |
| 132 | std::vector<std::string> args; |
| 133 | for (int i = starting_arg; i < argc; i++) { |
| 134 | args.push_back(argv[i]); |
| 135 | } |
| 136 | |
David Benjamin | 4e78e30 | 2017-03-30 10:16:07 -0500 | [diff] [blame] | 137 | if (!tool(args)) { |
| 138 | ERR_print_errors_fp(stderr); |
| 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | } |