third_party/boringssl-with-bazel/src/tool/internal.h
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 #ifndef OPENSSL_HEADER_TOOL_INTERNAL_H
16 #define OPENSSL_HEADER_TOOL_INTERNAL_H
17 
18 #include <openssl/base.h>
19 #include <openssl/span.h>
20 
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 // MSVC issues warning C4702 for unreachable code in its xtree header when
26 // compiling with -D_HAS_EXCEPTIONS=0. See
27 // https://connect.microsoft.com/VisualStudio/feedback/details/809962
29 OPENSSL_MSVC_PRAGMA(warning(disable: 4702))
30 #include <map>
32 
34  void operator()(FILE *file) {
35  fclose(file);
36  }
37 };
38 
39 using ScopedFILE = std::unique_ptr<FILE, FileCloser>;
40 
41 // The following functions abstract between POSIX and Windows differences in
42 // file descriptor I/O functions.
43 
44 // CloseFD behaves like |close|.
45 void CloseFD(int fd);
46 
47 class ScopedFD {
48  public:
49  ScopedFD() {}
50  explicit ScopedFD(int fd) : fd_(fd) {}
51  ScopedFD(ScopedFD &&other) { *this = std::move(other); }
52  ScopedFD(const ScopedFD &) = delete;
53  ~ScopedFD() { reset(); }
54 
55  ScopedFD &operator=(const ScopedFD &) = delete;
57  reset();
58  fd_ = other.fd_;
59  other.fd_ = -1;
60  return *this;
61  }
62 
63  explicit operator bool() const { return fd_ >= 0; }
64 
65  int get() const { return fd_; }
66 
67  void reset() {
68  if (fd_ >= 0) {
69  CloseFD(fd_);
70  }
71  fd_ = -1;
72  }
73 
74  int release() {
75  int fd = fd_;
76  fd_ = -1;
77  return fd;
78  }
79 
80  private:
81  int fd_ = -1;
82 };
83 
84 // OpenFD behaves like |open| but handles |EINTR| and works on Windows.
85 ScopedFD OpenFD(const char *path, int flags);
86 
87 // ReadFromFD reads up to |num| bytes from |fd| and writes the result to |out|.
88 // On success, it returns true and sets |*out_bytes_read| to the number of bytes
89 // read. Otherwise, it returns false and leaves an error in |errno|. On POSIX,
90 // it handles |EINTR| internally.
91 bool ReadFromFD(int fd, size_t *out_bytes_read, void *out, size_t num);
92 
93 // WriteToFD writes up to |num| bytes from |in| to |fd|. On success, it returns
94 // true and sets |*out_bytes_written| to the number of bytes written. Otherwise,
95 // it returns false and leaves an error in |errno|. On POSIX, it handles |EINTR|
96 // internally.
97 bool WriteToFD(int fd, size_t *out_bytes_written, const void *in, size_t num);
98 
99 // FDToFILE behaves like |fdopen|.
100 ScopedFILE FDToFILE(ScopedFD fd, const char *mode);
101 
106 };
107 
108 struct argument {
109  const char *name;
111  const char *description;
112 };
113 
114 bool ParseKeyValueArguments(std::map<std::string, std::string> *out_args, const
115  std::vector<std::string> &args, const struct argument *templates);
116 
117 void PrintUsage(const struct argument *templates);
118 
119 bool GetUnsigned(unsigned *out, const std::string &arg_name,
120  unsigned default_value,
121  const std::map<std::string, std::string> &args);
122 
123 bool ReadAll(std::vector<uint8_t> *out, FILE *in);
124 bool WriteToFile(const std::string &path, bssl::Span<const uint8_t> in);
125 
126 bool Ciphers(const std::vector<std::string> &args);
127 bool Client(const std::vector<std::string> &args);
128 bool DoPKCS12(const std::vector<std::string> &args);
129 bool GenerateECH(const std::vector<std::string> &args);
130 bool GenerateEd25519Key(const std::vector<std::string> &args);
131 bool GenerateRSAKey(const std::vector<std::string> &args);
132 bool MD5Sum(const std::vector<std::string> &args);
133 bool Rand(const std::vector<std::string> &args);
134 bool SHA1Sum(const std::vector<std::string> &args);
135 bool SHA224Sum(const std::vector<std::string> &args);
136 bool SHA256Sum(const std::vector<std::string> &args);
137 bool SHA384Sum(const std::vector<std::string> &args);
138 bool SHA512Sum(const std::vector<std::string> &args);
139 bool SHA512256Sum(const std::vector<std::string> &args);
140 bool Server(const std::vector<std::string> &args);
141 bool Sign(const std::vector<std::string> &args);
142 bool Speed(const std::vector<std::string> &args);
143 
144 // These values are DER encoded, RSA private keys.
145 extern const uint8_t kDERRSAPrivate2048[];
146 extern const size_t kDERRSAPrivate2048Len;
147 extern const uint8_t kDERRSAPrivate4096[];
148 extern const size_t kDERRSAPrivate4096Len;
149 
150 
151 #endif // !OPENSSL_HEADER_TOOL_INTERNAL_H
SHA1Sum
bool SHA1Sum(const std::vector< std::string > &args)
Definition: digest.cc:436
ArgumentType
ArgumentType
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:102
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
GenerateEd25519Key
bool GenerateEd25519Key(const std::vector< std::string > &args)
Definition: generate_ed25519.cc:37
bool
bool
Definition: setup_once.h:312
WriteToFD
bool WriteToFD(int fd, size_t *out_bytes_written, const void *in, size_t num)
Definition: fd.cc:73
argument::type
ArgumentType type
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:110
kDERRSAPrivate4096Len
const size_t kDERRSAPrivate4096Len
Definition: const.cc:325
Speed
bool Speed(const std::vector< std::string > &args)
Definition: speed.cc:1327
check_version.warning
string warning
Definition: check_version.py:46
kRequiredArgument
@ kRequiredArgument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:103
SHA512Sum
bool SHA512Sum(const std::vector< std::string > &args)
Definition: digest.cc:452
PrintUsage
void PrintUsage(const struct argument *templates)
Definition: args.cc:75
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
ParseKeyValueArguments
bool ParseKeyValueArguments(std::map< std::string, std::string > *out_args, const std::vector< std::string > &args, const struct argument *templates)
Definition: args.cc:27
SHA512256Sum
bool SHA512256Sum(const std::vector< std::string > &args)
Definition: digest.cc:456
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
mode
const char int mode
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
check_documentation.path
path
Definition: check_documentation.py:57
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
kDERRSAPrivate4096
const uint8_t kDERRSAPrivate4096[]
Definition: const.cc:126
OpenFD
ScopedFD OpenFD(const char *path, int flags)
Definition: fd.cc:33
gen_build_yaml.struct
def struct(**kwargs)
Definition: test/core/end2end/gen_build_yaml.py:30
base.h
Rand
bool Rand(const std::vector< std::string > &args)
Definition: rand.cc:37
SHA384Sum
bool SHA384Sum(const std::vector< std::string > &args)
Definition: digest.cc:448
argument::name
const char * name
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:109
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
ScopedFD::reset
void reset()
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:67
CloseFD
void CloseFD(int fd)
Definition: fd.cc:45
FileCloser
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:33
ScopedFD::~ScopedFD
~ScopedFD()
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:53
Server
bool Server(const std::vector< std::string > &args)
Definition: third_party/boringssl-with-bazel/src/tool/server.cc:217
ScopedFD
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:47
kDERRSAPrivate2048
const uint8_t kDERRSAPrivate2048[]
Definition: const.cc:21
argument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:108
ReadAll
bool ReadAll(std::vector< uint8_t > *out, FILE *in)
Definition: boringssl-with-bazel/src/tool/file.cc:27
ScopedFD::ScopedFD
ScopedFD(int fd)
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:50
OPENSSL_MSVC_PRAGMA
OPENSSL_MSVC_PRAGMA(warning(disable:4702))
Definition: e_aes.c:69
ScopedFD::release
int release()
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:74
kBooleanArgument
@ kBooleanArgument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:105
ReadFromFD
bool ReadFromFD(int fd, size_t *out_bytes_read, void *out, size_t num)
Definition: fd.cc:53
Client
bool Client(const std::vector< std::string > &args)
Definition: third_party/boringssl-with-bazel/src/tool/client.cc:381
ScopedFD::fd_
int fd_
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:81
push
int push(void *desc, unsigned char *buf, unsigned len)
Definition: bloaty/third_party/zlib/test/infcover.c:463
ScopedFD::get
int get() const
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:65
FileCloser::operator()
void operator()(FILE *file)
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:34
Ciphers
bool Ciphers(const std::vector< std::string > &args)
Definition: ciphers.cc:26
ScopedFILE
std::unique_ptr< FILE, FileCloser > ScopedFILE
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:39
benchmark.FILE
FILE
Definition: benchmark.py:21
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
grpc::fclose
fclose(creds_file)
ScopedFD::operator=
ScopedFD & operator=(ScopedFD &&other)
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:56
WriteToFile
bool WriteToFile(const std::string &path, bssl::Span< const uint8_t > in)
Definition: boringssl-with-bazel/src/tool/file.cc:56
Sign
bool Sign(const std::vector< std::string > &args)
Definition: sign.cc:31
xds_manager.num
num
Definition: xds_manager.py:56
FDToFILE
ScopedFILE FDToFILE(ScopedFD fd, const char *mode)
Definition: fd.cc:93
kOptionalArgument
@ kOptionalArgument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:104
GenerateRSAKey
bool GenerateRSAKey(const std::vector< std::string > &args)
Definition: genrsa.cc:34
MD5Sum
bool MD5Sum(const std::vector< std::string > &args)
Definition: digest.cc:432
argument::description
const char * description
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:111
ScopedFD::operator=
ScopedFD & operator=(const ScopedFD &)=delete
GetUnsigned
bool GetUnsigned(unsigned *out, const std::string &arg_name, unsigned default_value, const std::map< std::string, std::string > &args)
Definition: args.cc:82
DoPKCS12
bool DoPKCS12(const std::vector< std::string > &args)
Definition: tool/pkcs12.cc:53
span.h
GenerateECH
bool GenerateECH(const std::vector< std::string > &args)
Definition: generate_ech.cc:69
SHA224Sum
bool SHA224Sum(const std::vector< std::string > &args)
Definition: digest.cc:440
ScopedFD::ScopedFD
ScopedFD(ScopedFD &&other)
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:51
kDERRSAPrivate2048Len
const size_t kDERRSAPrivate2048Len
Definition: const.cc:124
ScopedFD::ScopedFD
ScopedFD()
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:49
SHA256Sum
bool SHA256Sum(const std::vector< std::string > &args)
Definition: digest.cc:444


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:07