blob: 670d4e7fb2eef97a87c49896a2e278e75b5de31e [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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 Langley95c29f32014-06-20 12:00:00 -070016#include <vector>
17
David Benjamin7a1eefd2015-10-17 23:39:22 -040018#include <openssl/crypto.h>
Adam Langley95c29f32014-06-20 12:00:00 -070019#include <openssl/err.h>
Adam Langleyaacec172014-06-20 12:00:00 -070020#include <openssl/ssl.h>
Adam Langley95c29f32014-06-20 12:00:00 -070021
Brian Smithafdaeee2015-01-26 19:54:32 -080022#if defined(OPENSSL_WINDOWS)
23#include <fcntl.h>
24#include <io.h>
25#else
Adam Langleycca4d592015-01-12 12:01:23 -080026#include <libgen.h>
27#endif
28
Piotr Sikoraf9328942016-03-18 18:24:50 -070029#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070030
Adam Langley95c29f32014-06-20 12:00:00 -070031
Adam Langleyfd499932017-04-04 14:21:43 -070032static bool IsFIPS(const std::vector<std::string> &args) {
33 printf("%d\n", FIPS_mode());
34 return true;
35}
36
Adam Langleycca4d592015-01-12 12:01:23 -080037typedef bool (*tool_func_t)(const std::vector<std::string> &args);
38
39struct Tool {
David Benjaminafe57cb2015-12-18 18:22:27 -050040 const char *name;
Adam Langleycca4d592015-01-12 12:01:23 -080041 tool_func_t func;
42};
43
44static const Tool kTools[] = {
Adam Langleyeb8be012015-10-27 16:03:52 -070045 { "ciphers", Ciphers },
Adam Langleycca4d592015-01-12 12:01:23 -080046 { "client", Client },
Adam Langleyfd499932017-04-04 14:21:43 -070047 { "isfips", IsFIPS },
David Benjaminafe57cb2015-12-18 18:22:27 -050048 { "generate-ed25519", GenerateEd25519Key },
Adam Langley839b8812015-05-26 11:36:46 -070049 { "genrsa", GenerateRSAKey },
Adam Langleycca4d592015-01-12 12:01:23 -080050 { "md5sum", MD5Sum },
Adam Langley839b8812015-05-26 11:36:46 -070051 { "pkcs12", DoPKCS12 },
52 { "rand", Rand },
53 { "s_client", Client },
54 { "s_server", Server },
55 { "server", Server },
Adam Langleycca4d592015-01-12 12:01:23 -080056 { "sha1sum", SHA1Sum },
57 { "sha224sum", SHA224Sum },
58 { "sha256sum", SHA256Sum },
59 { "sha384sum", SHA384Sum },
60 { "sha512sum", SHA512Sum },
David Benjamin4e78e302017-03-30 10:16:07 -050061 { "sign", Sign },
Adam Langley839b8812015-05-26 11:36:46 -070062 { "speed", Speed },
Adam Langleycca4d592015-01-12 12:01:23 -080063 { "", nullptr },
64};
65
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070066static void usage(const char *name) {
David Benjaminafe57cb2015-12-18 18:22:27 -050067 printf("Usage: %s COMMAND\n", name);
68 printf("\n");
69 printf("Available commands:\n");
Adam Langleycca4d592015-01-12 12:01:23 -080070
71 for (size_t i = 0;; i++) {
72 const Tool &tool = kTools[i];
73 if (tool.func == nullptr) {
74 break;
75 }
David Benjaminafe57cb2015-12-18 18:22:27 -050076 printf(" %s\n", tool.name);
Adam Langleycca4d592015-01-12 12:01:23 -080077 }
Adam Langleycca4d592015-01-12 12:01:23 -080078}
79
Piotr Sikora9bb8ba62016-03-18 18:19:04 -070080static tool_func_t FindTool(const std::string &name) {
Adam Langleycca4d592015-01-12 12:01:23 -080081 for (size_t i = 0;; i++) {
82 const Tool &tool = kTools[i];
83 if (tool.func == nullptr || name == tool.name) {
84 return tool.func;
85 }
86 }
Adam Langley95c29f32014-06-20 12:00:00 -070087}
88
Adam Langley10f97f32016-07-12 08:09:33 -070089int main(int argc, char **argv) {
Brian Smithafdaeee2015-01-26 19:54:32 -080090#if defined(OPENSSL_WINDOWS)
91 // Read and write in binary mode. This makes bssl on Windows consistent with
92 // bssl on other platforms, and also makes it consistent with MSYS's commands
93 // like diff(1) and md5sum(1). This is especially important for the digest
94 // commands.
95 if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
96 perror("_setmode(_fileno(stdin), O_BINARY)");
97 return 1;
98 }
99 if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
100 perror("_setmode(_fileno(stdout), O_BINARY)");
101 return 1;
102 }
103 if (_setmode(_fileno(stderr), _O_BINARY) == -1) {
104 perror("_setmode(_fileno(stderr), O_BINARY)");
105 return 1;
106 }
107#endif
108
David Benjamin7a1eefd2015-10-17 23:39:22 -0400109 CRYPTO_library_init();
Adam Langley95c29f32014-06-20 12:00:00 -0700110
Adam Langleycca4d592015-01-12 12:01:23 -0800111 int starting_arg = 1;
112 tool_func_t tool = nullptr;
David Benjamineee73062014-10-31 16:01:29 -0400113#if !defined(OPENSSL_WINDOWS)
Adam Langleycca4d592015-01-12 12:01:23 -0800114 tool = FindTool(basename(argv[0]));
David Benjamineee73062014-10-31 16:01:29 -0400115#endif
Adam Langleycca4d592015-01-12 12:01:23 -0800116 if (tool == nullptr) {
117 starting_arg++;
118 if (argc > 1) {
119 tool = FindTool(argv[1]);
120 }
121 }
122 if (tool == nullptr) {
Adam Langley95c29f32014-06-20 12:00:00 -0700123 usage(argv[0]);
124 return 1;
125 }
Adam Langleycca4d592015-01-12 12:01:23 -0800126
127 std::vector<std::string> args;
128 for (int i = starting_arg; i < argc; i++) {
129 args.push_back(argv[i]);
130 }
131
David Benjamin4e78e302017-03-30 10:16:07 -0500132 if (!tool(args)) {
133 ERR_print_errors_fp(stderr);
134 return 1;
135 }
136
137 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700138}