|
4 | 4 | """
|
5 | 5 | import os
|
6 | 6 | import tempfile
|
| 7 | +import M2Crypto |
| 8 | +from packaging.version import Version |
7 | 9 | from M2Crypto import SSL, m2, X509
|
8 | 10 |
|
| 11 | + |
9 | 12 | from DIRAC.Core.DISET import DEFAULT_SSL_CIPHERS, DEFAULT_SSL_METHODS
|
10 | 13 | from DIRAC.Core.Security import Locations
|
11 | 14 | from DIRAC.Core.Security.m2crypto.X509Chain import X509Chain
|
|
15 | 18 | DEBUG_M2CRYPTO = os.getenv("DIRAC_DEBUG_M2CRYPTO", "No").lower() in ("yes", "true")
|
16 | 19 |
|
17 | 20 |
|
| 21 | +VERIFY_ALLOW_PROXY_CERTS = 0 |
| 22 | + |
| 23 | +# If the version of M2Crypto is recent enough, there is an API |
| 24 | +# to accept proxy certificate, and we do not need to rely on |
| 25 | +# OPENSSL_ALLOW_PROXY_CERT environment variable |
| 26 | +# which was removed as of openssl 1.1 |
| 27 | +# We need this to be merged in M2Crypto: https://gitlab.com/m2crypto/m2crypto/merge_requests/236 |
| 28 | +# We set the proper verify flag to the X509Store of the context |
| 29 | +# as described here https://www.openssl.org/docs/man1.1.1/man7/proxy-certificates.html |
| 30 | +if hasattr(SSL, "verify_allow_proxy_certs"): |
| 31 | + VERIFY_ALLOW_PROXY_CERTS = SSL.verify_allow_proxy_certs # pylint: disable=no-member |
| 32 | +# As of M2Crypto 0.37, the `verify_allow_proxy_certs` flag was moved |
| 33 | +# to X509 (https://gitlab.com/m2crypto/m2crypto/-/merge_requests/238) |
| 34 | +# It is more consistent with all the other flags, |
| 35 | +# but pySSL had it in SSL. Well... |
| 36 | +elif hasattr(X509, "verify_allow_proxy_certs"): |
| 37 | + VERIFY_ALLOW_PROXY_CERTS = X509.verify_allow_proxy_certs # pylint: disable=no-member |
| 38 | +# As of M2Crypto 0.38, M2Crypto did not export the symbol correctly |
| 39 | +# Anymore |
| 40 | +# https://gitlab.com/m2crypto/m2crypto/-/issues/298 |
| 41 | +elif Version(M2Crypto.__version__) >= Version("0.38.0"): |
| 42 | + VERIFY_ALLOW_PROXY_CERTS = 64 |
| 43 | + |
| 44 | + |
18 | 45 | def __loadM2SSLCTXHostcert(ctx):
|
19 | 46 | """Load hostcert & key from the default location and set them as the
|
20 | 47 | credentials for SSL context ctx.
|
@@ -125,21 +152,9 @@ def getM2SSLContext(ctx=None, **kwargs):
|
125 | 152 | raise RuntimeError(f"CA path ({caPath}) is not a valid directory")
|
126 | 153 | ctx.load_verify_locations(capath=caPath)
|
127 | 154 |
|
128 |
| - # If the version of M2Crypto is recent enough, there is an API |
129 |
| - # to accept proxy certificate, and we do not need to rely on |
130 |
| - # OPENSSL_ALLOW_PROXY_CERT environment variable |
131 |
| - # which was removed as of openssl 1.1 |
132 |
| - # We need this to be merged in M2Crypto: https://gitlab.com/m2crypto/m2crypto/merge_requests/236 |
133 |
| - # We set the proper verify flag to the X509Store of the context |
134 |
| - # as described here https://www.openssl.org/docs/man1.1.1/man7/proxy-certificates.html |
135 |
| - if hasattr(SSL, "verify_allow_proxy_certs"): |
136 |
| - ctx.get_cert_store().set_flags(SSL.verify_allow_proxy_certs) # pylint: disable=no-member |
137 |
| - # As of M2Crypto 0.37, the `verify_allow_proxy_certs` flag was moved |
138 |
| - # to X509 (https://gitlab.com/m2crypto/m2crypto/-/merge_requests/238) |
139 |
| - # It is more consistent with all the other flags, |
140 |
| - # but pySSL had it in SSL. Well... |
141 |
| - if hasattr(X509, "verify_allow_proxy_certs"): |
142 |
| - ctx.get_cert_store().set_flags(X509.verify_allow_proxy_certs) # pylint: disable=no-member |
| 155 | + # Allow proxy certificates to be used |
| 156 | + if VERIFY_ALLOW_PROXY_CERTS: |
| 157 | + ctx.get_cert_store().set_flags(VERIFY_ALLOW_PROXY_CERTS) |
143 | 158 |
|
144 | 159 | # Other parameters
|
145 | 160 | sslMethods = kwargs.get("sslMethods", DEFAULT_SSL_METHODS)
|
|
0 commit comments