diff options
Diffstat (limited to 'src/genca.cc')
| -rw-r--r-- | src/genca.cc | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/genca.cc b/src/genca.cc new file mode 100644 index 0000000..ee0f580 --- /dev/null +++ b/src/genca.cc @@ -0,0 +1,69 @@ +// -*- mode: c++; c-basic-offset: 2; -*- + +#include "common.hh" + +#include <fstream> +#include <iostream> +#include <memory> +#include <string> + +#include "args.hh" +#include "logger.hh" +#include "ssl.hh" + +namespace { + +bool genca(std::ostream& out, std::string const& name) { + std::unique_ptr<Logger> logger(Logger::create_stderr()); + std::unique_ptr<SSLEntropy> entropy(SSLEntropy::create(logger.get())); + if (!entropy) return false; + std::string key; + if (!SSLKey::generate(logger.get(), entropy.get(), &key)) return false; + std::string cert; + std::unique_ptr<SSLKey> pkey(SSLKey::load(logger.get(), key)); + if (!SSLCert::generate(logger.get(), entropy.get(), nullptr, nullptr, name, + pkey.get(), &cert)) return false; + out << cert << '\n' << key << std::endl; + return true; +} + +} // namespace + +int main(int argc, char** argv) { + std::unique_ptr<Args> args(Args::create()); + args->add('o', "output", "FILE", + "output certificate and key to FILE instead of stdout."); + args->add('n', "name", "NAME", + "Issuer name to use instead of TransparentProxy"); + args->add('h', "help", "display this text and exit."); + args->add('V', "version", "display version and exit."); + if (!args->run(argc, argv)) { + std::cerr << "Try `tp-genca --help` for usage." << std::endl; + return EXIT_FAILURE; + } + if (args->is_set('h')) { + std::cout << "Usage: `tp-genca [OPTIONS...]`\n" + << "Generate a self-signed CA to use for MITM SSL interception.\n" + << '\n'; + args->print_help(); + return EXIT_SUCCESS; + } + if (args->is_set('V')) { + std::cout << "TransparentProxy version " VERSION + << " written by Joel Klinghed <the_jk@yahoo.com>" << std::endl; + return EXIT_SUCCESS; + } + if (!args->arguments().empty()) { + std::cerr << "Too many arguments.\n" + << "Try `tp-genca --help` for usage." << std::endl; + return EXIT_FAILURE; + } + auto name = args->arg('n', "TransparentProxy"); + auto output = args->arg('o', nullptr); + if (output) { + std::ofstream out(output); + return genca(out, name) ? EXIT_SUCCESS : EXIT_FAILURE; + } else { + return genca(std::cout, name) ? EXIT_SUCCESS : EXIT_FAILURE; + } +} |
