diff options
author | sarthurdev <965089+sarthurdev@users.noreply.github.com> | 2022-06-25 23:04:31 +0200 |
---|---|---|
committer | sarthurdev <965089+sarthurdev@users.noreply.github.com> | 2022-06-29 01:37:59 +0200 |
commit | 0d5ac59894ae7c10bd9d69047fa7098de66f835f (patch) | |
tree | 2b36fb68c41ddb4f13a087833edd774cd841f005 /python/vyos/pki.py | |
parent | 56457c9f35c5273d1d7ad679f37278c6cf2c77b0 (diff) | |
download | vyos-1x-0d5ac59894ae7c10bd9d69047fa7098de66f835f.tar.gz vyos-1x-0d5ac59894ae7c10bd9d69047fa7098de66f835f.zip |
openvpn: T4485: Accept multiple `tls ca-certificate` values
Diffstat (limited to 'python/vyos/pki.py')
-rw-r--r-- | python/vyos/pki.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/python/vyos/pki.py b/python/vyos/pki.py index fd91fc9bf..648064a3a 100644 --- a/python/vyos/pki.py +++ b/python/vyos/pki.py @@ -332,6 +332,25 @@ def verify_certificate(cert, ca_cert): except InvalidSignature: return False +def verify_ca_chain(sorted_names, pki_node): + if len(sorted_names) == 1: # Single cert, no chain + return True + + for name in sorted_names: + cert = load_certificate(pki_node[name]['certificate']) + verified = False + for ca_name in sorted_names: + if name == ca_name: + continue + ca_cert = load_certificate(pki_node[ca_name]['certificate']) + if verify_certificate(cert, ca_cert): + verified = True + break + if not verified and name != sorted_names[-1]: + # Only permit top-most certificate to fail verify (e.g. signed by public CA not explicitly in chain) + return False + return True + # Certificate chain def find_parent(cert, ca_certs): @@ -357,3 +376,16 @@ def find_chain(cert, ca_certs): chain.append(parent) return chain + +def sort_ca_chain(ca_names, pki_node): + def ca_cmp(ca_name1, ca_name2, pki_node): + cert1 = load_certificate(pki_node[ca_name1]['certificate']) + cert2 = load_certificate(pki_node[ca_name2]['certificate']) + + if verify_certificate(cert1, cert2): # cert1 is child of cert2 + return -1 + return 1 + + from functools import cmp_to_key + return sorted(ca_names, key=cmp_to_key(lambda cert1, cert2: ca_cmp(cert1, cert2, pki_node))) + |