2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-10-18 04:39:22 +01:00
|
|
|
#include <openssl/crypto.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/err.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/ssl.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-27 03:54:32 +00:00
|
|
|
#if defined(OPENSSL_WINDOWS)
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.h>
|
|
|
|
#else
|
2015-01-12 20:01:23 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-10-27 23:03:52 +00:00
|
|
|
bool Ciphers(const std::vector<std::string> &args);
|
2014-06-20 20:00:00 +01:00
|
|
|
bool Client(const std::vector<std::string> &args);
|
2015-05-26 19:36:46 +01:00
|
|
|
bool DoPKCS12(const std::vector<std::string> &args);
|
2015-12-18 23:22:27 +00:00
|
|
|
bool GenerateEd25519Key(const std::vector<std::string> &args);
|
2015-05-26 19:36:46 +01:00
|
|
|
bool GenerateRSAKey(const std::vector<std::string> &args);
|
2015-01-12 20:01:23 +00:00
|
|
|
bool MD5Sum(const std::vector<std::string> &args);
|
2015-05-26 19:36:46 +01:00
|
|
|
bool Rand(const std::vector<std::string> &args);
|
2015-01-12 20:01:23 +00:00
|
|
|
bool SHA1Sum(const std::vector<std::string> &args);
|
|
|
|
bool SHA224Sum(const std::vector<std::string> &args);
|
|
|
|
bool SHA256Sum(const std::vector<std::string> &args);
|
|
|
|
bool SHA384Sum(const std::vector<std::string> &args);
|
|
|
|
bool SHA512Sum(const std::vector<std::string> &args);
|
2015-05-26 19:36:46 +01:00
|
|
|
bool Server(const std::vector<std::string> &args);
|
2014-08-21 22:11:39 +01:00
|
|
|
bool Speed(const std::vector<std::string> &args);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-12 20:01:23 +00:00
|
|
|
typedef bool (*tool_func_t)(const std::vector<std::string> &args);
|
|
|
|
|
|
|
|
struct Tool {
|
2015-12-18 23:22:27 +00:00
|
|
|
const char *name;
|
2015-01-12 20:01:23 +00:00
|
|
|
tool_func_t func;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const Tool kTools[] = {
|
2015-10-27 23:03:52 +00:00
|
|
|
{ "ciphers", Ciphers },
|
2015-01-12 20:01:23 +00:00
|
|
|
{ "client", Client },
|
2015-12-18 23:22:27 +00:00
|
|
|
{ "generate-ed25519", GenerateEd25519Key },
|
2015-05-26 19:36:46 +01:00
|
|
|
{ "genrsa", GenerateRSAKey },
|
|
|
|
{ "md5sum", MD5Sum },
|
|
|
|
{ "pkcs12", DoPKCS12 },
|
|
|
|
{ "rand", Rand },
|
2015-01-12 20:01:23 +00:00
|
|
|
{ "s_client", Client },
|
|
|
|
{ "s_server", Server },
|
2015-05-26 19:36:46 +01:00
|
|
|
{ "server", Server },
|
2015-01-12 20:01:23 +00:00
|
|
|
{ "sha1sum", SHA1Sum },
|
|
|
|
{ "sha224sum", SHA224Sum },
|
|
|
|
{ "sha256sum", SHA256Sum },
|
|
|
|
{ "sha384sum", SHA384Sum },
|
|
|
|
{ "sha512sum", SHA512Sum },
|
2015-05-26 19:36:46 +01:00
|
|
|
{ "speed", Speed },
|
2015-01-12 20:01:23 +00:00
|
|
|
{ "", nullptr },
|
|
|
|
};
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
static void usage(const char *name) {
|
2015-12-18 23:22:27 +00:00
|
|
|
printf("Usage: %s COMMAND\n", name);
|
|
|
|
printf("\n");
|
|
|
|
printf("Available commands:\n");
|
2015-01-12 20:01:23 +00:00
|
|
|
|
|
|
|
for (size_t i = 0;; i++) {
|
|
|
|
const Tool &tool = kTools[i];
|
|
|
|
if (tool.func == nullptr) {
|
|
|
|
break;
|
|
|
|
}
|
2015-12-18 23:22:27 +00:00
|
|
|
printf(" %s\n", tool.name);
|
2015-01-12 20:01:23 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-19 01:19:04 +00:00
|
|
|
static tool_func_t FindTool(const std::string &name) {
|
2015-01-12 20:01:23 +00:00
|
|
|
for (size_t i = 0;; i++) {
|
|
|
|
const Tool &tool = kTools[i];
|
|
|
|
if (tool.func == nullptr || name == tool.name) {
|
|
|
|
return tool.func;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-01-12 20:01:23 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-12 20:01:23 +00:00
|
|
|
int main(int argc, char **argv) {
|
2015-01-27 03:54:32 +00:00
|
|
|
#if defined(OPENSSL_WINDOWS)
|
|
|
|
// Read and write in binary mode. This makes bssl on Windows consistent with
|
|
|
|
// bssl on other platforms, and also makes it consistent with MSYS's commands
|
|
|
|
// like diff(1) and md5sum(1). This is especially important for the digest
|
|
|
|
// commands.
|
|
|
|
if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
|
|
|
|
perror("_setmode(_fileno(stdin), O_BINARY)");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
|
|
|
|
perror("_setmode(_fileno(stdout), O_BINARY)");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (_setmode(_fileno(stderr), _O_BINARY) == -1) {
|
|
|
|
perror("_setmode(_fileno(stderr), O_BINARY)");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-18 04:39:22 +01:00
|
|
|
CRYPTO_library_init();
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-12 20:01:23 +00:00
|
|
|
int starting_arg = 1;
|
|
|
|
tool_func_t tool = nullptr;
|
2014-10-31 20:01:29 +00:00
|
|
|
#if !defined(OPENSSL_WINDOWS)
|
2015-01-12 20:01:23 +00:00
|
|
|
tool = FindTool(basename(argv[0]));
|
2014-10-31 20:01:29 +00:00
|
|
|
#endif
|
2015-01-12 20:01:23 +00:00
|
|
|
if (tool == nullptr) {
|
|
|
|
starting_arg++;
|
|
|
|
if (argc > 1) {
|
|
|
|
tool = FindTool(argv[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tool == nullptr) {
|
2014-06-20 20:00:00 +01:00
|
|
|
usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-01-12 20:01:23 +00:00
|
|
|
|
|
|
|
std::vector<std::string> args;
|
|
|
|
for (int i = starting_arg; i < argc; i++) {
|
|
|
|
args.push_back(argv[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !tool(args);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|