| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 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 <map> |
| 22 | #include <string> |
| 23 | #include <utility> |
| 24 | #include <vector> |
| 25 |
| 26 | struct FileCloser { |
| 27 | void operator()(FILE *file) { |
| 28 | fclose(file); |
| 29 | } |
| 30 | }; |
| 31 |
| 32 | using ScopedFILE = std::unique_ptr<FILE, FileCloser>; |
| 33 |
| 34 | // The following functions abstract between POSIX and Windows differences in |
| 35 | // file descriptor I/O functions. |
| 36 |
| 37 | // CloseFD behaves like |close|. |
| 38 | void CloseFD(int fd); |
| 39 |
| 40 | class ScopedFD { |
| 41 | public: |
| 42 | ScopedFD() {} |
| 43 | explicit ScopedFD(int fd) : fd_(fd) {} |
| 44 | ScopedFD(ScopedFD &&other) { *this = std::move(other); } |
| 45 | ScopedFD(const ScopedFD &) = delete; |
| 46 | ~ScopedFD() { reset(); } |
| 47 |
| 48 | ScopedFD &operator=(const ScopedFD &) = delete; |
| 49 | ScopedFD &operator=(ScopedFD &&other) { |
| 50 | reset(); |
| 51 | fd_ = other.fd_; |
| 52 | other.fd_ = -1; |
| 53 | return *this; |
| 54 | } |
| 55 |
| 56 | explicit operator bool() const { return fd_ >= 0; } |
| 57 |
| 58 | int get() const { return fd_; } |
| 59 |
| 60 | void reset() { |
| 61 | if (fd_ >= 0) { |
| 62 | CloseFD(fd_); |
| 63 | } |
| 64 | fd_ = -1; |
| 65 | } |
| 66 |
| 67 | int release() { |
| 68 | int fd = fd_; |
| 69 | fd_ = -1; |
| 70 | return fd; |
| 71 | } |
| 72 |
| 73 | private: |
| 74 | int fd_ = -1; |
| 75 | }; |
| 76 |
| 77 | // OpenFD behaves like |open| but handles |EINTR| and works on Windows. |
| 78 | ScopedFD OpenFD(const char *path, int flags); |
| 79 |
| 80 | // ReadFromFD reads up to |num| bytes from |fd| and writes the result to |out|. |
| 81 | // On success, it returns true and sets |*out_bytes_read| to the number of bytes |
| 82 | // read. Otherwise, it returns false and leaves an error in |errno|. On POSIX, |
| 83 | // it handles |EINTR| internally. |
| 84 | bool ReadFromFD(int fd, size_t *out_bytes_read, void *out, size_t num); |
| 85 |
| 86 | // WriteToFD writes up to |num| bytes from |in| to |fd|. On success, it returns |
| 87 | // true and sets |*out_bytes_written| to the number of bytes written. Otherwise, |
| 88 | // it returns false and leaves an error in |errno|. On POSIX, it handles |EINTR| |
| 89 | // internally. |
| 90 | bool WriteToFD(int fd, size_t *out_bytes_written, const void *in, size_t num); |
| 91 |
| 92 | // FDToFILE behaves like |fdopen|. |
| 93 | ScopedFILE FDToFILE(ScopedFD fd, const char *mode); |
| 94 |
| 95 | enum ArgumentType { |
| 96 | kRequiredArgument, |
| 97 | kOptionalArgument, |
| 98 | kBooleanArgument, |
| 99 | }; |
| 100 |
| 101 | struct argument { |
| 102 | const char *name; |
| 103 | ArgumentType type; |
| 104 | const char *description; |
| 105 | }; |
| 106 |
| 107 | bool ParseKeyValueArguments(std::map<std::string, std::string> *out_args, const |
| 108 | std::vector<std::string> &args, const struct argument *templates); |
| 109 |
| 110 | void PrintUsage(const struct argument *templates); |
| 111 |
| 112 | bool GetUnsigned(unsigned *out, const std::string &arg_name, |
| 113 | unsigned default_value, |
| 114 | const std::map<std::string, std::string> &args); |
| 115 |
| 116 | bool ReadAll(std::vector<uint8_t> *out, FILE *in); |
| 117 | bool WriteToFile(const std::string &path, bssl::Span<const uint8_t> in); |
| 118 |
| 119 | bool Ciphers(const std::vector<std::string> &args); |
| 120 | bool Client(const std::vector<std::string> &args); |
| 121 | bool DoPKCS12(const std::vector<std::string> &args); |
| 122 | bool GenerateECH(const std::vector<std::string> &args); |
| 123 | bool GenerateEd25519Key(const std::vector<std::string> &args); |
| 124 | bool GenerateRSAKey(const std::vector<std::string> &args); |
| 125 | bool MD5Sum(const std::vector<std::string> &args); |
| 126 | bool Rand(const std::vector<std::string> &args); |
| 127 | bool SHA1Sum(const std::vector<std::string> &args); |
| 128 | bool SHA224Sum(const std::vector<std::string> &args); |
| 129 | bool SHA256Sum(const std::vector<std::string> &args); |
| 130 | bool SHA384Sum(const std::vector<std::string> &args); |
| 131 | bool SHA512Sum(const std::vector<std::string> &args); |
| 132 | bool SHA512256Sum(const std::vector<std::string> &args); |
| 133 | bool Server(const std::vector<std::string> &args); |
| 134 | bool Sign(const std::vector<std::string> &args); |
| 135 | bool Speed(const std::vector<std::string> &args); |
| 136 |
| 137 | // These values are DER encoded, RSA private keys. |
| 138 | extern const uint8_t kDERRSAPrivate2048[]; |
| 139 | extern const size_t kDERRSAPrivate2048Len; |
| 140 | extern const uint8_t kDERRSAPrivate3072[]; |
| 141 | extern const size_t kDERRSAPrivate3072Len; |
| 142 | extern const uint8_t kDERRSAPrivate4096[]; |
| 143 | extern const size_t kDERRSAPrivate4096Len; |
| 144 |
| 145 |
| 146 | #endif // !OPENSSL_HEADER_TOOL_INTERNAL_H |
| 147 |