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