| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2016-2017, Yanis Guenane <[email protected]> |
| 3 |
# Copyright (c) 2017, Markus Teufelberger <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: openssl_csr_info |
| 11 |
short_description: Provide information of OpenSSL Certificate Signing Requests (CSR) |
| 12 |
description: |
| 13 |
- This module allows one to query information on OpenSSL Certificate Signing Requests (CSR). |
| 14 |
- In case the CSR signature cannot be validated, the module will fail. In this case, all return variables are still returned. |
| 15 |
- It uses the cryptography python library to interact with OpenSSL. |
| 16 |
author: |
| 17 |
- Felix Fontein (@felixfontein) |
| 18 |
- Yanis Guenane (@Spredzy) |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.crypto._attributes |
| 21 |
- community.crypto._attributes.info_module |
| 22 |
- community.crypto._attributes.idempotent_not_modify_state |
| 23 |
- community.crypto._cryptography_dep.minimum |
| 24 |
- community.crypto._name_encoding |
| 25 |
options: |
| 26 |
path: |
| 27 |
description: |
| 28 |
- Remote absolute path where the CSR file is loaded from. |
| 29 |
- Either O(path) or O(content) must be specified, but not both. |
| 30 |
type: path |
| 31 |
content: |
| 32 |
description: |
| 33 |
- Content of the CSR file. |
| 34 |
- Either O(path) or O(content) must be specified, but not both. |
| 35 |
type: str |
| 36 |
version_added: "1.0.0" |
| 37 |
select_crypto_backend: |
| 38 |
description: |
| 39 |
- Determines which crypto backend to use. |
| 40 |
- The default choice is V(auto), which tries to use C(cryptography) if available. |
| 41 |
- If set to V(cryptography), will try to use the L(cryptography,https://cryptography.io/) library. |
| 42 |
- Note that with community.crypto 3.0.0, all values behave the same. |
| 43 |
This option will be deprecated in a later version. |
| 44 |
We recommend to not set it explicitly. |
| 45 |
type: str |
| 46 |
default: auto |
| 47 |
choices: [auto, cryptography] |
| 48 |
|
| 49 |
seealso: |
| 50 |
- module: community.crypto.openssl_csr |
| 51 |
- module: community.crypto.openssl_csr_pipe |
| 52 |
- plugin: community.crypto.openssl_csr_info |
| 53 |
plugin_type: filter |
| 54 |
description: A filter variant of this module. |
| 55 |
- plugin: community.crypto.to_serial |
| 56 |
plugin_type: filter |
| 57 |
""" |
| 58 |
|
| 59 |
EXAMPLES = r""" |
| 60 |
--- |
| 61 |
- name: Generate an OpenSSL Certificate Signing Request |
| 62 |
community.crypto.openssl_csr: |
| 63 |
path: /etc/ssl/csr/www.ansible.com.csr |
| 64 |
privatekey_path: /etc/ssl/private/ansible.com.pem |
| 65 |
common_name: www.ansible.com |
| 66 |
|
| 67 |
- name: Get information on the CSR |
| 68 |
community.crypto.openssl_csr_info: |
| 69 |
path: /etc/ssl/csr/www.ansible.com.csr |
| 70 |
register: result |
| 71 |
|
| 72 |
- name: Dump information |
| 73 |
ansible.builtin.debug: |
| 74 |
var: result |
| 75 |
""" |
| 76 |
|
| 77 |
RETURN = r""" |
| 78 |
signature_valid: |
| 79 |
description: |
| 80 |
- Whether the CSR's signature is valid. |
| 81 |
- In case the check returns V(false), the module will fail. |
| 82 |
returned: success |
| 83 |
type: bool |
| 84 |
basic_constraints: |
| 85 |
description: Entries in the C(basic_constraints) extension, or V(none) if extension is not present. |
| 86 |
returned: success |
| 87 |
type: list |
| 88 |
elements: str |
| 89 |
sample: ['CA:TRUE', 'pathlen:1'] |
| 90 |
basic_constraints_critical: |
| 91 |
description: Whether the C(basic_constraints) extension is critical. |
| 92 |
returned: success |
| 93 |
type: bool |
| 94 |
extended_key_usage: |
| 95 |
description: Entries in the C(extended_key_usage) extension, or V(none) if extension is not present. |
| 96 |
returned: success |
| 97 |
type: list |
| 98 |
elements: str |
| 99 |
sample: [Biometric Info, DVCS, Time Stamping] |
| 100 |
extended_key_usage_critical: |
| 101 |
description: Whether the C(extended_key_usage) extension is critical. |
| 102 |
returned: success |
| 103 |
type: bool |
| 104 |
extensions_by_oid: |
| 105 |
description: Returns a dictionary for every extension OID. |
| 106 |
returned: success |
| 107 |
type: dict |
| 108 |
contains: |
| 109 |
critical: |
| 110 |
description: Whether the extension is critical. |
| 111 |
returned: success |
| 112 |
type: bool |
| 113 |
value: |
| 114 |
description: |
| 115 |
- The Base64 encoded value (in DER format) of the extension. |
| 116 |
- B(Note) that depending on the C(cryptography) version used, it is not possible to extract the ASN.1 content of the |
| 117 |
extension, but only to provide the re-encoded content of the extension in case it was parsed by C(cryptography). |
| 118 |
This should usually result in exactly the same value, except if the original extension value was malformed. |
| 119 |
returned: success |
| 120 |
type: str |
| 121 |
sample: "MAMCAQU=" |
| 122 |
sample: {"1.3.6.1.5.5.7.1.24": {"critical": false, "value": "MAMCAQU="}} |
| 123 |
key_usage: |
| 124 |
description: Entries in the C(key_usage) extension, or V(none) if extension is not present. |
| 125 |
returned: success |
| 126 |
type: str |
| 127 |
sample: [Key Agreement, Data Encipherment] |
| 128 |
key_usage_critical: |
| 129 |
description: Whether the C(key_usage) extension is critical. |
| 130 |
returned: success |
| 131 |
type: bool |
| 132 |
subject_alt_name: |
| 133 |
description: |
| 134 |
- Entries in the C(subject_alt_name) extension, or V(none) if extension is not present. |
| 135 |
- See O(name_encoding) for how IDNs are handled. |
| 136 |
returned: success |
| 137 |
type: list |
| 138 |
elements: str |
| 139 |
sample: ["DNS:www.ansible.com", "IP:1.2.3.4"] |
| 140 |
subject_alt_name_critical: |
| 141 |
description: Whether the C(subject_alt_name) extension is critical. |
| 142 |
returned: success |
| 143 |
type: bool |
| 144 |
ocsp_must_staple: |
| 145 |
description: V(true) if the OCSP Must Staple extension is present, V(none) otherwise. |
| 146 |
returned: success |
| 147 |
type: bool |
| 148 |
ocsp_must_staple_critical: |
| 149 |
description: Whether the C(ocsp_must_staple) extension is critical. |
| 150 |
returned: success |
| 151 |
type: bool |
| 152 |
name_constraints_permitted: |
| 153 |
description: List of permitted subtrees to sign certificates for. |
| 154 |
returned: success |
| 155 |
type: list |
| 156 |
elements: str |
| 157 |
sample: ['email:.somedomain.com'] |
| 158 |
version_added: 1.1.0 |
| 159 |
name_constraints_excluded: |
| 160 |
description: |
| 161 |
- List of excluded subtrees the CA cannot sign certificates for. |
| 162 |
- Is V(none) if extension is not present. |
| 163 |
- See O(name_encoding) for how IDNs are handled. |
| 164 |
returned: success |
| 165 |
type: list |
| 166 |
elements: str |
| 167 |
sample: ['email:.com'] |
| 168 |
version_added: 1.1.0 |
| 169 |
name_constraints_critical: |
| 170 |
description: |
| 171 |
- Whether the C(name_constraints) extension is critical. |
| 172 |
- Is V(none) if extension is not present. |
| 173 |
returned: success |
| 174 |
type: bool |
| 175 |
version_added: 1.1.0 |
| 176 |
subject: |
| 177 |
description: |
| 178 |
- The CSR's subject as a dictionary. |
| 179 |
- Note that for repeated values, only the last one will be returned. |
| 180 |
returned: success |
| 181 |
type: dict |
| 182 |
sample: {"commonName": "www.example.com", "emailAddress": "[email protected]"} |
| 183 |
subject_ordered: |
| 184 |
description: The CSR's subject as an ordered list of tuples. |
| 185 |
returned: success |
| 186 |
type: list |
| 187 |
elements: list |
| 188 |
sample: [["commonName", "www.example.com"], ["emailAddress": "[email protected]"]] |
| 189 |
public_key: |
| 190 |
description: CSR's public key in PEM format. |
| 191 |
returned: success |
| 192 |
type: str |
| 193 |
sample: "-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8A..." |
| 194 |
public_key_type: |
| 195 |
description: |
| 196 |
- The CSR's public key's type. |
| 197 |
- One of V(RSA), V(DSA), V(ECC), V(Ed25519), V(X25519), V(Ed448), or V(X448). |
| 198 |
- Will start with V(unknown) if the key type cannot be determined. |
| 199 |
returned: success |
| 200 |
type: str |
| 201 |
version_added: 1.7.0 |
| 202 |
sample: RSA |
| 203 |
public_key_data: |
| 204 |
description: |
| 205 |
- Public key data. Depends on the public key's type. |
| 206 |
returned: success |
| 207 |
type: dict |
| 208 |
version_added: 1.7.0 |
| 209 |
contains: |
| 210 |
size: |
| 211 |
description: |
| 212 |
- Bit size of modulus (RSA) or prime number (DSA). |
| 213 |
type: int |
| 214 |
returned: When RV(public_key_type=RSA) or RV(public_key_type=DSA) |
| 215 |
modulus: |
| 216 |
description: |
| 217 |
- The RSA key's modulus. |
| 218 |
type: int |
| 219 |
returned: When RV(public_key_type=RSA) |
| 220 |
exponent: |
| 221 |
description: |
| 222 |
- The RSA key's public exponent. |
| 223 |
type: int |
| 224 |
returned: When RV(public_key_type=RSA) |
| 225 |
p: |
| 226 |
description: |
| 227 |
- The C(p) value for DSA. |
| 228 |
- This is the prime modulus upon which arithmetic takes place. |
| 229 |
type: int |
| 230 |
returned: When RV(public_key_type=DSA) |
| 231 |
q: |
| 232 |
description: |
| 233 |
- The C(q) value for DSA. |
| 234 |
- This is a prime that divides C(p - 1), and at the same time the order of the subgroup of the multiplicative group |
| 235 |
of the prime field used. |
| 236 |
type: int |
| 237 |
returned: When RV(public_key_type=DSA) |
| 238 |
g: |
| 239 |
description: |
| 240 |
- The C(g) value for DSA. |
| 241 |
- This is the element spanning the subgroup of the multiplicative group of the prime field used. |
| 242 |
type: int |
| 243 |
returned: When RV(public_key_type=DSA) |
| 244 |
curve: |
| 245 |
description: |
| 246 |
- The curve's name for ECC. |
| 247 |
type: str |
| 248 |
returned: When RV(public_key_type=ECC) |
| 249 |
exponent_size: |
| 250 |
description: |
| 251 |
- The maximum number of bits of a private key. This is basically the bit size of the subgroup used. |
| 252 |
type: int |
| 253 |
returned: When RV(public_key_type=ECC) |
| 254 |
x: |
| 255 |
description: |
| 256 |
- The C(x) coordinate for the public point on the elliptic curve. |
| 257 |
type: int |
| 258 |
returned: When RV(public_key_type=ECC) |
| 259 |
y: |
| 260 |
description: |
| 261 |
- For RV(public_key_type=ECC), this is the C(y) coordinate for the public point on the elliptic curve. |
| 262 |
- For RV(public_key_type=DSA), this is the publicly known group element whose discrete logarithm w.r.t. C(g) is the |
| 263 |
private key. |
| 264 |
type: int |
| 265 |
returned: When RV(public_key_type=DSA) or RV(public_key_type=ECC) |
| 266 |
public_key_fingerprints: |
| 267 |
description: |
| 268 |
- Fingerprints of CSR's public key. |
| 269 |
- For every hash algorithm available, the fingerprint is computed. |
| 270 |
returned: success |
| 271 |
type: dict |
| 272 |
sample: "{'sha256': 'd4:b3:aa:6d:c8:04:ce:4e:ba:f6:29:4d:92:a3:94:b0:c2:ff:bd:bf:33:63:11:43:34:0f:51:b0:95:09:2f:63', 'sha512': |
| 273 |
'f7:07:4a:f0:b0:f0:e6:8b:95:5f:f9:e6:61:0a:32:68:f1..." |
| 274 |
subject_key_identifier: |
| 275 |
description: |
| 276 |
- The CSR's subject key identifier. |
| 277 |
- The identifier is returned in hexadecimal, with V(:) used to separate bytes. |
| 278 |
- Is V(none) if the C(SubjectKeyIdentifier) extension is not present. |
| 279 |
returned: success |
| 280 |
type: str |
| 281 |
sample: '00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33' |
| 282 |
authority_key_identifier: |
| 283 |
description: |
| 284 |
- The CSR's authority key identifier. |
| 285 |
- The identifier is returned in hexadecimal, with V(:) used to separate bytes. |
| 286 |
- Is V(none) if the C(AuthorityKeyIdentifier) extension is not present. |
| 287 |
returned: success |
| 288 |
type: str |
| 289 |
sample: '00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33' |
| 290 |
authority_cert_issuer: |
| 291 |
description: |
| 292 |
- The CSR's authority cert issuer as a list of general names. |
| 293 |
- Is V(none) if the C(AuthorityKeyIdentifier) extension is not present. |
| 294 |
- See O(name_encoding) for how IDNs are handled. |
| 295 |
returned: success |
| 296 |
type: list |
| 297 |
elements: str |
| 298 |
sample: ["DNS:www.ansible.com", "IP:1.2.3.4"] |
| 299 |
authority_cert_serial_number: |
| 300 |
description: |
| 301 |
- The CSR's authority cert serial number. |
| 302 |
- Is V(none) if the C(AuthorityKeyIdentifier) extension is not present. |
| 303 |
- This return value is an B(integer). If you need the serial numbers as a colon-separated hex string, such as C(11:22:33), |
| 304 |
you need to convert it to that form with P(community.crypto.to_serial#filter). |
| 305 |
returned: success |
| 306 |
type: int |
| 307 |
sample: 12345 |
| 308 |
""" |
| 309 |
|
| 310 |
import typing as t |
| 311 |
|
| 312 |
from ansible.module_utils.basic import AnsibleModule |
| 313 |
|
| 314 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.basic import ( |
| 315 |
OpenSSLObjectError, |
| 316 |
) |
| 317 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.module_backends.csr_info import ( |
| 318 |
select_backend, |
| 319 |
) |
| 320 |
|
| 321 |
|
| 322 |
def main() -> t.NoReturn: |
| 323 |
module = AnsibleModule( |
| 324 |
argument_spec={ |
| 325 |
"path": {"type": "path"}, |
| 326 |
"content": {"type": "str"}, |
| 327 |
"name_encoding": { |
| 328 |
"type": "str", |
| 329 |
"default": "ignore", |
| 330 |
"choices": ["ignore", "idna", "unicode"], |
| 331 |
}, |
| 332 |
"select_crypto_backend": { |
| 333 |
"type": "str", |
| 334 |
"default": "auto", |
| 335 |
"choices": ["auto", "cryptography"], |
| 336 |
}, |
| 337 |
}, |
| 338 |
required_one_of=(["path", "content"],), |
| 339 |
mutually_exclusive=(["path", "content"],), |
| 340 |
supports_check_mode=True, |
| 341 |
) |
| 342 |
|
| 343 |
content: str | None = module.params["content"] |
| 344 |
path: str | None = module.params["path"] |
| 345 |
if content is not None: |
| 346 |
data = content.encode("utf-8") |
| 347 |
else: |
| 348 |
if path is None: |
| 349 |
module.fail_json(msg="One of content and path must be provided") |
| 350 |
try: |
| 351 |
with open(path, "rb") as f: |
| 352 |
data = f.read() |
| 353 |
except (IOError, OSError) as e: |
| 354 |
module.fail_json(msg=f"Error while reading CSR file from disk: {e}") |
| 355 |
|
| 356 |
module_backend = select_backend( |
| 357 |
module=module, content=data, validate_signature=True |
| 358 |
) |
| 359 |
|
| 360 |
try: |
| 361 |
result = module_backend.get_info() |
| 362 |
module.exit_json(**result) |
| 363 |
except OpenSSLObjectError as exc: |
| 364 |
module.fail_json(msg=str(exc)) |
| 365 |
|
| 366 |
|
| 367 |
if __name__ == "__main__": |
| 368 |
main() |