89 !strncasecmp(unit,
"", strlen(unit)))
110 char const * number_begin = value;
111 char const * number_end = value;
113 while ((*number_end >=
'0' && *number_end <=
'9')) {
117 if (number_end <= number_begin)
118 throw TextException(
ToSBuf(
"expecting a decimal number at the beginning of ", name,
" value but got: ", value),
Here());
120 std::string
number(number_begin, number_end - number_begin);
121 std::istringstream in(
number);
123 if (!(in >> base) || !in.eof())
127 static_assert(std::is_unsigned<
decltype(multiplier * base)>::value,
"no signed overflows");
128 const auto product = multiplier * base;
129 if (base && multiplier != product / base)
138 std::string example_host_name =
"host.dom";
140 std::stringstream request_string_size_stream;
141 request_string_size_stream << request_string.length();
142 std::string help_string =
143 "usage: security_file_certgen -hv -s directory -M size -b fs_block_size\n"
146 "\t-s directory Directory path of SSL storage database.\n"
147 "\t-M size Maximum size of SSL certificate disk storage.\n"
148 "\t-b fs_block_size File system block size in bytes. Need for processing\n"
149 "\t natural size of certificate on disk. Default value is\n"
152 "After running write requests in the next format:\n"
153 "<request code><whitespace><body_len><whitespace><body>\n"
154 "There are two kind of request now:\n"
156 "\tCreate new private key and selfsigned certificate for \"host.dom\".\n"
158 "-----BEGIN CERTIFICATE-----\n"
160 "-----END CERTIFICATE-----\n"
161 "-----BEGIN RSA PRIVATE KEY-----\n"
163 "-----END RSA PRIVATE KEY-----\n"
164 "\tCreate new private key and certificate request for \"host.dom\"\n"
165 "\tSign new request by received certificate and private key.\n"
166 "usage: security_file_certgen -c -s ssl_store_path\n"
167 "\t-c Init ssl db directories and exit.\n";
168 std::cerr << help_string << std::endl;
178 std::unique_ptr<Ssl::CertificateDb>
db;
183 Security::PrivateKeyPointer pkey;
187 bool dbFailed =
false;
190 db->find(certKey, certProperties.
mimicCert, cert, pkey);
198 if (!cert || !pkey) {
213 if (!dbFailed &&
db && !
db->addCertAndPrivateKey(certKey, cert, pkey, certProperties.
mimicCert))
223 std::string bufferToWrite;
225 throw TextException(
"Cannot write ssl certificate or/and private key to memory.",
Here());
228 response_message.
setCode(
"OK");
229 response_message.
setBody(bufferToWrite);
232 std::cout << response_message.
compose() <<
'\1' << std::flush;
238int main(
int argc,
char *argv[])
243 size_t max_db_size = 0;
244 size_t fs_block_size = 0;
246 bool create_new_db =
false;
249 while ((c =
getopt(argc, argv,
"dchvs:M:b:")) != -1) {
263 throw TextException(
"Error -M option requires an -s parameter be set first.",
Here());
268 std::cout <<
"security_file_certgen version " <<
VERSION << std::endl;
272 create_new_db =
true;
283 if (!
db_path.empty() && max_db_size == 0)
284 throw TextException(
"security_file_certgen -s requires an -M parameter",
Here());
289 throw TextException(
"security_file_certgen is missing the required parameter. There should be -s and -M parameters when -c is used.",
Here());
291 std::cout <<
"Initialization SSL db..." << std::endl;
293 std::cout <<
"Done" << std::endl;
299 if (fs_block_size == 0) {
303 fs_block_size = 2048;
307 if (fs_block_size < 512)
308 fs_block_size = 2048;
325 size_t gcount = strlen(request);
326 parse_result = request_message.
parse(request, gcount);
#define Here()
source code location of the caller
std::ostream & CurrentException(std::ostream &os)
prints active (i.e., thrown but not yet handled) exception
#define HELPER_INPUT_BUFFER
static std::ostream & Extra(std::ostream &)
static void NameThisHelper(const char *name)
static void Create(std::string const &db_path)
Create and initialize a database under the db_path.
static void Check(std::string const &db_path, size_t max_db_size, size_t fs_block_size)
Check the database stored under the db_path.
Security::CertPointer mimicCert
Certificate to mimic.
void setCode(std::string const &aCode)
Set new request/reply code to compose.
void setBody(std::string const &aBody)
Set new body to encode.
static const std::string code_new_certificate
String code for "new_certificate" messages.
ParseResult
Parse result codes.
static const std::string param_host
Parameter name for passing hostname.
std::string const & getCode() const
Current response/request code. If parsing is not finished the method may return incompleted code.
ParseResult parse(const char *buffer, size_t len)
void parseRequest(CertificateProperties &)
orchestrates entire request parsing
std::string compose() const
an std::runtime_error with thrower location info
#define debugs(SECTION, LEVEL, CONTENT)
int getopt(int nargc, char *const *nargv, const char *ostr)
std::string & OnDiskCertificateDbKey(const CertificateProperties &)
bool generateSslCertificate(Security::CertPointer &cert, Security::PrivateKeyPointer &pkey, CertificateProperties const &properties)
bool writeCertAndPrivateKeyToMemory(Security::CertPointer const &cert, Security::PrivateKeyPointer const &pkey, std::string &bufferToWrite)
void SQUID_OPENSSL_init_ssl(void)
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
static const char *const B_MBYTES_STR
static size_t parseBytesOptionValue(const char *const name, const char *const value)
static const char *const B_GBYTES_STR
static void usage()
Print help using response code.
static const char *const B_KBYTES_STR
static const char *const B_BYTES_STR
static size_t parseBytesUnits(const char *unit)
static bool processNewRequest(Ssl::CrtdMessage &request_message, std::string const &db_path, size_t max_db_size, size_t fs_block_size)
Process new request message.
int xstatvfs(const char *path, struct statvfs *sfs)