Debian Codegraph @

src/gen_ssl_cert/gen_ssl_cert.cpp

raw symbols SCIP
1 // Copyright (c) 2019-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include <boost/program_options.hpp>
30 #include <boost/format.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <openssl/ssl.h>
33 #include <openssl/pem.h>
34 #include "include_base_utils.h"
35 #include "file_io_utils.h"
36 #include "net/net_ssl.h"
37 #include "crypto/crypto.h"
38 #include "common/util.h"
39 #include "common/i18n.h"
40 #include "common/command_line.h"
41 #include "common/scoped_message_writer.h"
42 #include "common/password.h"
43 #include "version.h"
44
45 namespace po = boost::program_options;
46
47 #undef MONERO_DEFAULT_LOG_CATEGORY
48 #define MONERO_DEFAULT_LOG_CATEGORY "gen_ssl_cert"
49
50 namespace gencert
51 {
52 const char* tr(const char* str)
53 {
54 return i18n_translate(str, "tools::gen_ssl_cert");
55 }
56
57 }
58
59 namespace
60 {
61 const command_line::arg_descriptor<std::string> arg_certificate_filename = {"certificate-filename", gencert::tr("Filename to save the certificate"), ""};
62 const command_line::arg_descriptor<std::string> arg_private_key_filename = {"private-key-filename", gencert::tr("Filename to save the private key"), ""};
63 const command_line::arg_descriptor<std::string> arg_passphrase = {"passphrase", gencert::tr("Passphrase with which to encrypt the private key"), ""};
64 const command_line::arg_descriptor<std::string> arg_passphrase_file = {"passphrase-file", gencert::tr("File containing the passphrase with which to encrypt the private key"), ""};
65 const command_line::arg_descriptor<bool> arg_prompt_for_passphrase = {"prompt-for-passphrase", gencert::tr("Prompt for a passphrase with which to encrypt the private key"), false};
66 }
67
68 // adapted from openssl's apps/x509.c
69 static std::string get_fingerprint(X509 *cert, const EVP_MD *fdig)
70 {
71 unsigned int j;
72 unsigned int n;
73 unsigned char md[EVP_MAX_MD_SIZE];
74 std::string fingerprint;
75
76 if (!X509_digest(cert, fdig, md, &n))
77 {
78 tools::fail_msg_writer() << tr("Failed to create fingerprint: ") << ERR_reason_error_string(ERR_get_error());
79 return fingerprint;
80 }
81 fingerprint.resize(n * 3 - 1);
82 char *out = &fingerprint[0];
83 for (j = 0; j < n; ++j)
84 {
85 snprintf(out, 3 + (j + 1 < n), "%02X%s", md[j], (j + 1 == n) ? "" : ":");
86 out += 3;
87 }
88 return fingerprint;
89 }
90
91 int main(int argc, char* argv[])
92 {
93 TRY_ENTRY();
94
95 tools::on_startup();
96
97 po::options_description desc_cmd_only("Command line options");
98 po::options_description desc_cmd_sett("Command line options and settings options");
99
100 command_line::add_arg(desc_cmd_sett, arg_certificate_filename);
101 command_line::add_arg(desc_cmd_sett, arg_private_key_filename);
102 command_line::add_arg(desc_cmd_sett, arg_passphrase);
103 command_line::add_arg(desc_cmd_sett, arg_passphrase_file);
104 command_line::add_arg(desc_cmd_sett, arg_prompt_for_passphrase);
105
106 command_line::add_arg(desc_cmd_only, command_line::arg_help);
107 command_line::add_arg(desc_cmd_only, command_line::arg_version);
108
109 po::options_description desc_options("Allowed options");
110 desc_options.add(desc_cmd_only).add(desc_cmd_sett);
111
112 po::variables_map vm;
113 bool r = command_line::handle_error_helper(desc_options, [&]()
114 {
115 po::store(po::parse_command_line(argc, argv, desc_options), vm);
116 po::notify(vm);
117 return true;
118 });
119 if (!r)
120 return 1;
121
122 if (command_line::get_arg(vm, command_line::arg_help))
123 {
124 std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL;
125 std::cout << desc_options << std::endl;
126 return 0;
127 }
128 if (command_line::get_arg(vm, command_line::arg_version))
129 {
130 std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL;
131 return 0;
132 }
133
134 const std::string certificate_filename = command_line::get_arg(vm, arg_certificate_filename);
135 if (certificate_filename.empty())
136 {
137 tools::fail_msg_writer() << gencert::tr("Argument is needed: ") << "--" << arg_certificate_filename.name;
138 return 1;
139 }
140 const std::string private_key_filename = command_line::get_arg(vm, arg_private_key_filename);
141 if (private_key_filename.empty())
142 {
143 tools::fail_msg_writer() << gencert::tr("Argument is needed: ") << "--" << arg_private_key_filename.name;
144 return 1;
145 }
146
147 epee::wipeable_string private_key_passphrase;
148 if (command_line::get_arg(vm, arg_prompt_for_passphrase))
149 {
150 auto pwd_container = tools::password_container::prompt(true, "Enter passphrase for the new SSL private key");
151 if (!pwd_container)
152 {
153 tools::fail_msg_writer() << gencert::tr("Failed to read passphrase");
154 return 1;
155 }
156 private_key_passphrase = pwd_container->password();
157 }
158 else if (!command_line::is_arg_defaulted(vm, arg_passphrase_file))
159 {
160 std::string passphrase_file = command_line::get_arg(vm, arg_passphrase_file);
161 if (!passphrase_file.empty())
162 {
163 std::string passphrase;
164 if (!epee::file_io_utils::load_file_to_string(passphrase_file, passphrase))
165 {
166 MERROR("Failed to load passphrase");
167 return 1;
168 }
169
170 // Remove line breaks the user might have inserted
171 boost::trim_right_if(passphrase, boost::is_any_of("\r\n"));
172 private_key_passphrase = passphrase;
173 memwipe(&passphrase[0], passphrase.size());
174 }
175 }
176 else
177 {
178 private_key_passphrase = command_line::get_arg(vm, arg_passphrase);
179 }
180 if (private_key_passphrase.empty())
181 tools::msg_writer(epee::console_color_yellow) << (boost::format(tr("Empty passphrase, the private key will be saved to disk unencrypted, use --%s to set a passphrase or --%s to prompt for one")) % arg_passphrase.name % arg_prompt_for_passphrase.name).str();
182
183 EVP_PKEY *pkey;
184 X509 *cert;
185 r = epee::net_utils::create_rsa_ssl_certificate(pkey, cert);
186 if (!r)
187 {
188 tools::fail_msg_writer() << gencert::tr("Failed to create certificate");
189 return 1;
190 }
191
192 // write cert
193 BIO *bio_cert = BIO_new(BIO_s_mem());
194 r = PEM_write_bio_X509(bio_cert, cert);
195 if (!r)
196 {
197 BIO_free(bio_cert);
198 tools::fail_msg_writer() << gencert::tr("Failed to write certificate: ") << ERR_reason_error_string(ERR_get_error());
199 return 1;
200 }
201 BUF_MEM *buf = NULL;
202 BIO_get_mem_ptr(bio_cert, &buf);
203 if (!buf || !buf->data || !buf->length)
204 {
205 BIO_free(bio_cert);
206 tools::fail_msg_writer() << gencert::tr("Failed to write certificate: ") << ERR_reason_error_string(ERR_get_error());
207 return 1;
208 }
209 const std::string certificate(std::string(buf->data, buf->length));
210 BIO_free(bio_cert);
211
212 // write private key
213 BIO *bio_pkey = BIO_new(BIO_s_mem());
214 r = PEM_write_bio_PKCS8PrivateKey(bio_pkey, pkey, private_key_passphrase.empty() ? NULL : EVP_aes_128_cfb(), private_key_passphrase.data(), private_key_passphrase.size(), NULL, NULL);
215 if (!r)
216 {
217 BIO_free(bio_pkey);
218 tools::fail_msg_writer() << gencert::tr("Failed to write private key: ") << ERR_reason_error_string(ERR_get_error());
219 return 1;
220 }
221 buf = NULL;
222 BIO_get_mem_ptr(bio_pkey, &buf);
223 if (!buf || !buf->data || !buf->length)
224 {
225 BIO_free(bio_pkey);
226 tools::fail_msg_writer() << gencert::tr("Failed to write private key: ") << ERR_reason_error_string(ERR_get_error());
227 return 1;
228 }
229 const std::string private_key(std::string(buf->data, buf->length));
230 BIO_free(bio_pkey);
231
232 // write files
233 tools::set_strict_default_file_permissions(true);
234 r = epee::file_io_utils::save_string_to_file(certificate_filename, certificate);
235 if (!r)
236 {
237 tools::fail_msg_writer() << gencert::tr("Failed to save certificate file");
238 return 1;
239 }
240 r = epee::file_io_utils::save_string_to_file(private_key_filename, private_key);
241 if (!r)
242 {
243 tools::fail_msg_writer() << gencert::tr("Failed to save private key file");
244 return 1;
245 }
246
247 tools::success_msg_writer() << tr("New certificate created:");
248 tools::success_msg_writer() << tr("Certificate: ") << certificate_filename;
249 tools::success_msg_writer() << tr("SHA-256 Fingerprint: ") << get_fingerprint(cert, EVP_sha256());
250 tools::success_msg_writer() << tr("Private key: ") << private_key_filename << " (" << (private_key_passphrase.empty() ? "unencrypted" : "encrypted") << ")";
251
252 return 0;
253 CATCH_ENTRY_L0("main", 1);
254 }