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

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