blob: e4d5201bbce8cfdc184d022263c0ea1022057481 [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
Adam Langley95c29f32014-06-20 12:00:00 -070018#include <openssl/err.h>
Adam Langleyaacec172014-06-20 12:00:00 -070019#include <openssl/ssl.h>
Adam Langley95c29f32014-06-20 12:00:00 -070020
Brian Smithafdaeee2015-01-26 19:54:32 -080021#if defined(OPENSSL_WINDOWS)
22#include <fcntl.h>
23#include <io.h>
24#else
Adam Langleycca4d592015-01-12 12:01:23 -080025#include <libgen.h>
26#endif
27
Adam Langley95c29f32014-06-20 12:00:00 -070028
Adam Langleyaacec172014-06-20 12:00:00 -070029bool Client(const std::vector<std::string> &args);
Adam Langley839b8812015-05-26 11:36:46 -070030bool DoPKCS12(const std::vector<std::string> &args);
31bool GenerateRSAKey(const std::vector<std::string> &args);
Adam Langleycca4d592015-01-12 12:01:23 -080032bool MD5Sum(const std::vector<std::string> &args);
Adam Langley839b8812015-05-26 11:36:46 -070033bool Rand(const std::vector<std::string> &args);
Adam Langleycca4d592015-01-12 12:01:23 -080034bool SHA1Sum(const std::vector<std::string> &args);
35bool SHA224Sum(const std::vector<std::string> &args);
36bool SHA256Sum(const std::vector<std::string> &args);
37bool SHA384Sum(const std::vector<std::string> &args);
38bool SHA512Sum(const std::vector<std::string> &args);
Adam Langley839b8812015-05-26 11:36:46 -070039bool Server(const std::vector<std::string> &args);
Adam Langley8e16b6e2014-08-21 14:11:39 -070040bool Speed(const std::vector<std::string> &args);
Adam Langley95c29f32014-06-20 12:00:00 -070041
Adam Langleycca4d592015-01-12 12:01:23 -080042typedef bool (*tool_func_t)(const std::vector<std::string> &args);
43
44struct Tool {
45 char name[16];
46 tool_func_t func;
47};
48
49static const Tool kTools[] = {
Adam Langleycca4d592015-01-12 12:01:23 -080050 { "client", Client },
Adam Langley839b8812015-05-26 11:36:46 -070051 { "genrsa", GenerateRSAKey },
Adam Langleycca4d592015-01-12 12:01:23 -080052 { "md5sum", MD5Sum },
Adam Langley839b8812015-05-26 11:36:46 -070053 { "pkcs12", DoPKCS12 },
54 { "rand", Rand },
55 { "s_client", Client },
56 { "s_server", Server },
57 { "server", Server },
Adam Langleycca4d592015-01-12 12:01:23 -080058 { "sha1sum", SHA1Sum },
59 { "sha224sum", SHA224Sum },
60 { "sha256sum", SHA256Sum },
61 { "sha384sum", SHA384Sum },
62 { "sha512sum", SHA512Sum },
Adam Langley839b8812015-05-26 11:36:46 -070063 { "speed", Speed },
Adam Langleycca4d592015-01-12 12:01:23 -080064 { "", nullptr },
65};
66
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070067static void usage(const char *name) {
Adam Langleycca4d592015-01-12 12:01:23 -080068 printf("Usage: %s [", name);
69
70 for (size_t i = 0;; i++) {
71 const Tool &tool = kTools[i];
72 if (tool.func == nullptr) {
73 break;
74 }
75 if (i > 0) {
76 printf("|");
77 }
78 printf("%s", tool.name);
79 }
80 printf("]\n");
81}
82
83tool_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 }
Adam Langley95c29f32014-06-20 12:00:00 -070090}
91
92int main(int argc, char **argv) {
Brian Smithafdaeee2015-01-26 19:54:32 -080093#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#endif
111
Adam Langleyaacec172014-06-20 12:00:00 -0700112 SSL_library_init();
Adam Langley95c29f32014-06-20 12:00:00 -0700113
Adam Langleycca4d592015-01-12 12:01:23 -0800114 int starting_arg = 1;
115 tool_func_t tool = nullptr;
David Benjamineee73062014-10-31 16:01:29 -0400116#if !defined(OPENSSL_WINDOWS)
Adam Langleycca4d592015-01-12 12:01:23 -0800117 tool = FindTool(basename(argv[0]));
David Benjamineee73062014-10-31 16:01:29 -0400118#endif
Adam Langleycca4d592015-01-12 12:01:23 -0800119 if (tool == nullptr) {
120 starting_arg++;
121 if (argc > 1) {
122 tool = FindTool(argv[1]);
123 }
124 }
125 if (tool == nullptr) {
Adam Langley95c29f32014-06-20 12:00:00 -0700126 usage(argv[0]);
127 return 1;
128 }
Adam Langleycca4d592015-01-12 12:01:23 -0800129
130 std::vector<std::string> args;
131 for (int i = starting_arg; i < argc; i++) {
132 args.push_back(argv[i]);
133 }
134
135 return !tool(args);
Adam Langley95c29f32014-06-20 12:00:00 -0700136}