From 6777f7c853708d5fbb9029463788f2fc64d0afd0 Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Fri, 30 Aug 2024 18:02:13 +0200 Subject: [PATCH] Close #401: Write PEM data to a file especially for crlgen The new option --pem-file= behaves like --pem, just that it writes the PEM data (and nothing else) to the given file name --- lib/arguments.cpp | 2 ++ lib/cmdline.cpp | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/arguments.cpp b/lib/arguments.cpp index ff522f93..00a15318 100644 --- a/lib/arguments.cpp +++ b/lib/arguments.cpp @@ -54,6 +54,8 @@ const QList arguments::opts = { "Database password for unlocking the database. See below for password format options."), arg_option("pem", NULL, no_argument, true, false, "Print PEM representation of provided files. Prints only the public part of private keys."), + arg_option("pem-file", NULL, file_argument, true, false, + "Specify a file name for the PEM data. Implies '--pem'."), arg_option("print", NULL, no_argument, true, false, "Print a synopsis of provided files."), arg_option("select", "id-list", required_argument, true, true, diff --git a/lib/cmdline.cpp b/lib/cmdline.cpp index cad64f7e..2bf39b5a 100644 --- a/lib/cmdline.cpp +++ b/lib/cmdline.cpp @@ -242,7 +242,7 @@ int read_cmdline(int argc, char *argv[], bool console_only, } } - BioByteArray bba; + BioByteArray bba, bbafile; foreach(pki_base *pki, cmdline_items->get()) { QString filename = pki->getFilename(); if ((cmd_opts.has("text") || cmd_opts.has("print")) && @@ -255,11 +255,25 @@ int read_cmdline(int argc, char *argv[], bool console_only, pki->print(bba, pki_base::print_coloured); if (cmd_opts.has("text")) pki->print(bba, pki_base::print_openssl_txt); - if (cmd_opts.has("pem")) + if (cmd_opts.has("pem-file")) + pki->print(bbafile, pki_base::print_pem); + else if (cmd_opts.has("pem")) pki->print(bba, pki_base::print_pem); } if (bba.size() > 0) console_write(stdout, bba); + + if (bbafile.size() > 0) + { + QString filename = cmd_opts["pem-file"]; + XFile f(filename); + if (f.open_write()) + f.write(bbafile); + else + XCA_ERROR(QObject::tr("Failed to write PEM data to '%1'") + .arg(filename)); + f.close(); + } if (cmd_opts.has("import")) { Database.insert(cmdline_items); *_cmdline_items = nullptr;