blob: f67f0c7dd91523dec5528e8ee87ab70211a96f93 [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
Adam Langley95c29f32014-06-20 12:00:00 -070029
Adam Langleyaacec172014-06-20 12:00:00 -070030bool Client(const std::vector<std::string> &args);
Adam Langley839b8812015-05-26 11:36:46 -070031bool DoPKCS12(const std::vector<std::string> &args);
32bool GenerateRSAKey(const std::vector<std::string> &args);
Adam Langleycca4d592015-01-12 12:01:23 -080033bool MD5Sum(const std::vector<std::string> &args);
Adam Langley839b8812015-05-26 11:36:46 -070034bool Rand(const std::vector<std::string> &args);
Adam Langleycca4d592015-01-12 12:01:23 -080035bool SHA1Sum(const std::vector<std::string> &args);
36bool SHA224Sum(const std::vector<std::string> &args);
37bool SHA256Sum(const std::vector<std::string> &args);
38bool SHA384Sum(const std::vector<std::string> &args);
39bool SHA512Sum(const std::vector<std::string> &args);
Adam Langley839b8812015-05-26 11:36:46 -070040bool Server(const std::vector<std::string> &args);
Adam Langley8e16b6e2014-08-21 14:11:39 -070041bool Speed(const std::vector<std::string> &args);
Adam Langley95c29f32014-06-20 12:00:00 -070042
Adam Langleycca4d592015-01-12 12:01:23 -080043typedef bool (*tool_func_t)(const std::vector<std::string> &args);
44
45struct Tool {
46 char name[16];
47 tool_func_t func;
48};
49
50static const Tool kTools[] = {
Adam Langleycca4d592015-01-12 12:01:23 -080051 { "client", Client },
Adam Langley839b8812015-05-26 11:36:46 -070052 { "genrsa", GenerateRSAKey },
Adam Langleycca4d592015-01-12 12:01:23 -080053 { "md5sum", MD5Sum },
Adam Langley839b8812015-05-26 11:36:46 -070054 { "pkcs12", DoPKCS12 },
55 { "rand", Rand },
56 { "s_client", Client },
57 { "s_server", Server },
58 { "server", Server },
Adam Langleycca4d592015-01-12 12:01:23 -080059 { "sha1sum", SHA1Sum },
60 { "sha224sum", SHA224Sum },
61 { "sha256sum", SHA256Sum },
62 { "sha384sum", SHA384Sum },
63 { "sha512sum", SHA512Sum },
Adam Langley839b8812015-05-26 11:36:46 -070064 { "speed", Speed },
Adam Langleycca4d592015-01-12 12:01:23 -080065 { "", nullptr },
66};
67
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070068static void usage(const char *name) {
Adam Langleycca4d592015-01-12 12:01:23 -080069 printf("Usage: %s [", name);
70
71 for (size_t i = 0;; i++) {
72 const Tool &tool = kTools[i];
73 if (tool.func == nullptr) {
74 break;
75 }
76 if (i > 0) {
77 printf("|");
78 }
79 printf("%s", tool.name);
80 }
81 printf("]\n");
82}
83
84tool_func_t FindTool(const std::string &name) {
85 for (size_t i = 0;; i++) {
86 const Tool &tool = kTools[i];
87 if (tool.func == nullptr || name == tool.name) {
88 return tool.func;
89 }
90 }
Adam Langley95c29f32014-06-20 12:00:00 -070091}
92
93int main(int argc, char **argv) {
Brian Smithafdaeee2015-01-26 19:54:32 -080094#if defined(OPENSSL_WINDOWS)
95 // Read and write in binary mode. This makes bssl on Windows consistent with
96 // bssl on other platforms, and also makes it consistent with MSYS's commands
97 // like diff(1) and md5sum(1). This is especially important for the digest
98 // commands.
99 if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
100 perror("_setmode(_fileno(stdin), O_BINARY)");
101 return 1;
102 }
103 if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
104 perror("_setmode(_fileno(stdout), O_BINARY)");
105 return 1;
106 }
107 if (_setmode(_fileno(stderr), _O_BINARY) == -1) {
108 perror("_setmode(_fileno(stderr), O_BINARY)");
109 return 1;
110 }
111#endif
112
David Benjamin7a1eefd2015-10-17 23:39:22 -0400113 CRYPTO_library_init();
Adam Langley95c29f32014-06-20 12:00:00 -0700114
Adam Langleycca4d592015-01-12 12:01:23 -0800115 int starting_arg = 1;
116 tool_func_t tool = nullptr;
David Benjamineee73062014-10-31 16:01:29 -0400117#if !defined(OPENSSL_WINDOWS)
Adam Langleycca4d592015-01-12 12:01:23 -0800118 tool = FindTool(basename(argv[0]));
David Benjamineee73062014-10-31 16:01:29 -0400119#endif
Adam Langleycca4d592015-01-12 12:01:23 -0800120 if (tool == nullptr) {
121 starting_arg++;
122 if (argc > 1) {
123 tool = FindTool(argv[1]);
124 }
125 }
126 if (tool == nullptr) {
Adam Langley95c29f32014-06-20 12:00:00 -0700127 usage(argv[0]);
128 return 1;
129 }
Adam Langleycca4d592015-01-12 12:01:23 -0800130
131 std::vector<std::string> args;
132 for (int i = starting_arg; i < argc; i++) {
133 args.push_back(argv[i]);
134 }
135
136 return !tool(args);
Adam Langley95c29f32014-06-20 12:00:00 -0700137}