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