• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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>
16 #include <vector>
17 
18 #include <openssl/crypto.h>
19 #include <openssl/err.h>
20 #include <openssl/ssl.h>
21 
22 #if defined(OPENSSL_WINDOWS)
23 #include <fcntl.h>
24 #include <io.h>
25 #else
26 #include <libgen.h>
27 #endif
28 
29 
30 bool Ciphers(const std::vector<std::string> &args);
31 bool Client(const std::vector<std::string> &args);
32 bool DoPKCS12(const std::vector<std::string> &args);
33 bool GenerateEd25519Key(const std::vector<std::string> &args);
34 bool GenerateRSAKey(const std::vector<std::string> &args);
35 bool MD5Sum(const std::vector<std::string> &args);
36 bool Rand(const std::vector<std::string> &args);
37 bool SHA1Sum(const std::vector<std::string> &args);
38 bool SHA224Sum(const std::vector<std::string> &args);
39 bool SHA256Sum(const std::vector<std::string> &args);
40 bool SHA384Sum(const std::vector<std::string> &args);
41 bool SHA512Sum(const std::vector<std::string> &args);
42 bool Server(const std::vector<std::string> &args);
43 bool Speed(const std::vector<std::string> &args);
44 
45 typedef bool (*tool_func_t)(const std::vector<std::string> &args);
46 
47 struct Tool {
48   const char *name;
49   tool_func_t func;
50 };
51 
52 static const Tool kTools[] = {
53   { "ciphers", Ciphers },
54   { "client", Client },
55   { "generate-ed25519", GenerateEd25519Key },
56   { "genrsa", GenerateRSAKey },
57   { "md5sum", MD5Sum },
58   { "pkcs12", DoPKCS12 },
59   { "rand", Rand },
60   { "s_client", Client },
61   { "s_server", Server },
62   { "server", Server },
63   { "sha1sum", SHA1Sum },
64   { "sha224sum", SHA224Sum },
65   { "sha256sum", SHA256Sum },
66   { "sha384sum", SHA384Sum },
67   { "sha512sum", SHA512Sum },
68   { "speed", Speed },
69   { "", nullptr },
70 };
71 
usage(const char * name)72 static void usage(const char *name) {
73   printf("Usage: %s COMMAND\n", name);
74   printf("\n");
75   printf("Available commands:\n");
76 
77   for (size_t i = 0;; i++) {
78     const Tool &tool = kTools[i];
79     if (tool.func == nullptr) {
80       break;
81     }
82     printf("    %s\n", tool.name);
83   }
84 }
85 
FindTool(const std::string & name)86 tool_func_t FindTool(const std::string &name) {
87   for (size_t i = 0;; i++) {
88     const Tool &tool = kTools[i];
89     if (tool.func == nullptr || name == tool.name) {
90       return tool.func;
91     }
92   }
93 }
94 
main(int argc,char ** argv)95 int main(int argc, char **argv) {
96 #if defined(OPENSSL_WINDOWS)
97   // Read and write in binary mode. This makes bssl on Windows consistent with
98   // bssl on other platforms, and also makes it consistent with MSYS's commands
99   // like diff(1) and md5sum(1). This is especially important for the digest
100   // commands.
101   if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
102     perror("_setmode(_fileno(stdin), O_BINARY)");
103     return 1;
104   }
105   if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
106     perror("_setmode(_fileno(stdout), O_BINARY)");
107     return 1;
108   }
109   if (_setmode(_fileno(stderr), _O_BINARY) == -1) {
110     perror("_setmode(_fileno(stderr), O_BINARY)");
111     return 1;
112   }
113 #endif
114 
115   CRYPTO_library_init();
116 
117   int starting_arg = 1;
118   tool_func_t tool = nullptr;
119 #if !defined(OPENSSL_WINDOWS)
120   tool = FindTool(basename(argv[0]));
121 #endif
122   if (tool == nullptr) {
123     starting_arg++;
124     if (argc > 1) {
125       tool = FindTool(argv[1]);
126     }
127   }
128   if (tool == nullptr) {
129     usage(argv[0]);
130     return 1;
131   }
132 
133   std::vector<std::string> args;
134   for (int i = starting_arg; i < argc; i++) {
135     args.push_back(argv[i]);
136   }
137 
138   return !tool(args);
139 }
140