diff options
author | Adam Langley <agl@google.com> | 2015-01-12 12:01:23 -0800 |
---|---|---|
committer | Adam Langley <agl@google.com> | 2015-01-26 21:27:18 +0000 |
commit | cca4d5991f8319c967b3e3187e8a3a2cd162d702 (patch) | |
tree | 168b7833fa013dad1d4918cf12d67e4d03b8145d /tool/tool.cc | |
parent | cd5c892a871389dfa077bda6eec64ffa62dac722 (diff) | |
download | boringssl-cca4d5991f8319c967b3e3187e8a3a2cd162d702.zip boringssl-cca4d5991f8319c967b3e3187e8a3a2cd162d702.tar.gz boringssl-cca4d5991f8319c967b3e3187e8a3a2cd162d702.tar.bz2 |
Add digest sum handling to the tool.
Android might want to replace the system *sum (i.e. md5sum, sha256sum
etc) binaries with a symlink to the BoringSSL tool binary.
This change also allows the tool to figure out what to do based on
argv[0] if it matches one of the known commands.
Change-Id: Ia4fc3cff45ce2ae623dae6786eea5d7ad127d44b
Reviewed-on: https://boringssl-review.googlesource.com/2940
Reviewed-by: Adam Langley <agl@google.com>
Diffstat (limited to 'tool/tool.cc')
-rw-r--r-- | tool/tool.cc | 92 |
1 files changed, 73 insertions, 19 deletions
diff --git a/tool/tool.cc b/tool/tool.cc index a57cd16..88b6f24 100644 --- a/tool/tool.cc +++ b/tool/tool.cc @@ -18,43 +18,97 @@ #include <openssl/err.h> #include <openssl/ssl.h> +#if !defined(OPENSSL_WINDOWS) +#include <libgen.h> +#endif + #if !defined(OPENSSL_WINDOWS) bool Client(const std::vector<std::string> &args); bool Server(const std::vector<std::string> &args); +bool MD5Sum(const std::vector<std::string> &args); +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); #endif bool DoPKCS12(const std::vector<std::string> &args); bool Speed(const std::vector<std::string> &args); +typedef bool (*tool_func_t)(const std::vector<std::string> &args); + +struct Tool { + char name[16]; + tool_func_t func; +}; + +static const Tool kTools[] = { + { "speed", Speed }, + { "pkcs12", DoPKCS12 }, +#if !defined(OPENSSL_WINDOWS) + { "client", Client }, + { "s_client", Client }, + { "server", Server }, + { "s_server", Server }, + { "md5sum", MD5Sum }, + { "sha1sum", SHA1Sum }, + { "sha224sum", SHA224Sum }, + { "sha256sum", SHA256Sum }, + { "sha384sum", SHA384Sum }, + { "sha512sum", SHA512Sum }, +#endif + { "", nullptr }, +}; + static void usage(const char *name) { - printf("Usage: %s [speed|client|server|pkcs12]\n", name); + printf("Usage: %s [", name); + + for (size_t i = 0;; i++) { + const Tool &tool = kTools[i]; + if (tool.func == nullptr) { + break; + } + if (i > 0) { + printf("|"); + } + printf("%s", tool.name); + } + printf("]\n"); } -int main(int argc, char **argv) { - std::string tool; - if (argc >= 2) { - tool = argv[1]; +tool_func_t FindTool(const std::string &name) { + for (size_t i = 0;; i++) { + const Tool &tool = kTools[i]; + if (tool.func == nullptr || name == tool.name) { + return tool.func; + } } +} +int main(int argc, char **argv) { SSL_library_init(); - std::vector<std::string> args; - for (int i = 2; i < argc; i++) { - args.push_back(argv[i]); - } - - if (tool == "speed") { - return !Speed(args); + int starting_arg = 1; + tool_func_t tool = nullptr; #if !defined(OPENSSL_WINDOWS) - } else if (tool == "s_client" || tool == "client") { - return !Client(args); - } else if (tool == "s_server" || tool == "server") { - return !Server(args); + tool = FindTool(basename(argv[0])); #endif - } else if (tool == "pkcs12") { - return !DoPKCS12(args); - } else { + if (tool == nullptr) { + starting_arg++; + if (argc > 1) { + tool = FindTool(argv[1]); + } + } + if (tool == nullptr) { usage(argv[0]); return 1; } + + std::vector<std::string> args; + for (int i = starting_arg; i < argc; i++) { + args.push_back(argv[i]); + } + + return !tool(args); } |