name : cryptopolicies.cpython-39.pyc
a

C�h�J�@s4ddlZddlZddlZddlZddlZddlZddlmZmZe	�
dd�ddiBZddd	�Zd
Z
dZdd
hhd�hd�ddhhd�hd�hd�hd�hd�hd�ddhd�ZGdd�d�ZGdd�dej�Zdd �Ze�d!d"�Zd#d$�Zd2d&d'�ZGd(d)�d)e�Zd*d+�ZGd,d-�d-�Zd.d/�ZGd0d1�d1�ZdS)3�N�)�	alg_lists�
validation)Zarbitrary_dh_groupsZmin_dh_sizeZmin_dsa_sizeZmin_rsa_sizeZ
sha1_in_certsZ	ssh_certsZmin_ec_size�)�ANYZDISABLE_ETMZDISABLE_NON_ETM)ZDEFAULTZENFORCEZRELAX)ZetmZ__ems�*)�tls�ssl�openssl�nss�gnutls�java-tls�ssh�openssh�openssh-server�openssh-client�libssh�ipsec�ike�	libreswan�kerberos�krb5�dnssec�bind�rpm�rpm-sequoiarr>r	rr>r	rr
rr>rrr>rrr>rr	r>rrr>rrr>r
r	rrr)rrr
rrrrrrr
rc@s(eZdZefdd�Zdd�Zdd�ZdS)�
ScopeSelectorcCs�|��|_}|�d�|_|jr&|n
|dd�}tjj||jd�tjj||jd�|�d�rr|dd��d�n|g|_	tjj
|j	t|jd�dS)a=
        Initialize a scope selector.
        An example would be `ssh` in `ciphers@ssh = -NULL`.
        When openssh backend will request the configuration,
        it'll offer (`{'ssh', 'openssh'}`) as scopes
        and the rule above will be taken into account.
        Both patterns and scopes are cast to lowercase.
        For more examples, refer to tests/unit/parsing/test_scope_selector.py
        >>> ss = ScopeSelector('!{SSH,IPsec}')
        >>> ss.matches({'ipsec', 'libreswan'})
        False
        >>> ss.matches({'tls', 'openssl'})
        True
        �!rN)Zoriginal_pattern�{����,)�lower�pattern�
startswith�	_positiver�scopeZillegal_charactersZcurly_brackets�split�_globsZresulting_globs�
ALL_SCOPES)�selfr"�p�r+�B/usr/share/crypto-policies/python/cryptopolicies/cryptopolicies.py�__init__As$�zScopeSelector.__init__cCsd|j�d�S)Nz<ScopeSelector pattern=�>)r"�r)r+r+r,�__str__\szScopeSelector.__str__csh|jtkrdSdd��D��tdd��D��s2J�|jrPt�fdd�|jD��St�fdd�|jD��S)aE
        Checks whether ScopeSelector matches one of the scopes.
        For more examples, refer to tests/unit/parsing/test_scope_selector.py
        >>> ScopeSelector('{SSH,IPsec}').matches({'ipsec', 'libreswan'})
        True
        >>> ScopeSelector('!{SSH,IPsec}').matches({'ipsec', 'libreswan'})
        False
        TcSsg|]}|���qSr+)r!��.0�sr+r+r,�
<listcomp>j�z)ScopeSelector.matches.<locals>.<listcomp>css|]}|tvVqdS�N)r(r1r+r+r,�	<genexpr>kr5z(ScopeSelector.matches.<locals>.<genexpr>c3s|]}t��|�VqdSr6��fnmatch�filter�r2�g��scopesr+r,r7mr5c3s|]}t��|�VqdSr6r8r;r=r+r,r7nr5)r"�	SCOPE_ANY�allr$�anyr'�r)r>r+r=r,�matches_s	
zScopeSelector.matchesN)�__name__�
__module__�__qualname__r?r-r0rCr+r+r+r,r@src@s0eZdZdZdZdZdZdZdZdZ	dd	�Z
d
S)�	OperationzCAn operation that comes with the right-hand value of the directive.r�����cCsd|j��S)Nz
Operation.)�namer/r+r+r,�__repr__}szOperation.__repr__N)rDrErF�__doc__�RESET�PREPEND�APPEND�OMIT�SET_INT�SET_ENUMrNr+r+r+r,rGssrGcs�dd��|��rx�tjvr2�tvr2tjt|�fgS�tjvsD�tvrPtj	�
����tjvs^J��tvsjJ��tvs�J�nH�tvr�tj	�����tvr�|t�vr�tj	��|t���tj
|fgS|��}t�fdd�|D���s�fdd�|D�}tjdfgdd�|D�St�fd	d�|D���r�g}|D]�}|�d
��rbtj�t�|dd���ddd�}n\|�d
��r�tj�t�|dd���ddd�}n*|�d
��s�J�tj�t�|dd���}|��fdd�|D���q,|Stj	�|��dS)ae
    Parses right-hand parts of the directives
    into lists of operation/value pairs.
    For more examples, refer to tests/unit/test_parsing.py
    >>> parse_rhs('', 'cipher')
    [(Operation.RESET, None)]
    >>> parse_rhs('IDEA-CBC SEED-CBC', 'cipher')
    [(Operation.RESET, None),
     (Operation.APPEND, 'IDEA-CBC'),
     (Operation.APPEND, 'SEED-CBC')]
    >>> # 3DES-CBC gets prepended last for higher prio
    >>> parse_rhs('+*DES-CBC', 'cipher')
    [(Operation.PREPEND, 'DES-CBC'),
     (Operation.PREPEND, '3DES-CBC')]
    >>> parse_rhs('ENFORCE', '__ems')
    [(Operation.SET_ENUM, 'ENFORCE')]
    cSs|�d�p|�d�S)N)�+�-rV)r#�endswith)�vr+r+r,�differential�szparse_rhs.<locals>.differentialc3s|]}�|�VqdSr6r+�r2rY�rZr+r,r7�r5zparse_rhs.<locals>.<genexpr>cs"g|]}t�|��D]}|�qqSr+)r�glob)r2rY�x��	prop_namer+r,r4�r5zparse_rhs.<locals>.<listcomp>NcSsg|]}tj|f�qSr+)rGrRr[r+r+r,r4�r5c3s|]}�|�VqdSr6r+r[r\r+r,r7�r5rVrrrWcsg|]}�|f�qSr+r+r[)�opr+r,r4�r5)�isdigitr�ALL�INT_DEFAULTSrGrT�int�ENUMSr�rulesZNonIntPropertyIntValueErrorZIntPropertyNonIntValueErrorZBadEnumValueErrorrUr&rArPr@r#rQr]rXrRrS�extendZ%MixedDifferentialNonDifferentialError)�rhsr`�valuesZ
operations�valueZunglobr+)rZrar`r,�	parse_rhs�sL
�
�  rl�	Directive�r`r%�	operationrkcs�|��sgStj�|�|�d�\}}|��|��}}tj�||�d|vrZ|�dd�n|tf\����fdd�t|��D�S)aQ
    Parses configuration lines into tuples of directives.
    For more examples, refer to tests/unit/test_parsing.py
    >>> parse_line('cipher@TLS = RC4* NULL')
    [Directive(prop_name='cipher', scope='tls',
               operation=Operation.RESET, value=None),
     Directive(prop_name='cipher', scope='tls',
               operation=Operation.APPEND, value='RC4-40'),
     Directive(prop_name='cipher', scope='tls',
               operation=Operation.APPEND, value='RC4-128'),
     Directive(prop_name='cipher', scope='tls',
               operation=Operation.APPEND, value='NULL')]
    �=�@rcs$g|]\}}t����||d��qS)rn)rmr!)r2rork�r`r%r+r,r4�s�
�zparse_line.<locals>.<listcomp>)�striprrgZcount_equals_signsr&Z	empty_lhsr?rl)�lineZlhsrir+rrr,�
parse_line�s �ruFc
Cs\z t|�}|D]}t|j�qWn6tjyV}z|s8�t�|�WYd}~n
d}~00dSr6)rurr%rZPolicySyntaxError�warnings�warn)rtrw�l�d�exr+r+r,�syntax_check_line�sr{cseZdZd�fdd�	Z�ZS)�PolicySyntaxDeprecationWarning�optionFcsF|�dd�}|�d|�d�}|d|��7}|s6|d7}t��|�dS)N�
z and � z is deprecatedz", please rewrite your rules using z4; be advised that it is not always a 1-1 replacement)�replace�superr-)r)Z
deprecatedZreplacement�what�onetoone�msg��	__class__r+r,r-�sz'PolicySyntaxDeprecationWarning.__init__)r}F)rDrErFr-�
__classcell__r+r+r�r,r|�sr|c
CsJt�dd|�}|�dd�}d�dd�|�d�D��}|�dd�}d�d	d�|�d�D��}d�d
d�|�d�D��}t�dd|���}t�d|�r�t�t	d
d��ddddd�}|�
�D]`\}}d|d}t�||�}|r�t�t	||��t�|d|�}|D]}|d|�d|��7}q�q�t�dd|���}ddddddd�}|�
�D]x\}}d|d}i}t�||�D]}	t�|||�||	�d�<�qj|�
�D]\}
}t�t	|
|���q�t�|||�}�qFd d!i}|�
�D]�\}}d|d}i}t�||�D]$}	t�|||	�d��||	�d�<�q�|�
�D] \}
}t�t	|
|d"d#d$���q"t�|||�}�q�t
tjd%d%d&��}
|
�r�d'�d(d�|
d%d&�D��}t�d)|
d&d|�r�d*|��nd|�}|
���qjt�d+d|�}t
tjd%d%d&��}|�r<d'�d,d�|d%d&�D��}t�d-|d&d|�r(d*|��nd|�}|���q�t�d.d|�S)/a
    Preprocesses text before parsing.
    Fixes line breaks, handles backwards compatibility.
    >>> preprocess_text('cipher = c1 \\ \nc2#x')
    'cipher = c1 c2'
    >>> with warnings.catch_warnings():
    ...     warnings.simplefilter("ignore")
    ...     preprocess_text('ike_protocol = IKEv2')
    'protocol@IKE = IKEv2'
    >>> with warnings.catch_warnings():
    ...     warnings.simplefilter("ignore")
    ...     preprocess_text('min_tls_version=TLS1.3')
    'protocol@TLS = -SSL2.0 -SSL3.0 -TLS1.0 -TLS1.1 -TLS1.2'
    z#.*�rp� = r~css|]}|��VqdSr6�rs�r2rxr+r+r,r7r5z"preprocess_text.<locals>.<genexpr>z\
css|]}|��VqdSr6r�r�r+r+r,r7
r5css|]}t�dd|�VqdS)z\s+rN)�re�subr�r+r+r,r7r5z
+z\bprotocol\s*=�protocolzprotocol@TLSz
cipher@TLSz
cipher@SSHz	group@SSHzprotocol@IKE)Z
tls_cipherZ
ssh_cipherZ	ssh_groupZike_protocolz\bz\s*=(.*)z

z =z7hash@DNSSec = -SHA1
sign@DNSSec = -RSA-SHA1 -ECDSA-SHA1z7hash@DNSSec = SHA1+
sign@DNSSec = RSA-SHA1+ ECDSA-SHA1+zetm@SSH = DISABLE_ETMz
etm@SSH = ANYzetm@\1 = DISABLE_ETMzetm@\1 = ANY)zsha1_in_dnssec = 0zsha1_in_dnssec = 1zssh_etm = 0zssh_etm = 1zssh_etm@([^= ]+) = 0zssh_etm@([^= ]+) = 1rzX25519-MLKEM768zMLKEM768-X25519rkT)r�r�Nrrcss|]}d|VqdS�rWNr+r[r+r+r,r7Jr5z\bmin_dtls_version = zprotocol@TLS = z\bmin_dtls_version = 0\bcss|]}d|VqdSr�r+r[r+r+r,r7Rr5z\bmin_tls_version = z\bmin_tls_version = 0\b)r�r�r��joinr&rs�findallrvrwr|�items�finditer�group�listrZDTLS_PROTOCOLS�popZ
TLS_PROTOCOLS)�textZPOSTFIX_REPLACEMENTS�fr�toZregex�ms�mZPLAIN_REPLACEMENTSrC�matchZmatch_frZmatch_toZVALUE_REPLACEMENTSZ
dtls_versions�negZtls_versionsr+r+r,�preprocess_text�s�����"
���r�c@sJeZdZdZd
dd�Zedd��Zedd��Zed	d
��Zedd��Z	dS)�ScopedPolicya�
    An entity constructing lists of what's `.enabled` and what's `.disabled`
    when the given scopes are active.
    >>> sp = ScopedPolicy(parse_line('cipher@TLS = RC4* NULL'), {'tls'})
    >>> 'AES-192-GCM' in sp.disabled['cipher']
    True
    >>> sp.enabled['cipher']
    ['RC4-40', 'RC4-128', 'NULL']
    >>> ScopedPolicy(parse_line('min_dh_size=2048')).integers['min_dh_size']
    2048
    Ncs�|pt�}t���_dd�t��D��_dd�tjD��_	|D�]�t
�j�}|�|�r>�j
tjkrrg�j	�j<q>�j
tjkr��j	�j}�j|vr�|��j�q>�j
tjkr�j	�j}�j|vr�|��j�|�d�j�q>�j
tjk�r�fdd��j	�jD��j	�j<q>�j
tjk�r0�j�j�j<q>�j
tjk�sBJ��j�j�j<q>t�j	�tt�j	��k�spJ��fdd�tj��D��_dS)NcSsi|]\}}||d�qS)rr+)r2�krYr+r+r,�
<dictcomp>kr5z)ScopedPolicy.__init__.<locals>.<dictcomp>cSsi|]
}|g�qSr+r+)r2r`r+r+r,r�lr5rcsg|]}|�jkr|�qSr+)rk�r2�e)�	directiver+r,r4s
�z)ScopedPolicy.__init__.<locals>.<listcomp>cs&i|]\�}���fdd�|D��qS)csg|]}|�j�vr|�qSr+)�enabledr�)r`r)r+r,r4�s�z4ScopedPolicy.__init__.<locals>.<dictcomp>.<listcomp>r+)r2Zalg_listr/r_r,r��s�)�setrd�copy�integersrfr��enumsrrcr�rr%rCrorGrPr`rRrk�appendrQ�remove�insertrSrTrU�lenZdisabled)r)�
directivesZrelevant_scopes�ssr�r+)r�r)r,r-hs<








�
�zScopedPolicy.__init__cCst�|jd�S�Nr�)r�min_tls_versionr�r/r+r+r,r��szScopedPolicy.min_tls_versioncCst�|jd�Sr�)r�max_tls_versionr�r/r+r+r,r��szScopedPolicy.max_tls_versioncCst�|jd�Sr�)r�min_dtls_versionr�r/r+r+r,r��szScopedPolicy.min_dtls_versioncCst�|jd�Sr�)r�max_dtls_versionr�r/r+r+r,r��szScopedPolicy.max_dtls_version)N)
rDrErFrOr-�propertyr�r�r�r�r+r+r+r,r�[s
&


r�cCs@|D](}tj�||�}t�|tj�r|Sqt�|||��dSr6)�os�pathr��access�R_OKrZPolicyFileNotFoundError)�
policyname�fname�pathsryr*r+r+r,�lookup_file�s

r�c@sFeZdZdZdZdd�dd�Zdd�Zdd	d
�Zddd
�Zdd�Z	dS)�UnscopedCryptoPolicyz/etc/crypto-policiesz/usr/share/crypto-policiesN)�	policydircGsP||_d�|g|�R�|_g|_|�|�}|D]}||j|dd�7}q.||_dS)N�:T)�	subpolicy)r�r�r��lines�read_policy_file�_directives)r)Zpolicy_namer�Zsubpolicy_namesr�Zsubpolicy_namer+r+r,r-�s
zUnscopedCryptoPolicy.__init__cCs|jSr6)r�r/r+r+r,�is_empty�szUnscopedCryptoPolicy.is_emptycCst|j|pi�Sr6)r�r�rBr+r+r,�scoped�szUnscopedCryptoPolicy.scopedFc	
Cs�|jpd}|rtj�|d�}t|||s*dndtjj|tj�|j|�tj�|j|�f�}t|dd��}|�	�}Wd�n1s�0Yt
|�}|�d�}|D]}t|dd	�q�|D]}t|�q�d
d�|D�S)NZpolicies�modulesz.polz.pmodzutf-8)�encodingr~T)rwcSsg|]}t|�D]}|�qqSr+)ru)r2rxr^r+r+r,r4�r5z9UnscopedCryptoPolicy.read_policy_file.<locals>.<listcomp>)
r�r�r�r�r��curdir�
CONFIG_DIR�	SHARE_DIR�open�readr�r&r{)	r)rMr�Zpdirr*�fr�r�rxr+r+r,r��s(
��&

z%UnscopedCryptoPolicy.read_policy_filecCsdd�}|��}d|j�d�}|d7}|d7}|d7}|d7}|d7}i|j�|j�|j�}|��D]\}}||||�7}qdd	}t��D]p\}}	|j|	d
�}
i|
j�|
j�|
j�}|��D]<\}}|||kr�|s�|d7}d}|||�d
|��|�7}q�q�|�s|d7}|S)NcSs2t|t�rd�|�nt|�}|�d|����dS)Nrr�r~)�
isinstancer�r��str�rstrip)�keyrkr3r+r+r,�fmt�sz)UnscopedCryptoPolicy.__str__.<locals>.fmtz	# Policy z dump
z#
z?# Do not parse the contents of this file with automated tools,
z.# it is provided for review convenience only.
z"# Baseline values for all scopes:
Fr=z9# Scope-specific properties derived for select backends:
Trqz&# No scope-specific properties found.
)r�r�r�r�r�r��DUMPABLE_SCOPES)r)r�Zgeneric_scopedr3Zgeneric_allr`rkZanything_scope_specificZ
scope_nameZ	scope_setZspecific_scopedZspecific_allr+r+r,r0�s@����zUnscopedCryptoPolicy.__str__)N)F)
rDrErFr�r�r-r�r�r�r0r+r+r+r,r��s

r�)F)�collections�enumr9r�r�rvr�rr�dict�fromkeysrdrfr?r(r�r�EnumrGrl�
namedtuplermrur{�
FutureWarningr|r�r�r�r�r+r+r+r,�<module>sN���
�3B

aF

© 2025 Cubjrnet7