• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 // MSVC issues warning C4702 for unreachable code in its xtree header when
25 // compiling with -D_HAS_EXCEPTIONS=0. See
26 // https://connect.microsoft.com/VisualStudio/feedback/details/809962
27 OPENSSL_MSVC_PRAGMA(warning(push))
28 OPENSSL_MSVC_PRAGMA(warning(disable: 4702))
29 #include <map>
30 OPENSSL_MSVC_PRAGMA(warning(pop))
31 
32 struct FileCloser {
operatorFileCloser33   void operator()(FILE *file) {
34     fclose(file);
35   }
36 };
37 
38 using ScopedFILE = std::unique_ptr<FILE, FileCloser>;
39 
40 // The following functions abstract between POSIX and Windows differences in
41 // file descriptor I/O functions.
42 
43 // CloseFD behaves like |close|.
44 void CloseFD(int fd);
45 
46 class ScopedFD {
47  public:
ScopedFD()48   ScopedFD() {}
ScopedFD(int fd)49   explicit ScopedFD(int fd) : fd_(fd) {}
ScopedFD(ScopedFD && other)50   ScopedFD(ScopedFD &&other) { *this = std::move(other); }
51   ScopedFD(const ScopedFD &) = delete;
~ScopedFD()52   ~ScopedFD() { reset(); }
53 
54   ScopedFD &operator=(const ScopedFD &) = delete;
55   ScopedFD &operator=(ScopedFD &&other) {
56     reset();
57     fd_ = other.fd_;
58     other.fd_ = -1;
59     return *this;
60   }
61 
62   explicit operator bool() const { return fd_ >= 0; }
63 
get()64   int get() const { return fd_; }
65 
reset()66   void reset() {
67     if (fd_ >= 0) {
68       CloseFD(fd_);
69     }
70     fd_ = -1;
71   }
72 
release()73   int release() {
74     int fd = fd_;
75     fd_ = -1;
76     return fd;
77   }
78 
79  private:
80   int fd_ = -1;
81 };
82 
83 // OpenFD behaves like |open| but handles |EINTR| and works on Windows.
84 ScopedFD OpenFD(const char *path, int flags);
85 
86 // ReadFromFD reads up to |num| bytes from |fd| and writes the result to |out|.
87 // On success, it returns true and sets |*out_bytes_read| to the number of bytes
88 // read. Otherwise, it returns false and leaves an error in |errno|. On POSIX,
89 // it handles |EINTR| internally.
90 bool ReadFromFD(int fd, size_t *out_bytes_read, void *out, size_t num);
91 
92 // WriteToFD writes up to |num| bytes from |in| to |fd|. On success, it returns
93 // true and sets |*out_bytes_written| to the number of bytes written. Otherwise,
94 // it returns false and leaves an error in |errno|. On POSIX, it handles |EINTR|
95 // internally.
96 bool WriteToFD(int fd, size_t *out_bytes_written, const void *in, size_t num);
97 
98 // FDToFILE behaves like |fdopen|.
99 ScopedFILE FDToFILE(ScopedFD fd, const char *mode);
100 
101 enum ArgumentType {
102   kRequiredArgument,
103   kOptionalArgument,
104   kBooleanArgument,
105 };
106 
107 struct argument {
108   const char *name;
109   ArgumentType type;
110   const char *description;
111 };
112 
113 bool ParseKeyValueArguments(std::map<std::string, std::string> *out_args, const
114     std::vector<std::string> &args, const struct argument *templates);
115 
116 void PrintUsage(const struct argument *templates);
117 
118 bool GetUnsigned(unsigned *out, const std::string &arg_name,
119                  unsigned default_value,
120                  const std::map<std::string, std::string> &args);
121 
122 bool ReadAll(std::vector<uint8_t> *out, FILE *in);
123 
124 bool Ciphers(const std::vector<std::string> &args);
125 bool Client(const std::vector<std::string> &args);
126 bool DoPKCS12(const std::vector<std::string> &args);
127 bool GenerateEd25519Key(const std::vector<std::string> &args);
128 bool GenerateRSAKey(const std::vector<std::string> &args);
129 bool MD5Sum(const std::vector<std::string> &args);
130 bool Rand(const std::vector<std::string> &args);
131 bool SHA1Sum(const std::vector<std::string> &args);
132 bool SHA224Sum(const std::vector<std::string> &args);
133 bool SHA256Sum(const std::vector<std::string> &args);
134 bool SHA384Sum(const std::vector<std::string> &args);
135 bool SHA512Sum(const std::vector<std::string> &args);
136 bool SHA512256Sum(const std::vector<std::string> &args);
137 bool Server(const std::vector<std::string> &args);
138 bool Sign(const std::vector<std::string> &args);
139 bool Speed(const std::vector<std::string> &args);
140 
141 // These values are DER encoded, RSA private keys.
142 extern const uint8_t kDERRSAPrivate2048[];
143 extern const size_t kDERRSAPrivate2048Len;
144 extern const uint8_t kDERRSAPrivate4096[];
145 extern const size_t kDERRSAPrivate4096Len;
146 
147 
148 #endif  // !OPENSSL_HEADER_TOOL_INTERNAL_H
149