blob: 69d4689e4ffd06a8911316f30598483ef1201462 [file] [log] [blame]
Andrew Topdee8b292019-01-22 14:48:26 -08001/* 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 Kertb1089432024-03-18 19:46:49 -070019#include <openssl/span.h>
Andrew Topdee8b292019-01-22 14:48:26 -080020
21#include <string>
Kaido Kertb1089432024-03-18 19:46:49 -070022#include <utility>
Andrew Topdee8b292019-01-22 14:48:26 -080023#include <vector>
24
Andrew Topdee8b292019-01-22 14:48:26 -080025// 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 Kertb1089432024-03-18 19:46:49 -070028OPENSSL_MSVC_PRAGMA(warning(push))
Andrew Topdee8b292019-01-22 14:48:26 -080029OPENSSL_MSVC_PRAGMA(warning(disable: 4702))
Andrew Topdee8b292019-01-22 14:48:26 -080030#include <map>
31
Kaido Kertd6c34162019-09-09 15:36:21 -070032#if defined(STARBOARD)
33#define printf(fmt, ...) SbLogFormatF(fmt, ##__VA_ARGS__)
34#define fprintf(discard, fmt, ...) printf(fmt, ##__VA_ARGS__)
35#endif
36
Andrew Topdee8b292019-01-22 14:48:26 -080037OPENSSL_MSVC_PRAGMA(warning(pop))
38
Andrew Topdee8b292019-01-22 14:48:26 -080039struct FileCloser {
40 void operator()(FILE *file) {
41 fclose(file);
42 }
43};
44
45using ScopedFILE = std::unique_ptr<FILE, FileCloser>;
46
Kaido Kertb1089432024-03-18 19:46:49 -070047// The following functions abstract between POSIX and Windows differences in
48// file descriptor I/O functions.
49
50// CloseFD behaves like |close|.
51void CloseFD(int fd);
52
53class 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.
91ScopedFD 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.
97bool 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.
103bool WriteToFD(int fd, size_t *out_bytes_written, const void *in, size_t num);
104
105// FDToFILE behaves like |fdopen|.
106ScopedFILE FDToFILE(ScopedFD fd, const char *mode);
107
Andrew Topdee8b292019-01-22 14:48:26 -0800108enum ArgumentType {
109 kRequiredArgument,
110 kOptionalArgument,
111 kBooleanArgument,
112};
113
114struct argument {
115 const char *name;
116 ArgumentType type;
117 const char *description;
118};
119
120bool ParseKeyValueArguments(std::map<std::string, std::string> *out_args, const
121 std::vector<std::string> &args, const struct argument *templates);
122
123void PrintUsage(const struct argument *templates);
124
125bool GetUnsigned(unsigned *out, const std::string &arg_name,
126 unsigned default_value,
127 const std::map<std::string, std::string> &args);
128
129bool ReadAll(std::vector<uint8_t> *out, FILE *in);
Kaido Kertb1089432024-03-18 19:46:49 -0700130bool WriteToFile(const std::string &path, bssl::Span<const uint8_t> in);
Andrew Topdee8b292019-01-22 14:48:26 -0800131
132bool Ciphers(const std::vector<std::string> &args);
133bool Client(const std::vector<std::string> &args);
134bool DoPKCS12(const std::vector<std::string> &args);
Kaido Kertb1089432024-03-18 19:46:49 -0700135bool GenerateECH(const std::vector<std::string> &args);
Andrew Topdee8b292019-01-22 14:48:26 -0800136bool GenerateEd25519Key(const std::vector<std::string> &args);
137bool GenerateRSAKey(const std::vector<std::string> &args);
138bool MD5Sum(const std::vector<std::string> &args);
139bool Rand(const std::vector<std::string> &args);
140bool SHA1Sum(const std::vector<std::string> &args);
141bool SHA224Sum(const std::vector<std::string> &args);
142bool SHA256Sum(const std::vector<std::string> &args);
143bool SHA384Sum(const std::vector<std::string> &args);
144bool SHA512Sum(const std::vector<std::string> &args);
Kaido Kertb1089432024-03-18 19:46:49 -0700145bool SHA512256Sum(const std::vector<std::string> &args);
Andrew Topdee8b292019-01-22 14:48:26 -0800146bool Server(const std::vector<std::string> &args);
147bool Sign(const std::vector<std::string> &args);
148bool Speed(const std::vector<std::string> &args);
149
150// These values are DER encoded, RSA private keys.
151extern const uint8_t kDERRSAPrivate2048[];
152extern const size_t kDERRSAPrivate2048Len;
153extern const uint8_t kDERRSAPrivate4096[];
154extern const size_t kDERRSAPrivate4096Len;
155
156
157#endif // !OPENSSL_HEADER_TOOL_INTERNAL_H