Blame SOURCES/0002-Adjust-defaults-based-on-system-security-level.patch

19981c
From 942d9fa4f582a372af3d0bd499f073760dec2335 Mon Sep 17 00:00:00 2001
19981c
From: Stephen Gallagher <sgallagh@redhat.com>
19981c
Date: Tue, 27 Nov 2018 13:24:37 -0500
19981c
Subject: [PATCH 2/6] Adjust defaults based on system security level
19981c
19981c
Also permit arbitrary keylengths.
19981c
19981c
Disallow keylengths smaller than the configured system minimum.
19981c
19981c
Resolves: rhbz#1653323
19981c
19981c
Signed-off-by: Stephen Gallagher <sgallagh@redhat.com>
19981c
---
19981c
 config.h.in    |  1 -
19981c
 include/sscg.h |  1 +
19981c
 meson.build    | 10 ++++++--
19981c
 src/sscg.c     | 64 ++++++++++++++++++++++++++++++++++++++++++++++----
19981c
 4 files changed, 68 insertions(+), 8 deletions(-)
19981c
 delete mode 100644 config.h.in
19981c
19981c
diff --git a/config.h.in b/config.h.in
19981c
deleted file mode 100644
19981c
index 6044a4355f6c8bfac8d36e533f48f395c597e5ac..0000000000000000000000000000000000000000
19981c
--- a/config.h.in
19981c
+++ /dev/null
19981c
@@ -1 +0,0 @@
19981c
-#define PACKAGE_VERSION "@version@"
19981c
diff --git a/include/sscg.h b/include/sscg.h
19981c
index 2bd42bbee965c754efb91febd10b6a94af6f508e..3e97cfe49a5cd8fc734ecf43a94156e376227eb7 100644
19981c
--- a/include/sscg.h
19981c
+++ b/include/sscg.h
19981c
@@ -139,6 +139,7 @@ struct sscg_options
19981c
 
19981c
   /* Encryption requirements */
19981c
   int key_strength;
19981c
+  int minimum_key_strength;
19981c
   const EVP_MD *hash_fn;
19981c
 
19981c
   /* Output Files */
19981c
diff --git a/meson.build b/meson.build
19981c
index a2ca4ba1472bfff61fbbd30ba1ddc7ecc89e723c..c7b08ed3d6dff686f08a90ca869ba5881a9e8aaa 100644
19981c
--- a/meson.build
19981c
+++ b/meson.build
19981c
@@ -34,6 +34,7 @@ endforeach
19981c
 
19981c
 pkg = import('pkgconfig')
19981c
 crypto = dependency('libcrypto')
19981c
+ssl = dependency('libssl')
19981c
 path_utils = dependency('path_utils')
19981c
 talloc = dependency('talloc')
19981c
 
19981c
@@ -49,6 +50,10 @@ else
19981c
     popt_incdirs = include_directories('subprojects/popt')
19981c
 endif
19981c
 
19981c
+has_get_sec_level = cc.has_function(
19981c
+    'SSL_CTX_get_security_level',
19981c
+    dependencies: [ ssl])
19981c
+
19981c
 sscg_lib_srcs = [
19981c
     'src/authority.c',
19981c
     'src/bignum.c',
19981c
@@ -70,6 +75,7 @@ sscg_lib = static_library(
19981c
     sources : sscg_lib_srcs,
19981c
     dependencies : [
19981c
         crypto,
19981c
+        ssl,
19981c
         talloc,
19981c
     ],
19981c
     install : false,
19981c
@@ -135,9 +141,9 @@ init_bignum_test = executable(
19981c
 test('init_bignum_test', init_bignum_test)
19981c
 
19981c
 cdata = configuration_data()
19981c
-cdata.set('version', meson.project_version())
19981c
+cdata.set_quoted('PACKAGE_VERSION', meson.project_version())
19981c
+cdata.set('HAVE_SSL_CTX_GET_SECURITY_LEVEL', has_get_sec_level)
19981c
 configure_file(
19981c
-    input : 'config.h.in',
19981c
     output : 'config.h',
19981c
     configuration : cdata)
19981c
 
19981c
diff --git a/src/sscg.c b/src/sscg.c
19981c
index b2c7cbbfd9dc69d9f55a18bc91ed6023c0e64c2e..85a42404aa94524b560755d506b893300a4414cd 100644
19981c
--- a/src/sscg.c
19981c
+++ b/src/sscg.c
19981c
@@ -17,6 +17,7 @@
19981c
     Copyright 2017 by Stephen Gallagher <sgallagh@redhat.com>
19981c
 */
19981c
 
19981c
+#define _GNU_SOURCE
19981c
 #include <popt.h>
19981c
 #include <stdlib.h>
19981c
 #include <stdio.h>
19981c
@@ -25,6 +26,7 @@
19981c
 #include <path_utils.h>
19981c
 #include <unistd.h>
19981c
 #include <openssl/evp.h>
19981c
+#include <openssl/ssl.h>
19981c
 #include <sys/param.h>
19981c
 
19981c
 #include "config.h"
19981c
@@ -32,11 +34,59 @@
19981c
 #include "include/authority.h"
19981c
 #include "include/service.h"
19981c
 
19981c
+static int
19981c
+get_security_level (void)
19981c
+{
19981c
+#ifdef HAVE_SSL_CTX_GET_SECURITY_LEVEL
19981c
+  SSL_CTX *ssl_ctx = SSL_CTX_new (TLS_method ());
19981c
+  int security_level = SSL_CTX_get_security_level (ssl_ctx);
19981c
+  SSL_CTX_free (ssl_ctx);
19981c
+  ssl_ctx = NULL;
19981c
+  return security_level;
19981c
+#else
19981c
+  return 0;
19981c
+#endif
19981c
+}
19981c
+
19981c
 static int
19981c
 set_default_options (struct sscg_options *opts)
19981c
 {
19981c
+  int security_level = get_security_level ();
19981c
+
19981c
   opts->lifetime = 3650;
19981c
-  opts->key_strength = 2048;
19981c
+
19981c
+  /* Select the default key strength based on the system security level
19981c
+   * See:
19981c
+   * https://www.openssl.org/docs/man1.1.0/ssl/SSL_CTX_get_security_level.html
19981c
+   * for the specification of the minimums.
19981c
+   */
19981c
+  switch (security_level)
19981c
+    {
19981c
+    case 0:
19981c
+    case 1:
19981c
+    case 2:
19981c
+      /* Security level 2 and below permits lower key-strengths, but SSCG
19981c
+       * will set a minimum of 2048 bits
19981c
+       */
19981c
+      opts->key_strength = 2048;
19981c
+      break;
19981c
+
19981c
+    case 3: opts->key_strength = 3072; break;
19981c
+
19981c
+    case 4: opts->key_strength = 7680; break;
19981c
+
19981c
+    default:
19981c
+      /* Unknown security level. Default to the highest we know about */
19981c
+      fprintf (stderr,
19981c
+               "Unknown system security level %d. Defaulting to highest-known "
19981c
+               "level.\n",
19981c
+               security_level);
19981c
+      /* Fall through */
19981c
+
19981c
+    case 5: opts->key_strength = 15360; break;
19981c
+    }
19981c
+
19981c
+  opts->minimum_key_strength = opts->key_strength;
19981c
   return 0;
19981c
 }
19981c
 
19981c
@@ -117,6 +167,7 @@ main (int argc, const char **argv)
19981c
   size_t i;
19981c
   poptContext pc;
19981c
   struct sscg_options *options;
19981c
+  char *minimum_key_strength_help = NULL;
19981c
 
19981c
   char *country = NULL;
19981c
   char *state = NULL;
19981c
@@ -172,6 +223,9 @@ main (int argc, const char **argv)
19981c
   if (ret != EOK)
19981c
     goto done;
19981c
 
19981c
+  minimum_key_strength_help =
19981c
+    talloc_asprintf (main_ctx, "%d or larger", options->minimum_key_strength);
19981c
+
19981c
   options->verbosity = SSCG_DEFAULT;
19981c
   struct poptOption long_options[] = {
19981c
     POPT_AUTOHELP{ "quiet",
19981c
@@ -293,7 +347,7 @@ main (int argc, const char **argv)
19981c
       &options->key_strength,
19981c
       0,
19981c
       _ ("Strength of the certificate private keys in bits."),
19981c
-      _ ("{512,1024,2048,4096}") },
19981c
+      minimum_key_strength_help },
19981c
     {
19981c
       "hash-alg",
19981c
       '\0',
19981c
@@ -529,11 +583,11 @@ main (int argc, const char **argv)
19981c
         }
19981c
     }
19981c
 
19981c
-  if (options->key_strength != 512 && options->key_strength != 1024 &&
19981c
-      options->key_strength != 2048 && options->key_strength != 4096)
19981c
+  if (options->key_strength < options->minimum_key_strength)
19981c
     {
19981c
       fprintf (stderr,
19981c
-               "Key strength must be one of {512, 1024, 2048, 4096}.\n");
19981c
+               "Key strength must be at least %d bits.\n",
19981c
+               options->minimum_key_strength);
19981c
       ret = EINVAL;
19981c
       goto done;
19981c
     }
19981c
-- 
19981c
2.23.0
19981c