
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
."ÉdCB  ã               @   s6  d dl Z d dlZd dlZd dlZd dlZd dlZddlmZ ddlmZ dd„ d<D ƒZ	dZ
d=Zd dhdddhdddhddhdddhdddhdddhdddhdddhdddhd!œ
ZG d"d#„ d#ƒZG d$d%„ d%ejƒZd&d'„ Ze jd(d>ƒZd-d.„ Zd?d0d1„ZG d2d3„ d3eƒZd4d5„ ZG d6d7„ d7ƒZd8d9„ ZG d:d;„ d;ƒZdS )@é    Né   )Ú	alg_lists)Ú
validationc             C   s   i | ]
}d |“qS )r   © )Ú.0Úkr   r   úC./usr/share/crypto-policies/python/cryptopolicies/cryptopolicies.pyú
<dictcomp>   s    r	   Úarbitrary_dh_groupsÚmin_dh_sizeÚmin_dsa_sizeÚmin_rsa_sizeÚsha1_in_certsÚ	ssh_certsÚssh_etmÚ*ÚtlsÚsslÚopensslÚnssÚgnutlsújava-tlsÚsshÚopensshúopenssh-serverúopenssh-clientÚlibsshÚipsecÚikeÚ	libreswanÚkerberosÚkrb5ÚdnssecÚbind)
r#   r   zjava-tlsr!   r   r   r   zopenssh-clientzopenssh-serverr   c               @   s(   e Zd Zefdd„Zdd„ Zdd„ ZdS )ÚScopeSelectorc             C   s”   |j ƒ  | _}|jdƒ | _| jr&|n
|dd… }tjj|| jd� tjj|| jd� |jdƒrr|dd… jdƒn|g| _	tjj
| j	t| jd� dS )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
        ú!r   N)Zoriginal_patternú{ú,éÿÿÿÿ)ÚlowerÚpatternÚ
startswithÚ	_positiver   ÚscopeZillegal_charactersZcurly_bracketsÚsplitÚ_globsZresulting_globsÚ
ALL_SCOPES)Úselfr*   Úpr   r   r   Ú__init__5   s    $zScopeSelector.__init__c             C   s   dt | jƒ› d�S )Nz<ScopeSelector pattern=ú>)Úreprr*   )r1   r   r   r   Ú__str__P   s    zScopeSelector.__str__c                sh   | j tkrdS dd„ ˆ D ƒ‰ tdd„ ˆ D ƒƒs2t‚| jrPt‡ fdd„| jD ƒƒS t‡ fdd„| jD ƒƒ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
        Tc             S   s   g | ]}|j ƒ ‘qS r   )r)   )r   Úsr   r   r   ú
<listcomp>^   s    z)ScopeSelector.matches.<locals>.<listcomp>c             s   s   | ]}|t kV  qd S )N)r0   )r   r7   r   r   r   ú	<genexpr>_   s    z(ScopeSelector.matches.<locals>.<genexpr>c             3   s   | ]}t jˆ |ƒV  qd S )N)ÚfnmatchÚfilter)r   Úg)Úscopesr   r   r9   a   s    c             3   s   | ]}t jˆ |ƒ V  qd S )N)r:   r;   )r   r<   )r=   r   r   r9   b   s    )r*   Ú	SCOPE_ANYÚallÚAssertionErrorr,   Úanyr/   )r1   r=   r   )r=   r   ÚmatchesS   s    	
zScopeSelector.matchesN)Ú__name__Ú
__module__Ú__qualname__r>   r3   r6   rB   r   r   r   r   r$   4   s   r$   c               @   s$   e Zd ZdZdZdZdZdZdZdS )Ú	OperationzM
    An operation that comes with the right-hand value of the directive.
    r   é   é   é   é   N)	rC   rD   rE   Ú__doc__ÚRESETÚPREPENDÚAPPENDÚOMITÚSET_INTr   r   r   r   rF   g   s   rF   c                s®  dd„ ‰ | j ƒ rfˆtjkr2ˆtkr2tjt| ƒfgS ˆtjkrJtjj	ˆƒ‚qzˆtjksXt
‚ˆtkszt
‚nˆtkrztjjˆƒ‚| jƒ }t‡ fdd„|D ƒƒsÊt‡fdd„|D ƒg ƒ}tjdfgdd„ |D ƒ S t‡ fd	d„|D ƒƒ�ržg }x²|D ]ª}|jd
ƒ�r"tj‰tj|dd… ˆƒddd… }n\|jd
ƒ�rTtj‰tj|dd… ˆƒddd… }n*|jdƒ�sdt
‚tj‰tj|dd… ˆƒ}|j‡fdd„|D ƒƒ qìW |S tjj| ƒ‚dS )a7  
    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: 1>, None)]
    >>> parse_rhs('IDEA-CBC SEED-CBC', 'cipher')
    [(<Operation.RESET: 1>, None),
     (<Operation.APPEND: 3>, 'IDEA-CBC'),
     (<Operation.APPEND: 3>, 'SEED-CBC')]
    >>> # 3DES-CBC gets prepended last for higher prio
    >>> parse_rhs('+*DES-CBC', 'cipher')
    [(<Operation.PREPEND: 2>, 'DES-CBC'),
     (<Operation.PREPEND: 2>, '3DES-CBC')]
    c             S   s   | j dƒp| jdƒp| j dƒS )Nú+ú-)r+   Úendswith)Úvr   r   r   Údifferential‚   s    zparse_rhs.<locals>.differentialc             3   s   | ]}ˆ |ƒV  qd S )Nr   )r   rT   )rU   r   r   r9   ”   s    zparse_rhs.<locals>.<genexpr>c                s   g | ]}t j|ˆ ƒ‘qS r   )r   Úglob)r   rT   )Ú	prop_namer   r   r8   •   s    zparse_rhs.<locals>.<listcomp>Nc             S   s   g | ]}t j|f‘qS r   )rF   rN   )r   rT   r   r   r   r8   —   s    c             3   s   | ]}ˆ |ƒV  qd S )Nr   )r   rT   )rU   r   r   r9   ˜   s    rQ   r   rR   c                s   g | ]}ˆ |f‘qS r   r   )r   rT   )Úopr   r   r8   ¥   s    r(   r(   r(   )Úisdigitr   ÚALLÚINT_DEFAULTSrF   rP   Úintr   ÚrulesZNonIntPropertyIntValueErrorr@   ZIntPropertyNonIntValueErrorr.   rA   ÚsumrL   r?   r+   rM   rV   rS   rN   rO   ÚextendZ%MixedDifferentialNonDifferentialError)ÚrhsrW   ÚvaluesZ
operationsÚvalueZunglobr   )rU   rX   rW   r   Ú	parse_rhsr   s<    


  rc   Ú	DirectiverW   r-   Ú	operationrb   c                s€   | j ƒ sg S tjj| ƒ | jdƒ\}}|j ƒ |j ƒ  }}tjj|| ƒ d|krZ|jddƒn|tf\‰ ‰‡ ‡fdd„t|ˆ ƒD ƒS )ae  
    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: 1>, value=None),
     Directive(prop_name='cipher', scope='tls',
               operation=<Operation.APPEND: 3>, value='RC4-40'),
     Directive(prop_name='cipher', scope='tls',
               operation=<Operation.APPEND: 3>, value='RC4-128'),
     Directive(prop_name='cipher', scope='tls',
               operation=<Operation.APPEND: 3>, value='NULL')]
    ú=ú@r   c                s$   g | ]\}}t ˆ ˆjƒ ||d �‘qS ))rW   r-   re   rb   )rd   r)   )r   re   rb   )rW   r-   r   r   r8   Ê   s   zparse_line.<locals>.<listcomp>)Ústripr   r]   Zcount_equals_signsr.   Z	empty_lhsr>   rc   )ÚlineZlhsr`   r   )rW   r-   r   Ú
parse_line²   s     rj   Fc             C   s^   y$t | ƒ}x|D ]}t|jƒ qW W n4 tjk
rX } z|s>‚ tj|ƒ W Y d d }~X nX d S )N)rj   r$   r-   r   ZPolicySyntaxErrorÚwarningsÚwarn)ri   rl   ÚlÚdÚexr   r   r   Úsyntax_check_lineÏ   s    
rp   c                   s   e Zd Z‡ fdd„Z‡  ZS )ÚPolicySyntaxDeprecationWarningc                s@   |j ddƒ}d|› d�}|d|› d�7 }|d7 }tƒ j|ƒ d S )NÚ
z and zoption z is deprecatedz", please rewrite your rules using z; z2be advised that it is not always a 1-1 replacement)ÚreplaceÚsuperr3   )r1   Z
deprecatedZreplacementÚmsg)Ú	__class__r   r   r3   Û   s
    z'PolicySyntaxDeprecationWarning.__init__)rC   rD   rE   r3   Ú__classcell__r   r   )rv   r   rq   Ú   s   rq   c             C   s�  t jdd| ƒ} | jddƒ} djdd„ | jdƒD ƒƒ} | jddƒ} djd	d„ | jdƒD ƒƒ} djd
d„ | jdƒD ƒƒ} t jdd| ƒjƒ } t jd| ƒr¢tjt	ddƒƒ dddddœ}xr|j
ƒ D ]f\}}d| d }t j|| ƒ}|rîtjt	||ƒƒ t j|d| ƒ} x"|D ]}| d|› d|› �7 } �qW qºW t jdd| ƒjƒ } dddœ}xN|j
ƒ D ]B\}}d| d }t j|| ƒ�r|tjt	||ƒƒ t j||| ƒ} �qJW ttjddd%… ƒ}xZ|�rþdjdd„ |dd&… D ƒƒ}	t jd|d'  d |	�rêd |	› �nd| ƒ} |jƒ  �q¦W t jd!d| ƒ} ttjddd(… ƒ}
xZ|
�r|djd"d„ |
dd)… D ƒƒ}	t jd#|
d*  d |	�rhd |	› �nd| ƒ} |
jƒ  �q$W t j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#.*Ú rf   z = rr   c             s   s   | ]}|j ƒ V  qd S )N)rh   )r   rm   r   r   r   r9   ô   s    z"preprocess_text.<locals>.<genexpr>z\
c             s   s   | ]}|j ƒ V  qd S )N)rh   )r   rm   r   r   r   r9   ö   s    c             s   s   | ]}t jd d|ƒV  qdS )z\s+ú N)ÚreÚsub)r   rm   r   r   r   r9   ÷   s    z
+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+)zsha1_in_dnssec = 0zsha1_in_dnssec = 1Nr   ry   c             s   s   | ]}d | V  qdS )rR   Nr   )r   rT   r   r   r   r9     s    z\bmin_dtls_version = zprotocol@TLS = z\bmin_dtls_version = 0\bc             s   s   | ]}d | V  qdS )rR   Nr   )r   rT   r   r   r   r9   $  s    z\bmin_tls_version = z\bmin_tls_version = 0\br(   r(   r(   r(   r(   r(   )rz   r{   rs   Újoinr.   rh   Úfindallrk   rl   rq   ÚitemsÚsearchÚlistr   ZDTLS_PROTOCOLSÚpopZTLS_PROTOCOLS)ÚtextZPOSTFIX_REPLACEMENTSÚfrZtoZregexZmsÚmZPLAIN_REPLACEMENTSZdtls_versionsÚnegZtls_versionsr   r   r   Úpreprocess_textã   sZ    
r‡   c               @   sJ   e Zd ZdZddd„Zedd„ ƒZedd„ ƒZed	d
„ ƒZedd„ ƒZ	dS )Ú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-256-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
    Nc                sX  |pt ƒ }tjƒ ˆ_dd„ tjD ƒˆ_xø|D ]ð‰ tˆ jƒ}|j	|ƒr,ˆ j
tjkr^g ˆjˆ j< q,ˆ j
tjkrŽˆjˆ j }ˆ j|krŒ|jˆ jƒ q,ˆ j
tjkrÌˆjˆ j }ˆ j|kr¼|jˆ jƒ |jdˆ jƒ q,ˆ j
tjkrü‡ fdd„ˆjˆ j D ƒˆjˆ j< q,ˆ j
tjk�st‚ˆ jˆjˆ j< q,W tˆjƒtt ˆjƒƒk�s>t‚‡fdd„tjD ƒˆ_d S )Nc             S   s   i | ]
}g |“qS r   r   )r   rW   r   r   r   r	   >  s    z)ScopedPolicy.__init__.<locals>.<dictcomp>r   c                s   g | ]}|ˆ j kr|‘qS r   )rb   )r   Úe)Ú	directiver   r   r8   R  s    z)ScopedPolicy.__init__.<locals>.<listcomp>c                s(   i | ] ‰ ‡ ‡fd d„t jˆ  D ƒˆ “qS )c                s   g | ]}|ˆj ˆ  kr|‘qS r   )Úenabled)r   r‰   )rW   r1   r   r   r8   Z  s    z4ScopedPolicy.__init__.<locals>.<dictcomp>.<listcomp>)r   rZ   )r   )r1   )rW   r   r	   Z  s   )Úsetr[   ÚcopyÚintegersr   rZ   r‹   r$   r-   rB   re   rF   rL   rW   rN   rb   ÚappendrM   ÚremoveÚinsertrO   rP   r@   ÚlenZdisabled)r1   Ú
directivesZrelevant_scopesZssr‹   r   )rŠ   r1   r   r3   ;  s0    






$
zScopedPolicy.__init__c             C   s   t j| jd ƒS )Nr|   )r   Úmin_tls_versionr‹   )r1   r   r   r   r”   ^  s    zScopedPolicy.min_tls_versionc             C   s   t j| jd ƒS )Nr|   )r   Úmax_tls_versionr‹   )r1   r   r   r   r•   b  s    zScopedPolicy.max_tls_versionc             C   s   t j| jd ƒS )Nr|   )r   Úmin_dtls_versionr‹   )r1   r   r   r   r–   f  s    zScopedPolicy.min_dtls_versionc             C   s   t j| jd ƒS )Nr|   )r   Úmax_dtls_versionr‹   )r1   r   r   r   r—   j  s    zScopedPolicy.max_dtls_version)N)
rC   rD   rE   rK   r3   Úpropertyr”   r•   r–   r—   r   r   r   r   rˆ   /  s   
#rˆ   c             C   s@   x,|D ]$}t jj||ƒ}t j|t jƒr|S qW tj| ||ƒ‚d S )N)ÚosÚpathr}   ÚaccessÚR_OKr   ZPolicyFileNotFoundError)Ú
policynameÚfnameÚpathsrn   r2   r   r   r   Úlookup_fileq  s
    
r    c               @   sF   e Zd ZdZdZddœdd„Zdd„ Zdd	d
„Zddd„Zdd„ Z	dS )ÚUnscopedCryptoPolicyz/etc/crypto-policiesz/usr/share/crypto-policiesN)Ú	policydirc            G   sR   || _ dj|f| ƒ| _g | _| j|ƒ}x|D ]}|| j|dd�7 }q.W || _d S )Nú:T)Ú	subpolicy)r¢   r}   r�   ÚlinesÚread_policy_fileÚ_directives)r1   Zpolicy_namer¢   Zsubpolicy_namesr“   Zsubpolicy_namer   r   r   r3   €  s    

zUnscopedCryptoPolicy.__init__c             C   s   | j  S )N)r§   )r1   r   r   r   Úis_empty‹  s    zUnscopedCryptoPolicy.is_emptyc             C   s   t | j|pi ƒS )N)rˆ   r§   )r1   r=   r   r   r   ÚscopedŽ  s    zUnscopedCryptoPolicy.scopedFc       	      C   sÊ   | j pd}|rtjj|dƒ}t|||s*dnd tjj|tjj| j|ƒtjj| j|ƒfƒ}t|ƒ�}|j	ƒ }W d Q R X t
|ƒ}|jdƒ}x|D ]}t|dd� qŒW x|D ]}t|ƒ q¦W tdd	„ |D ƒg ƒS )
NZpoliciesÚmodulesz.polz.pmodrr   T)rl   c             S   s   g | ]}t |ƒ‘qS r   )rj   )r   rm   r   r   r   r8   ¥  s    z9UnscopedCryptoPolicy.read_policy_file.<locals>.<listcomp>)r¢   r™   rš   r}   r    ÚcurdirÚ
CONFIG_DIRÚ	SHARE_DIRÚopenÚreadr‡   r.   rp   r^   )	r1   Únamer¤   Zpdirr2   Úfrƒ   r¥   rm   r   r   r   r¦   ‘  s$    




z%UnscopedCryptoPolicy.read_policy_filec             C   s  dd„ }| j ƒ }d| j› d�}|d7 }|d7 }|d7 }|d7 }|d7 }|j|j–}x"|jƒ D ]\}}||||ƒ7 }q\W d	}xvtjƒ D ]j\}}	| j |	d
�}
|
j|
j–}xH|jƒ D ]<\}}||| kr®|sÒ|d7 }d}|||› d|› �|ƒ7 }q®W q„W |�s |d7 }|S )Nc             S   s2   t |tƒrdj|ƒnt|ƒ}| › d|› �jƒ d S )Nry   z = rr   )Ú
isinstancer�   r}   ÚstrÚrstrip)Úkeyrb   r7   r   r   r   Úfmt¨  s    z)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:
F)r=   z9# Scope-specific properties derived for select backends:
Trg   z&# No scope-specific properties found.
)r©   r�   r‹   rŽ   r   ÚDUMPABLE_SCOPES)r1   r¶   Zgeneric_scopedr7   Zgeneric_allrW   rb   Zanything_scope_specificZ
scope_nameZ	scope_setZspecific_scopedZspecific_allr   r   r   r6   §  s2     zUnscopedCryptoPolicy.__str__)N)F)
rC   rD   rE   r¬   r­   r3   r¨   r©   r¦   r6   r   r   r   r   r¡   {  s   

r¡   )r
   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   )rW   r-   re   rb   )F)ÚcollectionsÚenumr:   r™   rz   rk   rx   r   r   r[   r>   r0   r·   r$   ÚEnumrF   rc   Ú
namedtuplerd   rj   rp   ÚFutureWarningrq   r‡   rˆ   r    r¡   r   r   r   r   Ú<module>   sP          3;
	LB
