
    Vh+&                     j   d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlZd dlmZ dZdZ	 d dlZd dlZd dlZ eej&                        ZdZd d
lmZmZ d dlmZ d dlmZmZm Z m!Z!m"Z"m#Z# d dl$m%Z%m&Z&  G d de%      Z' G d de'      Z(d Z)e*dk(  r e)        yy# e$ r  ej.                         Zd	ZY gw xY w)    )absolute_importdivisionprint_functiona  
module: openssl_signature
version_added: 1.1.0
short_description: Sign data with openssl
description:
  - This module allows one to sign data using a private key.
  - The module uses the cryptography Python library.
requirements:
  - cryptography >= 1.4 (some key types require newer versions)
author:
  - Patrick Pichler (@aveexy)
  - Markus Teufelberger (@MarkusTeufelberger)
extends_documentation_fragment:
  - community.crypto.attributes
attributes:
  check_mode:
    support: full
    details:
      - This action does not modify state.
  diff_mode:
    support: none
  idempotent:
    support: partial
    details:
      - Signature algorithms are generally not deterministic. Thus the generated signature
        can change from one invocation to the next.
options:
  privatekey_path:
    description:
      - The path to the private key to use when signing.
      - Either O(privatekey_path) or O(privatekey_content) must be specified, but not both.
    type: path
  privatekey_content:
    description:
      - The content of the private key to use when signing the certificate signing request.
      - Either O(privatekey_path) or O(privatekey_content) must be specified, but not both.
    type: str
  privatekey_passphrase:
    description:
      - The passphrase for the private key.
      - This is required if the private key is password protected.
    type: str
  path:
    description:
      - The file to sign.
      - This file will only be read and not modified.
    type: path
    required: true
  select_crypto_backend:
    description:
      - Determines which crypto backend to use.
      - The default choice is V(auto), which tries to use C(cryptography) if available.
      - If set to V(cryptography), will try to use the L(cryptography,https://cryptography.io/) library.
    type: str
    default: auto
    choices: [auto, cryptography]
notes:
  - "When using the C(cryptography) backend, the following key types require at least the following C(cryptography) version:\n
    RSA keys: C(cryptography) >= 1.4\nDSA and ECDSA keys: C(cryptography) >= 1.5\ned448 and ed25519 keys: C(cryptography)
    >= 2.6."
seealso:
  - module: community.crypto.openssl_signature_info
  - module: community.crypto.openssl_privatekey
a  
---
- name: Sign example file
  community.crypto.openssl_signature:
    privatekey_path: private.key
    path: /tmp/example_file
  register: sig

- name: Verify signature of example file
  community.crypto.openssl_signature_info:
    certificate_path: cert.pem
    path: /tmp/example_file
    signature: "{{ sig.signature }}"
  register: verify

- name: Make sure the signature is valid
  ansible.builtin.assert:
    that:
      - verify.valid
zU
signature:
  description: Base64 encoded signature.
  returned: success
  type: str
N)LooseVersionz1.4TF)AnsibleModulemissing_required_lib)	to_native)CRYPTOGRAPHY_HAS_DSA_SIGNCRYPTOGRAPHY_HAS_EC_SIGNCRYPTOGRAPHY_HAS_ED448_SIGNCRYPTOGRAPHY_HAS_ED25519_SIGNCRYPTOGRAPHY_HAS_RSA_SIGNOpenSSLObjectError)OpenSSLObjectload_privatekeyc                   *     e Zd Z fdZd Zd Z xZS )SignatureBasec                 @   t         t        |   |j                  d   dd|j                         || _        |j                  d   | _        |j                  d   | _        | j                   | j                  j                  d      | _        |j                  d   | _	        y )	NpathpresentF)r   stateforce
check_modeprivatekey_pathprivatekey_contentzutf-8privatekey_passphrase)
superr   __init__paramsr   backendr   r   encoder   selfmoduler    	__class__s      v/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/crypto/plugins/modules/openssl_signature.pyr   zSignatureBase.__init__   s    mT+v&((	 	, 	
 %}}->?"(--0D"E"".&*&=&=&D&DW&MD#%+]]3J%K"    c                      y N r#   s    r&   generatezSignatureBase.generate       r'   c                      y r)   r*   r+   s    r&   dumpzSignatureBase.dump   r-   r'   )__name__
__module____qualname__r   r,   r/   __classcell__r%   s   @r&   r   r      s    L r'   r   c                   $     e Zd Z fdZd Z xZS )SignatureCryptographyc                 .    t         t        |   ||       y r)   )r   r6   r   r"   s      r&   r   zSignatureCryptography.__init__   s    #T3FGDr'   c                    t         j                  j                  j                  j                  j                         }t         j                  j                  j                  j                         }t               }	 t        | j                  d      5 }|j                         }d d d        t        | j                  | j                  | j                  | j                         }d }t"        rTt%        |t         j                  j                  j                  j&                  j(                        r|j+                  |      }t,        rt%        |t         j                  j                  j                  j.                  j0                        rM|j+                  t         j                  j                  j                  j.                  j3                  |            }t4        rSt%        |t         j                  j                  j                  j6                  j8                        r|j+                        }t:        rSt%        |t         j                  j                  j                  j<                  j>                        r|j+                        }t@        rUt%        |t         j                  j                  j                  jB                  jD                        r|j+                  ||      }|/| jF                  jI                  djK                  tL                     tO        jP                  |      |d<   |S # 1 sw Y   xY w# tR        $ r}tU        |      d }~ww xY w)Nrb)r   content
passphraser    z6Unsupported key type. Your cryptography version is {0}msg	signature)+cryptographyhazmat
primitives
asymmetricpaddingPKCS1v15hashesSHA256dictopenr   readr   r   r   r   r    r
   
isinstancedsaDSAPrivateKeysignr   ecEllipticCurvePrivateKeyECDSAr   ed25519Ed25519PrivateKeyr   ed448Ed448PrivateKeyr   rsaRSAPrivateKeyr$   	fail_jsonformatCRYPTOGRAPHY_VERSIONbase64	b64encode	Exceptionr   )	r#   _padding_hashresultf_inprivate_keyr>   es	            r&   runzSignatureCryptography.run   s   &&11<<DDMMO##..55<<>=	(dii& !ffh *))//55	K I( ''22==AAOO !, 0 0e <I' ''22==@@XX !, 0 0\00;;FFIIOOPUV!I - ''22==EEWW !, 0 0 5I* ''22==CCSS !, 0 0 5I( ''22==AAOO !, 0 0h FI %%PWW, &  #)"2"29"=F;Ms v  	($Q''	(s1   :L6 L)!JL6 )L3.L6 6	M?M

M)r0   r1   r2   r   rd   r3   r4   s   @r&   r6   r6      s    EC(r'   r6   c                     t        t        t        d      t        dd      t        dd      t        dd      t        dddgd	      
      ddgfddgfd      } t        j                  j	                  | j
                  d         s<| j                  | j
                  d   dj                  | j
                  d                | j
                  d   }|dk(  rLt        xr t        t        t              k\  }|rd}|dk(  r%| j                  dj                  t                     	 |dk(  rEt        s3| j                  t        dj                  t                    t               t        | |      }j                         } | j                   di | y # t"        $ r%}| j                  t%        |             Y d }~y d }~ww xY w)Nr   )typestrT)rf   no_log)rf   requiredautor?   )rf   choicesdefault)r   r   r   r   select_crypto_backendr   r   )argument_specmutually_exclusiverequired_one_ofsupports_check_modezThe file {0} does not exist)namer=   rm   z?Cannot detect the required Python library cryptography (>= {0})r<   zcryptography >= {0})r=   	exceptionr*   )r   rG   osr   isfiler   rW   rX   CRYPTOGRAPHY_FOUNDrY   r   MINIMAL_CRYPTOGRAPHY_VERSIONr   CRYPTOGRAPHY_IMP_ERRr6   rd   	exit_jsonr   r	   )r$   r    can_use_cryptography_signr_   excs         r&   mainr}      s    f-#t<"&E$"?6D1"&V^$<f#
 /0DEG+-ABD F 77>>&--/0v&-44V]]65JK 	 	

 mm34G&  S$5Q(RR 	  $G fX&56  
-n$%  ,-445QR 3	 !  *&':E"6" -Ys^,,-s   &A,F 	GF<<G__main__)+
__future__r   r   r   rf   __metaclass__DOCUMENTATIONEXAMPLESRETURNrZ   rt   	tracebackAansible_collections.community.crypto.plugins.module_utils.versionr   rw   rx   r?   1cryptography.hazmat.primitives.asymmetric.padding%cryptography.hazmat.primitives.hashes__version__rY   rv   ImportError
format_excansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr	   Fansible_collections.community.crypto.plugins.module_utils.crypto.basicr
   r   r   r   r   r   Hansible_collections.community.crypto.plugins.module_utils.crypto.supportr   r   r   r6   r}   r0   r*   r'   r&   <module>r      s    A @ ?B*
  	 
  %  
<0'(@(@A
  J A M 8H(M H(V8-v zF q  /9//1s   B B21B2