summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorYuriy Andamasov <yuriy@vyos.io>2026-09-26 16:58:39 +0000
committerChristian Breunig <christian@breunig.cc>2026-09-26 17:09:05 +0000
commitd497c3b7a7504bb578874cc706dc75849ad84c2d (patch)
treea107f73dd96864b314e3de866240461d72fd5c52 /python
parent430dda1e170576239d3580c1b7a6381525a05cbc (diff)
downloadvyos-1x-fix/T8859-dh-keysize-validation.tar.gz
vyos-1x-fix/T8859-dh-keysize-validation.zip
python: T8859: fail loudly on an invalid Diffie-Hellman keysize argumentfix/T8859-dh-keysize-validation
The Diffie-Hellman length check stringified the requested minimum keysize into a variable it never used, then converted the original argument again for the comparison. A non-numeric value therefore escaped as an uncaught ValueError from the comparison rather than being reported where it was passed in. The argument is now typed and validated up front: a non-integer raises TypeError, booleans included, and a non-positive integer raises ValueError. Both indicate a logic error in the calling script and should fail loudly instead of silently reporting an undersized key. The only callers today are the unit tests, so the contract is free to change. A file that exists but holds no readable parameters is user input rather than a logic error, and now fails the check instead of propagating the error from openssl. The bit-count pattern becomes a raw string, and the tests pass integer keysizes and cover the new paths.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/configverify.py38
1 files changed, 26 insertions, 12 deletions
diff --git a/python/vyos/configverify.py b/python/vyos/configverify.py
index c1c038a7a..17476f02d 100644
--- a/python/vyos/configverify.py
+++ b/python/vyos/configverify.py
@@ -424,24 +424,38 @@ def verify_vlan_config(config):
verify_mtu_ipv6(c_vlan)
-def verify_diffie_hellman_length(file, min_keysize):
- """ Verify Diffie-Hellamn keypair length given via file. It must be greater
- then or equal to min_keysize """
+def verify_diffie_hellman_length(file: str, min_keysize: int) -> bool:
+ """ Verify Diffie-Hellman keypair length given via file. It must be greater
+ than or equal to min_keysize.
+
+ Raises TypeError if min_keysize is not an integer and ValueError if it is
+ not a positive integer - both indicate a logic error in the caller. An
+ unreadable or malformed file is user input, not a logic error, and simply
+ fails the check. """
import os
import re
from vyos.utils.process import cmdl
- try:
- keysize = str(min_keysize)
- except:
- return False
+ if isinstance(min_keysize, bool) or not isinstance(min_keysize, int):
+ raise TypeError(
+ f'min_keysize must be an integer, got {type(min_keysize).__name__}')
+ if min_keysize < 1:
+ raise ValueError(
+ f'min_keysize must be a positive integer, got {min_keysize}')
if os.path.exists(file):
- out = cmdl(['openssl', 'dhparam', '-inform', 'PEM', '-in', file, '-text'])
- prog = re.compile('\d+\s+bit')
- if prog.search(out):
- bits = prog.search(out)[0].split()[0]
- if int(bits) >= int(min_keysize):
+ try:
+ out = cmdl(
+ ['openssl', 'dhparam', '-inform', 'PEM', '-in', file, '-text'])
+ except OSError:
+ # Not a readable PEM encoded Diffie-Hellman parameter file
+ return False
+
+ prog = re.compile(r'\d+\s+bit')
+ match = prog.search(out)
+ if match:
+ bits = match[0].split()[0]
+ if int(bits) >= min_keysize:
return True
return False