HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/x509.cpython-310.pyc
o

�N�g:��@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlmZddlmZzddlZdZWneyidZYnwzddlZdZWney}dZYnwdZe�e �Z!egd��Z"d	d
ddd
�Z#dd�Z$Gdd�dej%�Z&dgdd�Z'dhdd�Z(dd�Z)dd�Z*dd�Z+dd�Z,dd �Z-d!d"�Z.d#d$�Z/d%d&�Z0d'd(�Z1d)d*�Z2dgd+d,�Z3d-d.�Z4d/d0�Z5d1d2�Z6d3d4�Z7d5d6�Z8dgd7d8�Z9d9d:�Z:dgd;d<�Z;d=d>�Z<d?d@�Z=dAdB�Z>dCdD�Z?dEdF�Z@didGdH�ZAdgdIdJ�ZBdjdKdL�ZC			M		N	dkdOdP�ZD								Q	RdldSdT�ZEdUdV�ZFdWdX�ZGdmdYdZ�ZHdid[d\�ZIdgd]d^�ZJ	dnd_d`�ZKdadb�ZLdcdd�ZMdedf�ZNdS)oaF
Manage X509 certificates

.. versionadded:: 2015.8.0

:depends: M2Crypto

.. deprecated:: 3006.0

.. warning::
    This module has been deprecated and will be removed
    in Salt 3009 (Potassium). Please migrate to the replacement
    modules. For breaking changes between both versions,
    you can refer to the :ref:`x509_v2 execution module docs <x509-setup>`.

    They will become the default ``x509`` modules in Salt 3008 (Argon).
    You can explicitly switch to the new modules before that release
    by setting ``features: {x509_v2: true}`` in your minion configuration.
�N)�STATE_INTERNAL_KEYWORDS)�OrderedDictTF�x509))ZbasicConstraintszX509v3 Basic Constraints)ZkeyUsagezX509v3 Key Usage)ZextendedKeyUsagezX509v3 Extended Key Usage)�subjectKeyIdentifierzX509v3 Subject Key Identifier)�authorityKeyIdentifierzX509v3 Authority Key Identifier)ZissuserAltNamezX509v3 Issuer Alternative Name)ZauthorityInfoAccesszX509v3 Authority Info Access)�subjectAltNamezX509v3 Subject Alternative Name)ZcrlDistributionPointszX509v3 CRL Distribution Points)ZissuingDistributionPointz!X509v3 Issuing Distribution Point)ZcertificatePolicieszX509v3 Certificate Policies)ZpolicyConstraintszX509v3 Policy Constraints)ZinhibitAnyPolicyzX509v3 Inhibit Any Policy)ZnameConstraintszX509v3 Name Constraints)ZnoCheckzX509v3 OCSP No Check)Z	nsCommentzNetscape Comment)Z
nsCertTypezNetscape Certificate Typeim��@�sha256)�
days_valid�version�serial_bits�	algorithmcCs2t�di��d�rdStrtjj�dd�tSdS)z8
    only load this module if m2crypto is available
    ZfeaturesZx509_v2)FzSuperseded, using x509_v2Z	Potassiumz�The x509 modules are deprecated. Please migrate to the replacement modules (x509_v2). They are the default from Salt 3008 (Argon) onwards.)Fz0Could not load x509 module, m2crypto unavailable)�__opts__�get�HAS_M2�salt�utilsZversionsZ
warn_until�__virtualname__�rr�E/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/x509.py�__virtual__Zs�rc@sLeZdZdZdejfdejfdejfdejfdejfdejfdejfgZd	S)
�_Ctx��
    This is part of an ugly hack to fix an ancient bug in M2Crypto
    https://bugzilla.osafoundation.org/show_bug.cgi?id=7530#c13
    �flags�issuer_cert�subject_cert�subject_req�crlZdb_methZdbN)�__name__�
__module__�__qualname__�__doc__�ctypes�c_int�c_void_p�_fields_rrrrrls�rcCsHt�t|��}d|_d|_d|_d|_|durd|_dSt|j�|_dS)rrN)	r�from_address�intrrrrrr)Zm2_ctx�issuer�ctxrrr�_fix_ctx}s
r+�c		Cs�|dkr|�d�dkrtj�d��tjj�|�}tjj�|�}ztj�	�}t
||�|dur2td��tj�d|||�}d}Wn"t
y`tj��}tj�|�}t
||�tj�||||�}Ynw|durotj�d�||���tj�||�}|�|�|S)z�
    Create new X509_Extension, this is required because M2Crypto
    doesn't support getting the publickeyidentifier from the issuer
    to create the authoritykeyidentifier extension.
    rz0123456789abcdefABCDEF:�zvalue must be precomputed hashNz4Not enough memory when creating a new X509 extensionz:Cannot create X509_Extension with name '{}' and value '{}')�stripr�
exceptions�SaltInvocationErrorr�stringutils�to_str�M2Crypto�m2Zx509v3_set_nconfr+�MemoryErrorZx509v3_ext_conf�AttributeErrorZx509v3_lhashZx509v3_set_conf_lhash�X509Z	X509Error�formatZX509_ExtensionZset_critical)	�name�value�criticalr)Z_pyfreer*Zx509_ext_ptrZlhash�x509_extrrr�_new_extension�s4



���
r=cCsdtjj�d�s
tj�d��d|��}td|�}t�dd|�}t�dd|�}tjj	�
tjj�|��S)	�}
    Parses openssl command line output, this is a workaround for M2Crypto's
    inability to get them from CSR objects.
    �openssl� openssl binary not found in pathzopenssl req -text -noout -in �cmd.run_stdoutz: rsaEncryption�:z[0-9a-f]{2}:r-)
rr�path�whichr/r0�__salt__�re�sub�data�decode�yaml�	safe_load)Zcsr_filename�cmd�outputrrr�_parse_openssl_req�s
rNcCs�t�}tjdd�}|�|���|��t|j�}|��|rHd|ddvrH|ddd}|s3|St	�
�D]\}}|rG||vrG||||<q7|S)z
    Returns a list of dicts containing the name, value and critical value of
    any extension contained in a csr object.
    T��deletezRequested ExtensionszCertificate RequestZData)r�tempfile�NamedTemporaryFile�write�as_pem�flushrNr9�close�EXT_NAME_MAPPINGS�items)�csr�retZcsrtempfileZcsryaml�csrextsZ
short_nameZ	long_namerrr�_get_csr_extensions�s
�r\cCs<tjj�d�s
tj�d��d|��}td|�}i}|�d�D]�}|��}|�	d�r2|�
dd�|d<|�	d	�r?|�
d	d�|d
<|�	d�ri|�
dd�}i}|�d�D]}d
|vrd|�d
�}|d||d<qQ||d<|�	d�r�|�
dd�|d<tj�|dd�}|�
d�|d<|�	d�r�|�
dd�|d<tj�|dd�}|�
d�|d<|�	d�r�nqd|vr�g|d<|S|�d�d}|�d�d}g}	|�d�D]M}
|
��s�q�|
�d�d��}|dd�|
�d�dd��}
tjj�tjj�|
��}|��D]}
d|
v�rtj�|
dd�}|�
d�|
d<q�|	�|�q�|	|d<|S)r>r?r@zopenssl crl -text -noout -in rA�
zVersion r-�VersionzSignature Algorithm: zSignature AlgorithmzIssuer: �/�=r,r�Issuerz
Last Update: zLast Updatez%b %d %H:%M:%S %Y %Z�%Y-%m-%d %H:%M:%Sz
Next Update: zNext UpdatezRevoked Certificates:zNo Revoked Certificates.zRevoked CertificateszSignature Algorithm:zSerial Number: z:
NzRevocation Date)rrrCrDr/r0rE�splitr.�
startswith�replace�datetime�strptime�strftime�joinrHrIrJrK�values�append)Zcrl_filenamerLrMr�line�subjectZ	sub_entryZlast_updateZnext_update�rev�revokedZrev_snZrev_yamlZ
rev_values�rev_daterrr�_parse_openssl_crl�sr




�
�
�
� 
��rqcCs8tddd�}|r|�|�}|r|Stddi��|�S)Nz
pillar.getZx509_signing_policiesz
config.get�rEr)r9Zpolicies�signing_policyrrr�_get_signing_policy)s
rtcs@t��ddkrd��d��fdd�tdt��d�D����S)z$
    Nicely formats hex strings
    �r�0rBcsg|]
}�||d��qS)rur)�.0�i��hex_strrr�
<listcomp>8sz_pretty_hex.<locals>.<listcomp>)�lenri�range�upperryrryr�_pretty_hex2s(rcCst|d��S)zA
    Converts decimal values to nicely formatted hex strings
    �X)r)Zdecvalrrr�_dec2hex;sr�cCs$ztj�|�WStyYdSw)z�
    A wrapper around os.path.isfile that ignores ValueError exceptions which
    can be raised if the input to isfile is too long.
    F)�osrC�isfile�
ValueError)rCrrr�_isfileBs�r�cCsZt|�r&tjj�|��}tjj�|���Wd�S1swYdStjj�|�S)za
    Determines if input is a path to a file, or a string with the
    content to be parsed.
    N)r�rr�files�fopenr1r2�read)Zinput_Zfp_rrr�
_text_or_fileNs
$�r�cCs�t�}g}g}|j��D]8\}}||vrqzt||�}|r*|�|||f�|�|�WqtyD}z
t�d||�WYd}~qd}~wwt|�D]	\}}}|||<qI|S)zA
    Returns a dict containing all values in an X509 Subject
    z!Missing attribute '%s'. Error: %sN)	r�nidrX�getattrrk�	TypeError�log�trace�sorted)rmrZZnidsZret_listZnid_nameZnid_num�val�errrrr�_parse_subjectZs&

���
r�cCs2t|tjj�r	|St|�}t|dd�}tj�|�S)z9
    Returns a certificate object based on PEM text.
    �CERTIFICATE��pem_type)�
isinstancer3r7r��
get_pem_entry�load_cert_string)�cert�textrrr�_get_certificate_objps
r�cCs@t|�}t|dd�}tjj|t|�d�}tj��}|�|�|S)z9
    Returns a private key object based on PEM text.
    �(?:RSA )?PRIVATE KEYr���callback)	r�r�r3�RSA�load_key_string�_passphrase_callback�EVP�PKey�
assign_rsa)�private_key�
passphraseZ
rsaprivkeyZ
evpprivkeyrrr�_get_private_key_obj{s�

r�cs�fdd�}|S)zR
    Returns a callback function used to supply a passphrase for private keys
    cstjj���S�N)rrr1�to_bytes)�args�r�rr�f�sz_passphrase_callback.<locals>.fr)r�r�rr�rr��sr�cCs t|�}t|dd�}tj�|�S)z1
    Returns a CSR object based on PEM text.
    �CERTIFICATE REQUESTr�)r�r�r3r7�load_request_string)rYr�rrr�_get_request_obj�sr�cCst�|�������}t|�S)zx
    Returns the sha1 hash of the modulus of a public key in a cert
    Used for generating subject key identifiers
    )�hashlib�sha1�
get_pubkey�get_modulus�	hexdigestr)r�Zsha_hashrrr�_get_pubkey_hash�sr�cCsdS)zz
    Replacement keygen callback function which silences the output
    sent to stdout by the default keygen function
    Nrrrrr�_keygen_callback�sr�cCst�d�|�tj�S)z8
    Dynamically generate a regex to match pem_type
    z�\s*(?P<pem_header>-----BEGIN {0}-----)\s+(?:(?P<proc_type>Proc-Type: 4,ENCRYPTED)\s*)?(?:(?P<dek_info>DEK-Info: (?:DES-[3A-Z\-]+,[0-9A-F]{{16}}|[0-9A-Z\-]+,[0-9A-F]{{32}}))\s*)?(?P<pem_body>.+?)\s+(?P<pem_footer>-----END {0}-----)\s*)rF�compiler8�DOTALLr�rrr�_make_regex�s
�r�cCs8|durdn|}t|�}|�|�D]}|r|SqdS)Nz
[0-9A-Z ]+)r��finditer)Zpemr�Z_dregex�_matchrrr�
_valid_pem�s�r�cCs4d|vrtd|d|d�}|�|d�Std||�S)N�@�publish.publishzmatch.compound)�tgt�fun�argFz
match.globrr)�testZminion�matchrrr�_match_minions�sr�cCs�t|�}|�dd�}t|���dkrz|�d�rz|�d�rzg}|}|ru|�d�rC|�|d|�dd�d��||�dd�dd�}n0|dd��d�d	kr^|�|dd��|dd�}n|�|d|�d���||�d�d�}|s"d�	|�}d
|��}|r�d�
||�}t||�}|s�tj
�|��|��}|d}|d
}|d}	|d}
|d}d�	|���}|d}|r�||d7}|	r�||	dd7}td	t|�d�D]}
|||
|
d�d7}q�||
d7}tjjj|dd�S)a'
    Returns a properly formatted PEM string from the input text fixing
    any whitespace or line-break issues

    text:
        Text containing the X509 PEM entry to be returned or path to
        a file containing the text.

    pem_type:
        If specified, this function will only return a pem of a certain type,
        for example 'CERTIFICATE' or 'CERTIFICATE REQUEST'.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.get_pem_entry "-----BEGIN CERTIFICATE REQUEST-----MIICyzCC Ar8CAQI...-----END CERTIFICATE REQUEST"
    z\nr]r,z-----N�r	�-rzPEM text not valid:
z2PEM does not contain a single entry of type {}:
{}�
pem_header�	proc_type�dek_info�
pem_footer�pem_bodyr-�ascii��encoding)r�rer|�
splitlinesrd�endswithrk�index�countrir8r�rr/r0�	groupdictrcr}rr1r�)r�r�Z	pem_fixedZpem_temp�errmsgr�Z_match_dictr�r�r�r�r�rZrxrrrr��sX��
�

�
r�c	C�Ji}t�|�D]}tj�|�r"z	t|d�||<Wqty!Yqwq|S)z�
    Returns a dict containing PEM entries in files matching a glob

    glob_path:
        A path to certificates to be read and returned.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.get_pem_entries "/etc/pki/*.crt"
    )r�)�globr�rCr�r�r��Z	glob_pathrZrCrrr�get_pem_entries�
��r�c
Cst|�}|��d|����dt|���t|jdd��t|jdd��t|�	��t|�	��
��t|���t|���
��|���
��d�|���
��d�t|�d�}tdd	urat|jd
d��|d<t�}td|���D]}|�|�}|��}|��}|��r�d
|}|||<qk|r�||d<|S)av
    Returns a dict containing details of a certificate. Input can be a PEM
    string or file path.

    certificate:
        The certificate to be read. Can be a path to a certificate file, or
        a string containing the PEM formatted text of the certificate.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.read_certificate /etc/pki/mycert.crt
    r,�r
)Zmdr�rb)r^zKey Size�
Serial NumberzSHA-256 Finger PrintzSHA1 Finger Print�Subject�Subject HashrazIssuer Hashz
Not Before�	Not Afterz
Public KeyZ	fips_modeF�md5zMD5 Finger Printr�	critical �X509v3 Extensions)r��get_versionr��sizer�Zget_serial_numberrZget_fingerprintr��get_subject�as_hashZ
get_issuerZget_not_before�get_datetimerh�
get_not_after�get_public_keyrrr}Z
get_ext_countZ
get_ext_at�get_name�	get_valueZget_critical)�certificater�rZZextsZ	ext_index�extr9r�rrr�read_certificate6s:



�

r�c	Cr�)z�
    Returns a dict containing details of all certificates matching a glob

    glob_path:
        A path to certificates to be read and returned.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.read_certificates "/etc/pki/*.crt"
    )r�)r�r�rCr�r�r�r�rrr�read_certificateslr�r�cCsTt|�}|��dt|���t|�����t�|���	���
�d�}t|�|d<|S)a
    Returns a dict containing details of a certificate request.

    :depends:   - OpenSSL command line tool

    csr:
        A path or PEM encoded string containing the CSR to read.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.read_csr /etc/pki/mycert.csr
    r,)r^r�r�zPublic Key Hashr�)r�r�r�r�r�r�r�r�r�r�r�r\)rYrZrrr�read_csr�s

�r�cCsVt|�}t|dd�}tjdd�}|�tjjj|dd��|�	�t
|j�}|��|S)aP
    Returns a dict containing details of a certificate revocation list.
    Input can be a PEM string or file path.

    :depends:   - OpenSSL command line tool

    crl:
        A path or PEM encoded string containing the CRL to read.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.read_crl /etc/pki/mycrl.crl
    �X509 CRLr�TrOr�r�)
r�r�rQrRrSrrr1r�rUrqr9rV)rr��crltempfileZ	crlparsedrrr�read_crl�s
r�c	Cst|tjj�r|����}d}nt|�}t|�}|�d�r1|s!|Stj�	�}|�
|�tj�|�}tj�	�}|�d�rGtj�
|�}|����}|�d�rXtj�|�}|����}|�d�sb|�d�rltjj|t|�d�}|rztj��}|�|�|S|�|�|��S)a7
    Returns a string containing the public key in PEM format.

    key:
        A path or PEM encoded string containing a CSR, Certificate or
        Private Key from which a public key can be retrieved.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.get_public_key /etc/pki/mycert.cer
    �s-----BEGIN PUBLIC KEY-----s-----BEGIN CERTIFICATE-----s#-----BEGIN CERTIFICATE REQUEST-----s-----BEGIN PRIVATE KEY-----s-----BEGIN RSA PRIVATE KEY-----r�)r�r3r7r�Zget_rsar�r�rd�BIO�MemoryBufferrSr�Zload_pub_key_bior�r�r�r�r�r�r�Zsave_pub_key_bio�read_all)	�keyr��asObj�rsar��bior�rYZ	evppubkeyrrrr��s<





��


r�cCst||���dS)z�
    Returns the bit length of a private key in PEM format.

    private_key:
        A path or PEM encoded string containing a private key.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.get_private_key_size /etc/pki/mycert.key
    r�)r�r�)r�r�rrr�get_private_key_size�s
r�c	Cs�t||d�}tjj�d���d}d}|rv|dkrvtj�|�rv|svt|�}zt|d�}Wn!tj	j
yL}zt�d|�tj
||d�WYd}~nd}~wwzt|d	�}Wn!tj	j
yu}zt�d
|�tj
||d�WYd}~nd}~wwtjj�|d��6}|r�|dkr�|r�|�tjj�|��|�tjj�|��|r�|dkr�|r�|�tjj�|��Wd�n1s�wYWd�n1s�wYd|��S)
a�
    Writes out a PEM string fixing any formatting or whitespace
    issues before writing.

    text:
        PEM string input to be written out.

    path:
        Path of the file to write the PEM out to.

    overwrite:
        If ``True`` (default), write_pem will overwrite the entire PEM file.
        Set False to preserve existing private keys and dh params that may
        exist in the PEM file.

    pem_type:
        The PEM type to be saved, for example ``CERTIFICATE`` or
        ``PUBLIC KEY``. Adding this will allow the function to take
        input that may contain multiple PEM types.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.write_pem "-----BEGIN CERTIFICATE-----MIIGMzCCBBugA..." path=/etc/pki/mycert.crt
    r��?r-r�z
DH PARAMETERSz$Error when getting DH PARAMETERS: %s)�exc_infoNr�z"Error when getting PRIVATE KEY: %s�wzPEM written to )r�rrr�Z	set_umaskr�rCr�r�r/r0r��debugr�r�rSr1r2)	r�rC�	overwriter�Z	_dhparamsZ_private_keyZ
_filecontentsr��_fprrr�	write_pemsJ�
����������
r��aes_128_cbcc	Cs�|s
|s
tj�d��|r|rtj�d��|rtjj}nt}tj�|tjj	|�}tj
��}|dur2d}|j||t
|�d�|rGt|��|dd�Stjj�|���S)a�
    Creates a private key in PEM format.

    path:
        The path to write the file to, either ``path`` or ``text``
        are required.

    text:
        If ``True``, return the PEM text without writing to a file.
        Default ``False``.

    bits:
        Length of the private key in bits. Default 2048

    passphrase:
        Passphrase for encrypting the private key

    cipher:
        Cipher for encrypting the private key. Has no effect if passphrase is None.

    verbose:
        Provide visual feedback on stdout. Default True

        .. versionadded:: 2016.11.0

    CLI Example:

    .. code-block:: bash

        salt '*' x509.create_private_key path=/etc/pki/mykey.key
    �&Either path or text must be specified.�0Either path or text must be specified, not both.N)�cipherr�r��r�rCr�)rr/r0r3r�Zkeygen_callbackr�Zgen_keyr4ZRSA_F4r�r�Zsave_key_bior�rr�rr1r2)	rCr��bitsr�r�verboseZ_callback_funcr�r�rrr�create_private_key>s*'��


�r�dr-c		Cs:tstj�d��tj��}	|durg}|D]�}
d|
vr-t|
d�}|d|
d<|d|
d<|
d�dd	�}tj	j
�|�}d|
vrS|sStj�
|
dd
�}
tj��|
krSqd|
vratj���d
�|
d<tj�
|
dd
�}|�d�}tj	j
�|�}tj��}|�tj	j
�|��|�tj	j
�|��d
|
vr�tj	j
�|
d
�}|�|�|	�|�qt|�}tj�tjjt|dd��}t||d�jdd�}tj�tjjt|��}||tjj|d�}|r�tj	j
�|�|d<nt�d�z
|	jdi|��}Wntt f�yt�d�|�!dd�|	jdi|��}Ynw|�r|St"||dd�S)aD

    Create a CRL

    :depends:   - PyOpenSSL Python module

    path:
        Path to write the CRL to.

    text:
        If ``True``, return the PEM text without writing to a file.
        Default ``False``.

    signing_private_key:
        A path or string of the private key in PEM format that will be used
        to sign the CRL. This is required.

    signing_private_key_passphrase:
        Passphrase to decrypt the private key.

    signing_cert:
        A certificate matching the private key that will be used to sign
        the CRL. This is required.

    revoked:
        A list of dicts containing all the certificates to revoke. Each dict
        represents one certificate. A dict must contain either the key
        ``serial_number`` with the value of the serial number to revoke, or
        ``certificate`` with either the PEM encoded text of the certificate,
        or a path to the certificate to revoke.

        The dict can optionally contain the ``revocation_date`` key. If this
        key is omitted the revocation date will be set to now. If should be a
        string in the format "%Y-%m-%d %H:%M:%S".

        The dict can also optionally contain the ``not_after`` key. This is
        redundant if the ``certificate`` key is included. If the
        ``Certificate`` key is not included, this can be used for the logic
        behind the ``include_expired`` parameter. If should be a string in
        the format "%Y-%m-%d %H:%M:%S".

        The dict can also optionally contain the ``reason`` key. This is the
        reason code for the revocation. Available choices are ``unspecified``,
        ``keyCompromise``, ``CACompromise``, ``affiliationChanged``,
        ``superseded``, ``cessationOfOperation`` and ``certificateHold``.

    include_expired:
        Include expired certificates in the CRL. Default is ``False``.

    days_valid:
        The number of days that the CRL should be valid. This sets the Next
        Update field in the CRL.

    digest:
        The digest to use for signing the CRL.
        This has no effect on versions of pyOpenSSL less than 0.14

    .. note

        At this time the pyOpenSSL library does not allow choosing a signing
        algorithm for CRLs See https://github.com/pyca/pyopenssl/issues/159

    CLI Example:

    .. code-block:: bash

        salt '*' x509.create_crl path=/etc/pki/mykey.key \
                signing_private_key=/etc/pki/ca.key \
                signing_cert=/etc/pki/ca.crt \
                revoked="{'compromized-web-key': {'certificate': '/etc/pki/certs/www1.crt', 'revocation_date': '2015-03-01 00:00:00'}}"
    z2Could not load OpenSSL module, OpenSSL unavailableNr�r��
serial_numberr��	not_afterrBr-rbZrevocation_datez
%Y%m%d%H%M%SZ�reasonr�r�r�)r)r�r��type�days�digestz9No digest specified. The default md5 digest will be used.ztError signing crl with specified digest. Are you using pyopenssl 0.15 or newer? The default md5 digest will be used.r�rr)#�HAS_OPENSSLrr/r0�OpenSSLZcryptoZCRLr�rerr1r�rfrg�nowrhZRevokedZ
set_serialZset_rev_dateZ
set_reasonZadd_revokedr�Zload_certificateZFILETYPE_PEMr�r�rTZload_privatekeyr��warningZexportr�r��popr)rCr��signing_private_key�signing_private_key_passphrase�signing_certroZinclude_expiredrrrZrev_itemZrev_certr
rrprnrr�r�Z
export_kwargs�crltextrrr�
create_crl�s�V�
�
��


�����
��rc
Ks�d|vrdSt|t�st�|�}i}d|vr8t|d�}|s%d�|d�St|t�r8i}|D]}|�|�q.|}d|vrUd|vrBdSt|d|d�sUd�|d|d�Szt	ddd	d
�|��WSt
yv}z
t|�WYd}~Sd}~ww)a�
    Request a certificate to be remotely signed according to a signing policy.

    argdic:
        A dict containing all the arguments to be passed into the
        create_certificate function. This will become kwargs when passed
        to create_certificate.

    kwargs:
        kwargs delivered from publish.publish

    CLI Example:

    .. code-block:: bash

        salt '*' x509.sign_remote_certificate argdic="{'public_key': '/etc/pki/www.key', 'signing_policy': 'www'}" __pub_id='www1'
    rsz signing_policy must be specifiedz!Signing policy {} does not exist.ZminionsZ__pub_idz3minion sending this request could not be identifiedz){} not permitted to use signing policy {}NT)rCr�r)r��dict�astZliteral_evalrtr8�list�updater��create_certificate�	Exception�str)Zargdic�kwargsrs�dict_�itemZexcept_rrr�sign_remote_certificate-s6


���r'cCs�t|�}|sd|�d�St|t�ri}|D]}|�|�q|}z|d=Wn	ty-Ynwzt|dd�|d<W|StyDY|Sw)z�
    Returns the details of a names signing policy, including the text of
    the public key that will be used to sign it. Does not return the
    private key.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.get_signing_policy www
    zSigning policy z does not exist.rrr�)rtr�rr �KeyErrorr�)Zsigning_policy_namersr%r&rrr�get_signing_policy_s,

�
���r)c$	Ks|s|sd|vs|ddurtj�d��|r|rtj�d��d|vr&d|d<|r�d|vr5tj�d�|���d	|vrLtjj�t|d	d
d���dd
�|d	<d|vretjj�t	|d|dd���dd
�|d<t
t�gd�D]}|�|d�qmt
d|dtjjj|dd�dd�}t|�s�tj�d��||}t|t�r�t|d�s�tj�|��|r�t|||dd�S|Si}d|vr�t|d�}t|t
�r�i}	|D]}
|	�|
�q�|	}|�|�t��D]\}}||vr�|||<q�tj��}
|
�|dd�d|v�rtt�|d��|d<t|d�dd
�d�}tjj �!��r(d }||k�r'|t||�|8}n|t"j#k�r:|t|t"j#�t"j#8}|
�$|�d!}d"|v�rxzt%j%�&|d"|�}Wntj�d#�|d"|���|jtj'j(d$�}tj'�)�}|�*|�|
�+|�d%|v�r�zt%j%�&|d%|�}Wntj�d&�|d%|���|jtj'j(d$�}tj'�)�}|�*|�|
�,|�tj-�.|
j/�}tj-�0|
j/�}d"|v�r�tj-�1|d'�d%|v�r�d(|d)}tj-�1||�d|v�r�d	|v�r�|d*|d<d+|v�r�|d+|d<i}d	|v�r|d	|d<t2|d	�}|
�3|�4��t5|d	�d,}|
�6t	|d|ddd-��|
�4�}t7|j8�D]}||v�r>t9||||��q/d.|v�rLt:|d.�}n|
}|
�;|�4��t<��D]�\}}||v�po||v�po||v�po||vdu�ru�qY|�=|��p�|�=|��p�|�=|��p�|�=|�}d}|�>d/��r�d}|d0d�}|d1k�r�d2|v�r�|�d2t?|
��}d} |d3k�r�|} |d4k�r�|�d5d6�}t@|||| d7�}!|!jA�s�tB�Cd8||��qY|
�D|!��qYd+|v�r�d|d+<tE|d*|d+|d9��stj�d:�|d*|�=d.d
����|
�FtG|d*|d+d�|d;�tH|
|d<��s tj�d=��d|v�r>|ddu�r>tI|
�}"t	|d*|d+d�|"d><|"Sd?|v�rpd@|v�rX|d@du�rXt|dA�dB}#nd
}#t|
�J�tKjL�M|d?|#|ddC�ddD�|�r}t|
�J�||dd�Stjj�|
�J��S)Ea�"
    Create an X509 certificate.

    path:
        Path to write the certificate to.

    text:
        If ``True``, return the PEM text without writing to a file.
        Default ``False``.

    overwrite:
        If ``True`` (default), create_certificate will overwrite the entire PEM
        file. Set False to preserve existing private keys and dh params that
        may exist in the PEM file.

    kwargs:
        Any of the properties below can be included as additional
        keyword arguments.

    ca_server:
        Request a remotely signed certificate from ca_server. For this to
        work, a ``signing_policy`` must be specified, and that same policy
        must be configured on the ca_server. See ``signing_policy`` for
        details. Also, the salt master must permit peers to call the
        ``sign_remote_certificate`` function.

        Example:

        /etc/salt/master.d/peer.conf

        .. code-block:: yaml

            peer:
              .*:
                - x509.sign_remote_certificate

    subject properties:
        Any of the values below can be included to set subject properties
        Any other subject properties supported by OpenSSL should also work.

        C:
            2 letter Country code
        CN:
            Certificate common name, typically the FQDN.

        Email:
            Email address

        GN:
            Given Name

        L:
            Locality

        O:
            Organization

        OU:
            Organization Unit

        SN:
            SurName

        ST:
            State or Province

    signing_private_key:
        A path or string of the private key in PEM format that will be used
        to sign this certificate. If neither ``signing_cert``, ``public_key``,
        or ``csr`` are included, it will be assumed that this is a self-signed
        certificate, and the public key matching ``signing_private_key`` will
        be used to create the certificate.

    signing_private_key_passphrase:
        Passphrase used to decrypt the signing_private_key.

    signing_cert:
        A certificate matching the private key that will be used to sign this
        certificate. This is used to populate the issuer values in the
        resulting certificate. Do not include this value for
        self-signed certificates.

    public_key:
        The public key to be included in this certificate. This can be sourced
        from a public key, certificate, CSR or private key. If a private key
        is used, the matching public key from the private key will be
        generated before any processing is done. This means you can request a
        certificate from a remote CA using a private key file as your
        public_key and only the public key will be sent across the network to
        the CA. If neither ``public_key`` or ``csr`` are specified, it will be
        assumed that this is a self-signed certificate, and the public key
        derived from ``signing_private_key`` will be used. Specify either
        ``public_key`` or ``csr``, not both. Because you can input a CSR as a
        public key or as a CSR, it is important to understand the difference.
        If you import a CSR as a public key, only the public key will be added
        to the certificate, subject or extension information in the CSR will
        be lost.

    public_key_passphrase:
        If the public key is supplied as a private key, this is the passphrase
        used to decrypt it.

    csr:
        A file or PEM string containing a certificate signing request. This
        will be used to supply the subject, extensions and public key of a
        certificate. Any subject or extensions specified explicitly will
        overwrite any in the CSR.

    basicConstraints:
        X509v3 Basic Constraints extension.

    extensions:
        The following arguments set X509v3 Extension values. If the value
        starts with ``critical``, the extension will be marked as critical.

        Some special extensions are ``subjectKeyIdentifier`` and
        ``authorityKeyIdentifier``.

        ``subjectKeyIdentifier`` can be an explicit value or it can be the
        special string ``hash``. ``hash`` will set the subjectKeyIdentifier
        equal to the SHA1 hash of the modulus of the public key in this
        certificate. Note that this is not the exact same hashing method used
        by OpenSSL when using the hash value.

        ``authorityKeyIdentifier`` Use values acceptable to the openssl CLI
        tools. This will automatically populate ``authorityKeyIdentifier``
        with the ``subjectKeyIdentifier`` of ``signing_cert``. If this is a
        self-signed cert these values will be the same.

        basicConstraints:
            X509v3 Basic Constraints

        keyUsage:
            X509v3 Key Usage

        extendedKeyUsage:
            X509v3 Extended Key Usage

        subjectKeyIdentifier:
            X509v3 Subject Key Identifier

        issuerAltName:
            X509v3 Issuer Alternative Name

        subjectAltName:
            X509v3 Subject Alternative Name

        crlDistributionPoints:
            X509v3 CRL Distribution Points

        issuingDistributionPoint:
            X509v3 Issuing Distribution Point

        certificatePolicies:
            X509v3 Certificate Policies

        policyConstraints:
            X509v3 Policy Constraints

        inhibitAnyPolicy:
            X509v3 Inhibit Any Policy

        nameConstraints:
            X509v3 Name Constraints

        noCheck:
            X509v3 OCSP No Check

        nsComment:
            Netscape Comment

        nsCertType:
            Netscape Certificate Type

    days_valid:
        The number of days this certificate should be valid. This sets the
        ``notAfter`` property of the certificate. Defaults to 365.

    version:
        The version of the X509 certificate. Defaults to 3. This is
        automatically converted to the version value, so ``version=3``
        sets the certificate version field to 0x2.

    serial_number:
        The serial number to assign to this certificate. If omitted a random
        serial number of size ``serial_bits`` is generated.

    serial_bits:
        The number of bits to use when randomly generating a serial number.
        Defaults to 64.

    algorithm:
        The hashing algorithm to be used for signing this certificate.
        Defaults to sha256.

    copypath:
        An additional path to copy the resulting certificate to. Can be used
        to maintain a copy of all certificates issued for revocation purposes.

    prepend_cn:
        If set to True, the CN and a dash will be prepended to the copypath's filename.

        Example:
            /etc/pki/issued_certs/www.example.com-DE:CA:FB:AD:00:00:00:00.crt

    signing_policy:
        A signing policy that should be used to create this certificate.
        Signing policies should be defined in the minion configuration, or in
        a minion pillar. It should be a YAML formatted list of arguments
        which will override any arguments passed to this function. If the
        ``minions`` key is included in the signing policy, only minions
        matching that pattern (see match.glob and match.compound) will be
        permitted to remotely request certificates from that policy.
        In order to ``match.compound`` to work salt master must peers permit
        peers to call it.

        Example:

        /etc/salt/master.d/peer.conf

        .. code-block:: yaml

            peer:
              .*:
                - match.compound


        Example:

        .. code-block:: yaml

            x509_signing_policies:
              www:
                - minions: 'www*'
                - signing_private_key: /etc/pki/ca.key
                - signing_cert: /etc/pki/ca.crt
                - C: US
                - ST: Utah
                - L: Salt Lake City
                - basicConstraints: "critical CA:false"
                - keyUsage: "critical cRLSign, keyCertSign"
                - subjectKeyIdentifier: hash
                - authorityKeyIdentifier: keyid,issuer:always
                - days_valid: 90
                - copypath: /etc/pki/issued_certs/

        The above signing policy can be invoked with ``signing_policy=www``

    not_before:
        Initial validity date for the certificate. This date must be specified
        in the format '%Y-%m-%d %H:%M:%S'.

        .. versionadded:: 3001

    not_after:
        Final validity date for the certificate. This date must be specified in
        the format '%Y-%m-%d %H:%M:%S'.

        .. versionadded:: 3001

    CLI Example:

    .. code-block:: bash

        salt '*' x509.create_certificate path=/etc/pki/myca.crt signing_private_key='/etc/pki/myca.key' csr='/etc/pki/myca.csr'}
    ZtestrunFrr�public_key_passphraseNrszSsigning_policy must be specifiedif requesting remote certificate from ca_server {}.rYr�r�r]r-�
public_keyr�)Z	listen_inZprerequiredZ__prerequired__r�zx509.sign_remote_certificateT)r2�)r�r�r��timeoutzeca_server did not respond salt master must permit peers to call the sign_remote_certificate function.r�)r�rrCr�rr,r
r
rB�i���rb�
not_beforez+not_before: {} is not in required format {})�tzinforz*not_after: {} is not in required format {}ri�Qrrrr��r�r�rr��	r�hashrr�
IP Address�IP�r9r:r;r)� Invalid X509v3 Extension. %s: %s)r�r�r+z6signing_private_key: {} does no match signing_cert: {}r)�signing_pub_keyz&failed to verify certificate signaturezIssuer Public KeyZcopypathZ
prepend_cn�CNr�z.crtr)Nrr/r0r8rr1r2r�rer�r�_STATE_INTERNAL_KEYWORDSrrErHZdecode_dict�anyr�r#r�rrtr �
CERT_DEFAULTSrXr3r7�set_versionr��random�getrandbitsr(�platformZ
is_windows�sys�maxsizeZset_serial_numberrfrgZASN1�UTCZASN1_UTCTIMEZset_datetimeZset_not_beforeZ
set_not_afterr4Zx509_get_not_beforerZx509_get_not_afterZx509_gmtime_adjr�Zset_subjectr�r��
set_pubkeyr�r��setattrr�Z
set_issuerrWrrdr�r=r<r��infoZadd_ext�verify_private_key�signr��verify_signaturer�rTr�rCri)$rCr�rZ	ca_serverr$�ignore�certsZcert_txtrsr%r&�prop�defaultr�r
ZINT_MAX�fmt�timeZasn1_not_beforeZasn1_not_afterr/rZ
valid_secondsr[rYrm�entryr�extname�extlongname�extvalr;r)r�Z
cert_propsZprependrrrr!�s�����������
��

�

�


�

��	



��






��
�

������

�
������
�
���r!cKsr|s
|s
tj�d��|r|rtj�d��tj��}|��}t��D]\}}||vr-|||<q!|�	|dd�d|vrJd|vrJ|d|d<t
�d�d|vrTtj�d��d|vr^|d|d<d	|vrfd
|d	<d|vrnd
|d<|dr||d	s||d|d	<|d	r�|ds�|d	|d<|�t
|d|ddd
��t|j�D]}||vr�t||||�q�tj��}t��D]^\}	}
|	|vr�|
|vr�q�||	p�||
}d}|�d�r�d}|dd
�}|	dkr�d|vr�|�dt|��}|	dkr�|�dd�}|	dkr�q�d
}
t|	|||
d�}|j�st
�d|	|�q�|�|�q�|�|�|�t|d|d	d�|d�|�r5t|��|dd�S|��S)ap
    Create a certificate signing request.

    path:
        Path to write the certificate to.

    text:
        If ``True``, return the PEM text without writing to a file.
        Default ``False``.

    algorithm:
        The hashing algorithm to be used for signing this request. Defaults to sha256.

    kwargs:
        The subject, extension and version arguments from
        :mod:`x509.create_certificate <salt.modules.x509.create_certificate>`
        can be used.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.create_csr path=/etc/pki/myca.csr public_key='/etc/pki/myca.key' CN='My Cert'
    rrrr,r�r+z�OpenSSL no longer allows working with non-signed CSRs. A private_key must be specified. Attempting to use public_key as private_keyzprivate_key is requiredZprivate_key_passphraseNr*Tr1Fr�r2rr3rr4r5rr6r7r�rr�r)rr/r0r3r7ZRequestr�r<rXr=r�rrDr�r�r�rEZX509_Extension_StackrWrdrer�r=r<rF�pushZadd_extensionsrHr�rrT)rCr�r$rYrmrLrMrPZextstackrQrRrSr;r)r�rrr�
create_csr�s���
�����

�
��rUcCstt||�t|�k�S)a 
    Verify that 'private_key' matches 'public_key'

    private_key:
        The private key to verify, can be a string or path to a private
        key in PEM format.

    public_key:
        The public key to verify, can be a string or path to a PEM formatted
        certificate, CSR, or another private key.

    passphrase:
        Passphrase to decrypt the private key.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.verify_private_key private_key=/etc/pki/myca.key \
                public_key=/etc/pki/myca.crt
    )�boolr�)r�r+r�rrrrG+srGcCs.t|�}|r
t||dd�}t|j|d�dk�S)a`
    Verify that ``certificate`` has been signed by ``signing_pub_key``

    certificate:
        The certificate to verify. Can be a path or string containing a
        PEM formatted certificate.

    signing_pub_key:
        The public key to verify, can be a string or path to a PEM formatted
        certificate, CSR, or private key.

    signing_pub_key_passphrase:
        Passphrase to the signing_pub_key if it is an encrypted private key.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.verify_signature /etc/pki/mycert.pem \
                signing_pub_key=/etc/pki/myca.crt
    Tr1)Zpkeyr,)r�r�rVZverify)r�r8Zsigning_pub_key_passphraser�rrrrIDs�rIcCs�tjj�d�s
tj�d��t|�}t|dd�}tj	dd�}|�
tjjj|dd��|�
�t|�}t|d	d�}tj	dd�}|�
tjjj|dd��|�
�d
�|j|j�}td|�}|��|��d|vS)
a�
    Validate a CRL against a certificate.
    Parses openssl command line output, this is a workaround for M2Crypto's
    inability to get them from CSR objects.

    crl:
        The CRL to verify

    cert:
        The certificate to verify the CRL against

    CLI Example:

    .. code-block:: bash

        salt '*' x509.verify_crl crl=/etc/pki/myca.crl cert=/etc/pki/myca.crt
    r?r@r�r�TrOr�r�r�z$openssl crl -noout -in {} -CAfile {}zcmd.run_stderrz	verify OK)rrrCrDr/r0r�r�rQrRrSr1r�rUr8r9rErV)rr�rr�ZcerttextZcerttempfilerLrMrrr�
verify_crlfs&�rWcCs�i}tj�|�rHz5||d<t|�}tj��}|����}t|�	��d|d<|�
d�|�
d�kr7d|d<W|Sd|d<W|StyGY|Sw|S)a�
    Returns a dict containing limited details of a
    certificate and whether the certificate has expired.

    .. versionadded:: 2016.11.0

    certificate:
        The certificate to be read. Can be a path to a certificate file,
        or a string containing the PEM formatted text of the certificate.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.expired "/etc/pki/mycert.crt"
    rCr9�cnrbT�expiredF)r�rCr�r�rf�utcnowr�r�r�r�rhr�)r�rZr�Z_now�_expiration_daterrrrY�s(
�

���rYcCs�i}tj�|�rRz?||d<||d<t|�}tj��tj|d�}|����}t	|�
��d|d<|�d�|�d�krAd|d<W|Sd	|d<W|StyQY|Sw|S)
a�
    Returns a dict containing details of a certificate and whether
    the certificate will expire in the specified number of days.
    Input can be a PEM string or file path.

    .. versionadded:: 2016.11.0

    certificate:
        The certificate to be read. Can be a path to a certificate file,
        or a string containing the PEM formatted text of the certificate.

    CLI Example:

    .. code-block:: bash

        salt '*' x509.will_expire "/etc/pki/mycert.crt" days=30
    rCZ
check_days)rr9rXrbT�will_expireF)
r�rCr�r�rfrZ�	timedeltar�r�r�r�rhr�)r�rrZr�Z_check_timer[rrrr\�s*�

���r\r�)rNr,)NF)TN)NFrNrT)	NFNNNNFrr-)NFTN)NN)Or"rr#rfr�r��loggingr�r>rFrArQZsalt.exceptionsrZsalt.utils.dataZsalt.utils.filesZsalt.utils.pathZsalt.utils.platformZsalt.utils.stringutilsZsalt.utils.versionsZ
salt.staterr:Zsalt.utils.odictrr3r�ImportErrorrrr�	getLoggerrr�rWr<r�	Structurerr+r=rNr\rqrtrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr'r)r!rUrGrIrWrYr\rrrr�<module>s���
��

*F		
		
	
O6 

4
:
�D
�-2
$
1
{
�",)