#include "px5g-openssl.hpp" #include #include #include #include #include class argv_view { // TODO(pst): use std::span when available. private: std::basic_string_view data; public: argv_view(const argv_view &) = delete; argv_view(argv_view &&) = delete; auto operator=(const argv_view &) -> argv_view & = delete; auto operator=(argv_view &&) -> argv_view & = delete; argv_view(const char ** argv, int argc) : data{argv, static_cast(argc)} {} inline auto operator[] (size_t pos) const -> std::string_view { return std::string_view{data[pos]}; } [[nodiscard]] inline constexpr auto size() const noexcept -> size_t { return data.size(); } ~argv_view() = default; }; static const auto default_validity = 30; auto checkend(const argv_view & argv) -> int; void eckey(const argv_view & argv); void rsakey(const argv_view & argv); void selfsigned(const argv_view & argv); inline auto parse_int(const std::string_view & arg) -> int { size_t pos; int ret = stoi(std::string{arg}, &pos); if (pos < arg.size()) { throw std::runtime_error("number has trailing char"); } return ret; } inline auto parse_curve(const std::string_view & name) -> int { if (name=="P-384") { return NID_secp384r1; } if (name=="P-521") { return NID_secp521r1; } if (name=="P-256" || name=="secp256r1") { return NID_X9_62_prime256v1; } if (name=="secp192r1") { return NID_X9_62_prime192v1; } return OBJ_sn2nid(name.data()); // not: if (curve == 0) { curve = EC_curve_nist2nid(name.c_str()); } } auto checkend(const argv_view & argv) -> int { bool use_pem = true; std::string crtpath{}; time_t seconds = 0; for (size_t i=2; i= argv.size()) { throw std::runtime_error("checkend error: -in misses filename"); } if (!crtpath.empty()) { if (argv[i] == crtpath) { std::cerr<<"checkend warning: repeated same -in file\n"; } else { throw std::runtime_error ("checkend error: more than one -in file"); } } crtpath = argv[i]; } else if (argv[i][0]=='-') { std::cerr<<"checkend warning: skipping option "<(num); if (num!=static_cast(seconds)) { auto errmsg = std::string{"checkend error: time too big "}; errmsg += argv[i]; throw std::runtime_error(errmsg); } } } bool valid = checkend(crtpath, seconds, use_pem); std::cout<<"Certificate will"<<(valid ? " not " : " ")<<"expire"<= argv.size()) { throw std::runtime_error("eckey error: -out misses filename"); } if (!keypath.empty()) { if (argv[i]==keypath) { std::cerr<<"eckey warning: repeated same -out file\n"; } else { throw std::runtime_error ("eckey error: more than one -out file"); } } keypath = argv[i]; } else if (argv[i][0]=='-') { std::cerr<<"eckey warning: skipping option "<= argv.size()) { throw std::runtime_error("rsakey error: -out misses filename"); } if (!keypath.empty()) { if (argv[i]==keypath) { std::cerr<<"rsakey warning: repeated -out file"<= argv.size()) { throw std::runtime_error ("selfsigned error: -newkey misses algorithm option"); } static constexpr auto rsa_prefix = std::string_view{"rsa:"}; if (argv[i]=="ec") { use_rsa = false; } else if (argv[i].rfind(rsa_prefix, 0) == 0) { use_rsa = true; try { keysize = parse_int(argv[i].substr(rsa_prefix.size())); } catch (...) { std::string errmsg{"selfsigned error: invalid keysize "}; errmsg += argv[i].substr(4); std::throw_with_nested(std::runtime_error(errmsg)); } } else { throw std::runtime_error("selfsigned error: invalid algorithm"); } } else if (argv[i]=="-pkeyopt") { ++i; if (i >= argv.size()) { throw std::runtime_error ("selfsigned error: -pkeyopt misses value"); } static constexpr auto curve_prefix = std::string_view{"ec_paramgen_curve:"}; if (argv[i].rfind(curve_prefix, 0) != 0) { throw std::runtime_error("selfsigned error: -pkeyopt invalid"); } curve = parse_curve(argv[i].substr(curve_prefix.size())); } else if (argv[i]=="-keyout") { ++i; if (i >= argv.size()) { throw std::runtime_error ("selfsigned error: -keyout misses path"); } if (!keypath.empty()) { if (argv[i]==keypath) { std::cerr<<"selfsigned warning: repeated -keyout file\n"; } else { throw std::runtime_error ("selfsigned error: more than one -keyout file"); } } keypath = argv[i]; } else if (argv[i]=="-out") { ++i; if (i >= argv.size()) { throw std::runtime_error ("selfsigned error: -out misses filename"); } if (!crtpath.empty()) { if (argv[i]==crtpath) { std::cerr<<"selfsigned warning: repeated same -out file\n"; } else { throw std::runtime_error ("selfsigned error: more than one -out file"); } } crtpath = argv[i]; } else if (argv[i]=="-subj") { ++i; if (i >= argv.size()) { throw std::runtime_error ("selfsigned error: -subj misses value"); } if (!subject.empty()) { if (argv[i]==subject) { std::cerr<<"selfsigned warning: repeated same -subj\n"; } else { throw std::runtime_error ("selfsigned error: more than one -subj value"); } } subject = argv[i]; } else { std::cerr<<"selfsigned warning: skipping option "< int { auto args = argv_view{argv, argc}; auto cmds = std::array{ std::array{"checkend", " [-der] [-in certificate_path] [seconds_remaining]" }, std::array{"eckey", " [-der] [-out key_path] [curve_name]" }, std::array{"rsakey", " [-der] [-out key_path] [-3] [key_size]" }, std::array{"selfsigned", " [-der] [-keyout key_path] [-out certificate_path]" " [-newkey ec|rsa:key_size] [-pkeyopt ec_paramgen_curve:name]" " [-days validity] [-subj /C=.../ST=.../L=.../O=.../CN=.../... ]" }, }; try { if (argc < 2) { throw std::runtime_error("error: no argument"); } if (args[1]==cmds[0][0]) {return checkend(args);} if (args[1]==cmds[1][0]) { eckey(args); } else if (args[1]==cmds[2][0]) { rsakey(args); } else if (args[1]==cmds[3][0]) { selfsigned(args); } else { throw std::runtime_error("error: argument not recognized"); } } catch (const std::exception & e) { auto usage = std::string{"usage: \n"} ; for (auto cmd : cmds) { usage += std::string{4, ' '} + *argv +" "+ cmd[0] + cmd[1] +"\n"; } std::cerr< void { std::cerr<