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