Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 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> |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 19 | #include <openssl/span.h> |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 20 | |
| 21 | #include <string> |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 22 | #include <utility> |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 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 |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 28 | OPENSSL_MSVC_PRAGMA(warning(push)) |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 29 | OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 30 | #include <map> |
| 31 | |
Kaido Kert | d6c3416 | 2019-09-09 15:36:21 -0700 | [diff] [blame] | 32 | #if defined(STARBOARD) |
| 33 | #define printf(fmt, ...) SbLogFormatF(fmt, ##__VA_ARGS__) |
| 34 | #define fprintf(discard, fmt, ...) printf(fmt, ##__VA_ARGS__) |
| 35 | #endif |
| 36 | |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 37 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 38 | |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 39 | struct FileCloser { |
| 40 | void operator()(FILE *file) { |
| 41 | fclose(file); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | using ScopedFILE = std::unique_ptr<FILE, FileCloser>; |
| 46 | |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 47 | // The following functions abstract between POSIX and Windows differences in |
| 48 | // file descriptor I/O functions. |
| 49 | |
| 50 | // CloseFD behaves like |close|. |
| 51 | void CloseFD(int fd); |
| 52 | |
| 53 | class ScopedFD { |
| 54 | public: |
| 55 | ScopedFD() {} |
| 56 | explicit ScopedFD(int fd) : fd_(fd) {} |
| 57 | ScopedFD(ScopedFD &&other) { *this = std::move(other); } |
| 58 | ScopedFD(const ScopedFD &) = delete; |
| 59 | ~ScopedFD() { reset(); } |
| 60 | |
| 61 | ScopedFD &operator=(const ScopedFD &) = delete; |
| 62 | ScopedFD &operator=(ScopedFD &&other) { |
| 63 | reset(); |
| 64 | fd_ = other.fd_; |
| 65 | other.fd_ = -1; |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | explicit operator bool() const { return fd_ >= 0; } |
| 70 | |
| 71 | int get() const { return fd_; } |
| 72 | |
| 73 | void reset() { |
| 74 | if (fd_ >= 0) { |
| 75 | CloseFD(fd_); |
| 76 | } |
| 77 | fd_ = -1; |
| 78 | } |
| 79 | |
| 80 | int release() { |
| 81 | int fd = fd_; |
| 82 | fd_ = -1; |
| 83 | return fd; |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | int fd_ = -1; |
| 88 | }; |
| 89 | |
| 90 | // OpenFD behaves like |open| but handles |EINTR| and works on Windows. |
| 91 | ScopedFD OpenFD(const char *path, int flags); |
| 92 | |
| 93 | // ReadFromFD reads up to |num| bytes from |fd| and writes the result to |out|. |
| 94 | // On success, it returns true and sets |*out_bytes_read| to the number of bytes |
| 95 | // read. Otherwise, it returns false and leaves an error in |errno|. On POSIX, |
| 96 | // it handles |EINTR| internally. |
| 97 | bool ReadFromFD(int fd, size_t *out_bytes_read, void *out, size_t num); |
| 98 | |
| 99 | // WriteToFD writes up to |num| bytes from |in| to |fd|. On success, it returns |
| 100 | // true and sets |*out_bytes_written| to the number of bytes written. Otherwise, |
| 101 | // it returns false and leaves an error in |errno|. On POSIX, it handles |EINTR| |
| 102 | // internally. |
| 103 | bool WriteToFD(int fd, size_t *out_bytes_written, const void *in, size_t num); |
| 104 | |
| 105 | // FDToFILE behaves like |fdopen|. |
| 106 | ScopedFILE FDToFILE(ScopedFD fd, const char *mode); |
| 107 | |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 108 | enum ArgumentType { |
| 109 | kRequiredArgument, |
| 110 | kOptionalArgument, |
| 111 | kBooleanArgument, |
| 112 | }; |
| 113 | |
| 114 | struct argument { |
| 115 | const char *name; |
| 116 | ArgumentType type; |
| 117 | const char *description; |
| 118 | }; |
| 119 | |
| 120 | bool ParseKeyValueArguments(std::map<std::string, std::string> *out_args, const |
| 121 | std::vector<std::string> &args, const struct argument *templates); |
| 122 | |
| 123 | void PrintUsage(const struct argument *templates); |
| 124 | |
| 125 | bool GetUnsigned(unsigned *out, const std::string &arg_name, |
| 126 | unsigned default_value, |
| 127 | const std::map<std::string, std::string> &args); |
| 128 | |
| 129 | bool ReadAll(std::vector<uint8_t> *out, FILE *in); |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 130 | bool WriteToFile(const std::string &path, bssl::Span<const uint8_t> in); |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 131 | |
| 132 | bool Ciphers(const std::vector<std::string> &args); |
| 133 | bool Client(const std::vector<std::string> &args); |
| 134 | bool DoPKCS12(const std::vector<std::string> &args); |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 135 | bool GenerateECH(const std::vector<std::string> &args); |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 136 | bool GenerateEd25519Key(const std::vector<std::string> &args); |
| 137 | bool GenerateRSAKey(const std::vector<std::string> &args); |
| 138 | bool MD5Sum(const std::vector<std::string> &args); |
| 139 | bool Rand(const std::vector<std::string> &args); |
| 140 | bool SHA1Sum(const std::vector<std::string> &args); |
| 141 | bool SHA224Sum(const std::vector<std::string> &args); |
| 142 | bool SHA256Sum(const std::vector<std::string> &args); |
| 143 | bool SHA384Sum(const std::vector<std::string> &args); |
| 144 | bool SHA512Sum(const std::vector<std::string> &args); |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 145 | bool SHA512256Sum(const std::vector<std::string> &args); |
Andrew Top | dee8b29 | 2019-01-22 14:48:26 -0800 | [diff] [blame] | 146 | bool Server(const std::vector<std::string> &args); |
| 147 | bool Sign(const std::vector<std::string> &args); |
| 148 | bool Speed(const std::vector<std::string> &args); |
| 149 | |
| 150 | // These values are DER encoded, RSA private keys. |
| 151 | extern const uint8_t kDERRSAPrivate2048[]; |
| 152 | extern const size_t kDERRSAPrivate2048Len; |
| 153 | extern const uint8_t kDERRSAPrivate4096[]; |
| 154 | extern const size_t kDERRSAPrivate4096Len; |
| 155 | |
| 156 | |
| 157 | #endif // !OPENSSL_HEADER_TOOL_INTERNAL_H |