|
10 | 10 | import datetime |
11 | 11 | import io |
12 | 12 | import os |
| 13 | +import re |
13 | 14 | import stat |
14 | 15 | import sys |
15 | 16 | import traceback |
@@ -257,15 +258,26 @@ def get_cert_valid_until(cert): |
257 | 258 |
|
258 | 259 |
|
259 | 260 | # @brief convert certificate to PEM format |
260 | | -# @param cert certificate object in pyopenssl format |
| 261 | +# @param cert certificate object or a list thereof |
261 | 262 | # @return the certificate in PEM format |
262 | 263 | def convert_cert_to_pem_str(cert): |
263 | | - return cert.public_bytes(serialization.Encoding.PEM).decode('utf8') |
| 264 | + if not isinstance(cert, list): |
| 265 | + cert = [cert] |
| 266 | + result = list() |
| 267 | + for data in cert: |
| 268 | + result.append(data.public_bytes(serialization.Encoding.PEM).decode('utf8')) |
| 269 | + return '\n'.join(result) |
264 | 270 |
|
265 | 271 |
|
266 | 272 | # @brief load a PEM certificate from str |
| 273 | +# @return a certificate object or a list of objects if multiple are in the string |
267 | 274 | def convert_pem_str_to_cert(certdata): |
268 | | - return x509.load_pem_x509_certificate(certdata.encode('utf8'), default_backend()) |
| 275 | + certs = re.findall(r'(-----BEGIN CERTIFICATE-----\n[^\-]+\n-----END CERTIFICATE-----)', |
| 276 | + certdata, re.DOTALL) |
| 277 | + result = list() |
| 278 | + for data in certs: |
| 279 | + result.append(x509.load_pem_x509_certificate(data.encode('utf8'), default_backend())) |
| 280 | + return result[0] if len(result) == 1 else result |
269 | 281 |
|
270 | 282 |
|
271 | 283 | # @brief serialize cert/csr to DER bytes |
@@ -411,6 +423,9 @@ def is_ocsp_valid(cert, issuer, hash_algo): |
411 | 423 | log("Invalid hash algorithm '{}' used for OCSP validation. Validation ignored.".format(hash_algo), warning=True) |
412 | 424 | return True |
413 | 425 |
|
| 426 | + if isinstance(issuer, list): |
| 427 | + issuer = issuer[0] # First certificate in the CA chain is the immediate issuer |
| 428 | + |
414 | 429 | try: |
415 | 430 | ocsp_urls = [] |
416 | 431 | aia = cert.extensions.get_extension_for_oid(ExtensionOID.AUTHORITY_INFORMATION_ACCESS) |
|
0 commit comments