| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2019, Patrick Pichler <[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 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: openssl_signature_info |
| 10 |
version_added: 1.1.0 |
| 11 |
short_description: Verify signatures with openssl |
| 12 |
description: |
| 13 |
- This module allows one to verify a signature for a file by a certificate. |
| 14 |
- The module uses the cryptography Python library. |
| 15 |
author: |
| 16 |
- Patrick Pichler (@aveexy) |
| 17 |
- Markus Teufelberger (@MarkusTeufelberger) |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.crypto._attributes |
| 20 |
- community.crypto._attributes.info_module |
| 21 |
- community.crypto._attributes.idempotent_not_modify_state |
| 22 |
- community.crypto._cryptography_dep.minimum |
| 23 |
options: |
| 24 |
path: |
| 25 |
description: |
| 26 |
- The signed file to verify. |
| 27 |
- This file will only be read and not modified. |
| 28 |
type: path |
| 29 |
required: true |
| 30 |
certificate_path: |
| 31 |
description: |
| 32 |
- The path to the certificate used to verify the signature. |
| 33 |
- Either O(certificate_path) or O(certificate_content) must be specified, but not both. |
| 34 |
type: path |
| 35 |
certificate_content: |
| 36 |
description: |
| 37 |
- The content of the certificate used to verify the signature. |
| 38 |
- Either O(certificate_path) or O(certificate_content) must be specified, but not both. |
| 39 |
type: str |
| 40 |
signature: |
| 41 |
description: Base64 encoded signature. |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
select_crypto_backend: |
| 45 |
description: |
| 46 |
- Determines which crypto backend to use. |
| 47 |
- The default choice is V(auto), which tries to use C(cryptography) if available. |
| 48 |
- If set to V(cryptography), will try to use the L(cryptography,https://cryptography.io/) library. |
| 49 |
- Note that with community.crypto 3.0.0, all values behave the same. |
| 50 |
This option will be deprecated in a later version. |
| 51 |
We recommend to not set it explicitly. |
| 52 |
type: str |
| 53 |
default: auto |
| 54 |
choices: [auto, cryptography] |
| 55 |
seealso: |
| 56 |
- module: community.crypto.openssl_signature |
| 57 |
- module: community.crypto.x509_certificate |
| 58 |
""" |
| 59 |
|
| 60 |
EXAMPLES = r""" |
| 61 |
--- |
| 62 |
- name: Sign example file |
| 63 |
community.crypto.openssl_signature: |
| 64 |
privatekey_path: private.key |
| 65 |
path: /tmp/example_file |
| 66 |
register: sig |
| 67 |
|
| 68 |
- name: Verify signature of example file |
| 69 |
community.crypto.openssl_signature_info: |
| 70 |
certificate_path: cert.pem |
| 71 |
path: /tmp/example_file |
| 72 |
signature: "{{ sig.signature }}" |
| 73 |
register: verify |
| 74 |
|
| 75 |
- name: Make sure the signature is valid |
| 76 |
ansible.builtin.assert: |
| 77 |
that: |
| 78 |
- verify.valid |
| 79 |
""" |
| 80 |
|
| 81 |
RETURN = r""" |
| 82 |
valid: |
| 83 |
description: V(true) means the signature was valid for the given file, V(false) means it was not. |
| 84 |
returned: success |
| 85 |
type: bool |
| 86 |
""" |
| 87 |
|
| 88 |
import base64 |
| 89 |
import os |
| 90 |
import typing as t |
| 91 |
|
| 92 |
from ansible_collections.community.crypto.plugins.module_utils._cryptography_dep import ( |
| 93 |
COLLECTION_MINIMUM_CRYPTOGRAPHY_VERSION, |
| 94 |
assert_required_cryptography_version, |
| 95 |
) |
| 96 |
from ansible_collections.community.crypto.plugins.module_utils._version import ( |
| 97 |
LooseVersion, |
| 98 |
) |
| 99 |
|
| 100 |
MINIMAL_CRYPTOGRAPHY_VERSION = COLLECTION_MINIMUM_CRYPTOGRAPHY_VERSION |
| 101 |
|
| 102 |
try: |
| 103 |
import cryptography |
| 104 |
import cryptography.hazmat.primitives.asymmetric.padding |
| 105 |
import cryptography.hazmat.primitives.hashes |
| 106 |
|
| 107 |
except ImportError: |
| 108 |
CRYPTOGRAPHY_VERSION = LooseVersion("0.0") # pylint: disable=invalid-name |
| 109 |
else: |
| 110 |
# pylint: disable-next=invalid-name |
| 111 |
CRYPTOGRAPHY_VERSION = LooseVersion(cryptography.__version__) |
| 112 |
|
| 113 |
from ansible.module_utils.basic import AnsibleModule |
| 114 |
|
| 115 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.basic import ( |
| 116 |
OpenSSLObjectError, |
| 117 |
) |
| 118 |
from ansible_collections.community.crypto.plugins.module_utils._crypto.support import ( |
| 119 |
OpenSSLObject, |
| 120 |
load_certificate, |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
class SignatureInfoBase(OpenSSLObject): |
| 125 |
def __init__(self, module: AnsibleModule) -> None: |
| 126 |
super().__init__( |
| 127 |
path=module.params["path"], |
| 128 |
state="present", |
| 129 |
force=False, |
| 130 |
check_mode=module.check_mode, |
| 131 |
) |
| 132 |
|
| 133 |
self.module = module |
| 134 |
self.signature: str = module.params["signature"] |
| 135 |
self.certificate_path: str | None = module.params["certificate_path"] |
| 136 |
certificate_content: str | None = module.params["certificate_content"] |
| 137 |
if certificate_content is not None: |
| 138 |
self.certificate_content: bytes | None = certificate_content.encode("utf-8") |
| 139 |
else: |
| 140 |
self.certificate_content = None |
| 141 |
|
| 142 |
def generate(self, module: AnsibleModule) -> None: |
| 143 |
# Empty method because OpenSSLObject wants this |
| 144 |
pass |
| 145 |
|
| 146 |
def dump(self) -> dict[str, t.Any]: |
| 147 |
# Empty method because OpenSSLObject wants this |
| 148 |
return {} |
| 149 |
|
| 150 |
|
| 151 |
# Implementation with using cryptography |
| 152 |
class SignatureInfoCryptography(SignatureInfoBase): |
| 153 |
def __init__(self, module: AnsibleModule) -> None: |
| 154 |
super().__init__(module) |
| 155 |
|
| 156 |
def run(self) -> dict[str, t.Any]: |
| 157 |
_padding = cryptography.hazmat.primitives.asymmetric.padding.PKCS1v15() |
| 158 |
_hash = cryptography.hazmat.primitives.hashes.SHA256() |
| 159 |
|
| 160 |
result: dict[str, t.Any] = {} |
| 161 |
|
| 162 |
try: |
| 163 |
with open(self.path, "rb") as f: |
| 164 |
_in = f.read() |
| 165 |
|
| 166 |
_signature = base64.b64decode(self.signature) |
| 167 |
certificate = load_certificate( |
| 168 |
path=self.certificate_path, |
| 169 |
content=self.certificate_content, |
| 170 |
) |
| 171 |
public_key = certificate.public_key() |
| 172 |
verified = False |
| 173 |
valid = False |
| 174 |
|
| 175 |
try: |
| 176 |
if isinstance( |
| 177 |
public_key, |
| 178 |
cryptography.hazmat.primitives.asymmetric.dsa.DSAPublicKey, |
| 179 |
): |
| 180 |
public_key.verify(_signature, _in, _hash) |
| 181 |
verified = True |
| 182 |
valid = True |
| 183 |
|
| 184 |
elif isinstance( |
| 185 |
public_key, |
| 186 |
cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey, |
| 187 |
): |
| 188 |
public_key.verify( |
| 189 |
_signature, |
| 190 |
_in, |
| 191 |
cryptography.hazmat.primitives.asymmetric.ec.ECDSA(_hash), |
| 192 |
) |
| 193 |
verified = True |
| 194 |
valid = True |
| 195 |
|
| 196 |
elif isinstance( |
| 197 |
public_key, |
| 198 |
( |
| 199 |
cryptography.hazmat.primitives.asymmetric.ed25519.Ed25519PublicKey, |
| 200 |
cryptography.hazmat.primitives.asymmetric.ed448.Ed448PublicKey, |
| 201 |
), |
| 202 |
): |
| 203 |
public_key.verify(_signature, _in) |
| 204 |
verified = True |
| 205 |
valid = True |
| 206 |
|
| 207 |
elif isinstance( |
| 208 |
public_key, |
| 209 |
cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey, |
| 210 |
): |
| 211 |
public_key.verify(_signature, _in, _padding, _hash) |
| 212 |
verified = True |
| 213 |
valid = True |
| 214 |
except cryptography.exceptions.InvalidSignature: |
| 215 |
verified = True |
| 216 |
valid = False |
| 217 |
|
| 218 |
if not verified: |
| 219 |
self.module.fail_json( |
| 220 |
msg=f"Unsupported key type. Your cryptography version is {CRYPTOGRAPHY_VERSION}" |
| 221 |
) |
| 222 |
result["valid"] = valid |
| 223 |
return result |
| 224 |
|
| 225 |
except Exception as e: |
| 226 |
raise OpenSSLObjectError(e) from e |
| 227 |
|
| 228 |
|
| 229 |
def main() -> t.NoReturn: |
| 230 |
module = AnsibleModule( |
| 231 |
argument_spec={ |
| 232 |
"certificate_path": {"type": "path"}, |
| 233 |
"certificate_content": {"type": "str"}, |
| 234 |
"path": {"type": "path", "required": True}, |
| 235 |
"signature": {"type": "str", "required": True}, |
| 236 |
"select_crypto_backend": { |
| 237 |
"type": "str", |
| 238 |
"choices": ["auto", "cryptography"], |
| 239 |
"default": "auto", |
| 240 |
}, |
| 241 |
}, |
| 242 |
mutually_exclusive=(["certificate_path", "certificate_content"],), |
| 243 |
required_one_of=(["certificate_path", "certificate_content"],), |
| 244 |
supports_check_mode=True, |
| 245 |
) |
| 246 |
|
| 247 |
if not os.path.isfile(module.params["path"]): |
| 248 |
module.fail_json( |
| 249 |
name=module.params["path"], |
| 250 |
msg=f"The file {module.params['path']} does not exist", |
| 251 |
) |
| 252 |
|
| 253 |
assert_required_cryptography_version( |
| 254 |
module, minimum_cryptography_version=MINIMAL_CRYPTOGRAPHY_VERSION |
| 255 |
) |
| 256 |
|
| 257 |
try: |
| 258 |
_sign = SignatureInfoCryptography(module) |
| 259 |
|
| 260 |
result = _sign.run() |
| 261 |
|
| 262 |
module.exit_json(**result) |
| 263 |
except OpenSSLObjectError as exc: |
| 264 |
module.fail_json(msg=str(exc)) |
| 265 |
|
| 266 |
|
| 267 |
if __name__ == "__main__": |
| 268 |
main() |