Blame SOURCES/0006-Allow-specifying-keyfile-password-by-file.patch

3415ba
From 9cb7daa54708dcf5e6500cd20ec7b1cc2f6f6350 Mon Sep 17 00:00:00 2001
3415ba
From: Stephen Gallagher <sgallagh@redhat.com>
3415ba
Date: Mon, 10 Jun 2019 10:15:42 -0400
3415ba
Subject: [PATCH 6/6] Allow specifying keyfile password by file
3415ba
3415ba
Signed-off-by: Stephen Gallagher <sgallagh@redhat.com>
3415ba
---
3415ba
 src/sscg.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
3415ba
 1 file changed, 84 insertions(+)
3415ba
3415ba
diff --git a/src/sscg.c b/src/sscg.c
3415ba
index 9dc926c77038105ca881a612cccd1913bc2d42f1..a02e4df66c6cf9ec1865f425b4a15da82fbfdc72 100644
3415ba
--- a/src/sscg.c
3415ba
+++ b/src/sscg.c
3415ba
@@ -34,6 +34,10 @@
3415ba
 #include "include/authority.h"
3415ba
 #include "include/service.h"
3415ba
 
3415ba
+
3415ba
+/* Same as OpenSSL CLI */
3415ba
+#define MAX_PW_LEN 1024
3415ba
+
3415ba
 static int
3415ba
 get_security_level (void)
3415ba
 {
3415ba
@@ -209,6 +213,44 @@ sscg_options_destructor (TALLOC_CTX *opts)
3415ba
 }
3415ba
 
3415ba
 
3415ba
+static char *
3415ba
+sscg_read_pw_file (TALLOC_CTX *mem_ctx, char *path)
3415ba
+{
3415ba
+  int i;
3415ba
+  BIO *pwdbio = NULL;
3415ba
+  char tpass[MAX_PW_LEN];
3415ba
+  char *tmp = NULL;
3415ba
+  char *password = NULL;
3415ba
+
3415ba
+  pwdbio = BIO_new_file (path, "r");
3415ba
+  if (pwdbio == NULL)
3415ba
+    {
3415ba
+      fprintf (stderr, "Can't open file %s\n", path);
3415ba
+      return NULL;
3415ba
+    }
3415ba
+
3415ba
+  i = BIO_gets (pwdbio, tpass, MAX_PW_LEN);
3415ba
+  BIO_free_all (pwdbio);
3415ba
+  pwdbio = NULL;
3415ba
+
3415ba
+  if (i <= 0)
3415ba
+    {
3415ba
+      fprintf (stderr, "Error reading password from BIO\n");
3415ba
+      return NULL;
3415ba
+    }
3415ba
+
3415ba
+  tmp = strchr (tpass, '\n');
3415ba
+  if (tmp != NULL)
3415ba
+    *tmp = 0;
3415ba
+
3415ba
+  password = talloc_strdup (mem_ctx, tpass);
3415ba
+
3415ba
+  memset (tpass, 0, MAX_PW_LEN);
3415ba
+
3415ba
+  return password;
3415ba
+}
3415ba
+
3415ba
+
3415ba
 int
3415ba
 main (int argc, const char **argv)
3415ba
 {
3415ba
@@ -236,10 +278,12 @@ main (int argc, const char **argv)
3415ba
   int ca_mode = 0644;
3415ba
   int ca_key_mode = 0600;
3415ba
   char *ca_key_password = NULL;
3415ba
+  char *ca_key_passfile = NULL;
3415ba
 
3415ba
   int cert_mode = 0644;
3415ba
   int cert_key_mode = 0600;
3415ba
   char *cert_key_password = NULL;
3415ba
+  char *cert_key_passfile = NULL;
3415ba
 
3415ba
   char *create_mode = NULL;
3415ba
 
3415ba
@@ -470,6 +514,16 @@ main (int argc, const char **argv)
3415ba
       NULL
3415ba
     },
3415ba
 
3415ba
+    {
3415ba
+      "ca-key-passfile",
3415ba
+      '\0',
3415ba
+      POPT_ARG_STRING,
3415ba
+      &ca_key_passfile,
3415ba
+      0,
3415ba
+      _ ("A file containing the password to encrypt the CA key file."),
3415ba
+      NULL
3415ba
+    },
3415ba
+
3415ba
     {
3415ba
       "ca-key-password-prompt",
3415ba
       'C',
3415ba
@@ -531,6 +585,16 @@ main (int argc, const char **argv)
3415ba
       NULL
3415ba
     },
3415ba
 
3415ba
+    {
3415ba
+      "cert-key-passfile",
3415ba
+      '\0',
3415ba
+      POPT_ARG_STRING,
3415ba
+      &cert_key_passfile,
3415ba
+      0,
3415ba
+      _ ("A file containing the password to encrypt the service key file."),
3415ba
+      NULL
3415ba
+    },
3415ba
+
3415ba
     {
3415ba
       "cert-key-password-prompt",
3415ba
       'P',
3415ba
@@ -697,12 +761,32 @@ main (int argc, const char **argv)
3415ba
       options->ca_key_pass =
3415ba
         sscg_secure_string_steal (options, ca_key_password);
3415ba
     }
3415ba
+  else if (ca_key_passfile)
3415ba
+    {
3415ba
+      options->ca_key_pass = sscg_read_pw_file (options, ca_key_passfile);
3415ba
+      if (!options->ca_key_pass)
3415ba
+        {
3415ba
+          fprintf (
3415ba
+            stderr, "Failed to read passphrase from %s", ca_key_passfile);
3415ba
+          goto done;
3415ba
+        }
3415ba
+    }
3415ba
 
3415ba
   if (cert_key_password)
3415ba
     {
3415ba
       options->cert_key_pass =
3415ba
         sscg_secure_string_steal (options, cert_key_password);
3415ba
     }
3415ba
+  else if (cert_key_passfile)
3415ba
+    {
3415ba
+      options->cert_key_pass = sscg_read_pw_file (options, cert_key_passfile);
3415ba
+      if (!options->cert_key_pass)
3415ba
+        {
3415ba
+          fprintf (
3415ba
+            stderr, "Failed to read passphrase from %s", cert_key_passfile);
3415ba
+          goto done;
3415ba
+        }
3415ba
+    }
3415ba
 
3415ba
 
3415ba
   if (options->key_strength < options->minimum_key_strength)
3415ba
-- 
3415ba
2.23.0
3415ba