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

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