blob: 34851b47114c67f3d419e3add238635be3bc365a [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 Langleycca4d592015-01-12 12:01:23 -080032typedef bool (*tool_func_t)(const std::vector<std::string> &args);
33
34struct Tool {
David Benjaminafe57cb2015-12-18 18:22:27 -050035 const char *name;
Adam Langleycca4d592015-01-12 12:01:23 -080036 tool_func_t func;
37};
38
39static const Tool kTools[] = {
Adam Langleyeb8be012015-10-27 16:03:52 -070040 { "ciphers", Ciphers },
Adam Langleycca4d592015-01-12 12:01:23 -080041 { "client", Client },
David Benjaminafe57cb2015-12-18 18:22:27 -050042 { "generate-ed25519", GenerateEd25519Key },
Adam Langley839b8812015-05-26 11:36:46 -070043 { "genrsa", GenerateRSAKey },
Adam Langleycca4d592015-01-12 12:01:23 -080044 { "md5sum", MD5Sum },
Adam Langley839b8812015-05-26 11:36:46 -070045 { "pkcs12", DoPKCS12 },
46 { "rand", Rand },
47 { "s_client", Client },
48 { "s_server", Server },
49 { "server", Server },
Adam Langleycca4d592015-01-12 12:01:23 -080050 { "sha1sum", SHA1Sum },
51 { "sha224sum", SHA224Sum },
52 { "sha256sum", SHA256Sum },
53 { "sha384sum", SHA384Sum },
54 { "sha512sum", SHA512Sum },
Adam Langley839b8812015-05-26 11:36:46 -070055 { "speed", Speed },
Adam Langleycca4d592015-01-12 12:01:23 -080056 { "", nullptr },
57};
58
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070059static void usage(const char *name) {
David Benjaminafe57cb2015-12-18 18:22:27 -050060 printf("Usage: %s COMMAND\n", name);
61 printf("\n");
62 printf("Available commands:\n");
Adam Langleycca4d592015-01-12 12:01:23 -080063
64 for (size_t i = 0;; i++) {
65 const Tool &tool = kTools[i];
66 if (tool.func == nullptr) {
67 break;
68 }
David Benjaminafe57cb2015-12-18 18:22:27 -050069 printf(" %s\n", tool.name);
Adam Langleycca4d592015-01-12 12:01:23 -080070 }
Adam Langleycca4d592015-01-12 12:01:23 -080071}
72
Piotr Sikora9bb8ba62016-03-18 18:19:04 -070073static tool_func_t FindTool(const std::string &name) {
Adam Langleycca4d592015-01-12 12:01:23 -080074 for (size_t i = 0;; i++) {
75 const Tool &tool = kTools[i];
76 if (tool.func == nullptr || name == tool.name) {
77 return tool.func;
78 }
79 }
Adam Langley95c29f32014-06-20 12:00:00 -070080}
81
Adam Langley10f97f32016-07-12 08:09:33 -070082int main(int argc, char **argv) {
Brian Smithafdaeee2015-01-26 19:54:32 -080083#if defined(OPENSSL_WINDOWS)
84 // Read and write in binary mode. This makes bssl on Windows consistent with
85 // bssl on other platforms, and also makes it consistent with MSYS's commands
86 // like diff(1) and md5sum(1). This is especially important for the digest
87 // commands.
88 if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
89 perror("_setmode(_fileno(stdin), O_BINARY)");
90 return 1;
91 }
92 if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
93 perror("_setmode(_fileno(stdout), O_BINARY)");
94 return 1;
95 }
96 if (_setmode(_fileno(stderr), _O_BINARY) == -1) {
97 perror("_setmode(_fileno(stderr), O_BINARY)");
98 return 1;
99 }
100#endif
101
David Benjamin7a1eefd2015-10-17 23:39:22 -0400102 CRYPTO_library_init();
Adam Langley95c29f32014-06-20 12:00:00 -0700103
Adam Langleycca4d592015-01-12 12:01:23 -0800104 int starting_arg = 1;
105 tool_func_t tool = nullptr;
David Benjamineee73062014-10-31 16:01:29 -0400106#if !defined(OPENSSL_WINDOWS)
Adam Langleycca4d592015-01-12 12:01:23 -0800107 tool = FindTool(basename(argv[0]));
David Benjamineee73062014-10-31 16:01:29 -0400108#endif
Adam Langleycca4d592015-01-12 12:01:23 -0800109 if (tool == nullptr) {
110 starting_arg++;
111 if (argc > 1) {
112 tool = FindTool(argv[1]);
113 }
114 }
115 if (tool == nullptr) {
Adam Langley95c29f32014-06-20 12:00:00 -0700116 usage(argv[0]);
117 return 1;
118 }
Adam Langleycca4d592015-01-12 12:01:23 -0800119
120 std::vector<std::string> args;
121 for (int i = starting_arg; i < argc; i++) {
122 args.push_back(argv[i]);
123 }
124
125 return !tool(args);
Adam Langley95c29f32014-06-20 12:00:00 -0700126}