| 1 |
# Copyright (c) 2016 Michael Gruener <[email protected]> |
| 2 |
# Copyright (c) 2021 Felix Fontein <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 7 |
# Do not use this from other collections or standalone plugins/modules! |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
import abc |
| 12 |
import typing as t |
| 13 |
|
| 14 |
from ansible_collections.community.crypto.plugins.module_utils._acme.errors import ( |
| 15 |
ModuleFailException, |
| 16 |
) |
| 17 |
from ansible_collections.community.crypto.plugins.module_utils._acme.utils import ( |
| 18 |
der_to_pem, |
| 19 |
process_links, |
| 20 |
) |
| 21 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.pem import ( |
| 22 |
split_pem_list, |
| 23 |
) |
| 24 |
|
| 25 |
if t.TYPE_CHECKING: |
| 26 |
from ansible_collections.community.crypto.plugins.module_utils._acme.acme import ( # pragma: no cover |
| 27 |
ACMEClient, |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
_CertificateChain = t.TypeVar("_CertificateChain", bound="CertificateChain") |
| 32 |
|
| 33 |
|
| 34 |
class CertificateChain: |
| 35 |
""" |
| 36 |
Download and parse the certificate chain. |
| 37 |
https://tools.ietf.org/html/rfc8555#section-7.4.2 |
| 38 |
""" |
| 39 |
|
| 40 |
def __init__(self, url: str): |
| 41 |
self.url = url |
| 42 |
self.cert: str | None = None |
| 43 |
self.chain: list[str] = [] |
| 44 |
self.alternates: list[str] = [] |
| 45 |
|
| 46 |
@classmethod |
| 47 |
def download( |
| 48 |
cls: type[_CertificateChain], *, client: ACMEClient, url: str |
| 49 |
) -> _CertificateChain: |
| 50 |
content, info = client.get_request( |
| 51 |
url, |
| 52 |
parse_json_result=False, |
| 53 |
headers={"Accept": "application/pem-certificate-chain"}, |
| 54 |
) |
| 55 |
|
| 56 |
if not content or not info["content-type"].startswith( |
| 57 |
"application/pem-certificate-chain" |
| 58 |
): |
| 59 |
raise ModuleFailException( |
| 60 |
f"Cannot download certificate chain from {url}, as content type is not application/pem-certificate-chain: {content!r} (headers: {info})" |
| 61 |
) |
| 62 |
|
| 63 |
result = cls(url) |
| 64 |
|
| 65 |
# Parse data |
| 66 |
certs = split_pem_list(content.decode("utf-8"), keep_inbetween=True) |
| 67 |
if certs: |
| 68 |
result.cert = certs[0] |
| 69 |
result.chain = certs[1:] |
| 70 |
|
| 71 |
process_links( |
| 72 |
info=info, |
| 73 |
callback=lambda link, relation: result._process_links( # pylint: disable=protected-access |
| 74 |
client=client, link=link, relation=relation |
| 75 |
), |
| 76 |
) |
| 77 |
|
| 78 |
if result.cert is None: |
| 79 |
raise ModuleFailException( |
| 80 |
f"Failed to parse certificate chain download from {url}: {content!r} (headers: {info})" |
| 81 |
) |
| 82 |
|
| 83 |
return result |
| 84 |
|
| 85 |
def _process_links(self, *, client: ACMEClient, link: str, relation: str) -> None: |
| 86 |
if relation == "up": |
| 87 |
# Process link-up headers if there was no chain in reply |
| 88 |
if not self.chain: |
| 89 |
chain_result, chain_info = client.get_request( |
| 90 |
link, parse_json_result=False |
| 91 |
) |
| 92 |
if chain_info["status"] in [200, 201]: |
| 93 |
self.chain.append(der_to_pem(chain_result)) |
| 94 |
elif relation == "alternate": |
| 95 |
self.alternates.append(link) |
| 96 |
|
| 97 |
def to_json(self) -> dict[str, bytes]: |
| 98 |
if self.cert is None: |
| 99 |
raise ValueError("Has no certificate") |
| 100 |
cert = self.cert.encode("utf8") |
| 101 |
chain = ("\n".join(self.chain)).encode("utf8") |
| 102 |
return { |
| 103 |
"cert": cert, |
| 104 |
"chain": chain, |
| 105 |
"full_chain": cert + chain, |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
class Criterium: |
| 110 |
def __init__(self, *, criterium: dict[str, t.Any], index: int): |
| 111 |
self.index = index |
| 112 |
self.test_certificates: t.Literal["first", "last", "all"] = criterium[ |
| 113 |
"test_certificates" |
| 114 |
] |
| 115 |
self.subject: dict[str, t.Any] | None = criterium["subject"] |
| 116 |
self.issuer: dict[str, t.Any] | None = criterium["issuer"] |
| 117 |
self.subject_key_identifier: str | None = criterium["subject_key_identifier"] |
| 118 |
self.authority_key_identifier: str | None = criterium[ |
| 119 |
"authority_key_identifier" |
| 120 |
] |
| 121 |
|
| 122 |
|
| 123 |
class ChainMatcher(metaclass=abc.ABCMeta): |
| 124 |
@abc.abstractmethod |
| 125 |
def match(self, *, certificate: CertificateChain) -> bool: |
| 126 |
""" |
| 127 |
Check whether a certificate chain (CertificateChain instance) matches. |
| 128 |
""" |
| 129 |
|
| 130 |
|
| 131 |
__all__ = ("CertificateChain", "ChainMatcher", "Criterium") |