From 5e0babc3c7331fd9b74b8014537ce9095aa179ba Mon Sep 17 00:00:00 2001
From: paolodepa <paolo.depasquale@suse.com>
Date: Tue, 19 Mar 2024 11:18:27 +0100
Subject: [PATCH]  Make the openssl security level configurable

---
 bin/cepces-submit     |  6 ++++++
 cepces/config.py      | 20 ++++++++++++++++++--
 conf/cepces.conf.dist |  9 +++++++++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/bin/cepces-submit b/bin/cepces-submit
index 69a764d..09ccdb3 100755
--- a/bin/cepces-submit
+++ b/bin/cepces-submit
@@ -24,6 +24,7 @@ import os
 import sys
 import traceback
 import argparse
+import requests
 from cepces.certmonger.core import Result
 from cepces.certmonger.operation import Operation
 from cepces.config import Configuration
@@ -62,6 +63,8 @@ def main(global_overrides, krb5_overrides):
             # Load the configuration and instantiate a service.
             config = Configuration.load(global_overrides=global_overrides,
                                         krb5_overrides=krb5_overrides)
+            if config.openssl_seclevel and config.openssl_seclevel.isnumeric():
+                requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL:@SECLEVEL=%s' % config.openssl_seclevel
             service = Service(config)
 
             # Call the operation.
@@ -88,6 +91,7 @@ if __name__ == '__main__':
     parser.add_argument('--keytab', help='Use the specified keytab')
     parser.add_argument('--principals',
         help='A list of principals to try when requesting a ticket')
+    parser.add_argument('--openssl-seclevel', help='The openssl security level')
     args = parser.parse_args()
     g_overrides = {}
     if args.server is not None:
@@ -98,6 +102,8 @@ if __name__ == '__main__':
         g_overrides['endpoint'] = endpoint
     if args.poll_interval is not None:
         g_overrides['poll_interval'] = args.poll_interval
+    if args.openssl_seclevel is not None:
+        g_overrides['openssl_seclevel'] = args.openssl_seclevel
     k_overrides = {}
     if args.keytab is not None:
         k_overrides['keytab'] = args.keytab
diff --git a/cepces/config.py b/cepces/config.py
index 5f8e9b7..7fb3db4 100644
--- a/cepces/config.py
+++ b/cepces/config.py
@@ -55,7 +55,7 @@ class Configuration(Base):
         'Certificate': SOAPAuth.MessageCertificateAuthentication,
     }
 
-    def __init__(self, endpoint, endpoint_type, cas, auth, poll_interval):
+    def __init__(self, endpoint, endpoint_type, cas, auth, poll_interval, openssl_seclevel):
         super().__init__()
 
         self._endpoint = endpoint
@@ -63,6 +63,7 @@ def __init__(self, endpoint, endpoint_type, cas, auth, poll_interval):
         self._cas = cas
         self._auth = auth
         self._poll_interval = poll_interval
+        self._openssl_seclevel = openssl_seclevel
 
     @property
     def endpoint(self):
@@ -89,6 +90,16 @@ def poll_interval(self):
         """Return the poll interval."""
         return self._poll_interval
 
+    @property
+    def openssl_seclevel(self):
+        """Return the openssl security level."""
+        return self._openssl_seclevel
+
+    @property
+    def openssl_seclevel(self):
+        """Return the openssl security level."""
+        return self._openssl_seclevel
+
     @classmethod
     def load(cls, files=None, dirs=None, global_overrides=None,
              krb5_overrides=None):
@@ -116,6 +127,10 @@ def load(cls, files=None, dirs=None, global_overrides=None,
         config['DEFAULT']['shortname'] = shortname.lower()
         config['DEFAULT']['SHORTNAME'] = shortname.upper()
 
+        if not config.has_section('global'):
+            config.add_section('global')
+        config['global']['openssl_seclevel'] = ''
+
         if files is None:
             files = DEFAULT_CONFIG_FILES
 
@@ -178,8 +193,9 @@ def from_parser(cls, parser):
         authn = Configuration.AUTH_HANDLER_MAP[section['auth']](parser)
         cas = section.get('cas', True)
         poll_interval = section.get('poll_interval')
+        openssl_seclevel = section.get('openssl_seclevel')
 
         if cas == '':
             cas = False
 
-        return Configuration(endpoint, endpoint_type, cas, authn.handle(), poll_interval)
+        return Configuration(endpoint, endpoint_type, cas, authn.handle(), poll_interval, openssl_seclevel)
diff --git a/conf/cepces.conf.dist b/conf/cepces.conf.dist
index 4c71105..4729085 100644
--- a/conf/cepces.conf.dist
+++ b/conf/cepces.conf.dist
@@ -48,6 +48,15 @@ endpoint=https://${server}/ADPolicyProvider_CEP_${auth}/service.svc/CEP
 # Time in seconds before re-checking if the certificate has been issued
 poll_interval=3600
 
+# This is the openssl security level. The latest openssl sets the default to
+# level 2, which disables some less secure ciphers. You may encounter an error
+# of '[SSL: DH_KEY_TOO_SMALL] dh key too small' if this level is set to high
+# for your server. If this happens, you can work around this issue by setting
+# openssl_seclevel to 1.
+#
+# Default: 2
+#openssl_seclevel=2
+
 [kerberos]
 # Use the specified keytab. If unspecified, the system default is used.
 #
 