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 28 OPENSSL_MSVC_PRAGMA(warning(push)) 29 OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) 30 #include <map> 31 OPENSSL_MSVC_PRAGMA(warning(pop)) 32 33 struct FileCloser { operatorFileCloser34 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: ScopedFD()49 ScopedFD() {} ScopedFD(int fd)50 explicit ScopedFD(int fd) : fd_(fd) {} ScopedFD(ScopedFD && other)51 ScopedFD(ScopedFD &&other) { *this = std::move(other); } 52 ScopedFD(const ScopedFD &) = delete; ~ScopedFD()53 ~ScopedFD() { reset(); } 54 55 ScopedFD &operator=(const ScopedFD &) = delete; 56 ScopedFD &operator=(ScopedFD &&other) { 57 reset(); 58 fd_ = other.fd_; 59 other.fd_ = -1; 60 return *this; 61 } 62 63 explicit operator bool() const { return fd_ >= 0; } 64 get()65 int get() const { return fd_; } 66 reset()67 void reset() { 68 if (fd_ >= 0) { 69 CloseFD(fd_); 70 } 71 fd_ = -1; 72 } 73 release()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 102 enum ArgumentType { 103 kRequiredArgument, 104 kOptionalArgument, 105 kBooleanArgument, 106 }; 107 108 struct argument { 109 const char *name; 110 ArgumentType type; 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 152