tool.cc
Go to the documentation of this file.
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 
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;
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 
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 
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 
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 
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)) {
139  return 1;
140  }
141 
142  return 0;
143 }
FindTool
static tool_func_t FindTool(const std::string &name)
Definition: tool.cc:83
internal.h
bool
bool
Definition: setup_once.h:312
demumble_test.stdout
stdout
Definition: demumble_test.py:38
CRYPTO_library_init
#define CRYPTO_library_init
Definition: boringssl_prefix_symbols.h:1175
Speed
bool Speed(const std::vector< std::string > &args)
Definition: speed.cc:1327
MD5Sum
bool MD5Sum(const std::vector< std::string > &args)
Definition: digest.cc:432
kTools
static const Tool kTools[]
Definition: tool.cc:45
Client
bool Client(const std::vector< std::string > &args)
Definition: third_party/boringssl-with-bazel/src/tool/client.cc:381
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
ERR_print_errors_fp
#define ERR_print_errors_fp
Definition: boringssl_prefix_symbols.h:1437
Ciphers
bool Ciphers(const std::vector< std::string > &args)
Definition: ciphers.cc:26
framework.rpc.grpc_channelz.Server
Server
Definition: grpc_channelz.py:42
setup.name
name
Definition: setup.py:542
demumble_test.stdin
stdin
Definition: demumble_test.py:37
Rand
bool Rand(const std::vector< std::string > &args)
Definition: rand.cc:37
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
SHA512256Sum
bool SHA512256Sum(const std::vector< std::string > &args)
Definition: digest.cc:456
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
signal
static void signal(notification *n)
Definition: alts_tsi_handshaker_test.cc:107
IsFIPS
static bool IsFIPS(const std::vector< std::string > &args)
Definition: tool.cc:33
FIPS_mode
#define FIPS_mode
Definition: boringssl_prefix_symbols.h:1761
usage
static void usage(const char *name)
Definition: tool.cc:69
GenerateRSAKey
bool GenerateRSAKey(const std::vector< std::string > &args)
Definition: genrsa.cc:34
err.h
crypto.h
ssl.h
tool_func_t
bool(* tool_func_t)(const std::vector< std::string > &args)
Definition: tool.cc:38
main
int main(int argc, char **argv)
Definition: tool.cc:92
GenerateECH
bool GenerateECH(const std::vector< std::string > &args)
Definition: generate_ech.cc:69
Tool::name
const char * name
Definition: tool.cc:41
SHA224Sum
bool SHA224Sum(const std::vector< std::string > &args)
Definition: digest.cc:440
Tool::func
tool_func_t func
Definition: tool.cc:42
Tool
Definition: tool.cc:40
Sign
bool Sign(const std::vector< std::string > &args)
Definition: sign.cc:31
SHA512Sum
bool SHA512Sum(const std::vector< std::string > &args)
Definition: digest.cc:452
SHA384Sum
bool SHA384Sum(const std::vector< std::string > &args)
Definition: digest.cc:448
GenerateEd25519Key
bool GenerateEd25519Key(const std::vector< std::string > &args)
Definition: generate_ed25519.cc:37
DoPKCS12
bool DoPKCS12(const std::vector< std::string > &args)
Definition: tool/pkcs12.cc:53
SHA256Sum
bool SHA256Sum(const std::vector< std::string > &args)
Definition: digest.cc:444
SHA1Sum
bool SHA1Sum(const std::vector< std::string > &args)
Definition: digest.cc:436
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:40